4e18b667f8f470bff7bbec7aaac50aa0bc9de5b8
[alexxy/gromacs.git] / src / programs / mdrun / tests / tabulated_bonded_interactions.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016, 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
36 /*! \internal \file
37  * \brief
38  * Tests for tabulated bonded interactions
39  *
40  * \author Mark Abraham <mark.j.abraham@gmail.com>
41  * \ingroup module_mdrun_integration_tests
42  */
43 #include "gmxpre.h"
44
45 #include <gtest/gtest.h>
46
47 #include "gromacs/utility/stringutil.h"
48 #include "gromacs/utility/textwriter.h"
49
50 #include "testutils/cmdlinetest.h"
51
52 #include "moduletest.h"
53
54 namespace gmx
55 {
56 namespace test
57 {
58
59 //! Format string for building a configurable .top file
60 const char *g_butaneTopFileFormatString = "\
61 [ defaults ]\n\
62 ; nbfunc        comb-rule       gen-pairs       fudgeLJ fudgeQQ\n\
63   1             1               no              1.0     1.0\n\
64 \n\
65 [ atomtypes ]\n\
66 ;name        mass      charge   ptype            c6           c12\n\
67   CH2    14.02700       0.000       A   0.90975E-02   0.35333E-04\n\
68   CH3    15.03500       0.000       A   0.88765E-02   0.26150E-04\n\
69 \n\
70 [ moleculetype ]\n\
71 ;             name    nrexcl\n\
72             butane   3\n\
73 \n\
74 [ atoms ]\n\
75 ;   nr    type   resnr  residu    atom    cgnr\n\
76      1     CH3       1     BUT      C1       1\n\
77      2     CH2       1     BUT      C2       2\n\
78      3     CH2       1     BUT      C3       3\n\
79      4     CH3       1     BUT      C4       4\n\
80 \n\
81 %s\n\
82 \n\
83 [ system ]\n\
84 ; The name of the system to be simulated\n\
85 A single butane\n\
86 \n\
87 [ molecules ]\n\
88 ; Molname             Number\n\
89 Butane                   1\n\
90 ";
91
92 //! Test fixture for bonded interactions
93 class BondedInteractionsTest : public gmx::test::MdrunTestFixture
94 {
95     public:
96         //! Execute the trajectory writing test
97         void setupGrompp(const char *interaction)
98         {
99             runner_.topFileName_ = fileManager_.getTemporaryFilePath("butane1.top");
100             TextWriter::writeFileFromString(runner_.topFileName_, formatString(g_butaneTopFileFormatString, interaction));
101             runner_.groFileName_ = fileManager_.getInputFilePath("butane1.gro");
102             runner_.ndxFileName_ = fileManager_.getInputFilePath("butane1.ndx");
103             /* TODO Now that Verlet is the default, change the implementation
104                of useEmptyMdpFile() to do that. */
105             runner_.useStringAsMdpFile("");
106         }
107         //! Prepare an mdrun caller
108         CommandLine setupMdrun()
109         {
110             CommandLine rerunCaller;
111             rerunCaller.append("mdrun");
112             rerunCaller.addOption("-rerun", runner_.groFileName_);
113             return rerunCaller;
114         }
115         //! Check the output of mdrun
116         void checkMdrun()
117         {
118             // TODO verifying some energies and forces would be good,
119             // once other code in gerrit is reviewed
120         }
121 };
122
123 // This test ensures that a normal non-tabulated bond interaction works
124 TEST_F(BondedInteractionsTest, NormalBondWorks)
125 {
126     setupGrompp("[ bonds ]\n\
127 ;  ai    aj funct           c0           c1\n\
128     1     2     1 1.530000e-01 3.347000e+05");
129     EXPECT_EQ(0, runner_.callGrompp());
130
131     test::CommandLine rerunCaller = setupMdrun();
132     ASSERT_EQ(0, runner_.callMdrun(rerunCaller));
133     checkMdrun();
134 }
135
136 // This test ensures that a normal abulated bond interaction works
137 TEST_F(BondedInteractionsTest, TabulatedBondWorks)
138 {
139     setupGrompp("[ bonds ]\n\
140 ;  ai    aj funct  n     k\n\
141     1     2     8  0  1000");
142     EXPECT_EQ(0, runner_.callGrompp());
143
144     test::CommandLine rerunCaller   = setupMdrun();
145     std::string       tableFileName = fileManager_.getInputFilePath("butane_b0.xvg");
146     rerunCaller.addOption("-tableb", tableFileName);
147     ASSERT_EQ(0, runner_.callMdrun(rerunCaller));
148     checkMdrun();
149 }
150
151 // This test ensures that a normal non-tabulated angle interaction works
152 TEST_F(BondedInteractionsTest, NormalAngleWorks)
153 {
154     setupGrompp("[ angles ]\n\
155 ;  ai    aj    ak funct           c0           c1\n\
156     1     2     3     1 1.110000e+02 4.602000e+02");
157     EXPECT_EQ(0, runner_.callGrompp());
158
159     test::CommandLine rerunCaller = setupMdrun();
160     ASSERT_EQ(0, runner_.callMdrun(rerunCaller));
161     checkMdrun();
162 }
163
164 // This test ensures that a tabulated angle interaction works
165 TEST_F(BondedInteractionsTest, TabulatedAngleWorks)
166 {
167     setupGrompp("[ angles ]\n\
168 ;  ai    aj    ak funct  n     k\n\
169     1     2     3     8  0  1000");
170     EXPECT_EQ(0, runner_.callGrompp());
171
172     test::CommandLine rerunCaller   = setupMdrun();
173     std::string       tableFileName = fileManager_.getInputFilePath("butane_a0.xvg");
174     rerunCaller.addOption("-tableb", tableFileName);
175     ASSERT_EQ(0, runner_.callMdrun(rerunCaller));
176     checkMdrun();
177 }
178
179 // This test ensures that a normal non-tabulated dihedral interaction works
180 TEST_F(BondedInteractionsTest, NormalDihedralWorks)
181 {
182     setupGrompp("[ dihedrals ]\n \
183 ;  ai    aj    ak    al funct     c0     c1     c2      c3     c4      c5\n\
184     1     2     3     4     3 9.2789 12.156 -13.12 -3.0597  26.24 -31.495");
185     EXPECT_EQ(0, runner_.callGrompp());
186
187     test::CommandLine rerunCaller = setupMdrun();
188     ASSERT_EQ(0, runner_.callMdrun(rerunCaller));
189     checkMdrun();
190 }
191
192 // This test ensures that a tabulated dihedral interaction works
193 TEST_F(BondedInteractionsTest, TabulatedDihedralWorks)
194 {
195     setupGrompp("[ dihedrals ]\n\
196 ;  ai    aj    ak    al funct   n     k\n\
197     1     2     3     4     8   0  1000");
198     EXPECT_EQ(0, runner_.callGrompp());
199
200     test::CommandLine rerunCaller   = setupMdrun();
201     std::string       tableFileName = fileManager_.getInputFilePath("butane_d0.xvg");
202     rerunCaller.addOption("-tableb", tableFileName);
203     ASSERT_EQ(0, runner_.callMdrun(rerunCaller));
204     checkMdrun();
205 }
206
207 } // namespace
208
209 } // namespace