Reduce rounding errors in SETTLE
[gromacs.git] / src / gromacs / mdlib / csettle.cpp
blob082b14e176c49f8e32b73202394111269e05f9f2
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 #include "gmxpre.h"
39 #include <assert.h>
40 #include <math.h>
41 #include <stdio.h>
43 #include <algorithm>
45 #include "gromacs/math/functions.h"
46 #include "gromacs/math/invertmatrix.h"
47 #include "gromacs/math/vec.h"
48 #include "gromacs/mdlib/constr.h"
49 #include "gromacs/mdtypes/mdatom.h"
50 #include "gromacs/pbcutil/ishift.h"
51 #include "gromacs/pbcutil/pbc.h"
52 #include "gromacs/pbcutil/pbc-simd.h"
53 #include "gromacs/simd/simd.h"
54 #include "gromacs/simd/simd_math.h"
55 #include "gromacs/topology/mtop_util.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/gmxassert.h"
58 #include "gromacs/utility/smalloc.h"
60 using namespace gmx; // TODO: Remove when this file is moved into gmx namespace
62 typedef struct
64 real mO;
65 real mH;
66 real wh;
67 real dOH;
68 real dHH;
69 real ra;
70 real rb;
71 real rc;
72 real irc2;
73 /* For projection */
74 real imO;
75 real imH;
76 real invdOH;
77 real invdHH;
78 matrix invmat;
79 } settleparam_t;
81 typedef struct gmx_settledata
83 settleparam_t massw; /* Parameters for SETTLE for coordinates */
84 settleparam_t mass1; /* Parameters with all masses 1, for forces */
86 int nsettle; /* The number of settles on our rank */
87 int *ow1; /* Index to OW1 atoms, size nsettle + SIMD padding */
88 int *hw2; /* Index to HW2 atoms, size nsettle + SIMD padding */
89 int *hw3; /* Index to HW3 atoms, size nsettle + SIMD padding */
90 real *virfac; /* Virial factor 0 or 1, size nsettle + SIMD pad. */
91 int nalloc; /* Allocation size of ow1, hw2, hw3, virfac */
93 bool bUseSimd; /* Use SIMD intrinsics code, if possible */
94 } t_gmx_settledata;
97 static void init_proj_matrix(real invmO, real invmH, real dOH, real dHH,
98 matrix inverseCouplingMatrix)
100 /* We normalize the inverse masses with invmO for the matrix inversion.
101 * so we can keep using masses of almost zero for frozen particles,
102 * without running out of the float range in invertMatrix.
104 double invmORelative = 1.0;
105 double invmHRelative = invmH/static_cast<double>(invmO);
106 double distanceRatio = dHH/static_cast<double>(dOH);
108 /* Construct the constraint coupling matrix */
109 matrix mat;
110 mat[0][0] = invmORelative + invmHRelative;
111 mat[0][1] = invmORelative*(1.0 - 0.5*gmx::square(distanceRatio));
112 mat[0][2] = invmHRelative*0.5*distanceRatio;
113 mat[1][1] = mat[0][0];
114 mat[1][2] = mat[0][2];
115 mat[2][2] = invmHRelative + invmHRelative;
116 mat[1][0] = mat[0][1];
117 mat[2][0] = mat[0][2];
118 mat[2][1] = mat[1][2];
120 invertMatrix(mat, inverseCouplingMatrix);
122 msmul(inverseCouplingMatrix, 1/invmO, inverseCouplingMatrix);
125 static void settleparam_init(settleparam_t *p,
126 real mO, real mH, real invmO, real invmH,
127 real dOH, real dHH)
129 /* We calculate parameters in double precision to minimize errors.
130 * The velocity correction applied during SETTLE coordinate constraining
131 * introduces a systematic error of approximately 1 bit per atom,
132 * depending on what the compiler does with the code.
134 double wohh;
136 p->mO = mO;
137 p->mH = mH;
138 wohh = mO + 2.0*mH;
139 p->wh = mH/wohh;
140 p->dOH = dOH;
141 p->dHH = dHH;
142 double rc = dHH/2.0;
143 double ra = 2.0*mH*std::sqrt(dOH*dOH - rc*rc)/wohh;
144 p->rb = std::sqrt(dOH*dOH - rc*rc) - ra;
145 p->rc = rc;
146 p->ra = ra;
147 p->irc2 = 1.0/dHH;
149 /* For projection: inverse masses and coupling matrix inversion */
150 p->imO = invmO;
151 p->imH = invmH;
153 p->invdOH = 1.0/dOH;
154 p->invdHH = 1.0/dHH;
156 init_proj_matrix(invmO, invmH, dOH, dHH, p->invmat);
158 if (debug)
160 fprintf(debug, "wh =%g, rc = %g, ra = %g\n",
161 p->wh, p->rc, p->ra);
162 fprintf(debug, "rb = %g, irc2 = %g, dHH = %g, dOH = %g\n",
163 p->rb, p->irc2, p->dHH, p->dOH);
167 gmx_settledata_t settle_init(const gmx_mtop_t *mtop)
169 /* Check that we have only one settle type */
170 int settle_type = -1;
171 gmx_mtop_ilistloop_t iloop = gmx_mtop_ilistloop_init(mtop);
172 t_ilist *ilist;
173 int nmol;
174 const int nral1 = 1 + NRAL(F_SETTLE);
175 while (gmx_mtop_ilistloop_next(iloop, &ilist, &nmol))
177 for (int i = 0; i < ilist[F_SETTLE].nr; i += nral1)
179 if (settle_type == -1)
181 settle_type = ilist[F_SETTLE].iatoms[i];
183 else if (ilist[F_SETTLE].iatoms[i] != settle_type)
185 gmx_fatal(FARGS,
186 "The [molecules] section of your topology specifies more than one block of\n"
187 "a [moleculetype] with a [settles] block. Only one such is allowed.\n"
188 "If you are trying to partition your solvent into different *groups*\n"
189 "(e.g. for freezing, T-coupling, etc.), you are using the wrong approach. Index\n"
190 "files specify groups. Otherwise, you may wish to change the least-used\n"
191 "block of molecules with SETTLE constraints into 3 normal constraints.");
195 GMX_RELEASE_ASSERT(settle_type >= 0, "settle_init called without settles");
197 gmx_settledata_t settled;
199 snew(settled, 1);
201 /* We will not initialize the normal SETTLE parameters here yet,
202 * since the atom (inv)masses can depend on the integrator and
203 * free-energy perturbation. We set mO=-1 to trigger later initialization.
205 settled->massw.mO = -1;
207 real dOH = mtop->ffparams.iparams[settle_type].settle.doh;
208 real dHH = mtop->ffparams.iparams[settle_type].settle.dhh;
209 settleparam_init(&settled->mass1, 1.0, 1.0, 1.0, 1.0, dOH, dHH);
211 settled->ow1 = NULL;
212 settled->hw2 = NULL;
213 settled->hw3 = NULL;
214 settled->virfac = NULL;
215 settled->nalloc = 0;
217 /* Without SIMD configured, this bool is not used */
218 settled->bUseSimd = (getenv("GMX_DISABLE_SIMD_KERNELS") == NULL);
220 return settled;
223 void settle_set_constraints(gmx_settledata_t settled,
224 const t_ilist *il_settle,
225 const t_mdatoms *mdatoms)
227 #if GMX_SIMD_HAVE_REAL
228 const int pack_size = GMX_SIMD_REAL_WIDTH;
229 #else
230 const int pack_size = 1;
231 #endif
233 const int nral1 = 1 + NRAL(F_SETTLE);
234 int nsettle = il_settle->nr/nral1;
235 settled->nsettle = nsettle;
237 if (nsettle > 0)
239 const t_iatom *iatoms = il_settle->iatoms;
241 /* Here we initialize the normal SETTLE parameters */
242 if (settled->massw.mO < 0)
244 int firstO = iatoms[1];
245 int firstH = iatoms[2];
246 settleparam_init(&settled->massw,
247 mdatoms->massT[firstO],
248 mdatoms->massT[firstH],
249 mdatoms->invmass[firstO],
250 mdatoms->invmass[firstH],
251 settled->mass1.dOH,
252 settled->mass1.dHH);
255 if (nsettle + pack_size > settled->nalloc)
257 settled->nalloc = over_alloc_dd(nsettle + pack_size);
258 sfree_aligned(settled->ow1);
259 sfree_aligned(settled->hw2);
260 sfree_aligned(settled->hw3);
261 sfree_aligned(settled->virfac);
262 snew_aligned(settled->ow1, settled->nalloc, 64);
263 snew_aligned(settled->hw2, settled->nalloc, 64);
264 snew_aligned(settled->hw3, settled->nalloc, 64);
265 snew_aligned(settled->virfac, settled->nalloc, 64);
268 for (int i = 0; i < nsettle; i++)
270 settled->ow1[i] = iatoms[i*nral1 + 1];
271 settled->hw2[i] = iatoms[i*nral1 + 2];
272 settled->hw3[i] = iatoms[i*nral1 + 3];
273 /* We should avoid double counting of virial contributions for
274 * SETTLEs that appear in multiple DD domains, so we only count
275 * the contribution on the home range of the oxygen atom.
277 settled->virfac[i] = (iatoms[i*nral1 + 1] < mdatoms->homenr ? 1 : 0);
280 /* Pack the index array to the full SIMD width with copies from
281 * the last normal entry, but with no virial contribution.
283 int end_packed = ((nsettle + pack_size - 1)/pack_size)*pack_size;
284 for (int i = nsettle; i < end_packed; i++)
286 settled->ow1[i] = settled->ow1[nsettle - 1];
287 settled->hw2[i] = settled->hw2[nsettle - 1];
288 settled->hw3[i] = settled->hw3[nsettle - 1];
289 settled->virfac[i] = 0;
294 void settle_proj(gmx_settledata_t settled, int econq,
295 int nsettle, t_iatom iatoms[],
296 const t_pbc *pbc,
297 rvec x[],
298 rvec *der, rvec *derp,
299 int calcvir_atom_end, tensor vir_r_m_dder)
301 /* Settle for projection out constraint components
302 * of derivatives of the coordinates.
303 * Berk Hess 2008-1-10
306 settleparam_t *p;
307 real imO, imH, dOH, dHH, invdOH, invdHH;
308 matrix invmat;
309 int i, m, m2, ow1, hw2, hw3;
310 rvec roh2, roh3, rhh, dc, fc;
312 calcvir_atom_end *= DIM;
314 if (econq == econqForce)
316 p = &settled->mass1;
318 else
320 p = &settled->massw;
322 imO = p->imO;
323 imH = p->imH;
324 copy_mat(p->invmat, invmat);
325 dOH = p->dOH;
326 dHH = p->dHH;
327 invdOH = p->invdOH;
328 invdHH = p->invdHH;
330 #ifdef PRAGMAS
331 #pragma ivdep
332 #endif
334 const int nral1 = 1 + NRAL(F_SETTLE);
336 for (i = 0; i < nsettle; i++)
338 ow1 = iatoms[i*nral1 + 1];
339 hw2 = iatoms[i*nral1 + 2];
340 hw3 = iatoms[i*nral1 + 3];
342 if (pbc == NULL)
344 rvec_sub(x[ow1], x[hw2], roh2);
345 rvec_sub(x[ow1], x[hw3], roh3);
346 rvec_sub(x[hw2], x[hw3], rhh);
348 else
350 pbc_dx_aiuc(pbc, x[ow1], x[hw2], roh2);
351 pbc_dx_aiuc(pbc, x[ow1], x[hw3], roh3);
352 pbc_dx_aiuc(pbc, x[hw2], x[hw3], rhh);
354 svmul(invdOH, roh2, roh2);
355 svmul(invdOH, roh3, roh3);
356 svmul(invdHH, rhh, rhh);
357 /* 18 flops */
359 /* Determine the projections of der on the bonds */
360 clear_rvec(dc);
361 for (m = 0; m < DIM; m++)
363 dc[0] += (der[ow1][m] - der[hw2][m])*roh2[m];
364 dc[1] += (der[ow1][m] - der[hw3][m])*roh3[m];
365 dc[2] += (der[hw2][m] - der[hw3][m])*rhh [m];
367 /* 27 flops */
369 /* Determine the correction for the three bonds */
370 mvmul(invmat, dc, fc);
371 /* 15 flops */
373 /* Subtract the corrections from derp */
374 for (m = 0; m < DIM; m++)
376 derp[ow1][m] -= imO*( fc[0]*roh2[m] + fc[1]*roh3[m]);
377 derp[hw2][m] -= imH*(-fc[0]*roh2[m] + fc[2]*rhh [m]);
378 derp[hw3][m] -= imH*(-fc[1]*roh3[m] - fc[2]*rhh [m]);
381 /* 45 flops */
383 if (ow1 < calcvir_atom_end)
385 /* Determining r \dot m der is easy,
386 * since fc contains the mass weighted corrections for der.
389 for (m = 0; m < DIM; m++)
391 for (m2 = 0; m2 < DIM; m2++)
393 vir_r_m_dder[m][m2] +=
394 dOH*roh2[m]*roh2[m2]*fc[0] +
395 dOH*roh3[m]*roh3[m2]*fc[1] +
396 dHH*rhh [m]*rhh [m2]*fc[2];
404 /* The actual settle code, templated for real/SimdReal and for optimization */
405 template<typename T, typename TypeBool, int packSize,
406 typename TypePbc,
407 bool bCorrectVelocity,
408 bool bCalcVirial>
409 static void settleTemplate(const gmx_settledata_t settled,
410 int settleStart, int settleEnd,
411 const TypePbc pbc,
412 const real *x, real *xprime,
413 real invdt, real * gmx_restrict v,
414 tensor vir_r_m_dr,
415 bool *bErrorHasOccurred)
417 /* ******************************************************************* */
418 /* ** */
419 /* Original code by Shuichi Miyamoto, last update Oct. 1, 1992 ** */
420 /* ** */
421 /* Algorithm changes by Berk Hess: ** */
422 /* 2004-07-15 Convert COM to double precision to avoid drift ** */
423 /* 2006-10-16 Changed velocity update to use differences ** */
424 /* 2012-09-24 Use oxygen as reference instead of COM ** */
425 /* 2016-02 Complete rewrite of the code for SIMD ** */
426 /* ** */
427 /* Reference for the SETTLE algorithm ** */
428 /* S. Miyamoto et al., J. Comp. Chem., 13, 952 (1992). ** */
429 /* ** */
430 /* ******************************************************************* */
432 assert(settleStart % packSize == 0);
433 assert(settleEnd % packSize == 0);
435 TypeBool bError = TypeBool(false);
437 settleparam_t *p = &settled->massw;
438 T wh = T(p->wh);
439 T rc = T(p->rc);
440 T ra = T(p->ra);
441 T rb = T(p->rb);
442 T irc2 = T(p->irc2);
443 T mO = T(p->mO);
444 T mH = T(p->mH);
446 T almost_zero = T(1e-12);
448 T sum_r_m_dr[DIM][DIM];
450 if (bCalcVirial)
452 for (int d2 = 0; d2 < DIM; d2++)
454 for (int d = 0; d < DIM; d++)
456 sum_r_m_dr[d2][d] = T(0);
461 for (int i = settleStart; i < settleEnd; i += packSize)
463 /* Here we pad up to packSize with copies from the last valid entry.
464 * This gives correct results, since we store (not increment) all
465 * output, so we store the same output multiple times.
467 const int *ow1 = settled->ow1 + i;
468 const int *hw2 = settled->hw2 + i;
469 const int *hw3 = settled->hw3 + i;
471 T x_ow1[DIM], x_hw2[DIM], x_hw3[DIM];
473 gatherLoadUTranspose<3>(x, ow1, &x_ow1[XX], &x_ow1[YY], &x_ow1[ZZ]);
474 gatherLoadUTranspose<3>(x, hw2, &x_hw2[XX], &x_hw2[YY], &x_hw2[ZZ]);
475 gatherLoadUTranspose<3>(x, hw3, &x_hw3[XX], &x_hw3[YY], &x_hw3[ZZ]);
477 T xprime_ow1[DIM], xprime_hw2[DIM], xprime_hw3[DIM];
479 gatherLoadUTranspose<3>(xprime, ow1, &xprime_ow1[XX], &xprime_ow1[YY], &xprime_ow1[ZZ]);
480 gatherLoadUTranspose<3>(xprime, hw2, &xprime_hw2[XX], &xprime_hw2[YY], &xprime_hw2[ZZ]);
481 gatherLoadUTranspose<3>(xprime, hw3, &xprime_hw3[XX], &xprime_hw3[YY], &xprime_hw3[ZZ]);
483 T dist21[DIM], dist31[DIM];
484 T doh2[DIM], doh3[DIM];
485 T sh_hw2[DIM], sh_hw3[DIM];
487 pbc_dx_aiuc(pbc, x_hw2, x_ow1, dist21);
489 pbc_dx_aiuc(pbc, x_hw3, x_ow1, dist31);
491 /* Tedious way of doing pbc */
492 pbc_dx_aiuc(pbc, xprime_hw2, xprime_ow1, doh2);
493 for (int d = 0; d < DIM; d++)
495 sh_hw2[d] = xprime_hw2[d] - (xprime_ow1[d] + doh2[d]);
496 xprime_hw2[d] = xprime_hw2[d] - sh_hw2[d];
498 pbc_dx_aiuc(pbc, xprime_hw3, xprime_ow1, doh3);
499 for (int d = 0; d < DIM; d++)
501 sh_hw3[d] = xprime_hw3[d] - (xprime_ow1[d] + doh3[d]);
502 xprime_hw3[d] = xprime_hw3[d] - sh_hw3[d];
505 /* Not calculating the center of mass using the oxygen position
506 * and the O-H distances, as done below, will make SETTLE
507 * the largest source of energy drift for simulations of water,
508 * as then the oxygen coordinate is multiplied by 0.89 at every step,
509 * which can then transfer a systematic rounding to the oxygen velocity.
511 T a1[DIM], com[DIM];
512 for (int d = 0; d < DIM; d++)
514 a1[d] = -(doh2[d] + doh3[d]) * wh;
515 com[d] = xprime_ow1[d] - a1[d];
517 T b1[DIM];
518 for (int d = 0; d < DIM; d++)
520 b1[d] = xprime_hw2[d] - com[d];
522 T c1[DIM];
523 for (int d = 0; d < DIM; d++)
525 c1[d] = xprime_hw3[d] - com[d];
527 /* 15 flops */
529 T xakszd = dist21[YY] * dist31[ZZ] - dist21[ZZ] * dist31[YY];
530 T yakszd = dist21[ZZ] * dist31[XX] - dist21[XX] * dist31[ZZ];
531 T zakszd = dist21[XX] * dist31[YY] - dist21[YY] * dist31[XX];
532 T xaksxd = a1[YY] * zakszd - a1[ZZ] * yakszd;
533 T yaksxd = a1[ZZ] * xakszd - a1[XX] * zakszd;
534 T zaksxd = a1[XX] * yakszd - a1[YY] * xakszd;
535 T xaksyd = yakszd * zaksxd - zakszd * yaksxd;
536 T yaksyd = zakszd * xaksxd - xakszd * zaksxd;
537 T zaksyd = xakszd * yaksxd - yakszd * xaksxd;
538 /* 27 flops */
540 T axlng = gmx::invsqrt(xaksxd * xaksxd + yaksxd * yaksxd + zaksxd * zaksxd);
541 T aylng = gmx::invsqrt(xaksyd * xaksyd + yaksyd * yaksyd + zaksyd * zaksyd);
542 T azlng = gmx::invsqrt(xakszd * xakszd + yakszd * yakszd + zakszd * zakszd);
544 T trns1[DIM], trns2[DIM], trns3[DIM];
546 trns1[XX] = xaksxd * axlng;
547 trns2[XX] = yaksxd * axlng;
548 trns3[XX] = zaksxd * axlng;
549 trns1[YY] = xaksyd * aylng;
550 trns2[YY] = yaksyd * aylng;
551 trns3[YY] = zaksyd * aylng;
552 trns1[ZZ] = xakszd * azlng;
553 trns2[ZZ] = yakszd * azlng;
554 trns3[ZZ] = zakszd * azlng;
555 /* 24 flops */
557 T b0d[2], c0d[2];
559 for (int d = 0; d < 2; d++)
561 b0d[d] = trns1[d] * dist21[XX] + trns2[d] * dist21[YY] + trns3[d] * dist21[ZZ];
562 c0d[d] = trns1[d] * dist31[XX] + trns2[d] * dist31[YY] + trns3[d] * dist31[ZZ];
565 T a1d_z, b1d[DIM], c1d[DIM];
567 a1d_z = trns1[ZZ] * a1[XX] + trns2[ZZ] * a1[YY] + trns3[ZZ] * a1[ZZ];
568 for (int d = 0; d < DIM; d++)
570 b1d[d] = trns1[d] * b1[XX] + trns2[d] * b1[YY] + trns3[d] * b1[ZZ];
571 c1d[d] = trns1[d] * c1[XX] + trns2[d] * c1[YY] + trns3[d] * c1[ZZ];
573 /* 65 flops */
575 T tmp, tmp2;
577 T sinphi = a1d_z * gmx::invsqrt(ra*ra);
578 tmp2 = 1.0 - sinphi * sinphi;
580 /* If tmp2 gets close to or beyond zero we have severly distorted
581 * water molecules and we should terminate the simulation.
582 * Below we take the max with almost_zero to continue the loop.
584 bError = bError || (tmp2 <= almost_zero);
586 tmp2 = max(tmp2, almost_zero);
587 tmp = gmx::invsqrt(tmp2);
588 T cosphi = tmp2*tmp;
589 T sinpsi = (b1d[ZZ] - c1d[ZZ]) * irc2 * tmp;
590 tmp2 = 1.0 - sinpsi * sinpsi;
592 T cospsi = tmp2*gmx::invsqrt(tmp2);
593 /* 46 flops */
595 T a2d_y = ra * cosphi;
596 T b2d_x = -rc * cospsi;
597 T t1 = -rb * cosphi;
598 T t2 = rc * sinpsi * sinphi;
599 T b2d_y = t1 - t2;
600 T c2d_y = t1 + t2;
601 /* 7 flops */
603 /* --- Step3 al,be,ga --- */
604 T alpha = b2d_x * (b0d[XX] - c0d[XX]) + b0d[YY] * b2d_y + c0d[YY] * c2d_y;
605 T beta = b2d_x * (c0d[YY] - b0d[YY]) + b0d[XX] * b2d_y + c0d[XX] * c2d_y;
606 T gamma = b0d[XX] * b1d[YY] - b1d[XX] * b0d[YY] + c0d[XX] * c1d[YY] - c1d[XX] * c0d[YY];
607 T al2be2 = alpha * alpha + beta * beta;
608 tmp2 = (al2be2 - gamma * gamma);
609 T sinthe = (alpha * gamma - beta * tmp2*gmx::invsqrt(tmp2)) * gmx::invsqrt(al2be2*al2be2);
610 /* 47 flops */
612 /* --- Step4 A3' --- */
613 tmp2 = 1.0 - sinthe * sinthe;
614 T costhe = tmp2*gmx::invsqrt(tmp2);
616 T a3d[DIM], b3d[DIM], c3d[DIM];
618 a3d[XX] = -a2d_y * sinthe;
619 a3d[YY] = a2d_y * costhe;
620 a3d[ZZ] = a1d_z;
621 b3d[XX] = b2d_x * costhe - b2d_y * sinthe;
622 b3d[YY] = b2d_x * sinthe + b2d_y * costhe;
623 b3d[ZZ] = b1d[ZZ];
624 c3d[XX] = -b2d_x * costhe - c2d_y * sinthe;
625 c3d[YY] = -b2d_x * sinthe + c2d_y * costhe;
626 c3d[ZZ] = c1d[ZZ];
627 /* 26 flops */
629 /* --- Step5 A3 --- */
630 T a3[DIM], b3[DIM], c3[DIM];
632 a3[XX] = trns1[XX]*a3d[XX] + trns1[YY]*a3d[YY] + trns1[ZZ]*a3d[ZZ];
633 a3[YY] = trns2[XX]*a3d[XX] + trns2[YY]*a3d[YY] + trns2[ZZ]*a3d[ZZ];
634 a3[ZZ] = trns3[XX]*a3d[XX] + trns3[YY]*a3d[YY] + trns3[ZZ]*a3d[ZZ];
635 b3[XX] = trns1[XX]*b3d[XX] + trns1[YY]*b3d[YY] + trns1[ZZ]*b3d[ZZ];
636 b3[YY] = trns2[XX]*b3d[XX] + trns2[YY]*b3d[YY] + trns2[ZZ]*b3d[ZZ];
637 b3[ZZ] = trns3[XX]*b3d[XX] + trns3[YY]*b3d[YY] + trns3[ZZ]*b3d[ZZ];
638 c3[XX] = trns1[XX]*c3d[XX] + trns1[YY]*c3d[YY] + trns1[ZZ]*c3d[ZZ];
639 c3[YY] = trns2[XX]*c3d[XX] + trns2[YY]*c3d[YY] + trns2[ZZ]*c3d[ZZ];
640 c3[ZZ] = trns3[XX]*c3d[XX] + trns3[YY]*c3d[YY] + trns3[ZZ]*c3d[ZZ];
641 /* 45 flops */
643 /* Compute and store the corrected new coordinate */
644 for (int d = 0; d < DIM; d++)
646 xprime_ow1[d] = com[d] + a3[d];
648 for (int d = 0; d < DIM; d++)
650 xprime_hw2[d] = com[d] + b3[d] + sh_hw2[d];;
652 for (int d = 0; d < DIM; d++)
654 xprime_hw3[d] = com[d] + c3[d] + sh_hw3[d];
656 /* 9 flops + 6 pbc flops */
658 transposeScatterStoreU<3>(xprime, ow1, xprime_ow1[XX], xprime_ow1[YY], xprime_ow1[ZZ]);
659 transposeScatterStoreU<3>(xprime, hw2, xprime_hw2[XX], xprime_hw2[YY], xprime_hw2[ZZ]);
660 transposeScatterStoreU<3>(xprime, hw3, xprime_hw3[XX], xprime_hw3[YY], xprime_hw3[ZZ]);
662 // cppcheck-suppress duplicateExpression
663 if (bCorrectVelocity || bCalcVirial)
665 T da[DIM], db[DIM], dc[DIM];
666 for (int d = 0; d < DIM; d++)
668 da[d] = a3[d] - a1[d];
670 for (int d = 0; d < DIM; d++)
672 db[d] = b3[d] - b1[d];
674 for (int d = 0; d < DIM; d++)
676 dc[d] = c3[d] - c1[d];
678 /* 9 flops */
680 if (bCorrectVelocity)
682 T v_ow1[DIM], v_hw2[DIM], v_hw3[DIM];
684 gatherLoadUTranspose<3>(v, ow1, &v_ow1[XX], &v_ow1[YY], &v_ow1[ZZ]);
685 gatherLoadUTranspose<3>(v, hw2, &v_hw2[XX], &v_hw2[YY], &v_hw2[ZZ]);
686 gatherLoadUTranspose<3>(v, hw3, &v_hw3[XX], &v_hw3[YY], &v_hw3[ZZ]);
688 /* Add the position correction divided by dt to the velocity */
689 for (int d = 0; d < DIM; d++)
691 v_ow1[d] = gmx::fma(da[d], invdt, v_ow1[d]);
693 for (int d = 0; d < DIM; d++)
695 v_hw2[d] = gmx::fma(db[d], invdt, v_hw2[d]);
697 for (int d = 0; d < DIM; d++)
699 v_hw3[d] = gmx::fma(dc[d], invdt, v_hw3[d]);
701 /* 3*6 flops */
703 transposeScatterStoreU<3>(v, ow1, v_ow1[XX], v_ow1[YY], v_ow1[ZZ]);
704 transposeScatterStoreU<3>(v, hw2, v_hw2[XX], v_hw2[YY], v_hw2[ZZ]);
705 transposeScatterStoreU<3>(v, hw3, v_hw3[XX], v_hw3[YY], v_hw3[ZZ]);
708 if (bCalcVirial)
710 /* Filter out the non-local settles */
711 T filter = load(settled->virfac + i);
712 T mOf = filter*mO;
713 T mHf = filter*mH;
715 T mdo[DIM], mdb[DIM], mdc[DIM];
717 for (int d = 0; d < DIM; d++)
719 mdb[d] = mHf*db[d];
720 mdc[d] = mHf*dc[d];
721 mdo[d] = mOf*da[d] + mdb[d] + mdc[d];
724 for (int d2 = 0; d2 < DIM; d2++)
726 for (int d = 0; d < DIM; d++)
728 sum_r_m_dr[d2][d] = sum_r_m_dr[d2][d] -
729 (x_ow1[d2]*mdo[d] +
730 dist21[d2]*mdb[d] +
731 dist31[d2]*mdc[d]);
734 /* 71 flops */
739 if (bCalcVirial)
741 for (int d2 = 0; d2 < DIM; d2++)
743 for (int d = 0; d < DIM; d++)
745 vir_r_m_dr[d2][d] += reduce(sum_r_m_dr[d2][d]);
750 *bErrorHasOccurred = anyTrue(bError);
753 /* Wrapper template function that divides the settles over threads
754 * and instantiates the core template with instantiated booleans.
756 template<typename T, typename TypeBool, int packSize, typename TypePbc>
757 static void settleTemplateWrapper(gmx_settledata_t settled,
758 int nthread, int thread,
759 TypePbc pbc,
760 const real x[], real xprime[],
761 real invdt, real *v,
762 bool bCalcVirial, tensor vir_r_m_dr,
763 bool *bErrorHasOccurred)
765 /* We need to assign settles to threads in groups of pack_size */
766 int numSettlePacks = (settled->nsettle + packSize - 1)/packSize;
767 /* Round the end value up to give thread 0 more work */
768 int settleStart = ((numSettlePacks* thread + nthread - 1)/nthread)*packSize;
769 int settleEnd = ((numSettlePacks*(thread + 1) + nthread - 1)/nthread)*packSize;
771 if (v != NULL)
773 if (!bCalcVirial)
775 settleTemplate<T, TypeBool, packSize,
776 TypePbc,
777 true,
778 false>
779 (settled, settleStart, settleEnd,
780 pbc,
781 x, xprime,
782 invdt, v,
783 NULL,
784 bErrorHasOccurred);
786 else
788 settleTemplate<T, TypeBool, packSize,
789 TypePbc,
790 true,
791 true>
792 (settled, settleStart, settleEnd,
793 pbc,
794 x, xprime,
795 invdt, v,
796 vir_r_m_dr,
797 bErrorHasOccurred);
800 else
802 if (!bCalcVirial)
804 settleTemplate<T, TypeBool, packSize,
805 TypePbc,
806 false,
807 false>
808 (settled, settleStart, settleEnd,
809 pbc,
810 x, xprime,
811 invdt, v,
812 NULL,
813 bErrorHasOccurred);
815 else
817 settleTemplate<T, TypeBool, packSize,
818 TypePbc,
819 false,
820 true>
821 (settled, settleStart, settleEnd,
822 pbc,
823 x, xprime,
824 invdt, v,
825 vir_r_m_dr,
826 bErrorHasOccurred);
831 void csettle(gmx_settledata_t settled,
832 int nthread, int thread,
833 const t_pbc *pbc,
834 const real x[], real xprime[],
835 real invdt, real *v,
836 bool bCalcVirial, tensor vir_r_m_dr,
837 bool *bErrorHasOccurred)
839 #if GMX_SIMD_HAVE_REAL
840 if (settled->bUseSimd)
842 /* Convert the pbc struct for SIMD */
843 GMX_ALIGNED(real, GMX_SIMD_REAL_WIDTH) pbcSimd[9*GMX_SIMD_REAL_WIDTH];
844 set_pbc_simd(pbc, pbcSimd);
846 settleTemplateWrapper<SimdReal, SimdBool, GMX_SIMD_REAL_WIDTH,
847 const real *>(settled,
848 nthread, thread,
849 pbcSimd,
850 x, xprime,
851 invdt,
853 bCalcVirial, vir_r_m_dr,
854 bErrorHasOccurred);
856 else
857 #endif
859 /* This construct is needed because pbc_dx_aiuc doesn't accept pbc=NULL */
860 t_pbc pbcNo;
861 const t_pbc *pbcNonNull;
863 if (pbc != NULL)
865 pbcNonNull = pbc;
867 else
869 set_pbc(&pbcNo, epbcNONE, NULL);
870 pbcNonNull = &pbcNo;
873 settleTemplateWrapper<real, bool, 1,
874 const t_pbc *>(settled,
875 nthread, thread,
876 pbcNonNull,
877 x, xprime,
878 invdt,
880 bCalcVirial, vir_r_m_dr,
881 bErrorHasOccurred);