Rework GMXAPI tests
[gromacs.git] / src / api / cpp / tests / restraint.cpp
blob33d4d05805160c20074daf6baad47d936489012d
1 /*
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.
35 #include <memory>
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"
50 namespace gmxapi
53 namespace testing
56 namespace
59 /*!
60 * \brief Restraint that does nothing other than note whether it has been used.
62 class NullRestraint : public gmx::IRestraintPotential
64 public:
65 /*! \cond Implement IRestraintPotential */
66 gmx::PotentialPointData evaluate(gmx::Vector /* r1 */,
67 gmx::Vector /* r2 */,
68 double /* t */ ) override
70 hasBeenCalled_ = true;
71 return {{0., 0., 0.}, 0.};
74 std::vector<int> sites() const override
76 return {{0, 1}};
79 void bindSession(gmxapi::SessionResources * /* resources */) override
82 //! \endcond
84 /*!
85 * \brief Check whether the restraint has been called as an IForceProvider.
87 * \return false until restraint is used in MD loop, then true.
89 bool hasBeenCalled() const
91 return hasBeenCalled_;
94 private:
95 //! State: false until after the first call to evaluate()
96 bool hasBeenCalled_ = false;
99 /*!
100 * \brief Wrap a NullRestraint for testing purposes.
102 class SimpleApiModule : public gmxapi::MDModule
104 public:
105 /*! \cond
106 * Implement gmxapi::MDModule interface.
108 SimpleApiModule() :
109 restraint_(std::make_shared<NullRestraint>())
112 const char *name() const override
114 return "SimpleApiModule";
117 std::shared_ptr<gmx::IRestraintPotential> getRestraint() override
119 return restraint_;
121 //! \endcond
124 * \brief Check whether the restraint has been called as an IForceProvider.
126 * \return false until restraint is used in MD loop, then true.
128 bool hasBeenCalled() const
130 return restraint_->hasBeenCalled();
133 private:
134 //! restraint to provide to client or MD simulator, as well as to use to implement hasBeenCalled.
135 std::shared_ptr<NullRestraint> restraint_;
139 * \brief Check that we can attach a restraint and have it called.
141 TEST_F(GmxApiTest, ApiRunnerRestrainedMD)
143 makeTprFile(2);
144 auto system = gmxapi::fromTprFile(runner_.tprFileName_);
147 auto context = std::make_shared<gmxapi::Context>();
148 gmxapi::MDArgs args = makeMdArgs();
150 context->setMDArgs(args);
152 auto restraint = std::make_shared<SimpleApiModule>();
154 auto session = system.launch(context);
155 EXPECT_TRUE(session != nullptr);
156 EXPECT_EQ(restraint->hasBeenCalled(), false);
158 gmxapi::addSessionRestraint(session.get(), restraint);
159 gmxapi::Status status;
160 ASSERT_NO_THROW(status = session->run());
161 EXPECT_TRUE(status.success());
162 EXPECT_EQ(restraint->hasBeenCalled(), true);
164 status = session->close();
165 EXPECT_TRUE(status.success());
166 EXPECT_EQ(restraint->hasBeenCalled(), true);
170 } // end anonymous namespace
172 } // end namespace testing
174 } // end namespace gmxapi