c4df99d9103c90ccf925548ce8dece6962ffd852
[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),
255     tolerances_(tolerances)
256 {
257 }
258
259 void TrajectoryComparison::operator()(const TrajectoryFrame& reference, const TrajectoryFrame& test) const
260 {
261     if (numComparedFrames_ >= matchSettings_.maxNumTrajectoryFrames)
262     {
263         // Nothing should be compared
264         return;
265     }
266     SCOPED_TRACE("Comparing trajectory reference frame " + reference.frameName()
267                  + " and test frame " + test.frameName());
268     EXPECT_EQ(reference.step(), test.step());
269     EXPECT_EQ(reference.time(), test.time());
270     compareBox(reference, test, matchSettings_, tolerances_.box);
271     compareCoordinates(reference, test, matchSettings_, tolerances_.coordinates);
272     compareVelocities(reference, test, matchSettings_, tolerances_.velocities);
273     compareForces(reference, test, matchSettings_, tolerances_.forces);
274     numComparedFrames_++;
275 }
276
277 /*! \brief Return whether the \c comparisonConditions and emptiness of
278  * the test frame means that a comparison should be attempted.
279  *
280  * This allows the framework to determine whether it is an error if a
281  * comparison cannot be made. */
282 static bool shouldDoComparison(ArrayRef<const RVec> values, ComparisonConditions comparisonConditions)
283 {
284     if (comparisonConditions == ComparisonConditions::NoComparison)
285     {
286         return false;
287     }
288     if (values.empty())
289     {
290         // Empty array ref is only acceptable if comparison is not required
291         if (comparisonConditions != ComparisonConditions::CompareIfTestFound
292             && comparisonConditions != ComparisonConditions::CompareIfBothFound)
293         {
294             ADD_FAILURE() << "Test frame lacked quantity for required comparison";
295         }
296         return false;
297     }
298     return true;
299 }
300
301 /*! \brief Compares the positions from \c frame to reference data
302  * according to the \c matchSettings and \c tolerance.
303  */
304 static void checkPositionsAgainstReference(const TrajectoryFrame&              frame,
305                                            const TrajectoryFrameMatchSettings& matchSettings,
306                                            const TrajectoryTolerances&         tolerance,
307                                            TestReferenceChecker*               checker)
308 {
309     SCOPED_TRACE("Comparing positions");
310     if (shouldDoComparison(frame.x(), matchSettings.coordinatesComparison))
311     {
312         ArrayRef<const RVec> positions{};
313         std::vector<RVec>    shiftedPositions{};
314         if (frame.hasBox() && (matchSettings.handlePbcIfPossible || matchSettings.requirePbcHandling))
315         {
316             shiftedPositions = putAtomsInBox(frame);
317             positions        = shiftedPositions;
318         }
319         else
320         {
321             positions = frame.x();
322         }
323
324         if (!frame.hasBox() && matchSettings.requirePbcHandling)
325         {
326             ADD_FAILURE() << "Comparing positions required PBC handling, "
327                              "but the test frame did not have a box";
328             return;
329         }
330         checker->setDefaultTolerance(tolerance.coordinates);
331         std::string id = frame.frameName() + " X";
332         checker->checkSequence(std::begin(positions), std::end(positions), id.c_str());
333     }
334 }
335
336 /*! \brief Compares the velocities from \c frame to reference data
337  * according to the \c matchSettings and \c tolerance.
338  */
339 static void checkVelocitiesAgainstReference(const TrajectoryFrame&              frame,
340                                             const TrajectoryFrameMatchSettings& matchSettings,
341                                             const TrajectoryTolerances&         tolerance,
342                                             TestReferenceChecker*               checker)
343 {
344     SCOPED_TRACE("Comparing velocities");
345     if (shouldDoComparison(frame.v(), matchSettings.velocitiesComparison))
346     {
347         checker->setDefaultTolerance(tolerance.velocities);
348         std::string id = frame.frameName() + " V";
349         checker->checkSequence(std::begin(frame.v()), std::end(frame.v()), id.c_str());
350     }
351 }
352
353 /*! \brief Compares the forces from \c frame to reference data
354  * according to the \c matchSettings and \c tolerance.
355  */
356 static void checkForcesAgainstReference(const TrajectoryFrame&              frame,
357                                         const TrajectoryFrameMatchSettings& matchSettings,
358                                         const TrajectoryTolerances&         tolerance,
359                                         TestReferenceChecker*               checker)
360 {
361     SCOPED_TRACE("Comparing forces");
362     if (shouldDoComparison(frame.f(), matchSettings.forcesComparison))
363     {
364         checker->setDefaultTolerance(tolerance.forces);
365         std::string id = frame.frameName() + " F";
366         checker->checkSequence(std::begin(frame.f()), std::end(frame.f()), id.c_str());
367     }
368 }
369
370 /*! \brief Compares the box from \c frame to reference data
371  * according to the \c matchSettings and \c tolerance.
372  */
373 static void checkBoxAgainstReference(const TrajectoryFrame&              frame,
374                                      const TrajectoryFrameMatchSettings& matchSettings,
375                                      const TrajectoryTolerances&         tolerance,
376                                      TestReferenceChecker*               checker)
377 {
378     SCOPED_TRACE("Comparing box");
379     if (!matchSettings.mustCompareBox)
380     {
381         return;
382     }
383     if (!frame.hasBox())
384     {
385         ADD_FAILURE() << "Comparing the box was required, "
386                          "but the test frame did not have one";
387         return;
388     }
389     checker->setDefaultTolerance(tolerance.box);
390     // Do the comparison
391     for (int d = 0; d < DIM; ++d)
392     {
393         for (int dd = 0; dd < DIM; ++dd)
394         {
395             checker->checkReal(
396                     frame.box()[d][dd],
397                     (frame.frameName() + " Box[" + toString(d) + "][" + toString(dd) + "]").c_str());
398         }
399     }
400 }
401
402 void TrajectoryComparison::operator()(const TrajectoryFrame& frame, TestReferenceChecker* checker) const
403 {
404     SCOPED_TRACE("Comparing trajectory frame " + frame.frameName() + " against reference data");
405     checkBoxAgainstReference(frame, matchSettings_, tolerances_, checker);
406     checkPositionsAgainstReference(frame, matchSettings_, tolerances_, checker);
407     checkVelocitiesAgainstReference(frame, matchSettings_, tolerances_, checker);
408     checkForcesAgainstReference(frame, matchSettings_, tolerances_, checker);
409 }
410
411 void checkTrajectoryAgainstReferenceData(const std::string&          trajectoryFilename,
412                                          const TrajectoryComparison& trajectoryComparison,
413                                          TestReferenceChecker*       checker)
414 {
415     checkTrajectoryAgainstReferenceData(
416             trajectoryFilename, trajectoryComparison, checker, MaxNumFrames::compareAllFrames());
417 }
418
419 void checkTrajectoryAgainstReferenceData(const std::string&          trajectoryFilename,
420                                          const TrajectoryComparison& trajectoryComparison,
421                                          TestReferenceChecker*       checker,
422                                          MaxNumFrames                maxNumFrames)
423 {
424     ;
425     TrajectoryFrameReader reader(trajectoryFilename);
426     unsigned int          counter = 0;
427     for (counter = 0; counter < maxNumFrames && reader.readNextFrame(); counter++)
428     {
429         auto frame = reader.frame();
430         trajectoryComparison(frame, checker);
431     }
432
433     if (counter == maxNumFrames && reader.readNextFrame())
434     {
435         // There would have been at least one more frame!
436         checker->disableUnusedEntriesCheck();
437     }
438 }
439
440 } // namespace test
441 } // namespace gmx