de21db7179f34cf84051af8ef9743f8dfa00fe58
[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         ErrorAndCompareOutput,     //!< Expect at least one warning/error and compare output
91         ErrorAndDoNotCompareOutput //!< Expect at least one warning/error and do not compare output
92     };
93
94     /*! \brief Test mdp reading and writing
95      *
96      * \todo Modernize read_inp and write_inp to use streams,
97      * which will make these tests run faster, because they don't
98      * use disk files. */
99     void runTest(const std::string& inputMdpFileContents,
100                  const TestBehavior testBehavior = TestBehavior::NoErrorAndCompareOutput)
101     {
102         const bool expectError   = testBehavior != TestBehavior::NoErrorAndCompareOutput;
103         const bool compareOutput = testBehavior != TestBehavior::ErrorAndDoNotCompareOutput;
104
105         std::string inputMdpFilename = fileManager_.getTemporaryFilePath("input.mdp");
106         std::string outputMdpFilename;
107         if (compareOutput)
108         {
109             outputMdpFilename = fileManager_.getTemporaryFilePath("output.mdp");
110         }
111
112         TextWriter::writeFileFromString(inputMdpFilename, inputMdpFileContents);
113
114         get_ir(inputMdpFilename.c_str(),
115                outputMdpFilename.empty() ? nullptr : outputMdpFilename.c_str(),
116                &mdModules_,
117                &ir_,
118                &opts_,
119                WriteMdpHeader::no,
120                wi_);
121
122         check_ir(inputMdpFilename.c_str(), mdModules_.notifiers(), &ir_, &opts_, wi_);
123         // Now check
124         bool failure = warning_errors_exist(wi_);
125         EXPECT_EQ(failure, expectError);
126
127         if (compareOutput)
128         {
129             TestReferenceData    data;
130             TestReferenceChecker checker(data.rootChecker());
131             checker.checkBoolean(failure, "Error parsing mdp file");
132             warning_reset(wi_);
133
134             auto outputMdpContents = TextReader::readFileToString(outputMdpFilename);
135             checker.checkString(outputMdpContents, "OutputMdpFile");
136         }
137     }
138
139     TestFileManager                    fileManager_;
140     t_inputrec                         ir_;
141     MDModules                          mdModules_;
142     t_gromppopts                       opts_;
143     warninp_t                          wi_;
144     unique_cptr<warninp, free_warning> wiGuard_;
145 };
146
147 TEST_F(GetIrTest, HandlesDifferentKindsOfMdpLines)
148 {
149     const char* inputMdpFile[] = { "; File to run my simulation",
150                                    "title = simulation",
151                                    "define = -DBOOLVAR -DVAR=VALUE",
152                                    ";",
153                                    "xtc_grps = System ; was Protein",
154                                    "include = -I/home/me/stuff",
155                                    "",
156                                    "tau-t = 0.1 0.3",
157                                    "ref-t = ;290 290",
158                                    "tinit = 0.3",
159                                    "init_step = 0",
160                                    "nstcomm = 100",
161                                    "integrator = steep" };
162     runTest(joinStrings(inputMdpFile, "\n"));
163 }
164
165 TEST_F(GetIrTest, RejectsNonCommentLineWithNoEquals)
166 {
167     const char* inputMdpFile = "title simulation";
168     GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile), "No '=' to separate");
169 }
170
171 TEST_F(GetIrTest, AcceptsKeyWithoutValue)
172 {
173     // Users are probably using lines like this
174     const char* inputMdpFile = "xtc_grps = ";
175     runTest(inputMdpFile);
176 }
177
178 TEST_F(GetIrTest, RejectsValueWithoutKey)
179 {
180     const char* inputMdpFile = "= -I/home/me/stuff";
181     GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile), "No .mdp parameter name was found");
182 }
183
184 TEST_F(GetIrTest, RejectsEmptyKeyAndEmptyValue)
185 {
186     const char* inputMdpFile = " = ";
187     GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile),
188                                   "No .mdp parameter name or value was found");
189 }
190
191 TEST_F(GetIrTest, AcceptsDefineParametersWithValuesIncludingAssignment)
192 {
193     const char* inputMdpFile[] = {
194         "define = -DBOOL -DVAR=VALUE",
195     };
196     runTest(joinStrings(inputMdpFile, "\n"));
197 }
198
199 TEST_F(GetIrTest, AcceptsEmptyLines)
200 {
201     const char* inputMdpFile = "";
202     runTest(inputMdpFile);
203 }
204
205 TEST_F(GetIrTest, MtsCheckNstcalcenergy)
206 {
207     const char* inputMdpFile[] = {
208         "mts = yes", "mts-levels = 2", "mts-level2-factor = 2", "nstcalcenergy = 5"
209     };
210     runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
211 }
212
213 TEST_F(GetIrTest, MtsCheckNstenergy)
214 {
215     const char* inputMdpFile[] = {
216         "mts = yes", "mts-levels = 2", "mts-level2-factor = 2", "nstenergy = 5"
217     };
218     runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
219 }
220
221 TEST_F(GetIrTest, MtsCheckNstpcouple)
222 {
223     const char* inputMdpFile[] = { "mts = yes",
224                                    "mts-levels = 2",
225                                    "mts-level2-factor = 2",
226                                    "pcoupl = Berendsen",
227                                    "nstpcouple = 5" };
228     runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
229 }
230
231 TEST_F(GetIrTest, MtsCheckNstdhdl)
232 {
233     const char* inputMdpFile[] = {
234         "mts = yes", "mts-level2-factor = 2", "free-energy = yes", "nstdhdl = 5"
235     };
236     runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
237 }
238
239 // These tests observe how the electric-field keys behave, since they
240 // are currently the only ones using the new Options-style handling.
241 TEST_F(GetIrTest, AcceptsElectricField)
242 {
243     const char* inputMdpFile = "electric-field-x = 1.2 0 0 0";
244     runTest(inputMdpFile);
245 }
246
247 TEST_F(GetIrTest, AcceptsElectricFieldPulsed)
248 {
249     const char* inputMdpFile = "electric-field-y = 3.7 2.0 6.5 1.0";
250     runTest(inputMdpFile);
251 }
252
253 TEST_F(GetIrTest, AcceptsElectricFieldOscillating)
254 {
255     const char* inputMdpFile = "electric-field-z = 3.7 7.5 0 0";
256     runTest(inputMdpFile);
257 }
258
259 TEST_F(GetIrTest, RejectsDuplicateOldAndNewKeys)
260 {
261     const char* inputMdpFile[] = { "verlet-buffer-drift = 1.3", "verlet-buffer-tolerance = 2.7" };
262     GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(joinStrings(inputMdpFile, "\n")),
263                                   "A parameter is present with both");
264 }
265
266 TEST_F(GetIrTest, AcceptsImplicitSolventNo)
267 {
268     const char* inputMdpFile = "implicit-solvent = no";
269     runTest(inputMdpFile);
270 }
271
272 TEST_F(GetIrTest, RejectsImplicitSolventYes)
273 {
274     const char* inputMdpFile = "implicit-solvent = yes";
275     GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile), "Invalid enum");
276 }
277
278 TEST_F(GetIrTest, AcceptsMimic)
279 {
280     const char* inputMdpFile[] = { "integrator = mimic", "QMMM-grps = QMatoms" };
281     runTest(joinStrings(inputMdpFile, "\n"));
282 }
283
284 } // namespace test
285 } // namespace gmx