Split up compare.*
[gromacs.git] / src / gromacs / mdtypes / state.cpp
blob5e12f5bfb31c3873b07b4f6b2225e9131e3e86e3
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 nlambda)
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 init_df_history(&state->dfhist, nlambda);
193 init_swapstate(&state->swapstate);
194 state->ddp_count = 0;
195 state->ddp_count_cg_gl = 0;
196 state->cg_gl = NULL;
197 state->cg_gl_nalloc = 0;
200 void done_state(t_state *state)
202 if (state->x)
204 sfree(state->x);
206 if (state->v)
208 sfree(state->v);
210 if (state->cg_p)
212 sfree(state->cg_p);
214 state->nalloc = 0;
215 if (state->cg_gl)
217 sfree(state->cg_gl);
219 state->cg_gl_nalloc = 0;
220 if (state->lambda)
222 sfree(state->lambda);
224 if (state->ngtc > 0)
226 sfree(state->nosehoover_xi);
227 sfree(state->nosehoover_vxi);
228 sfree(state->therm_integral);
232 t_state *serial_init_local_state(t_state *state_global)
234 int i;
235 t_state *state_local;
237 snew(state_local, 1);
239 /* Copy all the contents */
240 *state_local = *state_global;
241 snew(state_local->lambda, efptNR);
242 /* local storage for lambda */
243 for (i = 0; i < efptNR; i++)
245 state_local->lambda[i] = state_global->lambda[i];
248 return state_local;
251 void comp_state(const t_state *st1, const t_state *st2,
252 gmx_bool bRMSD, real ftol, real abstol)
254 int i, j, nc;
256 fprintf(stdout, "comparing flags\n");
257 cmp_int(stdout, "flags", -1, st1->flags, st2->flags);
258 fprintf(stdout, "comparing box\n");
259 cmp_rvecs(stdout, "box", DIM, st1->box, st2->box, FALSE, ftol, abstol);
260 fprintf(stdout, "comparing box_rel\n");
261 cmp_rvecs(stdout, "box_rel", DIM, st1->box_rel, st2->box_rel, FALSE, ftol, abstol);
262 fprintf(stdout, "comparing boxv\n");
263 cmp_rvecs(stdout, "boxv", DIM, st1->boxv, st2->boxv, FALSE, ftol, abstol);
264 if (st1->flags & (1<<estSVIR_PREV))
266 fprintf(stdout, "comparing shake vir_prev\n");
267 cmp_rvecs(stdout, "svir_prev", DIM, st1->svir_prev, st2->svir_prev, FALSE, ftol, abstol);
269 if (st1->flags & (1<<estFVIR_PREV))
271 fprintf(stdout, "comparing force vir_prev\n");
272 cmp_rvecs(stdout, "fvir_prev", DIM, st1->fvir_prev, st2->fvir_prev, FALSE, ftol, abstol);
274 if (st1->flags & (1<<estPRES_PREV))
276 fprintf(stdout, "comparing prev_pres\n");
277 cmp_rvecs(stdout, "pres_prev", DIM, st1->pres_prev, st2->pres_prev, FALSE, ftol, abstol);
279 cmp_int(stdout, "ngtc", -1, st1->ngtc, st2->ngtc);
280 cmp_int(stdout, "nhchainlength", -1, st1->nhchainlength, st2->nhchainlength);
281 if (st1->ngtc == st2->ngtc && st1->nhchainlength == st2->nhchainlength)
283 for (i = 0; i < st1->ngtc; i++)
285 nc = i*st1->nhchainlength;
286 for (j = 0; j < nc; j++)
288 cmp_real(stdout, "nosehoover_xi",
289 i, st1->nosehoover_xi[nc+j], st2->nosehoover_xi[nc+j], ftol, abstol);
293 cmp_int(stdout, "nnhpres", -1, st1->nnhpres, st2->nnhpres);
294 if (st1->nnhpres == st2->nnhpres && st1->nhchainlength == st2->nhchainlength)
296 for (i = 0; i < st1->nnhpres; i++)
298 nc = i*st1->nhchainlength;
299 for (j = 0; j < nc; j++)
301 cmp_real(stdout, "nosehoover_xi",
302 i, st1->nhpres_xi[nc+j], st2->nhpres_xi[nc+j], ftol, abstol);
307 cmp_int(stdout, "natoms", -1, st1->natoms, st2->natoms);
308 if (st1->natoms == st2->natoms)
310 if ((st1->flags & (1<<estX)) && (st2->flags & (1<<estX)))
312 fprintf(stdout, "comparing x\n");
313 cmp_rvecs(stdout, "x", st1->natoms, st1->x, st2->x, bRMSD, ftol, abstol);
315 if ((st1->flags & (1<<estV)) && (st2->flags & (1<<estV)))
317 fprintf(stdout, "comparing v\n");
318 cmp_rvecs(stdout, "v", st1->natoms, st1->v, st2->v, bRMSD, ftol, abstol);