2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 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.
36 * \brief Defines the signallers for the modular simulator
38 * \author Pascal Merz <pascal.merz@me.com>
39 * \ingroup module_modularsimulator
43 #include "signallers.h"
47 #include "gromacs/mdlib/stat.h"
48 #include "gromacs/mdlib/stophandler.h"
49 #include "gromacs/mdrunutility/handlerestart.h"
51 #include "modularsimulatorinterfaces.h"
55 //! Helper function to call all callbacks in a list
56 static inline void runAllCallbacks(const std::vector<SignallerCallback>& callbacks, Step step, Time time)
58 for (const auto& callback : callbacks)
64 NeighborSearchSignaller::NeighborSearchSignaller(std::vector<SignallerCallback> callbacks,
68 callbacks_(std::move(callbacks)), nstlist_(nstlist), initStep_(initStep), initTime_(initTime)
72 void NeighborSearchSignaller::signal(Step step, Time time)
74 // Neighbor search happens at regular intervals, and always on first step of simulation
75 if (do_per_step(step, nstlist_) || step == initStep_)
77 runAllCallbacks(callbacks_, step, time);
81 LastStepSignaller::LastStepSignaller(std::vector<SignallerCallback> callbacks,
84 StopHandler* stopHandler) :
85 callbacks_(std::move(callbacks)),
86 stopStep_(initStep + nsteps),
87 signalledStopCondition_(false),
88 stopHandler_(stopHandler),
90 nsStepRegistrationDone_(false)
94 void LastStepSignaller::signal(Step step, Time time)
96 if (signalledStopCondition_)
100 bool isNSStep = (step == nextNSStep_);
101 signalledStopCondition_ = stopHandler_->stoppingAfterCurrentStep(isNSStep);
102 if (step == stopStep_ || signalledStopCondition_)
104 runAllCallbacks(callbacks_, step, time);
108 void LastStepSignaller::setup()
110 GMX_ASSERT(nsStepRegistrationDone_,
111 "LastStepSignaller needs to be registered to NeighborSearchSignaller.");
114 std::optional<SignallerCallback> LastStepSignaller::registerNSCallback()
116 nsStepRegistrationDone_ = true;
117 return [this](Step step, Time gmx_unused time) { this->nextNSStep_ = step; };
120 LoggingSignaller::LoggingSignaller(std::vector<SignallerCallback> callbacks,
123 StartingBehavior startingBehavior) :
124 callbacks_(std::move(callbacks)),
127 startingBehavior_(startingBehavior),
129 lastStepRegistrationDone_(false)
133 void LoggingSignaller::signal(Step step, Time time)
135 if (do_per_step(step, nstlog_) || step == lastStep_
136 || (step == initStep_ && startingBehavior_ == StartingBehavior::NewSimulation))
138 runAllCallbacks(callbacks_, step, time);
142 void LoggingSignaller::setup()
144 GMX_ASSERT(lastStepRegistrationDone_,
145 "LoggingSignaller needs to be registered to LastStepSignaller.");
148 std::optional<SignallerCallback> LoggingSignaller::registerLastStepCallback()
150 lastStepRegistrationDone_ = true;
151 return [this](Step step, Time gmx_unused time) { this->lastStep_ = step; };
154 TrajectorySignaller::TrajectorySignaller(std::vector<SignallerCallback> signalEnergyCallbacks,
155 std::vector<SignallerCallback> signalStateCallbacks,
159 int nstxoutCompressed,
162 int tngBoxOutCompressed,
163 int tngLambdaOutCompressed,
168 nstxoutCompressed_(nstxoutCompressed),
169 tngBoxOut_(tngBoxOut),
170 tngLambdaOut_(tngLambdaOut),
171 tngBoxOutCompressed_(tngBoxOutCompressed),
172 tngLambdaOutCompressed_(tngLambdaOutCompressed),
173 nstenergy_(nstenergy),
174 signalEnergyCallbacks_(std::move(signalEnergyCallbacks)),
175 signalStateCallbacks_(std::move(signalStateCallbacks)),
177 lastStepRegistrationDone_(false)
181 void TrajectorySignaller::setup()
183 GMX_ASSERT(lastStepRegistrationDone_,
184 "TrajectoryElement needs to be registered to LastStepSignaller.");
187 void TrajectorySignaller::signal(Step step, Time time)
189 if (do_per_step(step, nstxout_) || do_per_step(step, nstvout_) || do_per_step(step, nstfout_)
190 || do_per_step(step, nstxoutCompressed_) || do_per_step(step, tngBoxOut_)
191 || do_per_step(step, tngLambdaOut_) || do_per_step(step, tngBoxOutCompressed_)
192 || do_per_step(step, tngLambdaOutCompressed_))
194 for (const auto& callback : signalStateCallbacks_)
196 callback(step, time);
200 if (do_per_step(step, nstenergy_) || step == lastStep_)
202 for (const auto& callback : signalEnergyCallbacks_)
204 callback(step, time);
209 std::optional<SignallerCallback> TrajectorySignaller::registerLastStepCallback()
211 lastStepRegistrationDone_ = true;
212 return [this](Step step, Time gmx_unused time) { this->lastStep_ = step; };
215 EnergySignaller::EnergySignaller(std::vector<SignallerCallback> calculateEnergyCallbacks,
216 std::vector<SignallerCallback> calculateVirialCallbacks,
217 std::vector<SignallerCallback> calculateFreeEnergyCallbacks,
219 int nstcalcfreeenergy,
221 EnergySignallerVirialMode virialMode) :
222 calculateEnergyCallbacks_(std::move(calculateEnergyCallbacks)),
223 calculateVirialCallbacks_(std::move(calculateVirialCallbacks)),
224 calculateFreeEnergyCallbacks_(std::move(calculateFreeEnergyCallbacks)),
225 nstcalcenergy_(nstcalcenergy),
226 nstcalcfreeenergy_(nstcalcfreeenergy),
227 nstcalcvirial_(nstcalcvirial),
228 virialMode_(virialMode),
229 energyWritingStep_(-1),
230 trajectoryRegistrationDone_(false),
232 loggingRegistrationDone_(false)
236 void EnergySignaller::signal(Step step, Time time)
238 const bool writeEnergy = (energyWritingStep_ == step);
239 const bool writeLog = (loggingStep_ == step);
240 const bool calculateEnergy = writeEnergy || writeLog || do_per_step(step, nstcalcenergy_);
241 const bool calculateVirial = calculateEnergy
242 || ((virialMode_ == EnergySignallerVirialMode::OnStep
243 || virialMode_ == EnergySignallerVirialMode::OnStepAndNext)
244 && do_per_step(step, nstcalcvirial_))
245 || (virialMode_ == EnergySignallerVirialMode::OnStepAndNext
246 && do_per_step(step - 1, nstcalcvirial_));
247 const bool calculateFreeEnergy = do_per_step(step, nstcalcfreeenergy_);
251 runAllCallbacks(calculateEnergyCallbacks_, step, time);
255 runAllCallbacks(calculateVirialCallbacks_, step, time);
257 if (calculateFreeEnergy)
259 runAllCallbacks(calculateFreeEnergyCallbacks_, step, time);
263 void EnergySignaller::setup()
265 GMX_ASSERT(loggingRegistrationDone_,
266 "EnergySignaller needs to be registered to LoggingSignaller.");
267 GMX_ASSERT(trajectoryRegistrationDone_,
268 "EnergySignaller needs to be registered to TrajectoryElement.");
271 std::optional<SignallerCallback> EnergySignaller::registerTrajectorySignallerCallback(TrajectoryEvent event)
273 if (event == TrajectoryEvent::EnergyWritingStep)
275 trajectoryRegistrationDone_ = true;
276 return [this](Step step, Time gmx_unused time) { this->energyWritingStep_ = step; };
281 std::optional<SignallerCallback> EnergySignaller::registerLoggingCallback()
283 loggingRegistrationDone_ = true;
284 return [this](Step step, Time gmx_unused time) { this->loggingStep_ = step; };