Apply clang-format to source tree
[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, 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)),
73         second_(std::move(second))
74     {
75     }
76
77 private:
78     /*! \brief Probe for a pair of valid frames, and return true if both are found.
79      *
80      * Give a test failure if exactly one frame is found, because
81      * that file is longer than the other one, and this is not
82      * expected behaviour. */
83     bool shouldContinueComparing()
84     {
85         if (first_->readNextFrame())
86         {
87             if (second_->readNextFrame())
88             {
89                 // Two valid next frames exist, so we should continue comparing.
90                 return true;
91             }
92             else
93             {
94                 ADD_FAILURE() << "first file had at least one more frame than second file";
95             }
96         }
97         else
98         {
99             if (second_->readNextFrame())
100             {
101                 ADD_FAILURE() << "second file had at least one more frame than first file";
102             }
103             else
104             {
105                 // Both files ran out of frames at the same time, which is the expected behaviour.
106             }
107         }
108         // At least one file is out of frames, so should not continue comparing.
109         return false;
110     }
111
112 public:
113     /*! \brief Compare all possible pairs of frames using \c compareTwoFrames.
114      *
115      * \tparam Frame  The type of frame used in the comparison (returned
116      *                by FrameReader and used by compareTwoFrames). */
117     template<class Frame>
118     void compareAllFramePairs(std::function<void(const Frame&, const Frame&)> compareTwoFrames)
119     {
120         while (shouldContinueComparing())
121         {
122             Frame firstFrame  = first_->frame();
123             Frame secondFrame = second_->frame();
124             SCOPED_TRACE("Comparing frames from two runs '" + firstFrame.frameName() + "' and '"
125                          + secondFrame.frameName() + "'");
126             compareTwoFrames(firstFrame, secondFrame);
127         }
128     }
129
130 private:
131     FrameReaderPtr first_;
132     FrameReaderPtr second_;
133 };
134
135 } // namespace test
136 } // namespace gmx
137
138 #endif