Add preprocessing and integration test for transformation coordinate
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / tests / readir.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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 /*! \internal \file
36  * \brief
37  * Test routines that parse mdp fields from grompp input and writes
38  * mdp back out.
39  *
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.
43  *
44  * \author Mark Abraham <mark.j.abraham@gmail.com>
45  */
46 #include "gmxpre.h"
47
48 #include "gromacs/gmxpreprocess/readir.h"
49
50 #include <string>
51
52 #include <gtest/gtest.h>
53
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"
62
63 #include "testutils/refdata.h"
64 #include "testutils/testasserts.h"
65 #include "testutils/testfilemanager.h"
66 namespace gmx
67 {
68 namespace test
69 {
70
71 class GetIrTest : public ::testing::Test
72 {
73 public:
74     GetIrTest() : wi_(init_warning(FALSE, 0)), wiGuard_(wi_)
75     {
76         snew(opts_.include, STRLEN);
77         snew(opts_.define, STRLEN);
78     }
79     ~GetIrTest() override
80     {
81         done_inputrec_strings();
82         sfree(opts_.include);
83         sfree(opts_.define);
84     }
85
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
88     {
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
93     };
94
95     /*! \brief Test mdp reading and writing
96      *
97      * \todo Modernize read_inp and write_inp to use streams,
98      * which will make these tests run faster, because they don't
99      * use disk files. */
100     void runTest(const std::string& inputMdpFileContents,
101                  const TestBehavior testBehavior = TestBehavior::NoErrorAndCompareOutput)
102     {
103         const bool expectError = testBehavior == TestBehavior::ErrorAndCompareOutput
104                                  || testBehavior == TestBehavior::ErrorAndDoNotCompareOutput;
105         const bool compareOutput = testBehavior == TestBehavior::ErrorAndCompareOutput
106                                    || testBehavior == TestBehavior::NoErrorAndCompareOutput;
107
108         std::string inputMdpFilename = fileManager_.getTemporaryFilePath("input.mdp");
109         std::string outputMdpFilename;
110         if (compareOutput)
111         {
112             outputMdpFilename = fileManager_.getTemporaryFilePath("output.mdp");
113         }
114         TextWriter::writeFileFromString(inputMdpFilename, inputMdpFileContents);
115
116         get_ir(inputMdpFilename.c_str(),
117                outputMdpFilename.empty() ? nullptr : outputMdpFilename.c_str(),
118                &mdModules_,
119                &ir_,
120                &opts_,
121                WriteMdpHeader::no,
122                wi_);
123
124         check_ir(inputMdpFilename.c_str(), mdModules_.notifiers(), &ir_, &opts_, wi_);
125         // Now check
126         bool failure = warning_errors_exist(wi_);
127         EXPECT_EQ(failure, expectError);
128
129         if (compareOutput)
130         {
131             TestReferenceData    data;
132             TestReferenceChecker checker(data.rootChecker());
133             checker.checkBoolean(failure, "Error parsing mdp file");
134             warning_reset(wi_);
135
136             auto outputMdpContents = TextReader::readFileToString(outputMdpFilename);
137             checker.checkString(outputMdpContents, "OutputMdpFile");
138         }
139     }
140
141     TestFileManager                    fileManager_;
142     t_inputrec                         ir_;
143     MDModules                          mdModules_;
144     t_gromppopts                       opts_;
145     warninp_t                          wi_;
146     unique_cptr<warninp, free_warning> wiGuard_;
147 };
148
149 TEST_F(GetIrTest, HandlesDifferentKindsOfMdpLines)
150 {
151     const char* inputMdpFile[] = { "; File to run my simulation",
152                                    "title = simulation",
153                                    "define = -DBOOLVAR -DVAR=VALUE",
154                                    ";",
155                                    "xtc_grps = System ; was Protein",
156                                    "include = -I/home/me/stuff",
157                                    "",
158                                    "tau-t = 0.1 0.3",
159                                    "ref-t = ;290 290",
160                                    "tinit = 0.3",
161                                    "init_step = 0",
162                                    "nstcomm = 100",
163                                    "integrator = steep" };
164     runTest(joinStrings(inputMdpFile, "\n"));
165 }
166
167 TEST_F(GetIrTest, RejectsNonCommentLineWithNoEquals)
168 {
169     const char* inputMdpFile = "title simulation";
170     GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile), "No '=' to separate");
171 }
172
173 TEST_F(GetIrTest, AcceptsKeyWithoutValue)
174 {
175     // Users are probably using lines like this
176     const char* inputMdpFile = "xtc_grps = ";
177     runTest(inputMdpFile);
178 }
179
180 TEST_F(GetIrTest, RejectsValueWithoutKey)
181 {
182     const char* inputMdpFile = "= -I/home/me/stuff";
183     GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile), "No .mdp parameter name was found");
184 }
185
186 TEST_F(GetIrTest, RejectsEmptyKeyAndEmptyValue)
187 {
188     const char* inputMdpFile = " = ";
189     GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile),
190                                   "No .mdp parameter name or value was found");
191 }
192
193 TEST_F(GetIrTest, AcceptsDefineParametersWithValuesIncludingAssignment)
194 {
195     const char* inputMdpFile[] = {
196         "define = -DBOOL -DVAR=VALUE",
197     };
198     runTest(joinStrings(inputMdpFile, "\n"));
199 }
200
201 TEST_F(GetIrTest, AcceptsEmptyLines)
202 {
203     const char* inputMdpFile = "";
204     runTest(inputMdpFile);
205 }
206
207 TEST_F(GetIrTest, MtsCheckNstcalcenergy)
208 {
209     const char* inputMdpFile[] = {
210         "mts = yes", "mts-levels = 2", "mts-level2-factor = 2", "nstcalcenergy = 5"
211     };
212     runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
213 }
214
215 TEST_F(GetIrTest, MtsCheckNstenergy)
216 {
217     const char* inputMdpFile[] = {
218         "mts = yes", "mts-levels = 2", "mts-level2-factor = 2", "nstenergy = 5"
219     };
220     runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
221 }
222
223 TEST_F(GetIrTest, MtsCheckNstpcouple)
224 {
225     const char* inputMdpFile[] = { "mts = yes",
226                                    "mts-levels = 2",
227                                    "mts-level2-factor = 2",
228                                    "pcoupl = Berendsen",
229                                    "nstpcouple = 5" };
230     runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
231 }
232
233 TEST_F(GetIrTest, MtsCheckNstdhdl)
234 {
235     const char* inputMdpFile[] = {
236         "mts = yes", "mts-level2-factor = 2", "free-energy = yes", "nstdhdl = 5"
237     };
238     runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
239 }
240
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)
244 {
245     const char* inputMdpFile = "electric-field-x = 1.2 0 0 0";
246     runTest(inputMdpFile);
247 }
248
249 TEST_F(GetIrTest, AcceptsElectricFieldPulsed)
250 {
251     const char* inputMdpFile = "electric-field-y = 3.7 2.0 6.5 1.0";
252     runTest(inputMdpFile);
253 }
254
255 TEST_F(GetIrTest, AcceptsElectricFieldOscillating)
256 {
257     const char* inputMdpFile = "electric-field-z = 3.7 7.5 0 0";
258     runTest(inputMdpFile);
259 }
260
261 TEST_F(GetIrTest, RejectsDuplicateOldAndNewKeys)
262 {
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");
266 }
267
268 TEST_F(GetIrTest, AcceptsImplicitSolventNo)
269 {
270     const char* inputMdpFile = "implicit-solvent = no";
271     runTest(inputMdpFile);
272 }
273
274 TEST_F(GetIrTest, RejectsImplicitSolventYes)
275 {
276     const char* inputMdpFile = "implicit-solvent = yes";
277     GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile), "Invalid enum");
278 }
279
280 TEST_F(GetIrTest, AcceptsMimic)
281 {
282     const char* inputMdpFile[] = { "integrator = mimic", "QMMM-grps = QMatoms" };
283     runTest(joinStrings(inputMdpFile, "\n"));
284 }
285
286 #if HAVE_MUPARSER
287
288 TEST_F(GetIrTest, AcceptsTransformationCoord)
289 {
290     const char* inputMdpFile[] = {
291         "pull = yes",
292         "pull-ngroups = 2",
293         "pull-ncoords = 2",
294         "pull-coord1-geometry = distance",
295         "pull-coord1-groups = 1 2",
296         "pull-coord1-k = 1",
297         "pull-coord2-geometry = transformation",
298         "pull-coord2-expression = 1/(1+x1)",
299         "pull-coord2-k = 10",
300     };
301     runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::NoErrorAndDoNotCompareOutput);
302 }
303
304 TEST_F(GetIrTest, InvalidTransformationCoordWithConstraint)
305 {
306     const char* inputMdpFile[] = {
307         "pull = yes",
308         "pull-ncoords = 1",
309         "pull-coord1-geometry = transformation",
310         "pull-coord1-type = constraint", // INVALID
311         "pull-coord1-expression = 10",
312     };
313     runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
314 }
315
316 TEST_F(GetIrTest, InvalidPullCoordWithConstraintInTransformationExpression)
317 {
318     const char* inputMdpFile[] = {
319         "pull = yes",
320         "pull-ngroups = 2",
321         "pull-ncoords = 2",
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",
327     };
328     runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
329 }
330
331 TEST_F(GetIrTest, InvalidTransformationCoordDxValue)
332 {
333     const char* inputMdpFile[] = {
334         "pull = yes",
335         "pull-ncoords = 1",
336         "pull-coord1-geometry = transformation",
337         "pull-coord1-expression = 10",
338         "pull-coord1-dx = 0", // INVALID
339     };
340     runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
341 }
342
343 TEST_F(GetIrTest, MissingTransformationCoordExpression)
344 {
345     const char* inputMdpFile[] = {
346         "pull = yes",
347         "pull-ncoords = 1",
348         "pull-coord1-geometry = transformation",
349     };
350     runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
351 }
352 #endif // HAVE_MUPARSER
353
354 } // namespace test
355 } // namespace gmx