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.
7 * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
48 #include "gromacs/domdec/domdec_struct.h"
49 #include "gromacs/gmxlib/network.h"
50 #include "gromacs/gmxlib/nrnb.h"
51 #include "gromacs/listed_forces/disre.h"
52 #include "gromacs/listed_forces/orires.h"
53 #include "gromacs/math/functions.h"
54 #include "gromacs/math/paddedvector.h"
55 #include "gromacs/math/units.h"
56 #include "gromacs/math/vec.h"
57 #include "gromacs/mdlib/boxdeformation.h"
58 #include "gromacs/mdlib/constr.h"
59 #include "gromacs/mdlib/gmx_omp_nthreads.h"
60 #include "gromacs/mdlib/stat.h"
61 #include "gromacs/mdlib/tgroup.h"
62 #include "gromacs/mdtypes/commrec.h"
63 #include "gromacs/mdtypes/fcdata.h"
64 #include "gromacs/mdtypes/group.h"
65 #include "gromacs/mdtypes/inputrec.h"
66 #include "gromacs/mdtypes/md_enums.h"
67 #include "gromacs/mdtypes/state.h"
68 #include "gromacs/pbcutil/pbc.h"
69 #include "gromacs/pulling/pull.h"
70 #include "gromacs/random/tabulatednormaldistribution.h"
71 #include "gromacs/random/threefry.h"
72 #include "gromacs/simd/simd.h"
73 #include "gromacs/timing/wallcycle.h"
74 #include "gromacs/topology/atoms.h"
75 #include "gromacs/utility/exceptions.h"
76 #include "gromacs/utility/fatalerror.h"
77 #include "gromacs/utility/futil.h"
78 #include "gromacs/utility/gmxassert.h"
79 #include "gromacs/utility/smalloc.h"
81 using namespace gmx; // TODO: Remove when this file is moved into gmx namespace
96 std::vector<real> bd_rf;
98 std::vector<gmx_sd_const_t> sdc;
99 std::vector<gmx_sd_sigma_t> sdsig;
100 /* andersen temperature control stuff */
101 std::vector<bool> randomize_group;
102 std::vector<real> boltzfac;
104 explicit gmx_stochd_t(const t_inputrec& inputRecord);
107 //! pImpled implementation for Update
112 Impl(const t_inputrec& inputRecord, BoxDeformation* boxDeformation);
116 void update_coords(const t_inputrec& inputRecord,
119 bool havePartiallyFrozenAtoms,
120 gmx::ArrayRef<const ParticleType> ptype,
121 gmx::ArrayRef<const real> invMass,
122 gmx::ArrayRef<const rvec> invMassPerDim,
124 const gmx::ArrayRefWithPadding<const gmx::RVec>& f,
126 const gmx_ekindata_t* ekind,
130 bool haveConstraints);
132 void finish_update(const t_inputrec& inputRecord,
133 bool havePartiallyFrozenAtoms,
135 gmx::ArrayRef<const unsigned short> cFREEZE,
137 gmx_wallcycle* wcycle,
138 bool haveConstraints);
140 void update_sd_second_half(const t_inputrec& inputRecord,
144 gmx::ArrayRef<const ParticleType> ptype,
145 gmx::ArrayRef<const real> invMass,
149 gmx_wallcycle* wcycle,
150 gmx::Constraints* constr,
154 void update_for_constraint_virial(const t_inputrec& inputRecord,
156 bool havePartiallyFrozenAtoms,
157 gmx::ArrayRef<const real> invmass,
158 gmx::ArrayRef<const rvec> invMassPerDim,
159 const t_state& state,
160 const gmx::ArrayRefWithPadding<const gmx::RVec>& f,
161 const gmx_ekindata_t& ekind);
163 void update_temperature_constants(const t_inputrec& inputRecord);
165 const std::vector<bool>& getAndersenRandomizeGroup() const { return sd_.randomize_group; }
167 const std::vector<real>& getBoltzmanFactor() const { return sd_.boltzfac; }
169 PaddedVector<RVec>* xp() { return &xp_; }
171 BoxDeformation* deform() const { return deform_; }
173 //! Group index for freezing
174 gmx::ArrayRef<const unsigned short> cFREEZE_;
175 //! Group index for temperature coupling
176 gmx::ArrayRef<const unsigned short> cTC_;
179 //! stochastic dynamics struct
181 //! xprime for constraint algorithms
182 PaddedVector<RVec> xp_;
183 //! Box deformation handler (or nullptr if inactive).
184 BoxDeformation* deform_ = nullptr;
187 Update::Update(const t_inputrec& inputRecord, BoxDeformation* boxDeformation) :
188 impl_(new Impl(inputRecord, boxDeformation)){};
192 const std::vector<bool>& Update::getAndersenRandomizeGroup() const
194 return impl_->getAndersenRandomizeGroup();
197 const std::vector<real>& Update::getBoltzmanFactor() const
199 return impl_->getBoltzmanFactor();
202 PaddedVector<RVec>* Update::xp()
207 BoxDeformation* Update::deform() const
209 return impl_->deform();
212 void Update::update_coords(const t_inputrec& inputRecord,
215 const bool havePartiallyFrozenAtoms,
216 gmx::ArrayRef<const ParticleType> ptype,
217 gmx::ArrayRef<const real> invMass,
218 gmx::ArrayRef<const rvec> invMassPerDim,
220 const gmx::ArrayRefWithPadding<const gmx::RVec>& f,
222 const gmx_ekindata_t* ekind,
226 const bool haveConstraints)
228 return impl_->update_coords(inputRecord,
231 havePartiallyFrozenAtoms,
245 void Update::finish_update(const t_inputrec& inputRecord,
246 const bool havePartiallyFrozenAtoms,
249 gmx_wallcycle* wcycle,
250 const bool haveConstraints)
252 return impl_->finish_update(
253 inputRecord, havePartiallyFrozenAtoms, homenr, impl_->cFREEZE_, state, wcycle, haveConstraints);
256 void Update::update_sd_second_half(const t_inputrec& inputRecord,
260 gmx::ArrayRef<const ParticleType> ptype,
261 gmx::ArrayRef<const real> invMass,
265 gmx_wallcycle* wcycle,
266 gmx::Constraints* constr,
270 return impl_->update_sd_second_half(
271 inputRecord, step, dvdlambda, homenr, ptype, invMass, state, cr, nrnb, wcycle, constr, do_log, do_ene);
274 void Update::update_for_constraint_virial(const t_inputrec& inputRecord,
276 const bool havePartiallyFrozenAtoms,
277 gmx::ArrayRef<const real> invmass,
278 gmx::ArrayRef<const rvec> invMassPerDim,
279 const t_state& state,
280 const gmx::ArrayRefWithPadding<const gmx::RVec>& f,
281 const gmx_ekindata_t& ekind)
283 return impl_->update_for_constraint_virial(
284 inputRecord, homenr, havePartiallyFrozenAtoms, invmass, invMassPerDim, state, f, ekind);
287 void Update::update_temperature_constants(const t_inputrec& inputRecord)
289 return impl_->update_temperature_constants(inputRecord);
292 /*! \brief Sets whether we store the updated velocities */
293 enum class StoreUpdatedVelocities
295 yes, //!< Store the updated velocities
296 no //!< Do not store the updated velocities
299 /*! \brief Sets the number of different temperature coupling values */
300 enum class NumTempScaleValues
302 single, //!< Single T-scaling value (either one group or all values =1)
303 multiple //!< Multiple T-scaling values, need to use T-group indices
306 /*! \brief Sets if to apply no or diagonal Parrinello-Rahman pressure scaling
308 * Note that this enum is only used in updateMDLeapfrogSimple(), which does
309 * not handle fully anistropic Parrinello-Rahman scaling, so we only have
310 * options \p no and \p diagonal here and no anistropic option.
312 enum class ApplyParrinelloRahmanVScaling
314 no, //!< Do not apply velocity scaling (not a PR-coupling run or step)
315 diagonal //!< Apply velocity scaling using a diagonal matrix
318 /*! \brief Integrate using leap-frog with T-scaling and optionally diagonal Parrinello-Rahman p-coupling
320 * \tparam storeUpdatedVelocities Tells whether we should store the updated velocities
321 * \tparam numTempScaleValues The number of different T-couple values
322 * \tparam applyPRVScaling Apply Parrinello-Rahman velocity scaling
323 * \param[in] start Index of first atom to update
324 * \param[in] nrend Last atom to update: \p nrend - 1
325 * \param[in] dt The time step
326 * \param[in] dtPressureCouple Time step for pressure coupling
327 * \param[in] invMassPerDim 1/mass per atom and dimension
328 * \param[in] tcstat Temperature coupling information
329 * \param[in] cTC T-coupling group index per atom
330 * \param[in] pRVScaleMatrixDiagonal Parrinello-Rahman v-scale matrix diagonal
331 * \param[in] x Input coordinates
332 * \param[out] xprime Updated coordinates
333 * \param[inout] v Velocities, type either rvec* or const rvec*
334 * \param[in] f Forces
336 * We expect this template to get good SIMD acceleration by most compilers,
337 * unlike the more complex general template.
338 * Note that we might get even better SIMD acceleration when we introduce
339 * aligned (and padded) memory, possibly with some hints for the compilers.
341 template<StoreUpdatedVelocities storeUpdatedVelocities, NumTempScaleValues numTempScaleValues, ApplyParrinelloRahmanVScaling applyPRVScaling, typename VelocityType>
342 static std::enable_if_t<std::is_same<VelocityType, rvec>::value || std::is_same<VelocityType, const rvec>::value, void>
343 updateMDLeapfrogSimple(int start,
346 real dtPressureCouple,
347 gmx::ArrayRef<const rvec> invMassPerDim,
348 gmx::ArrayRef<const t_grp_tcstat> tcstat,
349 gmx::ArrayRef<const unsigned short> cTC,
350 const rvec pRVScaleMatrixDiagonal,
351 const rvec* gmx_restrict x,
352 rvec* gmx_restrict xprime,
353 VelocityType* gmx_restrict v,
354 const rvec* gmx_restrict f)
358 if (numTempScaleValues == NumTempScaleValues::single)
360 lambdaGroup = tcstat[0].lambda;
363 for (int a = start; a < nrend; a++)
365 if (numTempScaleValues == NumTempScaleValues::multiple)
367 lambdaGroup = tcstat[cTC[a]].lambda;
370 for (int d = 0; d < DIM; d++)
372 /* Note that using rvec invMassPerDim results in more efficient
373 * SIMD code, but this increases the cache pressure.
374 * For large systems with PME on the CPU this slows down the
375 * (then already slow) update by 20%. If all data remains in cache,
376 * using rvec is much faster.
378 real vNew = lambdaGroup * v[a][d] + f[a][d] * invMassPerDim[a][d] * dt;
380 if (applyPRVScaling == ApplyParrinelloRahmanVScaling::diagonal)
382 vNew -= dtPressureCouple * pRVScaleMatrixDiagonal[d] * v[a][d];
384 if constexpr (storeUpdatedVelocities == StoreUpdatedVelocities::yes)
388 // NOLINTNEXTLINE(readability-misleading-indentation) remove when clang-tidy-13 is required
389 xprime[a][d] = x[a][d] + vNew * dt;
394 #if GMX_SIMD && GMX_SIMD_HAVE_REAL
395 # define GMX_HAVE_SIMD_UPDATE 1
397 # define GMX_HAVE_SIMD_UPDATE 0
400 #if GMX_HAVE_SIMD_UPDATE
402 /*! \brief Load (aligned) the contents of GMX_SIMD_REAL_WIDTH rvec elements sequentially into 3 SIMD registers
404 * The loaded output is:
405 * \p r0: { r[index][XX], r[index][YY], ... }
407 * \p r2: { ..., r[index+GMX_SIMD_REAL_WIDTH-1][YY], r[index+GMX_SIMD_REAL_WIDTH-1][ZZ] }
409 * \param[in] r Real to an rvec array, has to be aligned to SIMD register width
410 * \param[in] index Index of the first rvec triplet of reals to load
411 * \param[out] r0 Pointer to first SIMD register
412 * \param[out] r1 Pointer to second SIMD register
413 * \param[out] r2 Pointer to third SIMD register
415 static inline void simdLoadRvecs(const rvec* r, int index, SimdReal* r0, SimdReal* r1, SimdReal* r2)
417 const real* realPtr = r[index];
419 GMX_ASSERT(isSimdAligned(realPtr), "Pointer should be SIMD aligned");
421 *r0 = simdLoad(realPtr + 0 * GMX_SIMD_REAL_WIDTH);
422 *r1 = simdLoad(realPtr + 1 * GMX_SIMD_REAL_WIDTH);
423 *r2 = simdLoad(realPtr + 2 * GMX_SIMD_REAL_WIDTH);
426 /*! \brief Store (aligned) 3 SIMD registers sequentially to GMX_SIMD_REAL_WIDTH rvec elements
428 * The stored output is:
429 * \p r[index] = { { r0[0], r0[1], ... }
431 * \p r[index+GMX_SIMD_REAL_WIDTH-1] = { ... , r2[GMX_SIMD_REAL_WIDTH-2], r2[GMX_SIMD_REAL_WIDTH-1] }
433 * \param[out] r Pointer to an rvec array, has to be aligned to SIMD register width
434 * \param[in] index Index of the first rvec triplet of reals to store to
435 * \param[in] r0 First SIMD register
436 * \param[in] r1 Second SIMD register
437 * \param[in] r2 Third SIMD register
439 static inline void simdStoreRvecs(rvec* r, int index, SimdReal r0, SimdReal r1, SimdReal r2)
441 real* realPtr = r[index];
443 GMX_ASSERT(isSimdAligned(realPtr), "Pointer should be SIMD aligned");
445 store(realPtr + 0 * GMX_SIMD_REAL_WIDTH, r0);
446 store(realPtr + 1 * GMX_SIMD_REAL_WIDTH, r1);
447 store(realPtr + 2 * GMX_SIMD_REAL_WIDTH, r2);
450 /*! \brief Integrate using leap-frog with single group T-scaling and SIMD
452 * \tparam storeUpdatedVelocities Tells whether we should store the updated velocities
453 * \param[in] start Index of first atom to update
454 * \param[in] nrend Last atom to update: \p nrend - 1
455 * \param[in] dt The time step
456 * \param[in] invMass 1/mass per atom
457 * \param[in] tcstat Temperature coupling information
458 * \param[in] x Input coordinates
459 * \param[out] xprime Updated coordinates
460 * \param[inout] v Velocities, type either rvec* or const rvec*
461 * \param[in] f Forces
463 template<StoreUpdatedVelocities storeUpdatedVelocities, typename VelocityType>
464 static std::enable_if_t<std::is_same<VelocityType, rvec>::value || std::is_same<VelocityType, const rvec>::value, void>
465 updateMDLeapfrogSimpleSimd(int start,
468 gmx::ArrayRef<const real> invMass,
469 gmx::ArrayRef<const t_grp_tcstat> tcstat,
470 const rvec* gmx_restrict x,
471 rvec* gmx_restrict xprime,
472 VelocityType* gmx_restrict v,
473 const rvec* gmx_restrict f)
475 SimdReal timestep(dt);
476 SimdReal lambdaSystem(tcstat[0].lambda);
478 /* We declare variables here, since code is often slower when declaring them inside the loop */
480 /* Note: We should implement a proper PaddedVector, so we don't need this check */
481 GMX_ASSERT(isSimdAligned(invMass.data()), "invMass should be aligned");
483 for (int a = start; a < nrend; a += GMX_SIMD_REAL_WIDTH)
485 SimdReal invMass0, invMass1, invMass2;
486 expandScalarsToTriplets(simdLoad(invMass.data() + a), &invMass0, &invMass1, &invMass2);
490 simdLoadRvecs(v, a, &v0, &v1, &v2);
491 simdLoadRvecs(f, a, &f0, &f1, &f2);
493 v0 = fma(f0 * invMass0, timestep, lambdaSystem * v0);
494 v1 = fma(f1 * invMass1, timestep, lambdaSystem * v1);
495 v2 = fma(f2 * invMass2, timestep, lambdaSystem * v2);
497 if constexpr (storeUpdatedVelocities == StoreUpdatedVelocities::yes)
499 simdStoreRvecs(v, a, v0, v1, v2);
502 // NOLINTNEXTLINE(readability-misleading-indentation) remove when clang-tidy-13 is required
504 simdLoadRvecs(x, a, &x0, &x1, &x2);
506 SimdReal xprime0 = fma(v0, timestep, x0);
507 SimdReal xprime1 = fma(v1, timestep, x1);
508 SimdReal xprime2 = fma(v2, timestep, x2);
510 simdStoreRvecs(xprime, a, xprime0, xprime1, xprime2);
514 #endif // GMX_HAVE_SIMD_UPDATE
516 /*! \brief Sets the NEMD acceleration type */
517 enum class AccelerationType
523 /*! \brief Integrate using leap-frog with support for everything.
525 * \tparam accelerationType Type of NEMD acceleration.
526 * \param[in] start Index of first atom to update.
527 * \param[in] nrend Last atom to update: \p nrend - 1.
528 * \param[in] doNoseHoover If to apply Nose-Hoover T-coupling.
529 * \param[in] dt The time step.
530 * \param[in] dtPressureCouple Time step for pressure coupling, is 0 when no pressure
531 * coupling should be applied at this step.
532 * \param[in] cTC Temperature coupling group indices
533 * \param[in] invMassPerDim Inverse mass per dimension
534 * \param[in] ekind Kinetic energy data.
535 * \param[in] box The box dimensions.
536 * \param[in] x Input coordinates.
537 * \param[out] xprime Updated coordinates.
538 * \param[inout] v Velocities.
539 * \param[in] f Forces.
540 * \param[in] nh_vxi Nose-Hoover velocity scaling factors.
541 * \param[in] nsttcouple Frequency of the temperature coupling steps.
542 * \param[in] M Parrinello-Rahman scaling matrix.
544 template<AccelerationType accelerationType>
545 static void updateMDLeapfrogGeneral(int start,
549 real dtPressureCouple,
550 gmx::ArrayRef<const unsigned short> cTC,
551 gmx::ArrayRef<const rvec> invMassPerDim,
552 const gmx_ekindata_t* ekind,
554 const rvec* gmx_restrict x,
555 rvec* gmx_restrict xprime,
556 rvec* gmx_restrict v,
557 const rvec* gmx_restrict f,
558 const double* gmx_restrict nh_vxi,
559 const int nsttcouple,
562 /* This is a version of the leap-frog integrator that supports
563 * all combinations of T-coupling, P-coupling and NEMD.
564 * Nose-Hoover uses the reversible leap-frog integrator from
565 * Holian et al. Phys Rev E 52(3) : 2338, 1995
568 gmx::ArrayRef<const t_grp_tcstat> tcstat = ekind->tcstat;
570 /* Initialize group values, changed later when multiple groups are used */
573 real omega_Z = 2 * static_cast<real>(M_PI) / box[ZZ][ZZ];
575 for (int n = start; n < nrend; n++)
581 real lg = tcstat[gt].lambda;
584 real cosineZ, vCosine;
585 switch (accelerationType)
587 case AccelerationType::none: copy_rvec(v[n], vRel); break;
588 case AccelerationType::cosine:
589 cosineZ = std::cos(x[n][ZZ] * omega_Z);
590 vCosine = cosineZ * ekind->cosacc.vcos;
591 /* Avoid scaling the cosine profile velocity */
592 copy_rvec(v[n], vRel);
600 /* Here we account for multiple time stepping, by increasing
601 * the Nose-Hoover correction by nsttcouple
602 * TODO: This can be pre-computed.
604 factorNH = 0.5 * nsttcouple * dt * nh_vxi[gt];
607 for (int d = 0; d < DIM; d++)
609 real vNew = (lg * vRel[d]
610 + (f[n][d] * invMassPerDim[n][d] * dt - factorNH * vRel[d]
611 - dtPressureCouple * iprod(M[d], vRel)))
613 switch (accelerationType)
615 case AccelerationType::none: break;
616 case AccelerationType::cosine:
619 /* Add back the mean velocity and apply acceleration */
620 vNew += vCosine + cosineZ * ekind->cosacc.cos_accel * dt;
625 xprime[n][d] = x[n][d] + vNew * dt;
630 /*! \brief Handles the Leap-frog MD x and v integration */
631 static void do_update_md(int start,
635 const rvec* gmx_restrict x,
636 rvec* gmx_restrict xprime,
637 rvec* gmx_restrict v,
638 const rvec* gmx_restrict f,
639 const TemperatureCoupling etc,
640 const PressureCoupling epc,
641 const int nsttcouple,
642 const int nstpcouple,
643 gmx::ArrayRef<const unsigned short> cTC,
644 gmx::ArrayRef<const real> gmx_unused invmass,
645 gmx::ArrayRef<const rvec> invMassPerDim,
646 const gmx_ekindata_t* ekind,
648 const double* gmx_restrict nh_vxi,
650 bool gmx_unused havePartiallyFrozenAtoms)
652 GMX_ASSERT(nrend == start || xprime != x,
653 "For SIMD optimization certain compilers need to have xprime != x");
655 /* Note: Berendsen pressure scaling is handled after do_update_md() */
657 (etc != TemperatureCoupling::No && do_per_step(step + nsttcouple - 1, nsttcouple));
658 bool doNoseHoover = (etc == TemperatureCoupling::NoseHoover && doTempCouple);
659 bool doParrinelloRahman = (epc == PressureCoupling::ParrinelloRahman
660 && do_per_step(step + nstpcouple - 1, nstpcouple));
661 bool doPROffDiagonal = (doParrinelloRahman && (M[YY][XX] != 0 || M[ZZ][XX] != 0 || M[ZZ][YY] != 0));
663 real dtPressureCouple = (doParrinelloRahman ? nstpcouple * dt : 0);
665 /* NEMD cosine acceleration is applied in updateMDLeapFrogGeneral */
666 bool doAcceleration = (ekind->cosacc.cos_accel != 0);
668 if (doNoseHoover || doPROffDiagonal || doAcceleration)
671 if (!doParrinelloRahman)
673 /* We should not apply PR scaling at this step */
683 updateMDLeapfrogGeneral<AccelerationType::none>(start,
702 updateMDLeapfrogGeneral<AccelerationType::cosine>(start,
722 /* We determine if we have a single T-coupling lambda value for all
723 * atoms. That allows for better SIMD acceleration in the template.
724 * If we do not do temperature coupling (in the run or this step),
725 * all scaling values are 1, so we effectively have a single value.
727 bool haveSingleTempScaleValue = (!doTempCouple || ekind->ngtc == 1);
729 /* Extract some pointers needed by all cases */
730 gmx::ArrayRef<const t_grp_tcstat> tcstat = ekind->tcstat;
732 if (doParrinelloRahman)
734 GMX_ASSERT(!doPROffDiagonal,
735 "updateMDLeapfrogSimple only support diagonal Parrinello-Rahman scaling "
739 for (int d = 0; d < DIM; d++)
744 if (haveSingleTempScaleValue)
746 updateMDLeapfrogSimple<StoreUpdatedVelocities::yes, NumTempScaleValues::single, ApplyParrinelloRahmanVScaling::diagonal>(
747 start, nrend, dt, dtPressureCouple, invMassPerDim, tcstat, cTC, diagM, x, xprime, v, f);
751 updateMDLeapfrogSimple<StoreUpdatedVelocities::yes, NumTempScaleValues::multiple, ApplyParrinelloRahmanVScaling::diagonal>(
752 start, nrend, dt, dtPressureCouple, invMassPerDim, tcstat, cTC, diagM, x, xprime, v, f);
757 if (haveSingleTempScaleValue)
759 /* Note that modern compilers are pretty good at vectorizing
760 * updateMDLeapfrogSimple(). But the SIMD version will still
761 * be faster because invMass lowers the cache pressure
762 * compared to invMassPerDim.
764 #if GMX_HAVE_SIMD_UPDATE
765 /* Check if we can use invmass instead of invMassPerDim */
766 if (!havePartiallyFrozenAtoms)
768 updateMDLeapfrogSimpleSimd<StoreUpdatedVelocities::yes>(
769 start, nrend, dt, invmass, tcstat, x, xprime, v, f);
774 updateMDLeapfrogSimple<StoreUpdatedVelocities::yes, NumTempScaleValues::single, ApplyParrinelloRahmanVScaling::no>(
775 start, nrend, dt, dtPressureCouple, invMassPerDim, tcstat, cTC, nullptr, x, xprime, v, f);
780 updateMDLeapfrogSimple<StoreUpdatedVelocities::yes, NumTempScaleValues::multiple, ApplyParrinelloRahmanVScaling::no>(
781 start, nrend, dt, dtPressureCouple, invMassPerDim, tcstat, cTC, nullptr, x, xprime, v, f);
786 /*! \brief Handles the Leap-frog MD x and v integration */
787 static void doUpdateMDDoNotUpdateVelocities(int start,
790 const rvec* gmx_restrict x,
791 rvec* gmx_restrict xprime,
792 const rvec* gmx_restrict v,
793 const rvec* gmx_restrict f,
794 bool gmx_unused havePartiallyFrozenAtoms,
795 gmx::ArrayRef<const real> gmx_unused invmass,
796 gmx::ArrayRef<const rvec> invMassPerDim,
797 const gmx_ekindata_t& ekind)
799 GMX_ASSERT(nrend == start || xprime != x,
800 "For SIMD optimization certain compilers need to have xprime != x");
802 gmx::ArrayRef<const t_grp_tcstat> tcstat = ekind.tcstat;
804 /* Check if we can use invmass instead of invMassPerDim */
805 #if GMX_HAVE_SIMD_UPDATE
806 if (!havePartiallyFrozenAtoms)
808 updateMDLeapfrogSimpleSimd<StoreUpdatedVelocities::no>(
809 start, nrend, dt, invmass, tcstat, x, xprime, v, f);
814 updateMDLeapfrogSimple<StoreUpdatedVelocities::no, NumTempScaleValues::single, ApplyParrinelloRahmanVScaling::no>(
815 start, nrend, dt, dt, invMassPerDim, tcstat, gmx::ArrayRef<const unsigned short>(), nullptr, x, xprime, v, f);
819 static void do_update_vv_vel(int start,
822 gmx::ArrayRef<const ivec> nFreeze,
823 gmx::ArrayRef<const real> invmass,
824 gmx::ArrayRef<const ParticleType> ptype,
825 gmx::ArrayRef<const unsigned short> cFREEZE,
838 g = 0.25 * dt * veta * alpha;
840 mv2 = gmx::series_sinhx(g);
847 for (n = start; n < nrend; n++)
849 real w_dt = invmass[n] * dt;
850 if (!cFREEZE.empty())
855 for (d = 0; d < DIM; d++)
857 if ((ptype[n] != ParticleType::Shell) && !nFreeze[gf][d])
859 v[n][d] = mv1 * (mv1 * v[n][d] + 0.5 * (w_dt * mv2 * f[n][d]));
867 } /* do_update_vv_vel */
869 static void do_update_vv_pos(int start,
872 gmx::ArrayRef<const ivec> nFreeze,
873 gmx::ArrayRef<const ParticleType> ptype,
874 gmx::ArrayRef<const unsigned short> cFREEZE,
885 /* Would it make more sense if Parrinello-Rahman was put here? */
890 mr2 = gmx::series_sinhx(g);
898 for (n = start; n < nrend; n++)
901 if (!cFREEZE.empty())
906 for (d = 0; d < DIM; d++)
908 if ((ptype[n] != ParticleType::Shell) && !nFreeze[gf][d])
910 xprime[n][d] = mr1 * (mr1 * x[n][d] + mr2 * dt * v[n][d]);
914 xprime[n][d] = x[n][d];
918 } /* do_update_vv_pos */
920 gmx_stochd_t::gmx_stochd_t(const t_inputrec& inputRecord)
922 const t_grpopts* opts = &inputRecord.opts;
923 const int ngtc = opts->ngtc;
925 if (inputRecord.eI == IntegrationAlgorithm::BD)
929 else if (EI_SD(inputRecord.eI))
934 for (int gt = 0; gt < ngtc; gt++)
936 if (opts->tau_t[gt] > 0)
938 sdc[gt].em = std::exp(-inputRecord.delta_t / opts->tau_t[gt]);
942 /* No friction and noise on this group */
947 else if (ETC_ANDERSEN(inputRecord.etc))
949 randomize_group.resize(ngtc);
950 boltzfac.resize(ngtc);
952 /* for now, assume that all groups, if randomized, are randomized at the same rate, i.e. tau_t is the same. */
953 /* since constraint groups don't necessarily match up with temperature groups! This is checked in readir.c */
955 for (int gt = 0; gt < ngtc; gt++)
957 real reft = std::max<real>(0, opts->ref_t[gt]);
958 if ((opts->tau_t[gt] > 0)
959 && (reft > 0)) /* tau_t or ref_t = 0 means that no randomization is done */
961 randomize_group[gt] = true;
962 boltzfac[gt] = gmx::c_boltz * opts->ref_t[gt];
966 randomize_group[gt] = false;
972 void Update::Impl::update_temperature_constants(const t_inputrec& inputRecord)
974 if (inputRecord.eI == IntegrationAlgorithm::BD)
976 if (inputRecord.bd_fric != 0)
978 for (int gt = 0; gt < inputRecord.opts.ngtc; gt++)
980 sd_.bd_rf[gt] = std::sqrt(2.0 * gmx::c_boltz * inputRecord.opts.ref_t[gt]
981 / (inputRecord.bd_fric * inputRecord.delta_t));
986 for (int gt = 0; gt < inputRecord.opts.ngtc; gt++)
988 sd_.bd_rf[gt] = std::sqrt(2.0 * gmx::c_boltz * inputRecord.opts.ref_t[gt]);
992 if (inputRecord.eI == IntegrationAlgorithm::SD1)
994 for (int gt = 0; gt < inputRecord.opts.ngtc; gt++)
996 real kT = gmx::c_boltz * inputRecord.opts.ref_t[gt];
997 /* The mass is accounted for later, since this differs per atom */
998 sd_.sdsig[gt].V = std::sqrt(kT * (1 - sd_.sdc[gt].em * sd_.sdc[gt].em));
1003 Update::Impl::Impl(const t_inputrec& inputRecord, BoxDeformation* boxDeformation) :
1004 sd_(inputRecord), deform_(boxDeformation)
1006 update_temperature_constants(inputRecord);
1007 xp_.resizeWithPadding(0);
1010 void Update::updateAfterPartition(int numAtoms,
1011 gmx::ArrayRef<const unsigned short> cFREEZE,
1012 gmx::ArrayRef<const unsigned short> cTC)
1015 impl_->xp()->resizeWithPadding(numAtoms);
1016 impl_->cFREEZE_ = cFREEZE;
1020 /*! \brief Sets the SD update type */
1021 enum class SDUpdate : int
1024 FrictionAndNoiseOnly,
1028 /*! \brief SD integrator update
1030 * Two phases are required in the general case of a constrained
1031 * update, the first phase from the contribution of forces, before
1032 * applying constraints, and then a second phase applying the friction
1033 * and noise, and then further constraining. For details, see
1036 * Without constraints, the two phases can be combined, for
1039 * Thus three instantiations of this templated function will be made,
1040 * two with only one contribution, and one with both contributions. */
1041 template<SDUpdate updateType>
1042 static void doSDUpdateGeneral(const gmx_stochd_t& sd,
1046 gmx::ArrayRef<const ivec> nFreeze,
1047 gmx::ArrayRef<const real> invmass,
1048 gmx::ArrayRef<const ParticleType> ptype,
1049 gmx::ArrayRef<const unsigned short> cFREEZE,
1050 gmx::ArrayRef<const unsigned short> cTC,
1057 const int* gatindex)
1059 // cTC and cFREEZE can be nullptr any time, but various
1060 // instantiations do not make sense with particular pointer
1062 if (updateType == SDUpdate::ForcesOnly)
1064 GMX_ASSERT(f != nullptr, "SD update with only forces requires forces");
1065 GMX_ASSERT(cTC.empty(), "SD update with only forces cannot handle temperature groups");
1067 if (updateType == SDUpdate::FrictionAndNoiseOnly)
1069 GMX_ASSERT(f == nullptr, "SD update with only noise cannot handle forces");
1071 if (updateType == SDUpdate::Combined)
1073 GMX_ASSERT(f != nullptr, "SD update with forces and noise requires forces");
1076 // Even 0 bits internal counter gives 2x64 ints (more than enough for three table lookups)
1077 gmx::ThreeFry2x64<0> rng(seed, gmx::RandomDomain::UpdateCoordinates);
1078 gmx::TabulatedNormalDistribution<real, 14> dist;
1080 for (int n = start; n < nrend; n++)
1082 int globalAtomIndex = gatindex ? gatindex[n] : n;
1083 rng.restart(step, globalAtomIndex);
1086 real inverseMass = invmass[n];
1087 real invsqrtMass = std::sqrt(inverseMass);
1089 int freezeGroup = !cFREEZE.empty() ? cFREEZE[n] : 0;
1090 int temperatureGroup = !cTC.empty() ? cTC[n] : 0;
1092 for (int d = 0; d < DIM; d++)
1094 if ((ptype[n] != ParticleType::Shell) && !nFreeze[freezeGroup][d])
1096 if (updateType == SDUpdate::ForcesOnly)
1098 real vn = v[n][d] + inverseMass * f[n][d] * dt;
1100 // Simple position update.
1101 xprime[n][d] = x[n][d] + v[n][d] * dt;
1103 else if (updateType == SDUpdate::FrictionAndNoiseOnly)
1106 v[n][d] = (vn * sd.sdc[temperatureGroup].em
1107 + invsqrtMass * sd.sdsig[temperatureGroup].V * dist(rng));
1108 // The previous phase already updated the
1109 // positions with a full v*dt term that must
1110 // now be half removed.
1111 xprime[n][d] = xprime[n][d] + 0.5 * (v[n][d] - vn) * dt;
1115 real vn = v[n][d] + inverseMass * f[n][d] * dt;
1116 v[n][d] = (vn * sd.sdc[temperatureGroup].em
1117 + invsqrtMass * sd.sdsig[temperatureGroup].V * dist(rng));
1118 // Here we include half of the friction+noise
1119 // update of v into the position update.
1120 xprime[n][d] = x[n][d] + 0.5 * (vn + v[n][d]) * dt;
1125 // When using constraints, the update is split into
1126 // two phases, but we only need to zero the update of
1127 // virtual, shell or frozen particles in at most one
1129 if (updateType != SDUpdate::FrictionAndNoiseOnly)
1132 xprime[n][d] = x[n][d];
1139 static void do_update_sd(int start,
1143 const rvec* gmx_restrict x,
1144 rvec* gmx_restrict xprime,
1145 rvec* gmx_restrict v,
1146 const rvec* gmx_restrict f,
1147 gmx::ArrayRef<const ivec> nFreeze,
1148 gmx::ArrayRef<const real> invmass,
1149 gmx::ArrayRef<const ParticleType> ptype,
1150 gmx::ArrayRef<const unsigned short> cFREEZE,
1151 gmx::ArrayRef<const unsigned short> cTC,
1153 const t_commrec* cr,
1154 const gmx_stochd_t& sd,
1155 bool haveConstraints)
1157 if (haveConstraints)
1159 // With constraints, the SD update is done in 2 parts
1160 doSDUpdateGeneral<SDUpdate::ForcesOnly>(sd,
1168 gmx::ArrayRef<const unsigned short>(),
1179 doSDUpdateGeneral<SDUpdate::Combined>(
1195 haveDDAtomOrdering(*cr) ? cr->dd->globalAtomIndices.data() : nullptr);
1199 static void do_update_bd(int start,
1203 const rvec* gmx_restrict x,
1204 rvec* gmx_restrict xprime,
1205 rvec* gmx_restrict v,
1206 const rvec* gmx_restrict f,
1207 gmx::ArrayRef<const ivec> nFreeze,
1208 gmx::ArrayRef<const real> invmass,
1209 gmx::ArrayRef<const ParticleType> ptype,
1210 gmx::ArrayRef<const unsigned short> cFREEZE,
1211 gmx::ArrayRef<const unsigned short> cTC,
1212 real friction_coefficient,
1215 const int* gatindex)
1217 /* note -- these appear to be full step velocities . . . */
1222 // Use 1 bit of internal counters to give us 2*2 64-bits values per stream
1223 // Each 64-bit value is enough for 4 normal distribution table numbers.
1224 gmx::ThreeFry2x64<0> rng(seed, gmx::RandomDomain::UpdateCoordinates);
1225 gmx::TabulatedNormalDistribution<real, 14> dist;
1227 if (friction_coefficient != 0)
1229 invfr = 1.0 / friction_coefficient;
1232 for (n = start; (n < nrend); n++)
1234 int ng = gatindex ? gatindex[n] : n;
1236 rng.restart(step, ng);
1239 if (!cFREEZE.empty())
1247 for (d = 0; (d < DIM); d++)
1249 if ((ptype[n] != ParticleType::Shell) && !nFreeze[gf][d])
1251 if (friction_coefficient != 0)
1253 vn = invfr * f[n][d] + rf[gt] * dist(rng);
1257 /* NOTE: invmass = 2/(mass*friction_constant*dt) */
1258 vn = 0.5 * invmass[n] * f[n][d] * dt
1259 + std::sqrt(0.5 * invmass[n]) * rf[gt] * dist(rng);
1263 xprime[n][d] = x[n][d] + vn * dt;
1268 xprime[n][d] = x[n][d];
1274 extern void init_ekinstate(ekinstate_t* ekinstate, const t_inputrec* ir)
1276 ekinstate->ekin_n = ir->opts.ngtc;
1277 snew(ekinstate->ekinh, ekinstate->ekin_n);
1278 snew(ekinstate->ekinf, ekinstate->ekin_n);
1279 snew(ekinstate->ekinh_old, ekinstate->ekin_n);
1280 ekinstate->ekinscalef_nhc.resize(ekinstate->ekin_n);
1281 ekinstate->ekinscaleh_nhc.resize(ekinstate->ekin_n);
1282 ekinstate->vscale_nhc.resize(ekinstate->ekin_n);
1283 ekinstate->dekindl = 0;
1284 ekinstate->mvcos = 0;
1285 ekinstate->hasReadEkinState = false;
1288 void update_ekinstate(ekinstate_t* ekinstate, const gmx_ekindata_t* ekind)
1292 for (i = 0; i < ekinstate->ekin_n; i++)
1294 copy_mat(ekind->tcstat[i].ekinh, ekinstate->ekinh[i]);
1295 copy_mat(ekind->tcstat[i].ekinf, ekinstate->ekinf[i]);
1296 copy_mat(ekind->tcstat[i].ekinh_old, ekinstate->ekinh_old[i]);
1297 ekinstate->ekinscalef_nhc[i] = ekind->tcstat[i].ekinscalef_nhc;
1298 ekinstate->ekinscaleh_nhc[i] = ekind->tcstat[i].ekinscaleh_nhc;
1299 ekinstate->vscale_nhc[i] = ekind->tcstat[i].vscale_nhc;
1302 copy_mat(ekind->ekin, ekinstate->ekin_total);
1303 ekinstate->dekindl = ekind->dekindl;
1304 ekinstate->mvcos = ekind->cosacc.mvcos;
1307 void restore_ekinstate_from_state(const t_commrec* cr, gmx_ekindata_t* ekind, const ekinstate_t* ekinstate)
1313 for (i = 0; i < ekinstate->ekin_n; i++)
1315 copy_mat(ekinstate->ekinh[i], ekind->tcstat[i].ekinh);
1316 copy_mat(ekinstate->ekinf[i], ekind->tcstat[i].ekinf);
1317 copy_mat(ekinstate->ekinh_old[i], ekind->tcstat[i].ekinh_old);
1318 ekind->tcstat[i].ekinscalef_nhc = ekinstate->ekinscalef_nhc[i];
1319 ekind->tcstat[i].ekinscaleh_nhc = ekinstate->ekinscaleh_nhc[i];
1320 ekind->tcstat[i].vscale_nhc = ekinstate->vscale_nhc[i];
1323 copy_mat(ekinstate->ekin_total, ekind->ekin);
1325 ekind->dekindl = ekinstate->dekindl;
1326 ekind->cosacc.mvcos = ekinstate->mvcos;
1327 n = ekinstate->ekin_n;
1332 gmx_bcast(sizeof(n), &n, cr->mpi_comm_mygroup);
1333 for (i = 0; i < n; i++)
1335 gmx_bcast(DIM * DIM * sizeof(ekind->tcstat[i].ekinh[0][0]),
1336 ekind->tcstat[i].ekinh[0],
1337 cr->mpi_comm_mygroup);
1338 gmx_bcast(DIM * DIM * sizeof(ekind->tcstat[i].ekinf[0][0]),
1339 ekind->tcstat[i].ekinf[0],
1340 cr->mpi_comm_mygroup);
1341 gmx_bcast(DIM * DIM * sizeof(ekind->tcstat[i].ekinh_old[0][0]),
1342 ekind->tcstat[i].ekinh_old[0],
1343 cr->mpi_comm_mygroup);
1345 gmx_bcast(sizeof(ekind->tcstat[i].ekinscalef_nhc),
1346 &(ekind->tcstat[i].ekinscalef_nhc),
1347 cr->mpi_comm_mygroup);
1348 gmx_bcast(sizeof(ekind->tcstat[i].ekinscaleh_nhc),
1349 &(ekind->tcstat[i].ekinscaleh_nhc),
1350 cr->mpi_comm_mygroup);
1351 gmx_bcast(sizeof(ekind->tcstat[i].vscale_nhc), &(ekind->tcstat[i].vscale_nhc), cr->mpi_comm_mygroup);
1353 gmx_bcast(DIM * DIM * sizeof(ekind->ekin[0][0]), ekind->ekin[0], cr->mpi_comm_mygroup);
1355 gmx_bcast(sizeof(ekind->dekindl), &ekind->dekindl, cr->mpi_comm_mygroup);
1356 gmx_bcast(sizeof(ekind->cosacc.mvcos), &ekind->cosacc.mvcos, cr->mpi_comm_mygroup);
1360 void getThreadAtomRange(int numThreads, int threadIndex, int numAtoms, int* startAtom, int* endAtom)
1362 #if GMX_HAVE_SIMD_UPDATE
1363 constexpr int blockSize = GMX_SIMD_REAL_WIDTH;
1365 constexpr int blockSize = 1;
1367 int numBlocks = (numAtoms + blockSize - 1) / blockSize;
1369 *startAtom = ((numBlocks * threadIndex) / numThreads) * blockSize;
1370 *endAtom = ((numBlocks * (threadIndex + 1)) / numThreads) * blockSize;
1371 if (threadIndex == numThreads - 1)
1373 *endAtom = numAtoms;
1377 void Update::Impl::update_sd_second_half(const t_inputrec& inputRecord,
1381 gmx::ArrayRef<const ParticleType> ptype,
1382 gmx::ArrayRef<const real> invMass,
1384 const t_commrec* cr,
1386 gmx_wallcycle* wcycle,
1387 gmx::Constraints* constr,
1395 if (inputRecord.eI == IntegrationAlgorithm::SD1)
1398 /* Cast delta_t from double to real to make the integrators faster.
1399 * The only reason for having delta_t double is to get accurate values
1400 * for t=delta_t*step when step is larger than float precision.
1401 * For integration dt the accuracy of real suffices, since with
1402 * integral += dt*integrand the increment is nearly always (much) smaller
1403 * than the integral (and the integrand has real precision).
1405 real dt = inputRecord.delta_t;
1407 wallcycle_start(wcycle, WallCycleCounter::Update);
1409 int nth = gmx_omp_nthreads_get(ModuleMultiThread::Update);
1411 #pragma omp parallel for num_threads(nth) schedule(static)
1412 for (int th = 0; th < nth; th++)
1416 int start_th, end_th;
1417 getThreadAtomRange(nth, th, homenr, &start_th, &end_th);
1419 doSDUpdateGeneral<SDUpdate::FrictionAndNoiseOnly>(
1424 gmx::arrayRefFromArray(inputRecord.opts.nFreeze, inputRecord.opts.ngfrz),
1429 state->x.rvec_array(),
1431 state->v.rvec_array(),
1434 inputRecord.ld_seed,
1435 haveDDAtomOrdering(*cr) ? cr->dd->globalAtomIndices.data() : nullptr);
1437 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
1439 inc_nrnb(nrnb, eNR_UPDATE, homenr);
1440 wallcycle_stop(wcycle, WallCycleCounter::Update);
1442 /* Constrain the coordinates upd->xp for half a time step */
1443 bool computeVirial = false;
1444 constr->apply(do_log,
1449 state->x.arrayRefWithPadding(),
1450 xp_.arrayRefWithPadding(),
1453 state->lambda[static_cast<int>(FreeEnergyPerturbationCouplingType::Bonded)],
1455 state->v.arrayRefWithPadding(),
1458 ConstraintVariable::Positions);
1462 void Update::Impl::finish_update(const t_inputrec& inputRecord,
1463 const bool havePartiallyFrozenAtoms,
1465 gmx::ArrayRef<const unsigned short> cFREEZE,
1467 gmx_wallcycle* wcycle,
1468 const bool haveConstraints)
1470 /* NOTE: Currently we always integrate to a temporary buffer and
1471 * then copy the results back here.
1474 wallcycle_start_nocount(wcycle, WallCycleCounter::Update);
1476 auto xp = makeConstArrayRef(xp_).subArray(0, homenr);
1477 auto x = makeArrayRef(state->x).subArray(0, homenr);
1479 if (havePartiallyFrozenAtoms && haveConstraints)
1481 /* We have atoms that are frozen along some, but not all dimensions,
1482 * then constraints will have moved them also along the frozen dimensions.
1483 * To freeze such degrees of freedom we do not copy them back here.
1485 const ivec* nFreeze = inputRecord.opts.nFreeze;
1487 for (int i = 0; i < homenr; i++)
1489 const int g = cFREEZE[i];
1491 for (int d = 0; d < DIM; d++)
1493 if (nFreeze[g][d] == 0)
1502 /* We have no frozen atoms or fully frozen atoms which have not
1503 * been moved by the update, so we can simply copy all coordinates.
1505 int gmx_unused nth = gmx_omp_nthreads_get(ModuleMultiThread::Update);
1506 #pragma omp parallel for num_threads(nth) schedule(static)
1507 for (int i = 0; i < homenr; i++)
1509 // Trivial statement, does not throw
1514 wallcycle_stop(wcycle, WallCycleCounter::Update);
1517 void Update::Impl::update_coords(const t_inputrec& inputRecord,
1520 bool havePartiallyFrozenAtoms,
1521 gmx::ArrayRef<const ParticleType> ptype,
1522 gmx::ArrayRef<const real> invMass,
1523 gmx::ArrayRef<const rvec> invMassPerDim,
1525 const gmx::ArrayRefWithPadding<const gmx::RVec>& f,
1527 const gmx_ekindata_t* ekind,
1530 const t_commrec* cr,
1531 const bool haveConstraints)
1533 /* Running the velocity half does nothing except for velocity verlet */
1534 if ((updatePart == etrtVELOCITY1 || updatePart == etrtVELOCITY2) && !EI_VV(inputRecord.eI))
1536 gmx_incons("update_coords called for velocity without VV integrator");
1539 /* Cast to real for faster code, no loss in precision (see comment above) */
1540 real dt = inputRecord.delta_t;
1542 /* We need to update the NMR restraint history when time averaging is used */
1543 if (state->flags & enumValueToBitMask(StateEntry::DisreRm3Tav))
1545 update_disres_history(*fcdata->disres, &state->hist);
1547 if (state->flags & enumValueToBitMask(StateEntry::OrireDtav))
1549 GMX_ASSERT(fcdata, "Need valid fcdata");
1550 fcdata->orires->updateHistory();
1553 /* ############# START The update of velocities and positions ######### */
1554 int nth = gmx_omp_nthreads_get(ModuleMultiThread::Update);
1556 #pragma omp parallel for num_threads(nth) schedule(static)
1557 for (int th = 0; th < nth; th++)
1561 int start_th, end_th;
1562 getThreadAtomRange(nth, th, homenr, &start_th, &end_th);
1564 const rvec* x_rvec = state->x.rvec_array();
1565 rvec* xp_rvec = xp_.rvec_array();
1566 rvec* v_rvec = state->v.rvec_array();
1567 const rvec* f_rvec = as_rvec_array(f.unpaddedConstArrayRef().data());
1569 switch (inputRecord.eI)
1571 case (IntegrationAlgorithm::MD):
1572 do_update_md(start_th,
1582 inputRecord.nsttcouple,
1583 inputRecord.nstpcouple,
1589 state->nosehoover_vxi.data(),
1591 havePartiallyFrozenAtoms);
1593 case (IntegrationAlgorithm::SD1):
1594 do_update_sd(start_th,
1602 gmx::arrayRefFromArray(inputRecord.opts.nFreeze, inputRecord.opts.ngfrz),
1607 inputRecord.ld_seed,
1612 case (IntegrationAlgorithm::BD):
1613 do_update_bd(start_th,
1621 gmx::arrayRefFromArray(inputRecord.opts.nFreeze, inputRecord.opts.ngfrz),
1626 inputRecord.bd_fric,
1628 inputRecord.ld_seed,
1629 haveDDAtomOrdering(*cr) ? cr->dd->globalAtomIndices.data() : nullptr);
1631 case (IntegrationAlgorithm::VV):
1632 case (IntegrationAlgorithm::VVAK):
1634 gmx_bool bExtended = (inputRecord.etc == TemperatureCoupling::NoseHoover
1635 || inputRecord.epc == PressureCoupling::ParrinelloRahman
1636 || inputRecord.epc == PressureCoupling::Mttk);
1638 /* assuming barostat coupled to group 0 */
1639 real alpha = 1.0 + DIM / static_cast<real>(inputRecord.opts.nrdf[0]);
1644 do_update_vv_vel(start_th,
1647 gmx::arrayRefFromArray(inputRecord.opts.nFreeze,
1648 inputRecord.opts.ngfrz),
1659 do_update_vv_pos(start_th,
1662 gmx::arrayRefFromArray(inputRecord.opts.nFreeze,
1663 inputRecord.opts.ngfrz),
1675 default: gmx_fatal(FARGS, "Don't know how to update coordinates");
1678 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
1682 void Update::Impl::update_for_constraint_virial(const t_inputrec& inputRecord,
1684 bool havePartiallyFrozenAtoms,
1685 gmx::ArrayRef<const real> invmass,
1686 gmx::ArrayRef<const rvec> invMassPerDim,
1687 const t_state& state,
1688 const gmx::ArrayRefWithPadding<const gmx::RVec>& f,
1689 const gmx_ekindata_t& ekind)
1691 GMX_ASSERT(inputRecord.eI == IntegrationAlgorithm::MD || inputRecord.eI == IntegrationAlgorithm::SD1,
1692 "Only leap-frog is supported here");
1694 // Cast to real for faster code, no loss in precision
1695 const real dt = inputRecord.delta_t;
1697 const int nth = gmx_omp_nthreads_get(ModuleMultiThread::Update);
1699 #pragma omp parallel for num_threads(nth) schedule(static)
1700 for (int th = 0; th < nth; th++)
1704 int start_th, end_th;
1705 getThreadAtomRange(nth, th, homenr, &start_th, &end_th);
1707 const rvec* x_rvec = state.x.rvec_array();
1708 rvec* xp_rvec = xp_.rvec_array();
1709 const rvec* v_rvec = state.v.rvec_array();
1710 const rvec* f_rvec = as_rvec_array(f.unpaddedConstArrayRef().data());
1712 doUpdateMDDoNotUpdateVelocities(
1713 start_th, end_th, dt, x_rvec, xp_rvec, v_rvec, f_rvec, havePartiallyFrozenAtoms, invmass, invMassPerDim, ekind);
1715 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR