Apply clang-format-11
[alexxy/gromacs.git] / src / programs / mdrun / tests / mdruncomparison.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2018,2019,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 /*! \libinternal
36  *
37  * \brief Functionality for testing whether calls to mdrun produce the
38  * same energy and force quantities when they should do so.
39  */
40 #ifndef GMX_MDRUN_TESTS_MDRUNCOMPARISON_H
41 #define GMX_MDRUN_TESTS_MDRUNCOMPARISON_H
42
43 #include <functional>
44 #include <memory>
45
46 #include <gtest/gtest.h>
47
48 namespace gmx
49 {
50
51 namespace test
52 {
53
54 /*! \internal
55  * \brief Manages returning a pair of frames from two
56  * equivalent simulations that are meaningful to compare.
57  *
58  * \todo This is largely duplicated by ContinuationFrameManager. These
59  * could be refactored into components that compare iterators to
60  * frames.
61  *
62  * \tparam FrameReader  Has readNextFrame() and frame() methods
63  *                      useful for returning successive Frame objects */
64 template<class FrameReader>
65 class FramePairManager
66 {
67 public:
68     //! Convenience typedef
69     typedef std::unique_ptr<FrameReader> FrameReaderPtr;
70     //! Constructor
71     FramePairManager(FrameReaderPtr first, FrameReaderPtr second) :
72         first_(std::move(first)), second_(std::move(second))
73     {
74     }
75
76 private:
77     /*! \brief Probe for a pair of valid frames, and return true if both are found.
78      *
79      * Give a test failure if exactly one frame is found, because
80      * that file is longer than the other one, and this is not
81      * expected behaviour. */
82     bool shouldContinueComparing()
83     {
84         if (first_->readNextFrame())
85         {
86             if (second_->readNextFrame())
87             {
88                 // Two valid next frames exist, so we should continue comparing.
89                 return true;
90             }
91             else
92             {
93                 ADD_FAILURE() << "first file had at least one more frame than second file";
94             }
95         }
96         else
97         {
98             if (second_->readNextFrame())
99             {
100                 ADD_FAILURE() << "second file had at least one more frame than first file";
101             }
102             else
103             {
104                 // Both files ran out of frames at the same time, which is the expected behaviour.
105             }
106         }
107         // At least one file is out of frames, so should not continue comparing.
108         return false;
109     }
110
111 public:
112     /*! \brief Compare all possible pairs of frames using \c compareTwoFrames.
113      *
114      * \tparam Frame  The type of frame used in the comparison (returned
115      *                by FrameReader and used by compareTwoFrames). */
116     template<class Frame>
117     void compareAllFramePairs(std::function<void(const Frame&, const Frame&)> compareTwoFrames)
118     {
119         while (shouldContinueComparing())
120         {
121             Frame firstFrame  = first_->frame();
122             Frame secondFrame = second_->frame();
123             SCOPED_TRACE("Comparing frames from two runs '" + firstFrame.frameName() + "' and '"
124                          + secondFrame.frameName() + "'");
125             compareTwoFrames(firstFrame, secondFrame);
126         }
127     }
128
129 private:
130     FrameReaderPtr first_;
131     FrameReaderPtr second_;
132 };
133
134 } // namespace test
135 } // namespace gmx
136
137 #endif