Apply clang-format to source tree
[alexxy/gromacs.git] / src / api / cpp / tests / runner.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/session.h"
40 #include "gmxapi/status.h"
41 #include "gmxapi/system.h"
42
43 #include "gromacs/mdlib/sighandler.h"
44 #include "gromacs/mdtypes/iforceprovider.h"
45
46 namespace gmxapi
47 {
48
49 namespace testing
50 {
51
52 namespace
53 {
54
55 /*!
56  * \brief Check that we can run a basic simulation from a simple client.
57  */
58 TEST_F(GmxApiTest, RunnerBasicMD)
59 {
60     makeTprFile(2);
61     auto system = gmxapi::fromTprFile(runner_.tprFileName_);
62
63     {
64         auto           context = std::make_shared<gmxapi::Context>();
65         gmxapi::MDArgs args    = makeMdArgs();
66         // TODO the command line arguments should be set through the
67         // usual command line options settings for the tests
68
69         context->setMDArgs(args);
70         auto session = system.launch(context);
71         EXPECT_TRUE(session != nullptr);
72         gmxapi::Status status;
73         ASSERT_NO_THROW(status = session->run());
74         EXPECT_TRUE(status.success());
75         status = session->close();
76         EXPECT_TRUE(status.success());
77     }
78 }
79
80 /*!
81  * \brief Test our ability to reinitialize the libgromacs environment between simulations.
82  */
83 TEST_F(GmxApiTest, RunnerReinitialize)
84 {
85     auto           context = std::make_shared<gmxapi::Context>();
86     gmxapi::MDArgs args    = makeMdArgs();
87
88     makeTprFile(20);
89
90     {
91         context->setMDArgs(args);
92         auto system  = gmxapi::fromTprFile(runner_.tprFileName_);
93         auto session = system.launch(context);
94
95         // Try to simulate an interrupt signal to catch.
96         gmx_set_stop_condition(gmx_stop_cond_next_ns);
97
98         session->run();
99
100         // If this assertion fails, it is not an error, but it indicates expected behavior has
101         // changed and we need to consider the impact of whatever changes caused this.
102         EXPECT_NE(gmx_get_stop_condition(), gmx_stop_cond_none);
103
104         session->close();
105     } // allow system and session to be destroyed.
106
107     {
108         context->setMDArgs(args);
109         auto system = gmxapi::fromTprFile(runner_.tprFileName_);
110
111         // If this assertion fails, it is not an error, but it indicates expected behavior has
112         // changed and we need to consider the impact of whatever changes caused this.
113         // We are expecting that the libgromacs state has retained the stop condition from the
114         // previously issued SIGINT
115         EXPECT_NE(gmx_get_stop_condition(), gmx_stop_cond_none);
116
117         auto session = system.launch(context);
118
119         // Launching a session should clear the stop condition.
120         EXPECT_EQ(gmx_get_stop_condition(), gmx_stop_cond_none);
121
122         session->run();
123
124         // Stop condition should still be clear.
125         EXPECT_EQ(gmx_get_stop_condition(), gmx_stop_cond_none);
126
127         session->close();
128     }
129 }
130
131 /*!
132  * \brief Test simulation continuation.
133  *
134  * Run a simulation, then extend the target number of steps and continue the simulation.
135  */
136 TEST_F(GmxApiTest, RunnerContinuedMD)
137 {
138     // Run a simulation, then extend the target number of steps and continue the simulation
139     makeTprFile(10);
140     auto system = gmxapi::fromTprFile(runner_.tprFileName_);
141
142     {
143         auto context = std::make_shared<gmxapi::Context>();
144
145         {
146             EXPECT_TRUE(context != nullptr);
147             gmxapi::MDArgs args = makeMdArgs();
148
149             context->setMDArgs(args);
150             auto session = system.launch(context);
151             EXPECT_TRUE(session != nullptr);
152             gmxapi::Status status;
153             ASSERT_NO_THROW(status = session->run());
154             EXPECT_TRUE(status.success());
155             ASSERT_NO_THROW(status = session->close());
156             EXPECT_TRUE(status.success());
157         }
158
159         // Reuse the context. Add MD parameters. Run a new session extending the previous trajectory.
160         {
161             gmxapi::MDArgs args = makeMdArgs();
162             // TODO This needs to be changed to make a new tpr with convert-tpr instead.
163             args.emplace_back("-nsteps");
164             args.emplace_back("20");
165
166             context->setMDArgs(args);
167             auto session = system.launch(context);
168             EXPECT_TRUE(session != nullptr);
169             gmxapi::Status status;
170             ASSERT_NO_THROW(status = session->run());
171             EXPECT_TRUE(status.success());
172             ASSERT_NO_THROW(status = session->close());
173             EXPECT_TRUE(status.success());
174         }
175     }
176 }
177
178 } // end anonymous namespace
179
180 } // end namespace testing
181
182 } // end namespace gmxapi