80e407451b48b9619c57df2afe6942f6606a6d1c
[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, 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 "config.h"
51
52 #include <string>
53
54 #include <gtest/gtest.h>
55
56 #include "gromacs/fileio/warninp.h"
57 #include "gromacs/mdrun/mdmodules.h"
58 #include "gromacs/mdtypes/inputrec.h"
59 #include "gromacs/utility/cstringutil.h"
60 #include "gromacs/utility/stringutil.h"
61 #include "gromacs/utility/textreader.h"
62 #include "gromacs/utility/textwriter.h"
63 #include "gromacs/utility/unique_cptr.h"
64
65 #include "testutils/refdata.h"
66 #include "testutils/testasserts.h"
67 #include "testutils/testfilemanager.h"
68 namespace gmx
69 {
70 namespace test
71 {
72
73 class GetIrTest : public ::testing::Test
74 {
75 public:
76     GetIrTest() : opts_(), wi_(init_warning(FALSE, 0)), wiGuard_(wi_)
77
78     {
79         snew(opts_.include, STRLEN);
80         snew(opts_.define, STRLEN);
81     }
82     ~GetIrTest() override
83     {
84         done_inputrec_strings();
85         sfree(opts_.include);
86         sfree(opts_.define);
87     }
88     /*! \brief Test mdp reading and writing
89      *
90      * \todo Modernize read_inp and write_inp to use streams,
91      * which will make these tests run faster, because they don't
92      * use disk files. */
93     void runTest(const std::string& inputMdpFileContents)
94     {
95         auto inputMdpFilename  = fileManager_.getTemporaryFilePath("input.mdp");
96         auto outputMdpFilename = fileManager_.getTemporaryFilePath("output.mdp");
97
98         TextWriter::writeFileFromString(inputMdpFilename, inputMdpFileContents);
99
100         get_ir(inputMdpFilename.c_str(), outputMdpFilename.c_str(), &mdModules_, &ir_, &opts_,
101                WriteMdpHeader::no, wi_);
102
103         check_ir(inputMdpFilename.c_str(), mdModules_.notifier(), &ir_, &opts_, wi_);
104         // Now check
105         bool                 failure = warning_errors_exist(wi_);
106         TestReferenceData    data;
107         TestReferenceChecker checker(data.rootChecker());
108         checker.checkBoolean(failure, "Error parsing mdp file");
109         warning_reset(wi_);
110
111         auto outputMdpContents = TextReader::readFileToString(outputMdpFilename);
112         checker.checkString(outputMdpContents, "OutputMdpFile");
113     }
114
115     TestFileManager                    fileManager_;
116     t_inputrec                         ir_;
117     MDModules                          mdModules_;
118     t_gromppopts                       opts_;
119     warninp_t                          wi_;
120     unique_cptr<warninp, free_warning> wiGuard_;
121 };
122
123 TEST_F(GetIrTest, HandlesDifferentKindsOfMdpLines)
124 {
125     const char* inputMdpFile[] = { "; File to run my simulation",
126                                    "title = simulation",
127                                    "define = -DBOOLVAR -DVAR=VALUE",
128                                    ";",
129                                    "xtc_grps = System ; was Protein",
130                                    "include = -I/home/me/stuff",
131                                    "",
132                                    "tau-t = 0.1 0.3",
133                                    "ref-t = ;290 290",
134                                    "tinit = 0.3",
135                                    "init_step = 0",
136                                    "nstcomm = 100",
137                                    "integrator = steep" };
138     runTest(joinStrings(inputMdpFile, "\n"));
139 }
140
141 TEST_F(GetIrTest, RejectsNonCommentLineWithNoEquals)
142 {
143     const char* inputMdpFile = "title simulation";
144     GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile), "No '=' to separate");
145 }
146
147 TEST_F(GetIrTest, AcceptsKeyWithoutValue)
148 {
149     // Users are probably using lines like this
150     const char* inputMdpFile = "xtc_grps = ";
151     runTest(inputMdpFile);
152 }
153
154 TEST_F(GetIrTest, RejectsValueWithoutKey)
155 {
156     const char* inputMdpFile = "= -I/home/me/stuff";
157     GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile), "No .mdp parameter name was found");
158 }
159
160 TEST_F(GetIrTest, RejectsEmptyKeyAndEmptyValue)
161 {
162     const char* inputMdpFile = " = ";
163     GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile),
164                                   "No .mdp parameter name or value was found");
165 }
166
167 TEST_F(GetIrTest, AcceptsDefineParametersWithValuesIncludingAssignment)
168 {
169     const char* inputMdpFile[] = {
170         "define = -DBOOL -DVAR=VALUE",
171     };
172     runTest(joinStrings(inputMdpFile, "\n"));
173 }
174
175 TEST_F(GetIrTest, AcceptsEmptyLines)
176 {
177     const char* inputMdpFile = "";
178     runTest(inputMdpFile);
179 }
180
181 // These tests observe how the electric-field keys behave, since they
182 // are currently the only ones using the new Options-style handling.
183 TEST_F(GetIrTest, AcceptsElectricField)
184 {
185     const char* inputMdpFile = "electric-field-x = 1.2 0 0 0";
186     runTest(inputMdpFile);
187 }
188
189 TEST_F(GetIrTest, AcceptsElectricFieldPulsed)
190 {
191     const char* inputMdpFile = "electric-field-y = 3.7 2.0 6.5 1.0";
192     runTest(inputMdpFile);
193 }
194
195 TEST_F(GetIrTest, AcceptsElectricFieldOscillating)
196 {
197     const char* inputMdpFile = "electric-field-z = 3.7 7.5 0 0";
198     runTest(inputMdpFile);
199 }
200
201 TEST_F(GetIrTest, RejectsDuplicateOldAndNewKeys)
202 {
203     const char* inputMdpFile[] = { "verlet-buffer-drift = 1.3", "verlet-buffer-tolerance = 2.7" };
204     GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(joinStrings(inputMdpFile, "\n")),
205                                   "A parameter is present with both");
206 }
207
208 TEST_F(GetIrTest, AcceptsImplicitSolventNo)
209 {
210     const char* inputMdpFile = "implicit-solvent = no";
211     runTest(inputMdpFile);
212 }
213
214 TEST_F(GetIrTest, RejectsImplicitSolventYes)
215 {
216     const char* inputMdpFile = "implicit-solvent = yes";
217     GMX_EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile), "Invalid enum");
218 }
219
220 TEST_F(GetIrTest, AcceptsMimic)
221 {
222     const char* inputMdpFile[] = { "integrator = mimic", "QMMM-grps = QMatoms" };
223     runTest(joinStrings(inputMdpFile, "\n"));
224 }
225
226 } // namespace test
227 } // namespace gmx