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.
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
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 */
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 */
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
,
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.
143 double ra
= 2.0*mH
*std::sqrt(dOH
*dOH
- rc
*rc
)/wohh
;
144 p
->rb
= std::sqrt(dOH
*dOH
- rc
*rc
) - ra
;
149 /* For projection: inverse masses and coupling matrix inversion */
156 init_proj_matrix(invmO
, invmH
, dOH
, dHH
, p
->invmat
);
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
);
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
)
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
;
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
);
214 settled
->virfac
= NULL
;
217 /* Without SIMD configured, this bool is not used */
218 settled
->bUseSimd
= (getenv("GMX_DISABLE_SIMD_KERNELS") == NULL
);
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
;
230 const int pack_size
= 1;
233 const int nral1
= 1 + NRAL(F_SETTLE
);
234 int nsettle
= il_settle
->nr
/nral1
;
235 settled
->nsettle
= nsettle
;
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
],
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
[],
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
307 real imO
, imH
, dOH
, dHH
, invdOH
, invdHH
;
309 int i
, m
, m2
, ow1
, hw2
, hw3
;
310 rvec roh2
, roh3
, rhh
, dc
, fc
;
312 calcvir_atom_end
*= DIM
;
314 if (econq
== econqForce
)
324 copy_mat(p
->invmat
, invmat
);
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];
344 rvec_sub(x
[ow1
], x
[hw2
], roh2
);
345 rvec_sub(x
[ow1
], x
[hw3
], roh3
);
346 rvec_sub(x
[hw2
], x
[hw3
], rhh
);
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
);
359 /* Determine the projections of der on the bonds */
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
];
369 /* Determine the correction for the three bonds */
370 mvmul(invmat
, dc
, fc
);
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
]);
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
,
407 bool bCorrectVelocity
,
409 static void settleTemplate(const gmx_settledata_t settled
,
410 int settleStart
, int settleEnd
,
412 const real
*x
, real
*xprime
,
413 real invdt
, real
* gmx_restrict v
,
415 bool *bErrorHasOccurred
)
417 /* ******************************************************************* */
419 /* Original code by Shuichi Miyamoto, last update Oct. 1, 1992 ** */
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 ** */
427 /* Reference for the SETTLE algorithm ** */
428 /* S. Miyamoto et al., J. Comp. Chem., 13, 952 (1992). ** */
430 /* ******************************************************************* */
432 assert(settleStart
% packSize
== 0);
433 assert(settleEnd
% packSize
== 0);
435 TypeBool bError
= TypeBool(false);
437 settleparam_t
*p
= &settled
->massw
;
446 T almost_zero
= T(1e-12);
448 T sum_r_m_dr
[DIM
][DIM
];
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.
512 for (int d
= 0; d
< DIM
; d
++)
514 a1
[d
] = -(doh2
[d
] + doh3
[d
]) * wh
;
515 com
[d
] = xprime_ow1
[d
] - a1
[d
];
518 for (int d
= 0; d
< DIM
; d
++)
520 b1
[d
] = xprime_hw2
[d
] - com
[d
];
523 for (int d
= 0; d
< DIM
; d
++)
525 c1
[d
] = xprime_hw3
[d
] - com
[d
];
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
;
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
;
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
];
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
);
589 T sinpsi
= (b1d
[ZZ
] - c1d
[ZZ
]) * irc2
* tmp
;
590 tmp2
= 1.0 - sinpsi
* sinpsi
;
592 T cospsi
= tmp2
*gmx::invsqrt(tmp2
);
595 T a2d_y
= ra
* cosphi
;
596 T b2d_x
= -rc
* cospsi
;
598 T t2
= rc
* sinpsi
* sinphi
;
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
);
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
;
621 b3d
[XX
] = b2d_x
* costhe
- b2d_y
* sinthe
;
622 b3d
[YY
] = b2d_x
* sinthe
+ b2d_y
* costhe
;
624 c3d
[XX
] = -b2d_x
* costhe
- c2d_y
* sinthe
;
625 c3d
[YY
] = -b2d_x
* sinthe
+ c2d_y
* costhe
;
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
];
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
];
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
]);
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
]);
710 /* Filter out the non-local settles */
711 T filter
= load(settled
->virfac
+ i
);
715 T mdo
[DIM
], mdb
[DIM
], mdc
[DIM
];
717 for (int d
= 0; d
< DIM
; 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
] -
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
,
760 const real x
[], real xprime
[],
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
;
775 settleTemplate
<T
, TypeBool
, packSize
,
779 (settled
, settleStart
, settleEnd
,
788 settleTemplate
<T
, TypeBool
, packSize
,
792 (settled
, settleStart
, settleEnd
,
804 settleTemplate
<T
, TypeBool
, packSize
,
808 (settled
, settleStart
, settleEnd
,
817 settleTemplate
<T
, TypeBool
, packSize
,
821 (settled
, settleStart
, settleEnd
,
831 void csettle(gmx_settledata_t settled
,
832 int nthread
, int thread
,
834 const real x
[], real xprime
[],
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
,
853 bCalcVirial
, vir_r_m_dr
,
859 /* This construct is needed because pbc_dx_aiuc doesn't accept pbc=NULL */
861 const t_pbc
*pbcNonNull
;
869 set_pbc(&pbcNo
, epbcNONE
, NULL
);
873 settleTemplateWrapper
<real
, bool, 1,
874 const t_pbc
*>(settled
,
880 bCalcVirial
, vir_r_m_dr
,