2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019,2020, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
36 * \brief Defines the Parrinello-Rahman barostat for the modular simulator
38 * \author Pascal Merz <pascal.merz@me.com>
39 * \ingroup module_modularsimulator
44 #include "parrinellorahmanbarostat.h"
46 #include "gromacs/domdec/domdec_network.h"
47 #include "gromacs/math/vec.h"
48 #include "gromacs/mdlib/mdatoms.h"
49 #include "gromacs/mdlib/stat.h"
50 #include "gromacs/mdlib/update.h"
51 #include "gromacs/mdtypes/commrec.h"
52 #include "gromacs/mdtypes/inputrec.h"
53 #include "gromacs/mdtypes/mdatom.h"
54 #include "gromacs/mdtypes/state.h"
55 #include "gromacs/pbcutil/boxutilities.h"
57 #include "energyelement.h"
58 #include "statepropagatordata.h"
63 ParrinelloRahmanBarostat::ParrinelloRahmanBarostat(int nstpcouple,
65 real couplingTimeStep,
67 ArrayRef<rvec> scalingTensor,
68 PropagatorCallbackPtr propagatorCallback,
69 StatePropagatorData* statePropagatorData,
70 EnergyElement* energyElement,
72 const t_inputrec* inputrec,
73 const MDAtoms* mdAtoms,
74 const t_state* globalState,
77 nstpcouple_(nstpcouple),
79 couplingTimeStep_(couplingTimeStep),
82 scalingTensor_(scalingTensor),
83 propagatorCallback_(std::move(propagatorCallback)),
84 statePropagatorData_(statePropagatorData),
85 energyElement_(energyElement),
92 clear_mat(boxVelocity_);
94 // TODO: This is only needed to restore the thermostatIntegral_ from cpt. Remove this when
95 // switching to purely client-based checkpointing.
100 copy_mat(globalState->boxv, boxVelocity_);
101 copy_mat(globalState->box_rel, boxRel_);
103 if (DOMAINDECOMP(cr))
105 dd_bcast(cr->dd, sizeof(boxVelocity_), boxVelocity_);
106 dd_bcast(cr->dd, sizeof(boxRel_), boxRel_);
111 void ParrinelloRahmanBarostat::scheduleTask(gmx::Step step,
112 gmx::Time gmx_unused time,
113 const gmx::RegisterRunFunctionPtr& registerRunFunction)
115 const bool scaleOnNextStep = do_per_step(step + nstpcouple_ + offset_ + 1, nstpcouple_);
116 const bool scaleOnThisStep = do_per_step(step + nstpcouple_ + offset_, nstpcouple_);
120 (*registerRunFunction)(
121 std::make_unique<SimulatorRunFunction>([this]() { scaleBoxAndPositions(); }));
125 (*registerRunFunction)(std::make_unique<SimulatorRunFunction>(
126 [this, step]() { integrateBoxVelocityEquations(step); }));
127 // let propagator know that it will have to scale on next step
128 (*propagatorCallback_)(step + 1);
132 void ParrinelloRahmanBarostat::integrateBoxVelocityEquations(Step step)
134 auto box = statePropagatorData_->constBox();
135 parrinellorahman_pcoupl(fplog_, step, inputrec_, couplingTimeStep_, energyElement_->pressure(step),
136 box, boxRel_, boxVelocity_, scalingTensor_.data(), mu_, isInitStep_);
137 // multiply matrix by the coupling time step to avoid having the propagator needing to know about that
138 msmul(scalingTensor_.data(), couplingTimeStep_, scalingTensor_.data());
141 void ParrinelloRahmanBarostat::scaleBoxAndPositions()
143 // Propagate the box by the box velocities
144 auto box = statePropagatorData_->box();
145 for (int i = 0; i < DIM; i++)
147 for (int m = 0; m <= i; m++)
149 box[i][m] += couplingTimeStep_ * boxVelocity_[i][m];
152 preserve_box_shape(inputrec_, boxRel_, box);
154 // Scale the coordinates
156 const int homenr = mdAtoms_->mdatoms()->homenr;
157 auto x = as_rvec_array(statePropagatorData_->positionsView().paddedArrayRef().data());
158 for (int n = start; n < start + homenr; n++)
160 tmvmul_ur0(mu_, x[n], x[n]);
164 void ParrinelloRahmanBarostat::elementSetup()
166 if (inputrecPreserveShape(inputrec_))
168 auto box = statePropagatorData_->box();
169 const int ndim = inputrec_->epct == epctSEMIISOTROPIC ? 2 : 3;
170 do_box_rel(ndim, inputrec_->deform, boxRel_, box, true);
173 const bool scaleOnInitStep = do_per_step(initStep_ + nstpcouple_ + offset_, nstpcouple_);
176 integrateBoxVelocityEquations(initStep_);
177 (*propagatorCallback_)(initStep_ + 1);
182 const rvec* ParrinelloRahmanBarostat::boxVelocities() const
187 void ParrinelloRahmanBarostat::writeCheckpoint(t_state* localState, t_state gmx_unused* globalState)
189 copy_mat(boxVelocity_, localState->boxv);
190 copy_mat(boxRel_, localState->box_rel);
191 localState->flags |= (1U << estBOXV) | (1U << estBOX_REL);