Apply clang-format-11
[alexxy/gromacs.git] / src / programs / mdrun / tests / trajectorycomparison.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2017,2018,2019,2020,2021, 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 Implemention of functions for comparing trajectories
38  * produced by mdrun.
39  *
40  * \author Mark Abraham <mark.j.abraham@gmail.com>
41  * \author Pascal Merz <pascal.merz@me.com>
42  * \ingroup module_mdrun_integration_tests
43  */
44 #include "gmxpre.h"
45
46 #include "trajectorycomparison.h"
47
48 #include <gmock/gmock.h>
49 #include <gtest/gtest-spi.h>
50
51 #include "gromacs/pbcutil/pbc.h"
52 #include "gromacs/trajectory/trajectoryframe.h"
53 #include "gromacs/utility/strconvert.h"
54
55 #include "testutils/refdata.h"
56 #include "testutils/testasserts.h"
57 #include "testutils/testmatchers.h"
58
59 #include "trajectoryreader.h"
60
61 namespace gmx
62 {
63 namespace test
64 {
65
66 using ::testing::Pointwise;
67
68 /*! \brief Compares the box from \c reference and \c test
69  * according to the \c matchSettings and \c tolerance.
70  *
71  * \todo This could be streamlined when we have a proper 3D matrix
72  * class and view. */
73 static void compareBox(const TrajectoryFrame&              reference,
74                        const TrajectoryFrame&              test,
75                        const TrajectoryFrameMatchSettings& matchSettings,
76                        const FloatingPointTolerance        tolerance)
77 {
78     if (!matchSettings.mustCompareBox)
79     {
80         return;
81     }
82     bool canCompareBox = true;
83     if (!reference.hasBox())
84     {
85         ADD_FAILURE() << "Comparing the box was required, "
86                          "but the reference frame did not have one";
87         canCompareBox = false;
88     }
89     if (!test.hasBox())
90     {
91         ADD_FAILURE() << "Comparing the box was required, "
92                          "but the test frame did not have one";
93         canCompareBox = false;
94     }
95     if (!canCompareBox)
96     {
97         return;
98     }
99
100     // Do the comparing.
101     for (int d = 0; d < DIM; ++d)
102     {
103         for (int dd = 0; dd < DIM; ++dd)
104         {
105             EXPECT_REAL_EQ_TOL(reference.box()[d][dd], test.box()[d][dd], tolerance);
106         }
107     }
108 }
109
110 /*! \brief Help put all atom coordinates in \c frame into its box.
111  *
112  * This can perhaps go away when frame->x is a container. */
113 static std::vector<RVec> putAtomsInBox(const TrajectoryFrame& frame)
114 {
115     std::vector<RVec> x(frame.x().begin(), frame.x().end());
116     matrix            box;
117     for (int d = 0; d < DIM; ++d)
118     {
119         for (int dd = 0; dd < DIM; ++dd)
120         {
121             box[d][dd] = frame.box()[d][dd];
122         }
123     }
124     // Note we don't need to compare bPBC because put_atoms_in_box
125     // implements a fallback if nothing specific was set in the
126     // trajectory frame.
127     put_atoms_in_box(frame.pbc(), box, x);
128     return x;
129 }
130
131 /*! \brief Return whether the \c comparisonConditions and emptiness of
132  * reference and test frames means that a comparison should be
133  * attempted.
134  *
135  * This allows the framework to determine whether it is an error if a
136  * comparison cannot be made. */
137 static bool shouldDoComparison(const ComparisonConditions comparisonConditions,
138                                const bool                 referenceIsEmpty,
139                                const bool                 testIsEmpty)
140 {
141     if (comparisonConditions == ComparisonConditions::NoComparison)
142     {
143         return false;
144     }
145
146     bool doComparison = true;
147     if (testIsEmpty)
148     {
149         if (comparisonConditions == ComparisonConditions::MustCompare
150             || comparisonConditions == ComparisonConditions::CompareIfBothFound
151             || (!referenceIsEmpty && comparisonConditions == ComparisonConditions::CompareIfReferenceFound))
152         {
153             ADD_FAILURE() << "Test frame lacked quantity for required comparison";
154         }
155         doComparison = false;
156     }
157     if (referenceIsEmpty)
158     {
159         if (comparisonConditions == ComparisonConditions::MustCompare
160             || comparisonConditions == ComparisonConditions::CompareIfBothFound
161             || (!testIsEmpty && comparisonConditions == ComparisonConditions::CompareIfTestFound))
162         {
163             ADD_FAILURE() << "Reference frame lacked quantity for required comparison";
164         }
165         doComparison = false;
166     }
167     return doComparison;
168 }
169
170 /*! \brief Compares the position coordinates from \c reference and \c test
171  * according to the \c matchSettings and \c tolerance. */
172 static void compareCoordinates(const TrajectoryFrame&              reference,
173                                const TrajectoryFrame&              test,
174                                const TrajectoryFrameMatchSettings& matchSettings,
175                                const FloatingPointTolerance        tolerance)
176 {
177     SCOPED_TRACE("Comparing coordinates");
178     if (!shouldDoComparison(matchSettings.coordinatesComparison, reference.x().empty(), test.x().empty()))
179     {
180         return;
181     }
182
183     bool canHandlePbc = true;
184     if (!reference.hasBox())
185     {
186         ADD_FAILURE() << "Comparing positions required PBC handling, "
187                          "but the reference frame did not have a box";
188         canHandlePbc = false;
189     }
190     if (!test.hasBox())
191     {
192         ADD_FAILURE() << "Comparing positions required PBC handling, "
193                          "but the test frame did not have a box";
194         canHandlePbc = false;
195     }
196
197     if (matchSettings.requirePbcHandling && !canHandlePbc)
198     {
199         ADD_FAILURE() << "Cannot compare positions for the above reason(s)";
200         return;
201     }
202
203     if ((matchSettings.handlePbcIfPossible || matchSettings.requirePbcHandling) && canHandlePbc)
204     {
205         EXPECT_THAT(putAtomsInBox(test), Pointwise(RVecEq(tolerance), putAtomsInBox(reference)));
206     }
207     else
208     {
209         EXPECT_THAT(test.x(), Pointwise(RVecEq(tolerance), reference.x()));
210     }
211 }
212
213 /*! \brief Compares the velocities from \c reference and \c test
214  * according to the \c matchSettings and \c tolerance. */
215 static void compareVelocities(const TrajectoryFrame&              reference,
216                               const TrajectoryFrame&              test,
217                               const TrajectoryFrameMatchSettings& matchSettings,
218                               const FloatingPointTolerance        tolerance)
219 {
220     SCOPED_TRACE("Comparing velocities");
221     if (!shouldDoComparison(matchSettings.velocitiesComparison, reference.v().empty(), test.v().empty()))
222     {
223         return;
224     }
225     EXPECT_THAT(test.v(), Pointwise(RVecEq(tolerance), reference.v()));
226 }
227
228 /*! \brief Compares the forces from \c reference and \c test
229  * according to the \c matchSettings and \c tolerance. */
230 static void compareForces(const TrajectoryFrame&              reference,
231                           const TrajectoryFrame&              test,
232                           const TrajectoryFrameMatchSettings& matchSettings,
233                           const FloatingPointTolerance        tolerance)
234 {
235     SCOPED_TRACE("Comparing forces");
236     if (!shouldDoComparison(matchSettings.forcesComparison, reference.f().empty(), test.f().empty()))
237     {
238         return;
239     }
240     EXPECT_THAT(test.f(), Pointwise(RVecEq(tolerance), reference.f()));
241 }
242
243
244 const TrajectoryTolerances TrajectoryComparison::s_defaultTrajectoryTolerances{
245     defaultRealTolerance(),                                               // box
246     relativeToleranceAsFloatingPoint(1.0, 1.0e-3),                        // positions
247     defaultRealTolerance(),                                               // velocities
248     relativeToleranceAsFloatingPoint(100.0, GMX_DOUBLE ? 1.0e-7 : 5.0e-5) // forces
249 };
250
251
252 TrajectoryComparison::TrajectoryComparison(const TrajectoryFrameMatchSettings& matchSettings,
253                                            const TrajectoryTolerances&         tolerances) :
254     matchSettings_(matchSettings), tolerances_(tolerances)
255 {
256 }
257
258 void TrajectoryComparison::operator()(const TrajectoryFrame& reference, const TrajectoryFrame& test) const
259 {
260     if (numComparedFrames_ >= matchSettings_.maxNumTrajectoryFrames)
261     {
262         // Nothing should be compared
263         return;
264     }
265     SCOPED_TRACE("Comparing trajectory reference frame " + reference.frameName()
266                  + " and test frame " + test.frameName());
267     EXPECT_EQ(reference.step(), test.step());
268     EXPECT_EQ(reference.time(), test.time());
269     compareBox(reference, test, matchSettings_, tolerances_.box);
270     compareCoordinates(reference, test, matchSettings_, tolerances_.coordinates);
271     compareVelocities(reference, test, matchSettings_, tolerances_.velocities);
272     compareForces(reference, test, matchSettings_, tolerances_.forces);
273     numComparedFrames_++;
274 }
275
276 /*! \brief Return whether the \c comparisonConditions and emptiness of
277  * the test frame means that a comparison should be attempted.
278  *
279  * This allows the framework to determine whether it is an error if a
280  * comparison cannot be made. */
281 static bool shouldDoComparison(ArrayRef<const RVec> values, ComparisonConditions comparisonConditions)
282 {
283     if (comparisonConditions == ComparisonConditions::NoComparison)
284     {
285         return false;
286     }
287     if (values.empty())
288     {
289         // Empty array ref is only acceptable if comparison is not required
290         if (comparisonConditions != ComparisonConditions::CompareIfTestFound
291             && comparisonConditions != ComparisonConditions::CompareIfBothFound)
292         {
293             ADD_FAILURE() << "Test frame lacked quantity for required comparison";
294         }
295         return false;
296     }
297     return true;
298 }
299
300 /*! \brief Compares the positions from \c frame to reference data
301  * according to the \c matchSettings and \c tolerance.
302  */
303 static void checkPositionsAgainstReference(const TrajectoryFrame&              frame,
304                                            const TrajectoryFrameMatchSettings& matchSettings,
305                                            const TrajectoryTolerances&         tolerance,
306                                            TestReferenceChecker*               checker)
307 {
308     SCOPED_TRACE("Comparing positions");
309     if (shouldDoComparison(frame.x(), matchSettings.coordinatesComparison))
310     {
311         ArrayRef<const RVec> positions{};
312         std::vector<RVec>    shiftedPositions{};
313         if (frame.hasBox() && (matchSettings.handlePbcIfPossible || matchSettings.requirePbcHandling))
314         {
315             shiftedPositions = putAtomsInBox(frame);
316             positions        = shiftedPositions;
317         }
318         else
319         {
320             positions = frame.x();
321         }
322
323         if (!frame.hasBox() && matchSettings.requirePbcHandling)
324         {
325             ADD_FAILURE() << "Comparing positions required PBC handling, "
326                              "but the test frame did not have a box";
327             return;
328         }
329         checker->setDefaultTolerance(tolerance.coordinates);
330         std::string id = frame.frameName() + " X";
331         checker->checkSequence(std::begin(positions), std::end(positions), id.c_str());
332     }
333 }
334
335 /*! \brief Compares the velocities from \c frame to reference data
336  * according to the \c matchSettings and \c tolerance.
337  */
338 static void checkVelocitiesAgainstReference(const TrajectoryFrame&              frame,
339                                             const TrajectoryFrameMatchSettings& matchSettings,
340                                             const TrajectoryTolerances&         tolerance,
341                                             TestReferenceChecker*               checker)
342 {
343     SCOPED_TRACE("Comparing velocities");
344     if (shouldDoComparison(frame.v(), matchSettings.velocitiesComparison))
345     {
346         checker->setDefaultTolerance(tolerance.velocities);
347         std::string id = frame.frameName() + " V";
348         checker->checkSequence(std::begin(frame.v()), std::end(frame.v()), id.c_str());
349     }
350 }
351
352 /*! \brief Compares the forces from \c frame to reference data
353  * according to the \c matchSettings and \c tolerance.
354  */
355 static void checkForcesAgainstReference(const TrajectoryFrame&              frame,
356                                         const TrajectoryFrameMatchSettings& matchSettings,
357                                         const TrajectoryTolerances&         tolerance,
358                                         TestReferenceChecker*               checker)
359 {
360     SCOPED_TRACE("Comparing forces");
361     if (shouldDoComparison(frame.f(), matchSettings.forcesComparison))
362     {
363         checker->setDefaultTolerance(tolerance.forces);
364         std::string id = frame.frameName() + " F";
365         checker->checkSequence(std::begin(frame.f()), std::end(frame.f()), id.c_str());
366     }
367 }
368
369 /*! \brief Compares the box from \c frame to reference data
370  * according to the \c matchSettings and \c tolerance.
371  */
372 static void checkBoxAgainstReference(const TrajectoryFrame&              frame,
373                                      const TrajectoryFrameMatchSettings& matchSettings,
374                                      const TrajectoryTolerances&         tolerance,
375                                      TestReferenceChecker*               checker)
376 {
377     SCOPED_TRACE("Comparing box");
378     if (!matchSettings.mustCompareBox)
379     {
380         return;
381     }
382     if (!frame.hasBox())
383     {
384         ADD_FAILURE() << "Comparing the box was required, "
385                          "but the test frame did not have one";
386         return;
387     }
388     checker->setDefaultTolerance(tolerance.box);
389     // Do the comparison
390     for (int d = 0; d < DIM; ++d)
391     {
392         for (int dd = 0; dd < DIM; ++dd)
393         {
394             checker->checkReal(
395                     frame.box()[d][dd],
396                     (frame.frameName() + " Box[" + toString(d) + "][" + toString(dd) + "]").c_str());
397         }
398     }
399 }
400
401 void TrajectoryComparison::operator()(const TrajectoryFrame& frame, TestReferenceChecker* checker) const
402 {
403     SCOPED_TRACE("Comparing trajectory frame " + frame.frameName() + " against reference data");
404     checkBoxAgainstReference(frame, matchSettings_, tolerances_, checker);
405     checkPositionsAgainstReference(frame, matchSettings_, tolerances_, checker);
406     checkVelocitiesAgainstReference(frame, matchSettings_, tolerances_, checker);
407     checkForcesAgainstReference(frame, matchSettings_, tolerances_, checker);
408 }
409
410 void checkTrajectoryAgainstReferenceData(const std::string&          trajectoryFilename,
411                                          const TrajectoryComparison& trajectoryComparison,
412                                          TestReferenceChecker*       checker)
413 {
414     checkTrajectoryAgainstReferenceData(
415             trajectoryFilename, trajectoryComparison, checker, MaxNumFrames::compareAllFrames());
416 }
417
418 void checkTrajectoryAgainstReferenceData(const std::string&          trajectoryFilename,
419                                          const TrajectoryComparison& trajectoryComparison,
420                                          TestReferenceChecker*       checker,
421                                          MaxNumFrames                maxNumFrames)
422 {
423     ;
424     TrajectoryFrameReader reader(trajectoryFilename);
425     unsigned int          counter = 0;
426     for (counter = 0; counter < maxNumFrames && reader.readNextFrame(); counter++)
427     {
428         auto frame = reader.frame();
429         trajectoryComparison(frame, checker);
430     }
431
432     if (counter == maxNumFrames && reader.readNextFrame())
433     {
434         // There would have been at least one more frame!
435         checker->disableUnusedEntriesCheck();
436     }
437 }
438
439 } // namespace test
440 } // namespace gmx