Update CI image to OneAPI 2021.1.1, add ICC tests.
[alexxy/gromacs.git] / src / programs / mdrun / tests / checkpoint.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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
36 /*! \internal \file
37  * \brief Tests for checkpoint writing sanity checks
38  *
39  * Checks that final checkpoint is equal to final trajectory output.
40  *
41  * \author Pascal Merz <pascal.merz@me.com>
42  * \ingroup module_mdrun_integration_tests
43  */
44 #include "gmxpre.h"
45
46 #include "config.h"
47
48 #include "gromacs/utility/strconvert.h"
49 #include "gromacs/utility/stringutil.h"
50
51 #include "testutils/simulationdatabase.h"
52
53 #include "moduletest.h"
54 #include "simulatorcomparison.h"
55 #include "trajectoryreader.h"
56
57 namespace gmx::test
58 {
59 namespace
60 {
61
62 class CheckpointCoordinatesSanityChecks :
63     public MdrunTestFixture,
64     public ::testing::WithParamInterface<std::tuple<std::string, std::string, std::string, std::string>>
65 {
66 public:
67     void runSimulation(MdpFieldValues     mdpFieldValues,
68                        int                numSteps,
69                        const std::string& trrFileName,
70                        const std::string& cptFileName)
71     {
72         mdpFieldValues["nsteps"] = toString(numSteps);
73         // Trajectories have the initial and the last frame
74         mdpFieldValues["nstxout"] = toString(numSteps);
75         mdpFieldValues["nstvout"] = toString(numSteps);
76         mdpFieldValues["nstfout"] = toString(0);
77
78         // Run grompp
79         runner_.useStringAsMdpFile(prepareMdpFileContents(mdpFieldValues));
80         runGrompp(&runner_);
81
82         // Do first mdrun
83         runner_.fullPrecisionTrajectoryFileName_ = trrFileName;
84         runMdrun(&runner_, { { "-cpo", cptFileName } });
85     }
86
87     static void compareCptAndTrr(const std::string&          trrFileName,
88                                  const std::string&          cptFileName,
89                                  const TrajectoryComparison& trajectoryComparison)
90     {
91         TrajectoryFrameReader trrReader(trrFileName);
92         TrajectoryFrameReader cptReader(cptFileName);
93         // Checkpoint has at least one frame
94         EXPECT_TRUE(cptReader.readNextFrame());
95         // Trajectory has at least two frames
96         EXPECT_TRUE(trrReader.readNextFrame());
97         EXPECT_NO_THROW(trrReader.frame());
98         EXPECT_TRUE(trrReader.readNextFrame());
99
100         // Now compare frames
101         trajectoryComparison(cptReader.frame(), trrReader.frame());
102
103         // Files had exactly 1 / 2 frames
104         EXPECT_FALSE(cptReader.readNextFrame());
105         EXPECT_FALSE(trrReader.readNextFrame());
106     }
107 };
108
109 TEST_P(CheckpointCoordinatesSanityChecks, WithinTolerances)
110 {
111     const auto& params              = GetParam();
112     const auto& simulationName      = std::get<0>(params);
113     const auto& integrator          = std::get<1>(params);
114     const auto& temperatureCoupling = std::get<2>(params);
115     const auto& pressureCoupling    = std::get<3>(params);
116
117     // Specify how trajectory frame matching must work.
118     TrajectoryFrameMatchSettings trajectoryMatchSettings{ true,
119                                                           true,
120                                                           true,
121                                                           ComparisonConditions::MustCompare,
122                                                           ComparisonConditions::MustCompare,
123                                                           ComparisonConditions::NoComparison,
124                                                           MaxNumFrames::compareAllFrames() };
125     if (integrator == "md-vv")
126     {
127         // When using md-vv and modular simulator, the velocities are expected to be off by
128         // 1/2 dt between checkpoint (top of the loop) and trajectory (full time step state)
129         trajectoryMatchSettings.velocitiesComparison = ComparisonConditions::NoComparison;
130     }
131     const TrajectoryTolerances trajectoryTolerances{ defaultRealTolerance(), defaultRealTolerance(),
132                                                      defaultRealTolerance(), defaultRealTolerance() };
133
134     const auto mdpFieldValues =
135             prepareMdpFieldValues(simulationName, integrator, temperatureCoupling, pressureCoupling);
136     runner_.useTopGroAndNdxFromDatabase(simulationName);
137     // Set file names
138     const auto cptFileName = fileManager_.getTemporaryFilePath(".cpt");
139     const auto trrFileName = fileManager_.getTemporaryFilePath(".trr");
140
141     SCOPED_TRACE(formatString(
142             "Checking the sanity of the checkpointed coordinates using system '%s' "
143             "with integrator '%s', '%s' temperature coupling, and '%s' pressure coupling ",
144             simulationName.c_str(), integrator.c_str(), temperatureCoupling.c_str(),
145             pressureCoupling.c_str()));
146
147     SCOPED_TRACE("End of trajectory sanity");
148     // Running a few steps - we expect the checkpoint to be equal
149     // to the final configuration
150     runSimulation(mdpFieldValues, 16, trrFileName, cptFileName);
151     compareCptAndTrr(trrFileName, cptFileName, { trajectoryMatchSettings, trajectoryTolerances });
152 }
153
154 #if !GMX_GPU_OPENCL
155 INSTANTIATE_TEST_CASE_P(CheckpointCoordinatesAreSane,
156                         CheckpointCoordinatesSanityChecks,
157                         ::testing::Combine(::testing::Values("spc2"),
158                                            ::testing::Values("md", "md-vv"),
159                                            ::testing::Values("no"),
160                                            ::testing::Values("no")));
161 #else
162 INSTANTIATE_TEST_CASE_P(DISABLED_CheckpointCoordinatesAreSane,
163                         CheckpointCoordinatesSanityChecks,
164                         ::testing::Combine(::testing::Values("spc2"),
165                                            ::testing::Values("md", "md-vv"),
166                                            ::testing::Values("no"),
167                                            ::testing::Values("no")));
168 #endif
169
170 } // namespace
171 } // namespace gmx::test