|
| 1 | +/* |
| 2 | + * This file is part of the GROMACS molecular simulation package. |
| 3 | + * |
| 4 | + * Copyright 2024- The GROMACS Authors |
| 5 | + * and the project initiators Erik Lindahl, Berk Hess and David van der Spoel. |
| 6 | + * Consult the AUTHORS/COPYING files and https://www.gromacs.org for details. |
| 7 | + * |
| 8 | + * GROMACS is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public License |
| 10 | + * as published by the Free Software Foundation; either version 2.1 |
| 11 | + * of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * GROMACS is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public |
| 19 | + * License along with GROMACS; if not, see |
| 20 | + * https://www.gnu.org/licenses, or write to the Free Software Foundation, |
| 21 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | + * |
| 23 | + * If you want to redistribute modifications to GROMACS, please |
| 24 | + * consider that scientific software is very special. Version |
| 25 | + * control is crucial - bugs must be traceable. We will be happy to |
| 26 | + * consider code for inclusion in the official distribution, but |
| 27 | + * derived work must not be called official GROMACS. Details are found |
| 28 | + * in the README & COPYING files - if they are missing, get the |
| 29 | + * official version at https://www.gromacs.org. |
| 30 | + * |
| 31 | + * To help us fund GROMACS development, we humbly ask that you cite |
| 32 | + * the research papers on the package. Check out https://www.gromacs.org. |
| 33 | + */ |
| 34 | +/*! \internal \file |
| 35 | + * \brief |
| 36 | + * Implements Plumed MDModule class |
| 37 | + * |
| 38 | + * \author Daniele Rapetti <drapetti@sissa.it> |
| 39 | + * \ingroup module_applied_forces |
| 40 | + */ |
| 41 | +#include "gmxpre.h" |
| 42 | + |
| 43 | +#include "plumedMDModule.h" |
| 44 | + |
| 45 | +#include <memory> |
| 46 | +#include <string> |
| 47 | + |
| 48 | +#include "gromacs/domdec/localatomsetmanager.h" |
| 49 | +#include "gromacs/fileio/checkpoint.h" |
| 50 | +#include "gromacs/mdrunutility/mdmodulesnotifiers.h" |
| 51 | +#include "gromacs/mdtypes/commrec.h" |
| 52 | +#include "gromacs/mdtypes/imdmodule.h" |
| 53 | +#include "gromacs/utility/keyvaluetreebuilder.h" |
| 54 | + |
| 55 | +#include "plumedOptions.h" |
| 56 | +#include "plumedforceprovider.h" |
| 57 | + |
| 58 | +class gmx_multisim_t; |
| 59 | + |
| 60 | +namespace gmx |
| 61 | +{ |
| 62 | + |
| 63 | +namespace |
| 64 | +{ |
| 65 | + |
| 66 | +/*! \internal |
| 67 | + * \brief Plumed module |
| 68 | + * |
| 69 | + * Class that implements the plumed MDModule |
| 70 | + */ |
| 71 | +class PlumedMDModule final : public IMDModule |
| 72 | +{ |
| 73 | +public: |
| 74 | + //! \brief Construct the plumed module. |
| 75 | + explicit PlumedMDModule() = default; |
| 76 | + // Now callbacks for several kinds of MdModuleNotification are created |
| 77 | + // and subscribed, and will be dispatched correctly at run time |
| 78 | + // based on the type of the parameter required by the lambda. |
| 79 | + |
| 80 | + /*! \brief Requests to be notified during pre-processing. |
| 81 | + * |
| 82 | + * Plumed does not act during the preprocessing phase of a simulation, so the input are ignored |
| 83 | + */ |
| 84 | + void subscribeToPreProcessingNotifications(MDModulesNotifiers* /*notifier*/) override {} |
| 85 | + |
| 86 | + /*! \brief Subscribe to MDModules notifications for information needed just before the simulation. |
| 87 | + */ |
| 88 | + void subscribeToSimulationSetupNotifications(MDModulesNotifiers* notifier) override |
| 89 | + { |
| 90 | + // TODO: add a check for threadmpi (see #5104, https://gitlab.com/gromacs/gromacs/-/merge_requests/4367#note_2102475958, the manual and the force provider for the details) |
| 91 | + |
| 92 | + // Access the plumed filename this is used to activate the plumed module |
| 93 | + notifier->simulationSetupNotifier_.subscribe( |
| 94 | + [this](const PlumedInputFilename& plumedFilename) |
| 95 | + { this->options_.setPlumedFile(plumedFilename.plumedFilename_); }); |
| 96 | + // Retrieve the Multisim options |
| 97 | + notifier->simulationSetupNotifier_.subscribe( |
| 98 | + [this](const gmx_multisim_t* ms) |
| 99 | + { this->options_.setMultisim(ms); }); |
| 100 | + // Access the temperature if it is constant during the simulation |
| 101 | + notifier->simulationSetupNotifier_.subscribe( |
| 102 | + [this](const EnsembleTemperature& ensembleT) |
| 103 | + { this->options_.setEnsembleTemperature(ensembleT); }); |
| 104 | + // Access of the topology |
| 105 | + notifier->simulationSetupNotifier_.subscribe([this](const gmx_mtop_t& mtop) |
| 106 | + { this->options_.setTopology(mtop); }); |
| 107 | + // Retrieve the Communication Record during simulations setup |
| 108 | + notifier->simulationSetupNotifier_.subscribe([this](const t_commrec& cr) |
| 109 | + { this->options_.setComm(cr); }); |
| 110 | + // setting the simulation time step |
| 111 | + notifier->simulationSetupNotifier_.subscribe( |
| 112 | + [this](const SimulationTimeStep& simulationTimeStep) |
| 113 | + { this->options_.setSimulationTimeStep(simulationTimeStep.delta_t); }); |
| 114 | + // Retrieve the starting behavior |
| 115 | + notifier->simulationSetupNotifier_.subscribe( |
| 116 | + [this](const StartingBehavior& startingBehavior) |
| 117 | + { this->options_.setStartingBehavior(startingBehavior); }); |
| 118 | + // writing checkpoint data |
| 119 | + notifier->checkpointingNotifier_.subscribe( |
| 120 | + [this](MDModulesWriteCheckpointData /*checkpointData*/) |
| 121 | + { |
| 122 | + if (options_.active()) |
| 123 | + { |
| 124 | + plumedForceProvider_->writeCheckpointData(); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + //! From IMDModule |
| 130 | + IMdpOptionProvider* mdpOptionProvider() override { return nullptr; } |
| 131 | + //! From IMDModule |
| 132 | + IMDOutputProvider* outputProvider() override |
| 133 | + { // Plumed provide its own output |
| 134 | + return nullptr; |
| 135 | + } |
| 136 | + //! From IMDModule - Adds this module to the force providers if active |
| 137 | + void initForceProviders(ForceProviders* forceProviders) override |
| 138 | + { |
| 139 | + if (options_.active()) |
| 140 | + { |
| 141 | + plumedForceProvider_ = std::make_unique<PlumedForceProvider>(options_.options()); |
| 142 | + forceProviders->addForceProvider(plumedForceProvider_.get(), "Plumed"); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | +private: |
| 148 | + //! Parameters that become available at simulation setup time. |
| 149 | + PlumedOptionProvider options_{}; |
| 150 | + //! Object that evaluates the forces |
| 151 | + std::unique_ptr<PlumedForceProvider> plumedForceProvider_{}; |
| 152 | +}; |
| 153 | + |
| 154 | +} // namespace |
| 155 | + |
| 156 | +std::unique_ptr<IMDModule> PlumedModuleInfo::create() |
| 157 | +{ |
| 158 | + return std::make_unique<PlumedMDModule>(); |
| 159 | +} |
| 160 | +} // namespace gmx |
0 commit comments