Merge release-4-6 into master
[alexxy/gromacs.git] / src / programs / mdrun / tests / moduletest.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2013, 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  * Implements classes in moduletest.h.
38  *
39  * \author Mark Abraham <mark.j.abraham@gmail.com>
40  * \ingroup module_mdrun
41  */
42 #include "moduletest.h"
43
44 #ifdef HAVE_CONFIG_H
45 #  include <config.h>
46 #endif
47
48 #include "testutils/integrationtests.h"
49 #include "testutils/testoptions.h"
50 #include "testutils/cmdlinetest.h"
51 #include "gromacs/options/options.h"
52 #include "gromacs/options/basicoptions.h"
53 #include "gromacs/utility/file.h"
54 #include "gromacs/legacyheaders/network.h"
55 #include "gromacs/legacyheaders/types/simple.h"
56 #include "programs/mdrun/mdrun_main.h"
57 #include "programs/gmx/legacycmainfunctions.h"
58
59 namespace gmx
60 {
61 namespace test
62 {
63
64 /********************************************************************
65  * MdrunTestFixture
66  */
67
68
69 /*! /brief Number of tMPI threads for child mdrun call */
70 static int gmx_unused numThreads = 1;
71 /*! /brief Number of OpenMP threads for child mdrun call */
72 static int gmx_unused numOpenMPThreads = 1;
73
74 namespace
75 {
76
77 GMX_TEST_OPTIONS(MdrunTestOptions, options)
78 {
79     GMX_UNUSED_VALUE(options);
80 #ifdef GMX_THREAD_MPI
81     options->addOption(IntegerOption("nt").store(&numThreads)
82                            .description("Number of thread-MPI threads/ranks for child mdrun call"));
83 #endif
84 #ifdef GMX_OPENMP
85     options->addOption(IntegerOption("nt_omp").store(&numOpenMPThreads)
86                            .description("Number of OpenMP threads for child mdrun call"));
87 #endif
88 }
89
90 }
91
92 MdrunTestFixture::MdrunTestFixture() :
93     topFileName(),
94     groFileName(),
95     trrFileName(),
96     mdpInputFileName(fileManager_.getTemporaryFilePath("input.mdp")),
97     mdpOutputFileName(fileManager_.getTemporaryFilePath("output.mdp")),
98     tprFileName(fileManager_.getTemporaryFilePath(".tpr")),
99     logFileName(fileManager_.getTemporaryFilePath(".log")),
100     edrFileName(fileManager_.getTemporaryFilePath(".edr"))
101 {
102 #ifdef GMX_LIB_MPI
103     GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
104 #endif
105 }
106
107 MdrunTestFixture::~MdrunTestFixture()
108 {
109 }
110
111 // TODO The combination of defaulting to Verlet cut-off scheme, NVE,
112 // and verlet-buffer-tolerance = -1 gives a grompp error. If we keep
113 // things that way, this function should be renamed. For now,
114 // force the use of the group scheme.
115 void
116 MdrunTestFixture::useEmptyMdpFile()
117 {
118     useStringAsMdpFile("cutoff-scheme = Group\n");
119 }
120
121 void
122 MdrunTestFixture::useStringAsMdpFile(const char *mdpString)
123 {
124     gmx::File::writeFileFromString(mdpInputFileName, mdpString);
125 }
126
127 void
128 MdrunTestFixture::useTopAndGroFromDatabase(const char *name)
129 {
130     topFileName = fileManager_.getInputFilePath((std::string(name) + ".top").c_str());
131     groFileName = fileManager_.getInputFilePath((std::string(name) + ".gro").c_str());
132 }
133
134 int
135 MdrunTestFixture::callGrompp()
136 {
137 #ifdef GMX_LIB_MPI
138     // When compiled with external MPI, only call one instance of the grompp function
139     if (0 != gmx_node_rank())
140     {
141         return 0;
142     }
143 #endif
144
145     CommandLine caller;
146     caller.append("grompp");
147     caller.addOption("-f", mdpInputFileName);
148     caller.addOption("-p", topFileName);
149     caller.addOption("-c", groFileName);
150
151     caller.addOption("-po", mdpOutputFileName);
152     caller.addOption("-o", tprFileName);
153
154     return gmx_grompp(caller.argc(), caller.argv());
155 }
156
157 int
158 MdrunTestFixture::callMdrun()
159 {
160     CommandLine caller;
161     caller.append("mdrun");
162
163     caller.addOption("-s", tprFileName);
164     caller.addOption("-rerun", rerunFileName);
165
166     caller.addOption("-g", logFileName);
167     caller.addOption("-e", edrFileName);
168     caller.addOption("-o", trrFileName);
169     caller.addOption("-x", xtcFileName);
170
171 #ifdef GMX_THREAD_MPI
172     caller.addOption("-nt", numThreads);
173 #endif
174 #ifdef GMX_OPENMP
175     caller.addOption("-ntomp", numOpenMPThreads);
176 #endif
177
178     return gmx_mdrun(caller.argc(), caller.argv());
179 }
180
181 } // namespace test
182 } // namespace gmx