File: stopsignaler.cpp

package info (click to toggle)
gromacs 2025.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 287,236 kB
  • sloc: xml: 3,718,478; cpp: 654,820; ansic: 75,282; python: 20,471; sh: 3,471; perl: 2,218; yacc: 644; fortran: 397; lisp: 265; makefile: 171; lex: 125; awk: 68; csh: 39
file content (275 lines) | stat: -rw-r--r-- 9,138 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * This file is part of the GROMACS molecular simulation package.
 *
 * Copyright 2018- The GROMACS Authors
 * and the project initiators Erik Lindahl, Berk Hess and David van der Spoel.
 * Consult the AUTHORS/COPYING files and https://www.gromacs.org for details.
 *
 * GROMACS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * GROMACS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GROMACS; if not, see
 * https://www.gnu.org/licenses, or write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
 *
 * If you want to redistribute modifications to GROMACS, please
 * consider that scientific software is very special. Version
 * control is crucial - bugs must be traceable. We will be happy to
 * consider code for inclusion in the official distribution, but
 * derived work must not be called official GROMACS. Details are found
 * in the README & COPYING files - if they are missing, get the
 * official version at https://www.gromacs.org.
 *
 * To help us fund GROMACS development, we humbly ask that you cite
 * the research papers on the package. Check out https://www.gromacs.org.
 */
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include <gtest/gtest.h>

#include "gromacs/math/functions.h"
#include "gromacs/math/vectypes.h"
#include "gromacs/restraint/restraintpotential.h"
#include "gromacs/utility/exceptions.h"

#include "programs/mdrun/tests/moduletest.h"

#include "gmxapi/context.h"
#include "gmxapi/md.h"
#include "gmxapi/md/mdmodule.h"
#include "gmxapi/md/mdsignals.h"
#include "gmxapi/session.h"
#include "gmxapi/session/resources.h"
#include "gmxapi/status.h"
#include "gmxapi/system.h"

#include "testingconfiguration.h"

namespace gmxapi
{
class SessionResources;

namespace testing
{

namespace
{

/*!
 * \brief Restraint that can optionally issue an immediate stop signal.
 */
class StopSignalIssuer : public gmx::IRestraintPotential
{
public:
    /*!
     * \brief Construct a restraint that does nothing.
     */
    StopSignalIssuer() : StopSignalIssuer(false) {}

    /*!
     * \brief Choose whether or not to issue stop signal when called.
     *
     * \param sendStopSignal If true, issue stop signal at every opportunity.
     */
    explicit StopSignalIssuer(bool sendStopSignal) : sendStopSignal_{ sendStopSignal } {}

    /*! \cond Implement IRestraintPotential */
    gmx::PotentialPointData evaluate(gmx::Vector /* r_site */, gmx::Vector /*  r_ref */, double t) override
    {
        // Note that evaluate gets called once for each site,
        // which is twice per time step for a pair restraint.
        // The following initialization logic is not atomic, but it is sufficient.
        if (!isInitialized())
        {
            // Force is also calculated for initial step.
            simulationStartTime_ = t;
        }
        lastSimulationTime_ = t;

        if (sendStopSignal_)
        {
            auto signalSender = gmxapi::getMdrunnerSignal(resources_, gmxapi::md::signals::STOP);
            signalSender();
        }

        return { { 0., 0., 0. }, 0. };
    }

    std::vector<int> sites() const override { return { { 0, 1 } }; }

    void bindSession(gmxapi::SessionResources* resources) override { resources_ = resources; }
    //! \endcond

    /*!
     * \brief Note simulation start time when called on the zeroeth step.
     */
    double simulationStartTime_ = 0.;

    /*!
     * \brief Record the simulation time at the last step active.
     */
    std::optional<double> lastSimulationTime_;

    /*!
     * \brief Whether restraint was ever used
     */
    bool isInitialized() const { return lastSimulationTime_.has_value(); }

private:
    /*!
     * \brief Handle through which to get signalling resources.
     */
    gmxapi::SessionResources* resources_ = nullptr;

    /*!
     * \brief Whether to issue stop signal when called.
     */
    bool sendStopSignal_ = false;
};

/*!
 * \brief Wrap a StopSignalIssuer for testing purposes.
 */
class SimpleSignalingClient : public gmxapi::MDModule
{
public:
    /*! \cond
     * Implement gmxapi::MDModule interface.
     */
    SimpleSignalingClient() : restraint_(std::make_shared<StopSignalIssuer>()) {}

