Apply clang-format to source tree
[alexxy/gromacs.git] / src / api / cpp / tests / restraint.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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 #include <memory>
36
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"
44
45 #include "gromacs/math/vectypes.h"
46 #include "gromacs/restraint/restraintpotential.h"
47 #include "gromacs/utility/arrayref.h"
48 #include "gromacs/utility/classhelpers.h"
49
50 namespace gmxapi
51 {
52
53 namespace testing
54 {
55
56 namespace
57 {
58
59 /*!
60  * \brief Restraint that does nothing other than note whether it has been used.
61  */
62 class NullRestraint : public gmx::IRestraintPotential
63 {
64 public:
65     /*! \cond Implement IRestraintPotential */
66     gmx::PotentialPointData evaluate(gmx::Vector /*  r1 */, gmx::Vector /*  r2 */, double /*   t */) override
67     {
68         hasBeenCalled_ = true;
69         return { { 0., 0., 0. }, 0. };
70     }
71
72     std::vector<int> sites() const override { return { { 0, 1 } }; }
73
74     void bindSession(gmxapi::SessionResources* /* resources */) override {}
75     //! \endcond
76
77     /*!
78      * \brief Check whether the restraint has been called as an IForceProvider.
79      *
80      * \return false until restraint is used in MD loop, then true.
81      */
82     bool hasBeenCalled() const { return hasBeenCalled_; }
83
84 private:
85     //! State: false until after the first call to evaluate()
86     bool hasBeenCalled_ = false;
87 };
88
89 /*!
90  * \brief Wrap a NullRestraint for testing purposes.
91  */
92 class SimpleApiModule : public gmxapi::MDModule
93 {
94 public:
95     /*! \cond
96      * Implement gmxapi::MDModule interface.
97      */
98     SimpleApiModule() : restraint_(std::make_shared<NullRestraint>()) {}
99
100     const char* name() const override { return "SimpleApiModule"; }
101
102     std::shared_ptr<gmx::IRestraintPotential> getRestraint() override { return restraint_; }
103     //! \endcond
104
105     /*!
106      * \brief Check whether the restraint has been called as an IForceProvider.
107      *
108      * \return false until restraint is used in MD loop, then true.
109      */
110     bool hasBeenCalled() const { return restraint_->hasBeenCalled(); }
111
112 private:
113     //! restraint to provide to client or MD simulator, as well as to use to implement hasBeenCalled.
114     std::shared_ptr<NullRestraint> restraint_;
115 };
116
117 /*!
118  * \brief Check that we can attach a restraint and have it called.
119  */
120 TEST_F(GmxApiTest, ApiRunnerRestrainedMD)
121 {
122     makeTprFile(2);
123     auto system = gmxapi::fromTprFile(runner_.tprFileName_);
124
125     {
126         auto           context = std::make_shared<gmxapi::Context>();
127         gmxapi::MDArgs args    = makeMdArgs();
128
129         context->setMDArgs(args);
130
131         auto restraint = std::make_shared<SimpleApiModule>();
132
133         auto session = system.launch(context);
134         EXPECT_TRUE(session != nullptr);
135         EXPECT_EQ(restraint->hasBeenCalled(), false);
136
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);
142
143         status = session->close();
144         EXPECT_TRUE(status.success());
145         EXPECT_EQ(restraint->hasBeenCalled(), true);
146     }
147 }
148
149 } // end anonymous namespace
150
151 } // end namespace testing
152
153 } // end namespace gmxapi