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