2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2017,2018,2019,2020,2021, 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.
38 * Implements the Awh class.
40 * \author Viveca Lindahl
41 * \author Berk Hess <hess@kth.se>
42 * \author Magnus Lundborg
58 #include "gromacs/fileio/enxio.h"
59 #include "gromacs/gmxlib/network.h"
60 #include "gromacs/math/units.h"
61 #include "gromacs/math/utilities.h"
62 #include "gromacs/mdrunutility/multisim.h"
63 #include "gromacs/mdtypes/awh_history.h"
64 #include "gromacs/mdtypes/awh_params.h"
65 #include "gromacs/mdtypes/commrec.h"
66 #include "gromacs/mdtypes/forceoutput.h"
67 #include "gromacs/mdtypes/inputrec.h"
68 #include "gromacs/mdtypes/pull_params.h"
69 #include "gromacs/mdtypes/state.h"
70 #include "gromacs/pbcutil/pbc.h"
71 #include "gromacs/pulling/pull.h"
72 #include "gromacs/timing/wallcycle.h"
73 #include "gromacs/trajectory/energyframe.h"
74 #include "gromacs/utility/arrayref.h"
75 #include "gromacs/utility/exceptions.h"
76 #include "gromacs/utility/gmxassert.h"
77 #include "gromacs/utility/pleasecite.h"
80 #include "biassharing.h"
81 #include "correlationgrid.h"
82 #include "pointstate.h"
88 * \brief A bias and its coupling to the system.
90 * This struct is used to separate the bias machinery in the Bias class,
91 * which should be independent from the reaction coordinate, from the
92 * obtaining of the reaction coordinate values and passing the computed forces.
93 * Currently the AWH method couples to the system by mapping each
94 * AWH bias to a pull coordinate. This can easily be generalized here.
96 struct BiasCoupledToSystem
98 /*! \brief Constructor, couple a bias to a set of pull coordinates.
100 * \param[in] bias The bias.
101 * \param[in] pullCoordIndex The pull coordinate indices.
103 BiasCoupledToSystem(Bias bias, const std::vector<int>& pullCoordIndex);
105 Bias bias_; /**< The bias. */
106 const std::vector<int> pullCoordIndex_; /**< The pull coordinates this bias acts on. */
108 /* Here AWH can be extended to work on other coordinates than pull. */
111 /*! \brief Checks whether any dimension uses pulling as a coordinate provider.
113 * \param[in] awhBiasParams The bias params to check.
114 * \returns true if any dimension of the bias is linked to pulling.
116 static bool anyDimUsesPull(const AwhBiasParams& awhBiasParams)
118 for (int d = 0; d < awhBiasParams.ndim; d++)
120 const AwhDimParams& awhDimParams = awhBiasParams.dimParams[d];
121 if (awhDimParams.eCoordProvider == AwhCoordinateProviderType::Pull)
129 /*! \brief Checks whether any dimension uses pulling as a coordinate provider.
131 * \param[in] awhParams The AWH params to check.
132 * \returns true if any dimension of awh is linked to pulling.
134 static bool anyDimUsesPull(const AwhParams& awhParams)
136 for (int k = 0; k < awhParams.numBias; k++)
138 const AwhBiasParams& awhBiasParams = awhParams.awhBiasParams[k];
139 if (anyDimUsesPull(awhBiasParams))
147 /*! \brief Checks whether any dimension uses pulling as a coordinate provider.
149 * \param[in] biasCoupledToSystem The AWH biases to check.
150 * \returns true if any dimension of the provided biases is linked to pulling.
152 static bool anyDimUsesPull(const ArrayRef<BiasCoupledToSystem> biasCoupledToSystem)
154 for (const auto& biasCts : biasCoupledToSystem)
156 if (!biasCts.pullCoordIndex_.empty())
164 BiasCoupledToSystem::BiasCoupledToSystem(Bias bias, const std::vector<int>& pullCoordIndex) :
165 bias_(std::move(bias)),
166 pullCoordIndex_(pullCoordIndex)
168 /* We already checked for this in grompp, but check again here. */
170 static_cast<size_t>(bias_.ndim()) == pullCoordIndex_.size() + bias_.hasFepLambdaDimension() ? 1 : 0,
171 "The bias dimensionality should match the number of pull and lambda coordinates.");
174 Awh::Awh(FILE* fplog,
175 const t_inputrec& inputRecord,
176 const t_commrec* commRecord,
177 const gmx_multisim_t* multiSimRecord,
178 const AwhParams& awhParams,
179 const std::string& biasInitFilename,
181 int numFepLambdaStates,
182 int fepLambdaState) :
183 seed_(awhParams.seed),
184 nstout_(awhParams.nstOut),
185 commRecord_(commRecord),
186 multiSimRecord_(multiSimRecord),
189 numFepLambdaStates_(numFepLambdaStates),
190 fepLambdaState_(fepLambdaState)
192 if (anyDimUsesPull(awhParams))
194 GMX_RELEASE_ASSERT(inputRecord.pull != nullptr, "With AWH we should have pull parameters");
195 GMX_RELEASE_ASSERT(pull_work != nullptr,
196 "With AWH pull should be initialized before initializing AWH");
199 if (fplog != nullptr)
201 please_cite(fplog, "Lindahl2014");
204 if (haveBiasSharingWithinSimulation(awhParams))
206 /* This has likely been checked by grompp, but throw anyhow. */
208 InvalidInputError("Biases within a simulation are shared, currently sharing of "
209 "biases is only supported between simulations"));
212 int numSharingSimulations = 1;
213 if (awhParams.shareBiasMultisim && isMultiSim(multiSimRecord_))
215 numSharingSimulations = multiSimRecord_->numSimulations_;
218 /* Initialize all the biases */
219 const double beta = 1 / (gmx::c_boltz * inputRecord.opts.ref_t[0]);
220 for (int k = 0; k < awhParams.numBias; k++)
222 const AwhBiasParams& awhBiasParams = awhParams.awhBiasParams[k];
224 std::vector<int> pullCoordIndex;
225 std::vector<DimParams> dimParams;
226 for (int d = 0; d < awhBiasParams.ndim; d++)
228 const AwhDimParams& awhDimParams = awhBiasParams.dimParams[d];
229 if (awhDimParams.eCoordProvider != AwhCoordinateProviderType::Pull
230 && awhDimParams.eCoordProvider != AwhCoordinateProviderType::FreeEnergyLambda)
233 InvalidInputError("Currently only the pull code and lambda are supported "
234 "as coordinate providers"));
236 if (awhDimParams.eCoordProvider == AwhCoordinateProviderType::Pull)
238 const t_pull_coord& pullCoord = inputRecord.pull->coord[awhDimParams.coordIndex];
239 if (pullCoord.eGeom == PullGroupGeometry::DirectionPBC)
241 GMX_THROW(InvalidInputError(
242 "Pull geometry 'direction-periodic' is not supported by AWH"));
244 double conversionFactor = pull_coordinate_is_angletype(&pullCoord) ? gmx::c_deg2Rad : 1;
245 pullCoordIndex.push_back(awhDimParams.coordIndex);
246 dimParams.push_back(DimParams::pullDimParams(
247 conversionFactor, awhDimParams.forceConstant, beta));
251 dimParams.push_back(DimParams::fepLambdaDimParams(numFepLambdaStates_, beta));
255 /* Construct the bias and couple it to the system. */
256 Bias::ThisRankWillDoIO thisRankWillDoIO =
257 (MASTER(commRecord_) ? Bias::ThisRankWillDoIO::Yes : Bias::ThisRankWillDoIO::No);
258 biasCoupledToSystem_.emplace_back(Bias(k,
260 awhParams.awhBiasParams[k],
264 numSharingSimulations,
269 biasCoupledToSystem_.back().bias_.printInitializationToLog(fplog);
272 /* Need to register the AWH coordinates to be allowed to apply forces to the pull coordinates. */
273 registerAwhWithPull(awhParams, pull_);
275 if (numSharingSimulations > 1 && MASTER(commRecord_))
277 std::vector<size_t> pointSize;
278 for (auto const& biasCts : biasCoupledToSystem_)
280 pointSize.push_back(biasCts.bias_.state().points().size());
282 /* Ensure that the shared biased are compatible between simulations */
283 biasesAreCompatibleForSharingBetweenSimulations(awhParams, pointSize, multiSimRecord_);
287 Awh::~Awh() = default;
289 bool Awh::isOutputStep(int64_t step) const
291 return (nstout_ > 0 && step % nstout_ == 0);
294 real Awh::applyBiasForcesAndUpdateBias(PbcType pbcType,
296 ArrayRef<const double> neighborLambdaEnergies,
297 ArrayRef<const double> neighborLambdaDhdl,
299 gmx::ForceWithVirial* forceWithVirial,
302 gmx_wallcycle* wallcycle,
305 if (anyDimUsesPull(biasCoupledToSystem_))
307 GMX_ASSERT(forceWithVirial, "Need a valid ForceWithVirial object");
310 wallcycle_start(wallcycle, ewcAWH);
313 set_pbc(&pbc, pbcType, box);
315 /* During the AWH update the potential can instantaneously jump due to either
316 an bias update or moving the umbrella. The jumps are kept track of and
317 subtracted from the potential in order to get a useful conserved energy quantity. */
318 double awhPotential = potentialOffset_;
320 for (auto& biasCts : biasCoupledToSystem_)
322 /* Update the AWH coordinate values with those of the corresponding
325 awh_dvec coordValue = { 0, 0, 0, 0 };
326 int numLambdaDimsCounted = 0;
327 for (int d = 0; d < biasCts.bias_.ndim(); d++)
329 if (biasCts.bias_.dimParams()[d].isPullDimension())
331 coordValue[d] = get_pull_coord_value(
332 pull_, biasCts.pullCoordIndex_[d - numLambdaDimsCounted], &pbc);
336 coordValue[d] = fepLambdaState_;
337 numLambdaDimsCounted += 1;
341 /* Perform an AWH biasing step: this means, at regular intervals,
342 * sampling observables based on the input pull coordinate value,
343 * setting the bias force and/or updating the AWH bias state.
345 double biasPotential;
346 double biasPotentialJump;
347 /* Note: In the near future this call will be split in calls
348 * to supports bias sharing within a single simulation.
350 gmx::ArrayRef<const double> biasForce =
351 biasCts.bias_.calcForceAndUpdateBias(coordValue,
352 neighborLambdaEnergies,
363 awhPotential += biasPotential;
365 /* Keep track of the total potential shift needed to remove the potential jumps. */
366 potentialOffset_ -= biasPotentialJump;
368 /* Communicate the bias force to the pull struct.
369 * The bias potential is returned at the end of this function,
370 * so that it can be added externally to the correct energy data block.
372 numLambdaDimsCounted = 0;
373 for (int d = 0; d < biasCts.bias_.ndim(); d++)
375 if (biasCts.bias_.dimParams()[d].isPullDimension())
377 apply_external_pull_coord_force(pull_,
378 biasCts.pullCoordIndex_[d - numLambdaDimsCounted],
385 int umbrellaGridpointIndex = biasCts.bias_.state().coordState().umbrellaGridpoint();
386 fepLambdaState_ = biasCts.bias_.getGridCoordValue(umbrellaGridpointIndex)[d];
387 numLambdaDimsCounted += 1;
391 if (isOutputStep(step))
393 /* We might have skipped updates for part of the grid points.
394 * Ensure all points are updated before writing out their data.
396 biasCts.bias_.doSkippedUpdatesForAllPoints();
400 wallcycle_stop(wallcycle, ewcAWH);
402 return MASTER(commRecord_) ? static_cast<real>(awhPotential) : 0;
405 std::shared_ptr<AwhHistory> Awh::initHistoryFromState() const
407 if (MASTER(commRecord_))
409 std::shared_ptr<AwhHistory> awhHistory(new AwhHistory);
410 awhHistory->bias.clear();
411 awhHistory->bias.resize(biasCoupledToSystem_.size());
413 for (size_t k = 0; k < awhHistory->bias.size(); k++)
415 biasCoupledToSystem_[k].bias_.initHistoryFromState(&awhHistory->bias[k]);
422 /* Return an empty pointer */
423 return std::shared_ptr<AwhHistory>();
427 void Awh::restoreStateFromHistory(const AwhHistory* awhHistory)
429 /* Restore the history to the current state */
430 if (MASTER(commRecord_))
432 GMX_RELEASE_ASSERT(awhHistory != nullptr,
433 "The master rank should have a valid awhHistory when restoring the "
434 "state from history.");
436 if (awhHistory->bias.size() != biasCoupledToSystem_.size())
438 GMX_THROW(InvalidInputError(
439 "AWH state and history contain different numbers of biases. Likely you "
440 "provided a checkpoint from a different simulation."));
443 potentialOffset_ = awhHistory->potentialOffset;
445 if (PAR(commRecord_))
447 gmx_bcast(sizeof(potentialOffset_), &potentialOffset_, commRecord_->mpi_comm_mygroup);
450 for (size_t k = 0; k < biasCoupledToSystem_.size(); k++)
452 biasCoupledToSystem_[k].bias_.restoreStateFromHistory(
453 awhHistory ? &awhHistory->bias[k] : nullptr, commRecord_);
457 void Awh::updateHistory(AwhHistory* awhHistory) const
459 if (!MASTER(commRecord_))
464 /* This assert will also catch a non-master rank calling this function. */
465 GMX_RELEASE_ASSERT(awhHistory->bias.size() == biasCoupledToSystem_.size(),
466 "AWH state and history bias count should match");
468 awhHistory->potentialOffset = potentialOffset_;
470 for (size_t k = 0; k < awhHistory->bias.size(); k++)
472 biasCoupledToSystem_[k].bias_.updateHistory(&awhHistory->bias[k]);
476 const char* Awh::externalPotentialString()
481 void Awh::registerAwhWithPull(const AwhParams& awhParams, pull_t* pull_work)
483 GMX_RELEASE_ASSERT(!anyDimUsesPull(awhParams) || pull_work, "Need a valid pull object");
485 for (int k = 0; k < awhParams.numBias; k++)
487 const AwhBiasParams& biasParams = awhParams.awhBiasParams[k];
489 for (int d = 0; d < biasParams.ndim; d++)
491 if (biasParams.dimParams[d].eCoordProvider == AwhCoordinateProviderType::Pull)
493 register_external_pull_potential(
494 pull_work, biasParams.dimParams[d].coordIndex, Awh::externalPotentialString());
500 /* Fill the AWH data block of an energy frame with data (if there is any). */
501 void Awh::writeToEnergyFrame(int64_t step, t_enxframe* frame) const
503 GMX_ASSERT(MASTER(commRecord_), "writeToEnergyFrame should only be called on the master rank");
504 GMX_ASSERT(frame != nullptr, "Need a valid energy frame");
506 if (!isOutputStep(step))
508 /* This is not an AWH output step, don't write any AWH data */
512 /* Get the total number of energy subblocks that AWH needs */
513 int numSubblocks = 0;
514 for (auto& biasCoupledToSystem : biasCoupledToSystem_)
516 numSubblocks += biasCoupledToSystem.bias_.numEnergySubblocksToWrite();
518 GMX_ASSERT(numSubblocks > 0, "We should always have data to write");
520 /* Add 1 energy block */
521 add_blocks_enxframe(frame, frame->nblock + 1);
523 /* Take the block that was just added and set the number of subblocks. */
524 t_enxblock* awhEnergyBlock = &(frame->block[frame->nblock - 1]);
525 add_subblocks_enxblock(awhEnergyBlock, numSubblocks);
527 /* Claim it as an AWH block. */
528 awhEnergyBlock->id = enxAWH;
530 /* Transfer AWH data blocks to energy sub blocks */
531 int energySubblockCount = 0;
532 for (auto& biasCoupledToSystem : biasCoupledToSystem_)
534 energySubblockCount += biasCoupledToSystem.bias_.writeToEnergySubblocks(
535 &(awhEnergyBlock->sub[energySubblockCount]));
539 bool Awh::hasFepLambdaDimension() const
542 std::begin(biasCoupledToSystem_),
543 std::end(biasCoupledToSystem_),
544 [](const auto& coupledBias) { return coupledBias.bias_.hasFepLambdaDimension(); });
547 bool Awh::needForeignEnergyDifferences(const int64_t step) const
549 /* If there is no FEP lambda dimension at all in any bias there will be no need for foreign
550 * energy differences */
551 if (!hasFepLambdaDimension())
559 /* Check whether the bias(es) that has/have a FEP lambda dimension should sample coordinates
560 * this step. Since the biases may have different sampleCoordStep it is necessary to check
561 * this combination. */
562 for (const auto& biasCts : biasCoupledToSystem_)
564 if (biasCts.bias_.hasFepLambdaDimension() && biasCts.bias_.isSampleCoordStep(step))
572 std::unique_ptr<Awh> prepareAwhModule(FILE* fplog,
573 const t_inputrec& inputRecord,
574 t_state* stateGlobal,
575 const t_commrec* commRecord,
576 const gmx_multisim_t* multiSimRecord,
577 const bool startingFromCheckpoint,
578 const bool usingShellParticles,
579 const std::string& biasInitFilename,
582 if (!inputRecord.bDoAwh)
586 if (usingShellParticles)
588 GMX_THROW(InvalidInputError("AWH biasing does not support shell particles."));
591 auto awh = std::make_unique<Awh>(fplog,
595 *inputRecord.awhParams,
598 inputRecord.fepvals->n_lambda,
599 inputRecord.fepvals->init_fep_state);
601 if (startingFromCheckpoint)
603 // Restore the AWH history read from checkpoint
604 awh->restoreStateFromHistory(MASTER(commRecord) ? stateGlobal->awhHistory.get() : nullptr);
606 else if (MASTER(commRecord))
608 // Initialize the AWH history here
609 stateGlobal->awhHistory = awh->initHistoryFromState();