Improved state_change_natoms and used it more
[gromacs.git] / src / gromacs / mdtypes / state.cpp
blob31f27f7499ce342cf16f35e466d6a85180d794bd
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,2017, 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/inputrec.h"
50 #include "gromacs/mdtypes/md_enums.h"
51 #include "gromacs/utility/compare.h"
52 #include "gromacs/utility/gmxassert.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 history_t::history_t() : disre_initf(0),
59 ndisrepairs(0),
60 disre_rm3tav(nullptr),
61 orire_initf(0),
62 norire_Dtav(0),
63 orire_Dtav(nullptr)
67 ekinstate_t::ekinstate_t() : bUpToDate(FALSE),
68 ekin_n(0),
69 ekinh(nullptr),
70 ekinf(nullptr),
71 ekinh_old(nullptr),
72 ekin_total(),
73 ekinscalef_nhc(),
74 ekinscaleh_nhc(),
75 vscale_nhc(),
76 dekindl(0),
77 mvcos(0)
79 clear_mat(ekin_total);
82 static void init_swapstate(swapstate_t *swapstate)
84 /* Ion/water position swapping */
85 swapstate->eSwapCoords = 0;
86 swapstate->nIonTypes = 0;
87 swapstate->nAverage = 0;
88 swapstate->fluxleak = 0;
89 swapstate->fluxleak_p = nullptr;
90 swapstate->bFromCpt = 0;
91 swapstate->nat[eChan0] = 0;
92 swapstate->nat[eChan1] = 0;
93 swapstate->xc_old_whole[eChan0] = nullptr;
94 swapstate->xc_old_whole[eChan1] = nullptr;
95 swapstate->xc_old_whole_p[eChan0] = nullptr;
96 swapstate->xc_old_whole_p[eChan1] = nullptr;
97 swapstate->ionType = nullptr;
100 void init_gtc_state(t_state *state, int ngtc, int nnhpres, int nhchainlength)
102 state->ngtc = ngtc;
103 state->nnhpres = nnhpres;
104 state->nhchainlength = nhchainlength;
105 state->nosehoover_xi.resize(state->nhchainlength*state->ngtc, 0);
106 state->nosehoover_vxi.resize(state->nhchainlength*state->ngtc, 0);
107 state->therm_integral.resize(state->ngtc, 0);
108 state->nhpres_xi.resize(state->nhchainlength*nnhpres, 0);
109 state->nhpres_vxi.resize(state->nhchainlength*nnhpres, 0);
113 /* Checkpoint code relies on this function having no effect if
114 state->natoms is > 0 and passed as natoms. */
115 void state_change_natoms(t_state *state, int natoms)
117 state->natoms = natoms;
118 if (state->natoms > 0)
120 /* We need to allocate one element extra, since we might use
121 * (unaligned) 4-wide SIMD loads to access rvec entries.
123 if (state->flags & (1 << estX))
125 state->x.resize(state->natoms + 1);
127 if (state->flags & (1 << estV))
129 state->v.resize(state->natoms + 1);
131 if (state->flags & (1 << estCGP))
133 state->cg_p.resize(state->natoms + 1);
136 else
138 state->x.resize(0);
139 state->v.resize(0);
140 state->cg_p.resize(0);
144 void init_dfhist_state(t_state *state, int dfhistNumLambda)
146 if (dfhistNumLambda > 0)
148 snew(state->dfhist, 1);
149 init_df_history(state->dfhist, dfhistNumLambda);
151 else
153 state->dfhist = nullptr;
157 void comp_state(const t_state *st1, const t_state *st2,
158 gmx_bool bRMSD, real ftol, real abstol)
160 int i, j, nc;
162 fprintf(stdout, "comparing flags\n");
163 cmp_int(stdout, "flags", -1, st1->flags, st2->flags);
164 fprintf(stdout, "comparing box\n");
165 cmp_rvecs(stdout, "box", DIM, st1->box, st2->box, FALSE, ftol, abstol);
166 fprintf(stdout, "comparing box_rel\n");
167 cmp_rvecs(stdout, "box_rel", DIM, st1->box_rel, st2->box_rel, FALSE, ftol, abstol);
168 fprintf(stdout, "comparing boxv\n");
169 cmp_rvecs(stdout, "boxv", DIM, st1->boxv, st2->boxv, FALSE, ftol, abstol);
170 if (st1->flags & (1<<estSVIR_PREV))
172 fprintf(stdout, "comparing shake vir_prev\n");
173 cmp_rvecs(stdout, "svir_prev", DIM, st1->svir_prev, st2->svir_prev, FALSE, ftol, abstol);
175 if (st1->flags & (1<<estFVIR_PREV))
177 fprintf(stdout, "comparing force vir_prev\n");
178 cmp_rvecs(stdout, "fvir_prev", DIM, st1->fvir_prev, st2->fvir_prev, FALSE, ftol, abstol);
180 if (st1->flags & (1<<estPRES_PREV))
182 fprintf(stdout, "comparing prev_pres\n");
183 cmp_rvecs(stdout, "pres_prev", DIM, st1->pres_prev, st2->pres_prev, FALSE, ftol, abstol);
185 cmp_int(stdout, "ngtc", -1, st1->ngtc, st2->ngtc);
186 cmp_int(stdout, "nhchainlength", -1, st1->nhchainlength, st2->nhchainlength);
187 if (st1->ngtc == st2->ngtc && st1->nhchainlength == st2->nhchainlength)
189 for (i = 0; i < st1->ngtc; i++)
191 nc = i*st1->nhchainlength;
192 for (j = 0; j < nc; j++)
194 cmp_real(stdout, "nosehoover_xi",
195 i, st1->nosehoover_xi[nc+j], st2->nosehoover_xi[nc+j], ftol, abstol);
199 cmp_int(stdout, "nnhpres", -1, st1->nnhpres, st2->nnhpres);
200 if (st1->nnhpres == st2->nnhpres && st1->nhchainlength == st2->nhchainlength)
202 for (i = 0; i < st1->nnhpres; i++)
204 nc = i*st1->nhchainlength;
205 for (j = 0; j < nc; j++)
207 cmp_real(stdout, "nosehoover_xi",
208 i, st1->nhpres_xi[nc+j], st2->nhpres_xi[nc+j], ftol, abstol);
213 cmp_int(stdout, "natoms", -1, st1->natoms, st2->natoms);
214 if (st1->natoms == st2->natoms)
216 if ((st1->flags & (1<<estX)) && (st2->flags & (1<<estX)))
218 fprintf(stdout, "comparing x\n");
219 cmp_rvecs(stdout, "x", st1->natoms, as_rvec_array(st1->x.data()), as_rvec_array(st2->x.data()), bRMSD, ftol, abstol);
221 if ((st1->flags & (1<<estV)) && (st2->flags & (1<<estV)))
223 fprintf(stdout, "comparing v\n");
224 cmp_rvecs(stdout, "v", st1->natoms, as_rvec_array(st1->v.data()), as_rvec_array(st2->v.data()), bRMSD, ftol, abstol);
229 rvec *getRvecArrayFromPaddedRVecVector(const PaddedRVecVector *v,
230 unsigned int n)
232 GMX_ASSERT(v->size() >= n, "We can't copy more elements than the vector size");
234 rvec *dest;
236 snew(dest, n);
238 const rvec *vPtr = as_rvec_array(v->data());
239 for (unsigned int i = 0; i < n; i++)
241 copy_rvec(vPtr[i], dest[i]);
244 return dest;
247 t_state::t_state() : natoms(0),
248 ngtc(0),
249 nnhpres(0),
250 nhchainlength(0),
251 flags(0),
252 fep_state(0),
253 lambda(),
254 nosehoover_xi(),
255 nosehoover_vxi(),
256 nhpres_xi(),
257 nhpres_vxi(),
258 therm_integral(),
259 veta(0),
260 vol0(0),
261 x(),
262 v(),
263 cg_p(),
264 ekinstate(),
265 hist(),
266 swapstate(nullptr),
267 dfhist(nullptr),
268 edsamstate(nullptr),
269 ddp_count(0),
270 ddp_count_cg_gl(0),
271 cg_gl()
273 // It would be nicer to initialize these with {} or {{0}} in the
274 // above initialization list, but uncrustify doesn't understand
275 // that.
276 // TODO Fix this if we switch to clang-format some time.
277 // cppcheck-suppress useInitializationList
278 lambda = {{ 0 }};
279 clear_mat(box);
280 clear_mat(box_rel);
281 clear_mat(boxv);
282 clear_mat(pres_prev);
283 clear_mat(svir_prev);
284 clear_mat(fvir_prev);