2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2017,2018,2019,2020,2021, 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.
37 * Test routines that parse mdp fields from grompp input and writes
40 * In particular these will provide test coverage as we refactor to
41 * use a new Options-based key-value-style mdp implementation to
42 * support a more modular mdrun.
44 * \author Mark Abraham <mark.j.abraham@gmail.com>
48 #include "gromacs/gmxpreprocess/readir.h"
52 #include <gtest/gtest.h>
54 #include "gromacs/fileio/warninp.h"
55 #include "gromacs/mdrun/mdmodules.h"
56 #include "gromacs/mdtypes/inputrec.h"
57 #include "gromacs/utility/cstringutil.h"
58 #include "gromacs/utility/stringutil.h"
59 #include "gromacs/utility/textreader.h"
60 #include "gromacs/utility/textwriter.h"
61 #include "gromacs/utility/unique_cptr.h"
63 #include "testutils/refdata.h"
64 #include "testutils/testasserts.h"
65 #include "testutils/testfilemanager.h"
71 class GetIrTest : public ::testing::Test
74 GetIrTest() : wi_(init_warning(FALSE, 0)), wiGuard_(wi_)
76 snew(opts_.include, STRLEN);
77 snew(opts_.define, STRLEN);
81 done_inputrec_strings();
86 //! Tells whether warnings and/or errors are expected from inputrec parsing and checking, and whether we should compare the output
87 enum class TestBehavior
89 NoErrorAndCompareOutput, //!< Expect no warnings/error and compare output
90 NoErrorAndDoNotCompareOutput, //!< Expect no warnings/error and do not compare output
91 ErrorAndCompareOutput, //!< Expect at least one warning/error and compare output
92 ErrorAndDoNotCompareOutput //!< Expect at least one warning/error and do not compare output
95 /*! \brief Test mdp reading and writing
97 * \todo Modernize read_inp and write_inp to use streams,
98 * which will make these tests run faster, because they don't
100 void runTest(const std::string& inputMdpFileContents,
101 const TestBehavior testBehavior = TestBehavior::NoErrorAndCompareOutput)
103 const bool expectError = testBehavior == TestBehavior::ErrorAndCompareOutput
104 || testBehavior == TestBehavior::ErrorAndDoNotCompareOutput;
105 const bool compareOutput = testBehavior == TestBehavior::ErrorAndCompareOutput
106 || testBehavior == TestBehavior::NoErrorAndCompareOutput;
108 std::string inputMdpFilename = fileManager_.getTemporaryFilePath("input.mdp");
109 std::string outputMdpFilename;
112 outputMdpFilename = fileManager_.getTemporaryFilePath("output.mdp");
114 TextWriter::writeFileFromString(inputMdpFilename, inputMdpFileContents);
116 get_ir(inputMdpFilename.c_str(),
117 outputMdpFilename.empty() ? nullptr : outputMdpFilename.c_str(),
124 check_ir(inputMdpFilename.c_str(), mdModules_.notifiers(), &ir_, &opts_, wi_);
126 bool failure = warning_errors_exist(wi_);
127 EXPECT_EQ(failure, expectError);
131 TestReferenceData data;
132 TestReferenceChecker checker(data.rootChecker());
133 checker.checkBoolean(failure, "Error parsing mdp file");
136 auto outputMdpContents = TextReader::readFileToString(outputMdpFilename);
137 checker.checkString(outputMdpContents, "OutputMdpFile");
141 TestFileManager fileManager_;
143 MDModules mdModules_;
146 unique_cptr<warninp, free_warning> wiGuard_;
149 TEST_F(GetIrTest, HandlesDifferentKindsOfMdpLines)
151 const char* inputMdpFile[] = { "; File to run my simulation",
152 "title = simulation",
153 "define = -DBOOLVAR -DVAR=VALUE",
155 "xtc_grps = System ; was Protein",
156 "include = -I/home/me/stuff",
163 "integrator = steep" };
164 runTest(joinStrings(inputMdpFile, "\n"));
167 TEST_F(GetIrTest, RejectsNonCommentLineWithNoEquals)
169 const char* inputMdpFile = "title simulation";
170 GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile), "No '=' to separate");
173 TEST_F(GetIrTest, AcceptsKeyWithoutValue)
175 // Users are probably using lines like this
176 const char* inputMdpFile = "xtc_grps = ";
177 runTest(inputMdpFile);
180 TEST_F(GetIrTest, RejectsValueWithoutKey)
182 const char* inputMdpFile = "= -I/home/me/stuff";
183 GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile), "No .mdp parameter name was found");
186 TEST_F(GetIrTest, RejectsEmptyKeyAndEmptyValue)
188 const char* inputMdpFile = " = ";
189 GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile),
190 "No .mdp parameter name or value was found");
193 TEST_F(GetIrTest, AcceptsDefineParametersWithValuesIncludingAssignment)
195 const char* inputMdpFile[] = {
196 "define = -DBOOL -DVAR=VALUE",
198 runTest(joinStrings(inputMdpFile, "\n"));
201 TEST_F(GetIrTest, AcceptsEmptyLines)
203 const char* inputMdpFile = "";
204 runTest(inputMdpFile);
207 TEST_F(GetIrTest, MtsCheckNstcalcenergy)
209 const char* inputMdpFile[] = {
210 "mts = yes", "mts-levels = 2", "mts-level2-factor = 2", "nstcalcenergy = 5"
212 runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
215 TEST_F(GetIrTest, MtsCheckNstenergy)
217 const char* inputMdpFile[] = {
218 "mts = yes", "mts-levels = 2", "mts-level2-factor = 2", "nstenergy = 5"
220 runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
223 TEST_F(GetIrTest, MtsCheckNstpcouple)
225 const char* inputMdpFile[] = { "mts = yes",
227 "mts-level2-factor = 2",
228 "pcoupl = Berendsen",
230 runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
233 TEST_F(GetIrTest, MtsCheckNstdhdl)
235 const char* inputMdpFile[] = {
236 "mts = yes", "mts-level2-factor = 2", "free-energy = yes", "nstdhdl = 5"
238 runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
241 // These tests observe how the electric-field keys behave, since they
242 // are currently the only ones using the new Options-style handling.
243 TEST_F(GetIrTest, AcceptsElectricField)
245 const char* inputMdpFile = "electric-field-x = 1.2 0 0 0";
246 runTest(inputMdpFile);
249 TEST_F(GetIrTest, AcceptsElectricFieldPulsed)
251 const char* inputMdpFile = "electric-field-y = 3.7 2.0 6.5 1.0";
252 runTest(inputMdpFile);
255 TEST_F(GetIrTest, AcceptsElectricFieldOscillating)
257 const char* inputMdpFile = "electric-field-z = 3.7 7.5 0 0";
258 runTest(inputMdpFile);
261 TEST_F(GetIrTest, RejectsDuplicateOldAndNewKeys)
263 const char* inputMdpFile[] = { "verlet-buffer-drift = 1.3", "verlet-buffer-tolerance = 2.7" };
264 GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(joinStrings(inputMdpFile, "\n")),
265 "A parameter is present with both");
268 TEST_F(GetIrTest, AcceptsImplicitSolventNo)
270 const char* inputMdpFile = "implicit-solvent = no";
271 runTest(inputMdpFile);
274 TEST_F(GetIrTest, RejectsImplicitSolventYes)
276 const char* inputMdpFile = "implicit-solvent = yes";
277 GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile), "Invalid enum");
280 TEST_F(GetIrTest, AcceptsMimic)
282 const char* inputMdpFile[] = { "integrator = mimic", "QMMM-grps = QMatoms" };
283 runTest(joinStrings(inputMdpFile, "\n"));
288 TEST_F(GetIrTest, AcceptsTransformationCoord)
290 const char* inputMdpFile[] = {
294 "pull-coord1-geometry = distance",
295 "pull-coord1-groups = 1 2",
297 "pull-coord2-geometry = transformation",
298 "pull-coord2-expression = 1/(1+x1)",
299 "pull-coord2-k = 10",
301 runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::NoErrorAndDoNotCompareOutput);
304 TEST_F(GetIrTest, InvalidTransformationCoordWithConstraint)
306 const char* inputMdpFile[] = {
309 "pull-coord1-geometry = transformation",
310 "pull-coord1-type = constraint", // INVALID
311 "pull-coord1-expression = 10",
313 runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
316 TEST_F(GetIrTest, InvalidPullCoordWithConstraintInTransformationExpression)
318 const char* inputMdpFile[] = {
322 "pull-coord1-geometry = distance",
323 "pull-coord1-type = constraint", // INVALID
324 "pull-coord1-groups = 1 2",
325 "pull-coord2-geometry = transformation",
326 "pull-coord2-expression = x1",
328 runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
331 TEST_F(GetIrTest, InvalidTransformationCoordDxValue)
333 const char* inputMdpFile[] = {
336 "pull-coord1-geometry = transformation",
337 "pull-coord1-expression = 10",
338 "pull-coord1-dx = 0", // INVALID
340 runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
343 TEST_F(GetIrTest, MissingTransformationCoordExpression)
345 const char* inputMdpFile[] = {
348 "pull-coord1-geometry = transformation",
350 runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
352 #endif // HAVE_MUPARSER