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/coupling.h"
49 #include "gromacs/mdlib/mdatoms.h"
50 #include "gromacs/mdlib/stat.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),
81 scalingTensor_(scalingTensor),
82 propagatorCallback_(std::move(propagatorCallback)),
83 statePropagatorData_(statePropagatorData),
84 energyElement_(energyElement),
91 clear_mat(boxVelocity_);
93 // TODO: This is only needed to restore the thermostatIntegral_ from cpt. Remove this when
94 // switching to purely client-based checkpointing.
99 copy_mat(globalState->boxv, boxVelocity_);
100 copy_mat(globalState->box_rel, boxRel_);
102 if (DOMAINDECOMP(cr))
104 dd_bcast(cr->dd, sizeof(boxVelocity_), boxVelocity_);
105 dd_bcast(cr->dd, sizeof(boxRel_), boxRel_);
110 void ParrinelloRahmanBarostat::scheduleTask(gmx::Step step,
111 gmx::Time gmx_unused time,
112 const gmx::RegisterRunFunctionPtr& registerRunFunction)
114 const bool scaleOnNextStep = do_per_step(step + nstpcouple_ + offset_ + 1, nstpcouple_);
115 const bool scaleOnThisStep = do_per_step(step + nstpcouple_ + offset_, nstpcouple_);
119 (*registerRunFunction)(
120 std::make_unique<SimulatorRunFunction>([this]() { scaleBoxAndPositions(); }));
124 (*registerRunFunction)(std::make_unique<SimulatorRunFunction>(
125 [this, step]() { integrateBoxVelocityEquations(step); }));
126 // let propagator know that it will have to scale on next step
127 (*propagatorCallback_)(step + 1);
131 void ParrinelloRahmanBarostat::integrateBoxVelocityEquations(Step step)
133 auto box = statePropagatorData_->constBox();
134 parrinellorahman_pcoupl(fplog_, step, inputrec_, couplingTimeStep_, energyElement_->pressure(step),
135 box, boxRel_, boxVelocity_, scalingTensor_.data(), mu_, false);
136 // multiply matrix by the coupling time step to avoid having the propagator needing to know about that
137 msmul(scalingTensor_.data(), couplingTimeStep_, scalingTensor_.data());
140 void ParrinelloRahmanBarostat::scaleBoxAndPositions()
142 // Propagate the box by the box velocities
143 auto box = statePropagatorData_->box();
144 for (int i = 0; i < DIM; i++)
146 for (int m = 0; m <= i; m++)
148 box[i][m] += couplingTimeStep_ * boxVelocity_[i][m];
151 preserve_box_shape(inputrec_, boxRel_, box);
153 // Scale the coordinates
155 const int homenr = mdAtoms_->mdatoms()->homenr;
156 auto x = as_rvec_array(statePropagatorData_->positionsView().paddedArrayRef().data());
157 for (int n = start; n < start + homenr; n++)
159 tmvmul_ur0(mu_, x[n], x[n]);
163 void ParrinelloRahmanBarostat::elementSetup()
165 if (inputrecPreserveShape(inputrec_))
167 auto box = statePropagatorData_->box();
168 const int ndim = inputrec_->epct == epctSEMIISOTROPIC ? 2 : 3;
169 do_box_rel(ndim, inputrec_->deform, boxRel_, box, true);
172 const bool scaleOnInitStep = do_per_step(initStep_ + nstpcouple_ + offset_, nstpcouple_);
175 // If we need to scale on the first step, we need to set the scaling matrix using the current
176 // box velocity. If this is a fresh start, we will hence not move the box (this does currently
177 // never happen as the offset is set to -1 in all cases). If this is a restart, we will use
178 // the saved box velocity which we would have updated right before checkpointing.
179 // Setting bFirstStep = true in parrinellorahman_pcoupl (last argument) makes sure that only
180 // the scaling matrix is calculated, without updating the box velocities.
181 // The call to parrinellorahman_pcoupl is using nullptr for fplog (since we don't expect any
182 // output here) and for the pressure (since it might not be calculated yet, and we don't need it).
183 auto box = statePropagatorData_->constBox();
184 parrinellorahman_pcoupl(nullptr, initStep_, inputrec_, couplingTimeStep_, nullptr, box,
185 boxRel_, boxVelocity_, scalingTensor_.data(), mu_, true);
186 // multiply matrix by the coupling time step to avoid having the propagator needing to know about that
187 msmul(scalingTensor_.data(), couplingTimeStep_, scalingTensor_.data());
189 (*propagatorCallback_)(initStep_);
193 const rvec* ParrinelloRahmanBarostat::boxVelocities() const
198 void ParrinelloRahmanBarostat::writeCheckpoint(t_state* localState, t_state gmx_unused* globalState)
200 copy_mat(boxVelocity_, localState->boxv);
201 copy_mat(boxRel_, localState->box_rel);
202 localState->flags |= (1U << estBOXV) | (1U << estBOX_REL);