Remove state duplication in serial runs
[gromacs.git] / src / gromacs / mdtypes / state.cpp
blob0c8b7b3ac5ed06171c05643ed848916c26b8df62
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
37 /* This file is completely threadsafe - keep it that way! */
38 #include "gmxpre.h"
40 #include "state.h"
42 #include <cstring>
44 #include <algorithm>
46 #include "gromacs/math/vec.h"
47 #include "gromacs/math/veccompare.h"
48 #include "gromacs/mdtypes/df_history.h"
49 #include "gromacs/mdtypes/energyhistory.h"
50 #include "gromacs/mdtypes/inputrec.h"
51 #include "gromacs/mdtypes/md_enums.h"
52 #include "gromacs/utility/compare.h"
53 #include "gromacs/utility/smalloc.h"
55 /* The source code in this file should be thread-safe.
56 Please keep it that way. */
58 static void zero_history(history_t *hist)
60 hist->disre_initf = 0;
61 hist->ndisrepairs = 0;
62 hist->disre_rm3tav = NULL;
63 hist->orire_initf = 0;
64 hist->norire_Dtav = 0;
65 hist->orire_Dtav = NULL;
68 static void zero_ekinstate(ekinstate_t *eks)
70 eks->ekin_n = 0;
71 eks->ekinh = NULL;
72 eks->ekinf = NULL;
73 eks->ekinh_old = NULL;
74 eks->ekinscalef_nhc = NULL;
75 eks->ekinscaleh_nhc = NULL;
76 eks->vscale_nhc = NULL;
77 eks->dekindl = 0;
78 eks->mvcos = 0;
81 static void init_swapstate(swapstate_t *swapstate)
83 /* Ion/water position swapping */
84 swapstate->eSwapCoords = 0;
85 swapstate->nIonTypes = 0;
86 swapstate->nAverage = 0;
87 swapstate->fluxleak = 0;
88 swapstate->fluxleak_p = NULL;
89 swapstate->bFromCpt = 0;
90 swapstate->nat[eChan0] = 0;
91 swapstate->nat[eChan1] = 0;
92 swapstate->xc_old_whole[eChan0] = NULL;
93 swapstate->xc_old_whole[eChan1] = NULL;
94 swapstate->xc_old_whole_p[eChan0] = NULL;
95 swapstate->xc_old_whole_p[eChan1] = NULL;
96 swapstate->ionType = NULL;
99 void init_gtc_state(t_state *state, int ngtc, int nnhpres, int nhchainlength)
101 int i, j;
103 state->ngtc = ngtc;
104 state->nnhpres = nnhpres;
105 state->nhchainlength = nhchainlength;
106 if (state->ngtc > 0)
108 snew(state->nosehoover_xi, state->nhchainlength*state->ngtc);
109 snew(state->nosehoover_vxi, state->nhchainlength*state->ngtc);
110 snew(state->therm_integral, state->ngtc);
111 for (i = 0; i < state->ngtc; i++)
113 for (j = 0; j < state->nhchainlength; j++)
115 state->nosehoover_xi[i*state->nhchainlength + j] = 0.0;
116 state->nosehoover_vxi[i*state->nhchainlength + j] = 0.0;
119 for (i = 0; i < state->ngtc; i++)
121 state->therm_integral[i] = 0.0;
124 else
126 state->nosehoover_xi = NULL;
127 state->nosehoover_vxi = NULL;
128 state->therm_integral = NULL;
131 if (state->nnhpres > 0)
133 snew(state->nhpres_xi, state->nhchainlength*nnhpres);
134 snew(state->nhpres_vxi, state->nhchainlength*nnhpres);
135 for (i = 0; i < nnhpres; i++)
137 for (j = 0; j < state->nhchainlength; j++)
139 state->nhpres_xi[i*nhchainlength + j] = 0.0;
140 state->nhpres_vxi[i*nhchainlength + j] = 0.0;
144 else
146 state->nhpres_xi = NULL;
147 state->nhpres_vxi = NULL;
152 void init_state(t_state *state, int natoms, int ngtc, int nnhpres, int nhchainlength, int dfhistNumLambda)
154 int i;
156 state->natoms = natoms;
157 state->flags = 0;
158 state->fep_state = 0;
159 state->lambda = 0;
160 snew(state->lambda, efptNR);
161 for (i = 0; i < efptNR; i++)
163 state->lambda[i] = 0;
165 state->veta = 0;
166 clear_mat(state->box);
167 clear_mat(state->box_rel);
168 clear_mat(state->boxv);
169 clear_mat(state->pres_prev);
170 clear_mat(state->svir_prev);
171 clear_mat(state->fvir_prev);
172 init_gtc_state(state, ngtc, nnhpres, nhchainlength);
173 state->nalloc = state->natoms;
174 if (state->nalloc > 0)
176 /* We need to allocate one element extra, since we might use
177 * (unaligned) 4-wide SIMD loads to access rvec entries.
179 snew(state->x, state->nalloc + 1);
180 snew(state->v, state->nalloc + 1);
182 else
184 state->x = NULL;
185 state->v = NULL;
187 state->cg_p = NULL;
188 zero_history(&state->hist);
189 zero_ekinstate(&state->ekinstate);
190 snew(state->enerhist, 1);
191 init_energyhistory(state->enerhist);
192 if (dfhistNumLambda > 0)
194 snew(state->dfhist, 1);
195 init_df_history(state->dfhist, dfhistNumLambda);
197 else
199 state->dfhist = NULL;
201 state->swapstate = NULL;
202 state->edsamstate = NULL;
203 state->ddp_count = 0;
204 state->ddp_count_cg_gl = 0;
205 state->cg_gl = NULL;
206 state->cg_gl_nalloc = 0;
209 void done_state(t_state *state)
211 sfree(state->x);
212 sfree(state->v);
213 sfree(state->cg_p);
214 sfree(state->enerhist);
215 state->nalloc = 0;
216 sfree(state->cg_gl);
217 state->cg_gl_nalloc = 0;
218 sfree(state->lambda);
219 if (state->ngtc > 0)
221 sfree(state->nosehoover_xi);
222 sfree(state->nosehoover_vxi);
223 sfree(state->therm_integral);
227 void comp_state(const t_state *st1, const t_state *st2,
228 gmx_bool bRMSD, real ftol, real abstol)
230 int i, j, nc;
232 fprintf(stdout, "comparing flags\n");
233 cmp_int(stdout, "flags", -1, st1->flags, st2->flags);
234 fprintf(stdout, "comparing box\n");
235 cmp_rvecs(stdout, "box", DIM, st1->box, st2->box, FALSE, ftol, abstol);
236 fprintf(stdout, "comparing box_rel\n");
237 cmp_rvecs(stdout, "box_rel", DIM, st1->box_rel, st2->box_rel, FALSE, ftol, abstol);
238 fprintf(stdout, "comparing boxv\n");
239 cmp_rvecs(stdout, "boxv", DIM, st1->boxv, st2->boxv, FALSE, ftol, abstol);
240 if (st1->flags & (1<<estSVIR_PREV))
242 fprintf(stdout, "comparing shake vir_prev\n");
243 cmp_rvecs(stdout, "svir_prev", DIM, st1->svir_prev, st2->svir_prev, FALSE, ftol, abstol);
245 if (st1->flags & (1<<estFVIR_PREV))
247 fprintf(stdout, "comparing force vir_prev\n");
248 cmp_rvecs(stdout, "fvir_prev", DIM, st1->fvir_prev, st2->fvir_prev, FALSE, ftol, abstol);
250 if (st1->flags & (1<<estPRES_PREV))
252 fprintf(stdout, "comparing prev_pres\n");
253 cmp_rvecs(stdout, "pres_prev", DIM, st1->pres_prev, st2->pres_prev, FALSE, ftol, abstol);
255 cmp_int(stdout, "ngtc", -1, st1->ngtc, st2->ngtc);
256 cmp_int(stdout, "nhchainlength", -1, st1->nhchainlength, st2->nhchainlength);
257 if (st1->ngtc == st2->ngtc && st1->nhchainlength == st2->nhchainlength)
259 for (i = 0; i < st1->ngtc; i++)
261 nc = i*st1->nhchainlength;
262 for (j = 0; j < nc; j++)
264 cmp_real(stdout, "nosehoover_xi",
265 i, st1->nosehoover_xi[nc+j], st2->nosehoover_xi[nc+j], ftol, abstol);
269 cmp_int(stdout, "nnhpres", -1, st1->nnhpres, st2->nnhpres);
270 if (st1->nnhpres == st2->nnhpres && st1->nhchainlength == st2->nhchainlength)
272 for (i = 0; i < st1->nnhpres; i++)
274 nc = i*st1->nhchainlength;
275 for (j = 0; j < nc; j++)
277 cmp_real(stdout, "nosehoover_xi",
278 i, st1->nhpres_xi[nc+j], st2->nhpres_xi[nc+j], ftol, abstol);
283 cmp_int(stdout, "natoms", -1, st1->natoms, st2->natoms);
284 if (st1->natoms == st2->natoms)
286 if ((st1->flags & (1<<estX)) && (st2->flags & (1<<estX)))
288 fprintf(stdout, "comparing x\n");
289 cmp_rvecs(stdout, "x", st1->natoms, st1->x, st2->x, bRMSD, ftol, abstol);
291 if ((st1->flags & (1<<estV)) && (st2->flags & (1<<estV)))
293 fprintf(stdout, "comparing v\n");
294 cmp_rvecs(stdout, "v", st1->natoms, st1->v, st2->v, bRMSD, ftol, abstol);