Merge release-2019 into master
[gromacs.git] / src / programs / mdrun / tests / termination.cpp
blobca34f572522d188ed17ea6a0881567e714322576
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018,2019, 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.
36 /*! \internal \file
37 * \brief
38 * Tests for the mdrun termination functionality
40 * \todo This approach is not very elegant, but "stuff doesn't
41 * segfault or give a fatal error" is a useful result. We can improve
42 * it when we can mock out more do_md() functionality. Before that,
43 * we'd probably prefer not to run this test case in per-patchset
44 * verification, but this is the best we can do for now.
46 * \author Mark Abraham <mark.j.abraham@gmail.com>
47 * \ingroup module_mdrun_integration_tests
49 #include "gmxpre.h"
51 #include <gtest/gtest.h>
53 #include "gromacs/utility/path.h"
54 #include "gromacs/utility/stringutil.h"
55 #include "gromacs/utility/textreader.h"
57 #include "testutils/testasserts.h"
58 #include "testutils/testfilemanager.h"
60 #include "moduletest.h"
61 #include "terminationhelper.h"
63 namespace gmx
65 namespace test
68 //! Build a simple .mdp file
69 static void organizeMdpFile(SimulationRunner *runner,
70 int nsteps = 2)
72 // Make sure -maxh has a chance to propagate
73 runner->useStringAsMdpFile(formatString("nsteps = %d\n"
74 "tcoupl = v-rescale\n"
75 "tc-grps = System\n"
76 "tau-t = 1\n"
77 "ref-t = 298\n",
78 nsteps));
81 //! Convenience typedef
82 typedef MdrunTestFixture MdrunTerminationTest;
84 TEST_F(MdrunTerminationTest, CheckpointRestartAppendsByDefault)
86 runner_.cptFileName_ = fileManager_.getTemporaryFilePath(".cpt");
88 runner_.useTopGroAndNdxFromDatabase("spc2");
89 organizeMdpFile(&runner_);
90 EXPECT_EQ(0, runner_.callGrompp());
92 SCOPED_TRACE("Running the first simulation part");
94 CommandLine firstPart;
95 firstPart.append("mdrun");
96 firstPart.addOption("-cpo", runner_.cptFileName_);
97 ASSERT_EQ(0, runner_.callMdrun(firstPart));
98 ASSERT_TRUE(File::exists(runner_.cptFileName_, File::returnFalseOnError)) <<
99 runner_.cptFileName_ << " was not found and should be";
101 SCOPED_TRACE("Running the second simulation part with default appending behavior");
103 runner_.changeTprNsteps(4);
105 CommandLine secondPart;
106 secondPart.append("mdrun");
107 secondPart.addOption("-cpi", runner_.cptFileName_);
108 ASSERT_EQ(0, runner_.callMdrun(secondPart));
110 auto logFileContents = TextReader::readFileToString(runner_.logFileName_);
111 EXPECT_NE(std::string::npos, logFileContents.find("Restarting from checkpoint, appending to previous log file")) << "appending was not detected";
115 TEST_F(MdrunTerminationTest, WritesCheckpointAfterMaxhTerminationAndThenRestarts)
117 runner_.cptFileName_ = fileManager_.getTemporaryFilePath(".cpt");
119 runner_.useTopGroAndNdxFromDatabase("spc2");
120 organizeMdpFile(&runner_, 100);
121 EXPECT_EQ(0, runner_.callGrompp());
123 SCOPED_TRACE("Running the first simulation part with -maxh");
125 CommandLine firstPart;
126 firstPart.append("mdrun");
127 firstPart.addOption("-cpo", runner_.cptFileName_);
128 // Ensure maxh will trigger the halt, and that the signal will
129 // have time to be propagated.
131 // TODO It would be nicer to set nstlist in the .mdp file, but
132 // then it is not a command.
133 firstPart.addOption("-maxh", 1e-7);
134 firstPart.addOption("-nstlist", 1);
135 ASSERT_EQ(0, runner_.callMdrun(firstPart));
136 EXPECT_EQ(true, File::exists(runner_.cptFileName_, File::returnFalseOnError)) <<
137 runner_.cptFileName_ << " was not found";
140 SCOPED_TRACE("Running the second simulation part");
142 runner_.changeTprNsteps(102);
144 CommandLine secondPart;
145 secondPart.append("mdrun");
146 secondPart.addOption("-cpi", runner_.cptFileName_);
147 ASSERT_EQ(0, runner_.callMdrun(secondPart));
149 auto logFileContents = TextReader::readFileToString(runner_.logFileName_);
150 EXPECT_NE(std::string::npos, logFileContents.find("Writing checkpoint, step 102")) << "completion of restarted simulation was not detected";
154 TEST_F(MdrunTerminationTest, CheckpointRestartWithNoAppendWorksAndCannotLaterAppend)
156 runner_.cptFileName_ = fileManager_.getTemporaryFilePath(".cpt");
158 runner_.useTopGroAndNdxFromDatabase("spc2");
159 organizeMdpFile(&runner_);
160 EXPECT_EQ(0, runner_.callGrompp());
162 SCOPED_TRACE("Running the first simulation part");
164 CommandLine firstPart;
165 firstPart.append("mdrun");
166 firstPart.addOption("-cpo", runner_.cptFileName_);
167 ASSERT_EQ(0, runner_.callMdrun(firstPart));
168 EXPECT_EQ(true, File::exists(runner_.cptFileName_, File::returnFalseOnError)) <<
169 runner_.cptFileName_ << " was not found";
172 SCOPED_TRACE("Running the second simulation part with -noappend");
174 runner_.changeTprNsteps(4);
176 CommandLine secondPart;
177 secondPart.append("mdrun");
178 secondPart.addOption("-cpi", runner_.cptFileName_);
179 secondPart.addOption("-cpo", runner_.cptFileName_);
180 secondPart.append("-noappend");
181 ASSERT_EQ(0, runner_.callMdrun(secondPart));
183 auto expectedLogFileName = fileManager_.getTemporaryFilePath(".part0002.log");
184 ASSERT_EQ(true, File::exists(expectedLogFileName, File::returnFalseOnError)) <<
185 expectedLogFileName << " was not found";
186 auto expectedEdrFileName = fileManager_.getTemporaryFilePath(".part0002.edr");
187 ASSERT_EQ(true, File::exists(expectedEdrFileName, File::returnFalseOnError)) <<
188 expectedEdrFileName << " was not found";
191 SCOPED_TRACE("Running the third simulation part with -append, which will fail");
192 runner_.logFileName_ = fileManager_.getTemporaryFilePath(".part0002.log");
193 runner_.changeTprNsteps(6);
196 CommandLine thirdPart;
197 thirdPart.append("mdrun");
198 thirdPart.addOption("-cpi", runner_.cptFileName_);
199 thirdPart.addOption("-cpo", runner_.cptFileName_);
200 thirdPart.append("-append");
201 EXPECT_THROW_GMX(runner_.callMdrun(thirdPart), InconsistentInputError);
203 SCOPED_TRACE("Running the third simulation part with -noappend");
205 CommandLine thirdPart;
206 thirdPart.append("mdrun");
207 thirdPart.addOption("-cpi", runner_.cptFileName_);
208 thirdPart.addOption("-cpo", runner_.cptFileName_);
209 thirdPart.append("-noappend");
210 runner_.edrFileName_ = fileManager_.getTemporaryFilePath(".part0003.edr");
211 ASSERT_EQ(0, runner_.callMdrun(thirdPart));
213 auto expectedLogFileName = fileManager_.getTemporaryFilePath(".part0003.log");
214 EXPECT_EQ(true, File::exists(expectedLogFileName, File::returnFalseOnError)) <<
215 expectedLogFileName << " was not found";
216 auto expectedEdrFileName = fileManager_.getTemporaryFilePath(".part0003.edr");
217 ASSERT_EQ(true, File::exists(expectedEdrFileName, File::returnFalseOnError)) <<
218 expectedEdrFileName << " was not found";
220 SCOPED_TRACE("Running the fourth simulation part with default appending");
221 runner_.changeTprNsteps(8);
223 CommandLine fourthPart;
224 fourthPart.append("mdrun");
225 fourthPart.addOption("-cpi", runner_.cptFileName_);
226 fourthPart.addOption("-cpo", runner_.cptFileName_);
227 // TODO this is necessary, but ought not be. Is this the issue in Redmine #2804?
228 fourthPart.append("-noappend");
229 runner_.edrFileName_ = fileManager_.getTemporaryFilePath(".part0004.edr");
230 runner_.logFileName_ = fileManager_.getTemporaryFilePath(".part0004.log");
231 ASSERT_EQ(0, runner_.callMdrun(fourthPart));
233 auto expectedLogFileName = fileManager_.getTemporaryFilePath(".part0004.log");
234 ASSERT_EQ(true, File::exists(expectedLogFileName, File::returnFalseOnError)) <<
235 expectedLogFileName << " was not found";
236 auto expectedEdrFileName = fileManager_.getTemporaryFilePath(".part0004.edr");
237 ASSERT_EQ(true, File::exists(expectedEdrFileName, File::returnFalseOnError)) <<
238 expectedEdrFileName << " was not found";
240 SCOPED_TRACE("Running the fifth simulation part with no extra steps");
242 CommandLine fifthPart;
243 fifthPart.append("mdrun");
244 fifthPart.addOption("-cpi", runner_.cptFileName_);
245 fifthPart.addOption("-cpo", runner_.cptFileName_);
246 // TODO this is necessary, but ought not be. Is this the issue in Redmine #2804?
247 fifthPart.append("-noappend");
248 runner_.edrFileName_ = fileManager_.getTemporaryFilePath(".part0005.edr");
249 runner_.logFileName_ = fileManager_.getTemporaryFilePath(".part0005.log");
250 ASSERT_EQ(0, runner_.callMdrun(fifthPart));
252 auto expectedLogFileName = fileManager_.getTemporaryFilePath(".part0005.log");
253 ASSERT_EQ(true, File::exists(expectedLogFileName, File::returnFalseOnError)) <<
254 expectedLogFileName << " was not found";
255 auto expectedEdrFileName = fileManager_.getTemporaryFilePath(".part0005.edr");
256 ASSERT_EQ(true, File::exists(expectedEdrFileName, File::returnFalseOnError)) <<
257 expectedEdrFileName << " was not found";
261 TEST_F(MdrunTerminationTest, CheckpointRestartWorksEvenWithMissingCheckpointFile)
263 runner_.cptFileName_ = fileManager_.getTemporaryFilePath(".cpt");
265 runner_.useTopGroAndNdxFromDatabase("spc2");
266 organizeMdpFile(&runner_);
267 EXPECT_EQ(0, runner_.callGrompp());
269 SCOPED_TRACE("Running the first simulation part");
271 CommandLine firstPart;
272 firstPart.append("mdrun");
273 firstPart.addOption("-cpo", runner_.cptFileName_);
274 ASSERT_EQ(0, runner_.callMdrun(firstPart));
275 EXPECT_EQ(true, File::exists(runner_.cptFileName_, File::returnFalseOnError)) <<
276 runner_.cptFileName_ << " was not found";
279 SCOPED_TRACE("Running the second simulation part after deleting the checkpoint file");
281 runner_.changeTprNsteps(4);
283 CommandLine secondPart;
284 secondPart.append("mdrun");
285 secondPart.addOption("-cpi", runner_.cptFileName_);
286 secondPart.addOption("-cpo", runner_.cptFileName_);
288 // Remove the checkpoint, so technically this can no longer be
289 // a restart. But it starts again from the beginning anyway.
291 // TODO what do we want the behaviour to be?
292 std::remove(runner_.cptFileName_.c_str());
294 ASSERT_EQ(0, runner_.callMdrun(secondPart));
295 auto logFileContents = TextReader::readFileToString(runner_.logFileName_);
296 EXPECT_EQ(std::string::npos, logFileContents.find("Restarting from checkpoint, appending to previous log file")) << "appending was not detected";
300 TEST_F(MdrunTerminationTest, CheckpointRestartWorksEvenWithAppendAndMissingCheckpointFile)
302 runner_.cptFileName_ = fileManager_.getTemporaryFilePath(".cpt");
304 runner_.useTopGroAndNdxFromDatabase("spc2");
305 organizeMdpFile(&runner_);
306 EXPECT_EQ(0, runner_.callGrompp());
308 SCOPED_TRACE("Running the first simulation part");
310 CommandLine firstPart;
311 firstPart.append("mdrun");
312 firstPart.addOption("-cpo", runner_.cptFileName_);
313 ASSERT_EQ(0, runner_.callMdrun(firstPart));
314 EXPECT_EQ(true, File::exists(runner_.cptFileName_, File::returnFalseOnError)) <<
315 runner_.cptFileName_ << " was not found";
318 SCOPED_TRACE("Running the second simulation part with -append after deleting the checkpoint file");
320 runner_.changeTprNsteps(4);
322 CommandLine secondPart;
323 secondPart.append("mdrun");
324 secondPart.addOption("-cpi", runner_.cptFileName_);
325 secondPart.addOption("-cpo", runner_.cptFileName_);
326 secondPart.append("-append");
328 // Remove the checkpoint, so this can no longer be a
329 // restart.
330 std::remove(runner_.cptFileName_.c_str());
332 EXPECT_THROW_GMX(runner_.callMdrun(secondPart), InconsistentInputError);
336 } // namespace test
337 } // namespace gmx