2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018, 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.
37 #include "testingconfiguration.h"
38 #include "gmxapi/context.h"
39 #include "gmxapi/md.h"
40 #include "gmxapi/session.h"
41 #include "gmxapi/status.h"
42 #include "gmxapi/system.h"
43 #include "gmxapi/md/mdmodule.h"
45 #include "gromacs/math/vectypes.h"
46 #include "gromacs/restraint/restraintpotential.h"
47 #include "gromacs/utility/arrayref.h"
48 #include "gromacs/utility/classhelpers.h"
60 * \brief Restraint that does nothing other than note whether it has been used.
62 class NullRestraint : public gmx::IRestraintPotential
65 /*! \cond Implement IRestraintPotential */
66 gmx::PotentialPointData evaluate(gmx::Vector /* r1 */, gmx::Vector /* r2 */, double /* t */) override
68 hasBeenCalled_ = true;
69 return { { 0., 0., 0. }, 0. };
72 std::vector<int> sites() const override { return { { 0, 1 } }; }
74 void bindSession(gmxapi::SessionResources* /* resources */) override {}
78 * \brief Check whether the restraint has been called as an IForceProvider.
80 * \return false until restraint is used in MD loop, then true.
82 bool hasBeenCalled() const { return hasBeenCalled_; }
85 //! State: false until after the first call to evaluate()
86 bool hasBeenCalled_ = false;
90 * \brief Wrap a NullRestraint for testing purposes.
92 class SimpleApiModule : public gmxapi::MDModule
96 * Implement gmxapi::MDModule interface.
98 SimpleApiModule() : restraint_(std::make_shared<NullRestraint>()) {}
100 const char* name() const override { return "SimpleApiModule"; }
102 std::shared_ptr<gmx::IRestraintPotential> getRestraint() override { return restraint_; }
106 * \brief Check whether the restraint has been called as an IForceProvider.
108 * \return false until restraint is used in MD loop, then true.
110 bool hasBeenCalled() const { return restraint_->hasBeenCalled(); }
113 //! restraint to provide to client or MD simulator, as well as to use to implement hasBeenCalled.
114 std::shared_ptr<NullRestraint> restraint_;
118 * \brief Check that we can attach a restraint and have it called.
120 TEST_F(GmxApiTest, ApiRunnerRestrainedMD)
123 auto system = gmxapi::fromTprFile(runner_.tprFileName_);
126 auto context = std::make_shared<gmxapi::Context>(gmxapi::createContext());
127 gmxapi::MDArgs args = makeMdArgs();
129 context->setMDArgs(args);
131 auto restraint = std::make_shared<SimpleApiModule>();
133 auto session = system.launch(context);
134 EXPECT_TRUE(session != nullptr);
135 EXPECT_EQ(restraint->hasBeenCalled(), false);
137 gmxapi::addSessionRestraint(session.get(), restraint);
138 gmxapi::Status status;
139 ASSERT_NO_THROW(status = session->run());
140 EXPECT_TRUE(status.success());
141 EXPECT_EQ(restraint->hasBeenCalled(), true);
143 status = session->close();
144 EXPECT_TRUE(status.success());
145 EXPECT_EQ(restraint->hasBeenCalled(), true);
149 } // end anonymous namespace
151 } // end namespace testing
153 } // end namespace gmxapi