Implemented changes to preprocessor to work with MiMiC QM/MM
[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, 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/mdrunutility/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/testfilemanager.h"
65
66 namespace gmx
67 {
68 namespace test
69 {
70
71 class GetIrTest : public ::testing::Test
72 {
73     public:
74         GetIrTest() :  opts_(),
75                        wi_(init_warning(FALSE, 0)), wiGuard_(wi_)
76
77         {
78             snew(opts_.include, STRLEN);
79             snew(opts_.define, STRLEN);
80         }
81         ~GetIrTest() override
82         {
83             done_inputrec_strings();
84             sfree(opts_.include);
85             sfree(opts_.define);
86         }
87         /*! \brief Test mdp reading and writing
88          *
89          * \todo Modernize read_inp and write_inp to use streams,
90          * which will make these tests run faster, because they don't
91          * use disk files. */
92         void runTest(const std::string &inputMdpFileContents)
93         {
94             auto inputMdpFilename  = fileManager_.getTemporaryFilePath("input.mdp");
95             auto outputMdpFilename = fileManager_.getTemporaryFilePath("output.mdp");
96
97             TextWriter::writeFileFromString(inputMdpFilename, inputMdpFileContents);
98
99             get_ir(inputMdpFilename.c_str(), outputMdpFilename.c_str(),
100                    &mdModules_, &ir_, &opts_, WriteMdpHeader::no, wi_);
101
102             // Now check
103             bool                 failure = warning_errors_exist(wi_);
104             TestReferenceData    data;
105             TestReferenceChecker checker(data.rootChecker());
106             checker.checkBoolean(failure, "Error parsing mdp file");
107             warning_reset(wi_);
108
109             auto outputMdpContents = TextReader::readFileToString(outputMdpFilename);
110             checker.checkString(outputMdpContents, "OutputMdpFile");
111         }
112
113         TestFileManager                    fileManager_;
114         t_inputrec                         ir_;
115         MDModules                          mdModules_;
116         t_gromppopts                       opts_;
117         warninp_t                          wi_;
118         unique_cptr<warninp, free_warning> wiGuard_;
119 };
120
121 TEST_F(GetIrTest, HandlesDifferentKindsOfMdpLines)
122 {
123     const char *inputMdpFile[] = {
124         "; File to run my simulation",
125         "title = simulation",
126         ";",
127         "xtc_grps = System ; was Protein",
128         "include = -I/home/me/stuff",
129         "",
130         "tau-t = 0.1 0.3",
131         "tinit = 0.3",
132         "init_step = 0",
133         "nstcomm = 100",
134         "integrator = steep"
135     };
136     runTest(joinStrings(inputMdpFile, "\n"));
137 }
138
139 // This case is used often by SimulationRunner::useEmptyMdpFile (see
140 // comments there for explanation). When we remove the group scheme,
141 // that usage will have to disappear, and so can this test.
142 TEST_F(GetIrTest, HandlesOnlyCutoffScheme)
143 {
144     const char *inputMdpFile = "cutoff-scheme = Group\n";
145     runTest(inputMdpFile);
146 }
147
148 // TODO Stop accepting any of these
149 TEST_F(GetIrTest, UserErrorsSilentlyTolerated)
150 {
151     const char *inputMdpFile[] = {
152         "title simulation",
153         "xtc_grps = ",
154         "= -I/home/me/stuff",
155         "="
156     };
157     runTest(joinStrings(inputMdpFile, "\n"));
158 }
159
160 TEST_F(GetIrTest, DefineHandlesAssignmentOnRhs)
161 {
162     const char *inputMdpFile[] = {
163         "define = -DBOOL -DVAR=VALUE",
164     };
165     runTest(joinStrings(inputMdpFile, "\n"));
166 }
167
168 TEST_F(GetIrTest, EmptyInputWorks)
169 {
170     const char *inputMdpFile = "";
171     runTest(inputMdpFile);
172 }
173
174 // These tests observe how the electric-field keys behave, since they
175 // are currently the only ones using the new Options-style handling.
176 TEST_F(GetIrTest, ProducesOutputFromElectricField)
177 {
178     const char *inputMdpFile = "electric-field-x = 1.2 0 0 0";
179     runTest(inputMdpFile);
180 }
181
182 TEST_F(GetIrTest, ProducesOutputFromElectricFieldPulsed)
183 {
184     const char *inputMdpFile = "electric-field-y = 3.7 2.0 6.5 1.0";
185     runTest(inputMdpFile);
186 }
187
188 TEST_F(GetIrTest, ProducesOutputFromElectricFieldOscillating)
189 {
190     const char *inputMdpFile = "electric-field-z = 3.7 7.5 0 0";
191     runTest(inputMdpFile);
192 }
193
194 TEST_F(GetIrTest, TerminatesOnDuplicateOldAndNewKeys)
195 {
196     const char *inputMdpFile[] = {"verlet-buffer-drift = 1.3", "verlet-buffer-tolerance = 2.7"};
197     EXPECT_DEATH_IF_SUPPORTED(runTest(joinStrings(inputMdpFile, "\n")), "A parameter is present with both");
198 }
199
200 TEST_F(GetIrTest, ImplicitSolventNoWorks)
201 {
202     const char *inputMdpFile = "implicit-solvent = no";
203     runTest(inputMdpFile);
204 }
205
206 TEST_F(GetIrTest, ImplicitSolventYesWorks)
207 {
208     const char *inputMdpFile = "implicit-solvent = yes";
209     EXPECT_DEATH_IF_SUPPORTED(runTest(inputMdpFile), "Invalid enum");
210 }
211
212 TEST_F(GetIrTest, HandlesMimic)
213 {
214     const char *inputMdpFile[] = {"integrator = mimic", "QMMM-grps = QMatoms"};
215     runTest(joinStrings(inputMdpFile, "\n"));
216 }
217
218 }  // namespace test
219 }  // namespace gmx