    explicit SimpleSignalingClient(bool sendStopSignal) :
        restraint_(std::make_shared<StopSignalIssuer>(sendStopSignal))
    {
    }

    const char* name() const override { return "SimpleSignalingClient"; }

    std::shared_ptr<gmx::IRestraintPotential> getRestraint() override { return restraint_; }
    //! \endcond

    /*!
     * \brief Last time this restraint was active, minus the simulation start time.
     *
     * \return Time elapsed since start.
     */
    double timeElapsedSinceStart() const
    {
        if (!restraint_->isInitialized())
        {
            GMX_THROW(gmx::InternalError("timeElapsedSinceStart called before restraint was used"));
        }
        return restraint_->lastSimulationTime_.value() - restraint_->simulationStartTime_;
    }

private:
    //! restraint to provide to client or MD simulator.
    std::shared_ptr<StopSignalIssuer> restraint_;
};

/*!
 * \brief Check that we can bind to and use the stop signaler.
 */
TEST_F(GmxApiTest, ApiRunnerStopSignalClient)
{
    const int nsteps = 4;
    makeTprFile(nsteps);

    // Check assumptions about basic simulation behavior.
    {
        const int nstlist = 1;

        auto system  = gmxapi::fromTprFile(runner_.tprFileName_);
        auto context = std::make_shared<gmxapi::Context>(gmxapi::createContext());

        gmxapi::MDArgs args = makeMdArgs();
        args.emplace_back("-nstlist");
        args.emplace_back(std::to_string(nstlist));

        context->setMDArgs(args);

        auto restraint = std::make_shared<SimpleSignalingClient>();

        auto session = system.launch(context);
        EXPECT_TRUE(session);

        gmxapi::addSessionRestraint(session.get(), restraint);
        EXPECT_THROW(restraint->timeElapsedSinceStart(), gmx::InternalError);

        gmxapi::Status status;
        ASSERT_NO_THROW(status = session->run());
        EXPECT_TRUE(status.success());
        EXPECT_EQ(nsteps, gmx::roundToInt(restraint->timeElapsedSinceStart() / getTestStepSize()));

        status = session->close();
        EXPECT_TRUE(status.success());
    }

    // Make sure that stop signal shortens simulation.
    {
        /* StopHandler promises to stop a simulation at the next NS step after the signal got communicated.
         * We don't know the communication interval, but we know that it is at most nstlist. We cannot assume
         * that the signal gets communicated on the step it is set, even if that step is a communication step.
         * As the signal is set on the first step, we know that the restraint will be called at
         * most 2*nstlist + 1 times.
         * Since the time elapsed after the first step is 0, however, we expect the elapsed time
         * divided by the step size to be at most 2*nstlist.
         */

        const int           nstlist  = 1;
        constexpr const int maxsteps = nstlist * 2 + 1;
        // This test is meaningless if the the simulation ends early without a signal.
        static_assert(
                maxsteps < nsteps,
                "Simulation is already scheduled to end before it can receive a stop signal.");

        auto system  = gmxapi::fromTprFile(runner_.tprFileName_);
        auto context = std::make_shared<gmxapi::Context>(gmxapi::createContext());

        gmxapi::MDArgs args = makeMdArgs();
        args.emplace_back("-nstlist");
        args.emplace_back(std::to_string(nstlist));
        // TODO (Ref #3256) use api functionality to extend simulation instead
        args.emplace_back("-nsteps");
        args.emplace_back(std::to_string(nsteps));

        context->setMDArgs(args);

        const bool issueImmediateStopSignal = true;
        auto       restraint = std::make_shared<SimpleSignalingClient>(issueImmediateStopSignal);

        auto session = system.launch(context);
        EXPECT_TRUE(session);

        gmxapi::addSessionRestraint(session.get(), restraint);
        EXPECT_THROW(restraint->timeElapsedSinceStart(), gmx::InternalError);

        gmxapi::Status status;
        ASSERT_NO_THROW(status = session->run());
        EXPECT_TRUE(status.success());

        const int steps_just_run =
                gmx::roundToInt(restraint->timeElapsedSinceStart() / getTestStepSize());
        EXPECT_LT(steps_just_run, maxsteps);

        status = session->close();
        EXPECT_TRUE(status.success());
    }
}

} // end anonymous namespace

} // end namespace testing

} // end namespace gmxapi