d84fb3328be1e646dec5ecfd1c08cf80ddae940b
[alexxy/gromacs.git] / src / gromacs / tools / tests / report-methods.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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  * Tests for functionality of the "report" tool to write system information.
38  *
39  * \author
40  */
41 #include "gmxpre.h"
42
43 #include "gromacs/tools/report-methods.h"
44
45 #include "gromacs/fileio/tpxio.h"
46 #include "gromacs/gmxpreprocess/grompp.h"
47 #include "gromacs/mdtypes/state.h"
48 #include "gromacs/utility/exceptions.h"
49 #include "gromacs/utility/stringstream.h"
50 #include "gromacs/utility/textwriter.h"
51
52 #include "testutils/cmdlinetest.h"
53 #include "testutils/refdata.h"
54 #include "testutils/testfilemanager.h"
55 #include "testutils/textblockmatchers.h"
56
57 namespace gmx
58 {
59
60 namespace test
61 {
62
63 namespace
64 {
65
66 /*! \brief
67  * Generates a tpr for the test.
68  *
69  * Generates the tpr from a sample pdb file using grompp,and returns the path
70  * to the file as std::string for reading it in later.
71  *
72  * \param[in] fileManager Filemanager to keep track of the input file.
73  * \param[in] filename Basename of the input and output files.
74  */
75 std::string generateTprInput(TestFileManager *fileManager, const std::string &filename)
76 {
77 // generate temporary tpr file from test system
78     const std::string mdpInputFileName = fileManager->getTemporaryFilePath(filename + ".mdp");
79     TextWriter::writeFileFromString(mdpInputFileName, "");
80     std::string       tprName = fileManager->getTemporaryFilePath(filename + ".tpr");
81     {
82         CommandLine caller;
83         caller.append("grompp");
84         caller.addOption("-f", mdpInputFileName);
85         caller.addOption("-p", TestFileManager::getInputFilePath(filename + ".top"));
86         caller.addOption("-c", TestFileManager::getInputFilePath(filename + ".pdb"));
87         caller.addOption("-o", tprName);
88         EXPECT_EQ(0, gmx_grompp(caller.argc(), caller.argv()));
89     }
90     return tprName;
91 }
92
93 /*! \brief
94  * Generates and reads a tpr for the test.
95  *
96  * Generates a tpr from a sample pdb file using grompp, and reads it in to
97  * have access to the system information for print out.
98  *
99  * \param[in] filename Basename of the input and output files.
100  * \param[in] mtop Pointer to topology datastructure to populate.
101  * \param[in] ir Pointer to inputrec to populate.
102  */
103 void generateAndReadTprInput(const std::string &filename, gmx_mtop_t *mtop, t_inputrec *ir)
104 {
105     TestFileManager   fileManager;
106     std::string       tprName = generateTprInput(&fileManager, filename);
107 // read tpr into variables needed for output
108     {
109         t_state     state;
110         read_tpx_state(tprName.c_str(), ir, &state, mtop);
111     }
112 }
113
114 TEST(ReportMethodsTest, WritesCorrectHeadersFormated)
115 {
116     gmx::StringOutputStream stream;
117     gmx::TextWriter         test(&stream);
118     writeHeader(&test, "Test", "Test", true);
119
120     std::string referenceString = "\\Test{Test}\n";
121
122     EXPECT_EQ(stream.toString(), referenceString);
123 }
124 TEST(ReportMethodsTest, WritesCorrectHeadersUnformatted)
125 {
126     gmx::StringOutputStream stream;
127     gmx::TextWriter         test(&stream);
128     writeHeader(&test, "Test", "Test", false);
129
130     std::string referenceString = "Test: Test\n";
131
132     EXPECT_EQ(stream.toString(), referenceString);
133 }
134
135 TEST(ReportMethodsTest, WritesCorrectInformation)
136 {
137     gmx_mtop_t top;
138     t_inputrec ir;
139     EXPECT_NO_THROW(generateAndReadTprInput("lysozyme", &top, &ir));
140
141     // Test both formatted and unformatted output in the same test
142     {
143         gmx::StringOutputStream stream;
144         gmx::TextWriter         test(&stream);
145
146         writeSystemInformation(&test, top, true);
147
148         std::string referenceString = "\\subsection{Simulation system}\n"
149             "A system of 1 molecules (156 atoms) was simulated.\n";
150
151         EXPECT_EQ(stream.toString(), referenceString);
152     }
153
154     {
155         gmx::StringOutputStream stream;
156         gmx::TextWriter         test(&stream);
157
158         writeSystemInformation(&test, top, false);
159
160         std::string referenceString = "subsection: Simulation system\n"
161             "A system of 1 molecules (156 atoms) was simulated.\n";
162
163         EXPECT_EQ(stream.toString(), referenceString);
164     }
165
166     // Test for correct parameter information as well
167     {
168         gmx::StringOutputStream stream;
169         gmx::TextWriter         test(&stream);
170
171         writeParameterInformation(&test, ir, true);
172
173         std::string referenceString = "\\subsection{Simulation settings}\n"
174             "A total of 0 ns were simulated with a time step of 1 fs.\n"
175             "Neighbor searching was performed every 10 steps.\n"
176             "The Cut-off algorithm was used for electrostatic interactions.\n"
177             "with a cut-off of 1 nm.\nA single cut-off of 1.1 nm was used "
178             "for Van der Waals interactions.\n";
179
180         EXPECT_EQ(stream.toString(), referenceString);
181     }
182 }
183
184 TEST(ReportMethodsTest, ToolEndToEndTest)
185 {
186     TestFileManager   fileManager;
187     std::string       tprName   = generateTprInput(&fileManager, "lysozyme");
188     const char *const command[] = {
189         "report-methods", "-s", tprName.c_str()
190     };
191     CommandLine       cmdline(command);
192     EXPECT_EQ(0, gmx::test::CommandLineTestHelper::runModuleFactory(
193                       &gmx::ReportMethodsInfo::create, &cmdline));
194
195 }
196
197 } // namespace
198
199 } // namespace test
200
201 } // namespace gmx