cfe49d757a8da5ec80fc311e2b7a4dabd3718b05
[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,2020, 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>(gmxapi::createContext());
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>(gmxapi::createContext());
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(StopCondition::NextNS);
97
98         auto status = session->run();
99         EXPECT_FALSE(status.success());
100
101         // If this assertion fails, it is not an error, but it indicates expected behavior has
102         // changed and we need to consider the impact of whatever changes caused this.
103         EXPECT_NE(gmx_get_stop_condition(), StopCondition::None);
104
105         session->close();
106     } // allow system and session to be destroyed.
107
108     {
109         context->setMDArgs(args);
110         auto system = gmxapi::fromTprFile(runner_.tprFileName_);
111
112         // If this assertion fails, it is not an error, but it indicates expected behavior has
113         // changed and we need to consider the impact of whatever changes caused this.
114         // We are expecting that the libgromacs state has retained the stop condition from the
115         // previously issued SIGINT
116         EXPECT_NE(gmx_get_stop_condition(), StopCondition::None);
117
118         auto session = system.launch(context);
119
120         // Launching a session should clear the stop condition.
121         EXPECT_EQ(gmx_get_stop_condition(), StopCondition::None);
122
123         auto status = session->run();
124         EXPECT_TRUE(status.success());
125
126         // Stop condition should still be clear.
127         EXPECT_EQ(gmx_get_stop_condition(), StopCondition::None);
128
129         session->close();
130     }
131 }
132
133 /*!
134  * \brief Test simulation continuation.
135  *
136  * Run a simulation, then extend the target number of steps and continue the simulation.
137  */
138 TEST_F(GmxApiTest, RunnerContinuedMD)
139 {
140     // Run a simulation, then extend the target number of steps and continue the simulation
141     makeTprFile(10);
142     auto system = gmxapi::fromTprFile(runner_.tprFileName_);
143
144     {
145         auto context = std::make_shared<gmxapi::Context>(gmxapi::createContext());
146
147         {
148             EXPECT_TRUE(context != nullptr);
149             gmxapi::MDArgs args = makeMdArgs();
150
151             context->setMDArgs(args);
152             auto session = system.launch(context);
153             EXPECT_TRUE(session != nullptr);
154             gmxapi::Status status;
155             ASSERT_NO_THROW(status = session->run());
156             EXPECT_TRUE(status.success());
157             ASSERT_NO_THROW(status = session->close());
158             EXPECT_TRUE(status.success());
159         }
160
161         // Reuse the context. Add MD parameters. Run a new session extending the previous trajectory.
162         {
163             gmxapi::MDArgs args = makeMdArgs();
164             // TODO This needs to be changed to make a new tpr with convert-tpr instead.
165             args.emplace_back("-nsteps");
166             args.emplace_back("20");
167
168             context->setMDArgs(args);
169             auto session = system.launch(context);
170             EXPECT_TRUE(session != nullptr);
171             gmxapi::Status status;
172             ASSERT_NO_THROW(status = session->run());
173             EXPECT_TRUE(status.success());
174             ASSERT_NO_THROW(status = session->close());
175             EXPECT_TRUE(status.success());
176         }
177     }
178 }
179
180 } // end anonymous namespace
181
182 } // end namespace testing
183
184 } // end namespace gmxapi