Apply clang-format-11
[alexxy/gromacs.git] / src / gromacs / modularsimulator / signallers.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief Defines the signallers for the modular simulator
37  *
38  * \author Pascal Merz <pascal.merz@me.com>
39  * \ingroup module_modularsimulator
40  */
41 #include "gmxpre.h"
42
43 #include "signallers.h"
44
45 #include <algorithm>
46
47 #include "gromacs/mdlib/stat.h"
48 #include "gromacs/mdlib/stophandler.h"
49 #include "gromacs/mdrunutility/handlerestart.h"
50
51 #include "modularsimulatorinterfaces.h"
52
53 namespace gmx
54 {
55 //! Helper function to call all callbacks in a list
56 static inline void runAllCallbacks(const std::vector<SignallerCallback>& callbacks, Step step, Time time)
57 {
58     for (const auto& callback : callbacks)
59     {
60         callback(step, time);
61     }
62 }
63
64 NeighborSearchSignaller::NeighborSearchSignaller(std::vector<SignallerCallback> callbacks,
65                                                  Step                           nstlist,
66                                                  Step                           initStep,
67                                                  Time                           initTime) :
68     callbacks_(std::move(callbacks)), nstlist_(nstlist), initStep_(initStep), initTime_(initTime)
69 {
70 }
71
72 void NeighborSearchSignaller::signal(Step step, Time time)
73 {
74     // Neighbor search happens at regular intervals, and always on first step of simulation
75     if (do_per_step(step, nstlist_) || step == initStep_)
76     {
77         runAllCallbacks(callbacks_, step, time);
78     }
79 }
80
81 LastStepSignaller::LastStepSignaller(std::vector<SignallerCallback> callbacks,
82                                      gmx::Step                      nsteps,
83                                      gmx::Step                      initStep,
84                                      StopHandler*                   stopHandler) :
85     callbacks_(std::move(callbacks)),
86     stopStep_(initStep + nsteps),
87     signalledStopCondition_(false),
88     stopHandler_(stopHandler),
89     nextNSStep_(-1),
90     nsStepRegistrationDone_(false)
91 {
92 }
93
94 void LastStepSignaller::signal(Step step, Time time)
95 {
96     if (signalledStopCondition_)
97     {
98         return;
99     }
100     bool isNSStep           = (step == nextNSStep_);
101     signalledStopCondition_ = stopHandler_->stoppingAfterCurrentStep(isNSStep);
102     if (step == stopStep_ || signalledStopCondition_)
103     {
104         runAllCallbacks(callbacks_, step, time);
105     }
106 }
107
108 void LastStepSignaller::setup()
109 {
110     GMX_ASSERT(nsStepRegistrationDone_,
111                "LastStepSignaller needs to be registered to NeighborSearchSignaller.");
112 }
113
114 std::optional<SignallerCallback> LastStepSignaller::registerNSCallback()
115 {
116     nsStepRegistrationDone_ = true;
117     return [this](Step step, Time gmx_unused time) { this->nextNSStep_ = step; };
118 }
119
120 LoggingSignaller::LoggingSignaller(std::vector<SignallerCallback> callbacks,
121                                    Step                           nstlog,
122                                    Step                           initStep,
123                                    StartingBehavior               startingBehavior) :
124     callbacks_(std::move(callbacks)),
125     nstlog_(nstlog),
126     initStep_(initStep),
127     startingBehavior_(startingBehavior),
128     lastStep_(-1),
129     lastStepRegistrationDone_(false)
130 {
131 }
132
133 void LoggingSignaller::signal(Step step, Time time)
134 {
135     if (do_per_step(step, nstlog_) || step == lastStep_
136         || (step == initStep_ && startingBehavior_ == StartingBehavior::NewSimulation))
137     {
138         runAllCallbacks(callbacks_, step, time);
139     }
140 }
141
142 void LoggingSignaller::setup()
143 {
144     GMX_ASSERT(lastStepRegistrationDone_,
145                "LoggingSignaller needs to be registered to LastStepSignaller.");
146 }
147
148 std::optional<SignallerCallback> LoggingSignaller::registerLastStepCallback()
149 {
150     lastStepRegistrationDone_ = true;
151     return [this](Step step, Time gmx_unused time) { this->lastStep_ = step; };
152 }
153
154 TrajectorySignaller::TrajectorySignaller(std::vector<SignallerCallback> signalEnergyCallbacks,
155                                          std::vector<SignallerCallback> signalStateCallbacks,
156                                          int                            nstxout,
157                                          int                            nstvout,
158                                          int                            nstfout,
159                                          int                            nstxoutCompressed,
160                                          int                            tngBoxOut,
161                                          int                            tngLambdaOut,
162                                          int                            tngBoxOutCompressed,
163                                          int                            tngLambdaOutCompressed,
164                                          int                            nstenergy) :
165     nstxout_(nstxout),
166     nstvout_(nstvout),
167     nstfout_(nstfout),
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)),
176     lastStep_(-1),
177     lastStepRegistrationDone_(false)
178 {
179 }
180
181 void TrajectorySignaller::setup()
182 {
183     GMX_ASSERT(lastStepRegistrationDone_,
184                "TrajectoryElement needs to be registered to LastStepSignaller.");
185 }
186
187 void TrajectorySignaller::signal(Step step, Time time)
188 {
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_))
193     {
194         for (const auto& callback : signalStateCallbacks_)
195         {
196             callback(step, time);
197         }
198     }
199
200     if (do_per_step(step, nstenergy_) || step == lastStep_)
201     {
202         for (const auto& callback : signalEnergyCallbacks_)
203         {
204             callback(step, time);
205         }
206     }
207 }
208
209 std::optional<SignallerCallback> TrajectorySignaller::registerLastStepCallback()
210 {
211     lastStepRegistrationDone_ = true;
212     return [this](Step step, Time gmx_unused time) { this->lastStep_ = step; };
213 }
214
215 EnergySignaller::EnergySignaller(std::vector<SignallerCallback> calculateEnergyCallbacks,
216                                  std::vector<SignallerCallback> calculateVirialCallbacks,
217                                  std::vector<SignallerCallback> calculateFreeEnergyCallbacks,
218                                  int                            nstcalcenergy,
219                                  int                            nstcalcfreeenergy,
220                                  int                            nstcalcvirial,
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),
231     loggingStep_(-1),
232     loggingRegistrationDone_(false)
233 {
234 }
235
236 void EnergySignaller::signal(Step step, Time time)
237 {
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_);
248
249     if (calculateEnergy)
250     {
251         runAllCallbacks(calculateEnergyCallbacks_, step, time);
252     }
253     if (calculateVirial)
254     {
255         runAllCallbacks(calculateVirialCallbacks_, step, time);
256     }
257     if (calculateFreeEnergy)
258     {
259         runAllCallbacks(calculateFreeEnergyCallbacks_, step, time);
260     }
261 }
262
263 void EnergySignaller::setup()
264 {
265     GMX_ASSERT(loggingRegistrationDone_,
266                "EnergySignaller needs to be registered to LoggingSignaller.");
267     GMX_ASSERT(trajectoryRegistrationDone_,
268                "EnergySignaller needs to be registered to TrajectoryElement.");
269 }
270
271 std::optional<SignallerCallback> EnergySignaller::registerTrajectorySignallerCallback(TrajectoryEvent event)
272 {
273     if (event == TrajectoryEvent::EnergyWritingStep)
274     {
275         trajectoryRegistrationDone_ = true;
276         return [this](Step step, Time gmx_unused time) { this->energyWritingStep_ = step; };
277     }
278     return std::nullopt;
279 }
280
281 std::optional<SignallerCallback> EnergySignaller::registerLoggingCallback()
282 {
283     loggingRegistrationDone_ = true;
284     return [this](Step step, Time gmx_unused time) { this->loggingStep_ = step; };
285 }
286
287 } // namespace gmx