Updates to nblib test infra and tests
[alexxy/gromacs.git] / api / nblib / tests / simstate.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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 /*! \internal \file
36  * \brief
37  * This implements SimulationState tests
38  *
39  * \author Victor Holanda <victor.holanda@cscs.ch>
40  * \author Joe Jordan <ejjordan@kth.se>
41  * \author Prashanth Kanduri <kanduri@cscs.ch>
42  * \author Sebastian Keller <keller@cscs.ch>
43  */
44 #include <vector>
45
46 #include "nblib/box.h"
47 #include "nblib/exception.h"
48 #include "nblib/simulationstate.h"
49 #include "nblib/simulationstateimpl.h"
50 #include "nblib/tests/testhelpers.h"
51 #include "nblib/tests/testsystems.h"
52 #include "nblib/topology.h"
53
54 #include "testutils/testasserts.h"
55
56 namespace nblib
57 {
58 namespace test
59 {
60 namespace
61 {
62
63 //! Utility function to compare 2 std::vectors of gmx::RVec used to compare cartesians
64 void compareValues(const std::vector<Vec3>& ref, const std::vector<Vec3>& test)
65 {
66     for (size_t i = 0; i < ref.size(); i++)
67     {
68         for (int j = 0; j < dimSize; j++)
69         {
70             EXPECT_EQ(ref[i][j], test.at(i)[j]);
71         }
72     }
73 }
74
75 TEST(NBlibTest, CanConstructSimulationState)
76 {
77     ArgonSimulationStateBuilder argonSimulationStateBuilder(fftypes::GROMOS43A1);
78     EXPECT_NO_THROW(argonSimulationStateBuilder.setupSimulationState());
79 }
80
81 TEST(NBlibTest, SimulationStateThrowsCoordinateNAN)
82 {
83     ArgonSimulationStateBuilder argonSimulationStateBuilder(fftypes::GROMOS43A1);
84     argonSimulationStateBuilder.setCoordinate(2, 0, NAN);
85     EXPECT_THROW(argonSimulationStateBuilder.setupSimulationState(), InputException);
86 }
87
88 TEST(NBlibTest, SimulationStateThrowsCoordinateINF)
89 {
90     ArgonSimulationStateBuilder argonSimulationStateBuilder(fftypes::GROMOS43A1);
91     argonSimulationStateBuilder.setCoordinate(2, 0, INFINITY);
92     EXPECT_THROW(argonSimulationStateBuilder.setupSimulationState(), InputException);
93 }
94
95 TEST(NBlibTest, SimulationStateThrowsVelocityNAN)
96 {
97     ArgonSimulationStateBuilder argonSimulationStateBuilder(fftypes::GROMOS43A1);
98     argonSimulationStateBuilder.setVelocity(2, 0, NAN);
99     EXPECT_THROW(argonSimulationStateBuilder.setupSimulationState(), InputException);
100 }
101
102 TEST(NBlibTest, SimulationStateThrowsVelocityINF)
103 {
104     ArgonSimulationStateBuilder argonSimulationStateBuilder(fftypes::GROMOS43A1);
105     argonSimulationStateBuilder.setVelocity(2, 0, INFINITY);
106     EXPECT_THROW(argonSimulationStateBuilder.setupSimulationState(), InputException);
107 }
108
109 TEST(NBlibTest, SimulationStateCanMove)
110 {
111     ArgonSimulationStateBuilder argonSimulationStateBuilder(fftypes::GROMOS43A1);
112     SimulationState             simState = argonSimulationStateBuilder.setupSimulationState();
113     EXPECT_NO_THROW(SimulationState movedSimState = std::move(simState));
114 }
115
116 TEST(NBlibTest, SimulationStateCanAssign)
117 {
118     ArgonSimulationStateBuilder argonSimulationStateBuilder(fftypes::GROMOS43A1);
119     SimulationState             simState = argonSimulationStateBuilder.setupSimulationState();
120     EXPECT_NO_THROW(const SimulationState& gmx_unused AssignedSimState = simState);
121 }
122
123 TEST(NBlibTest, SimulationStateHasBox)
124 {
125     ArgonSimulationStateBuilder argonSimulationStateBuilder(fftypes::GROMOS43A1);
126     SimulationState             simState = argonSimulationStateBuilder.setupSimulationState();
127     const Box&                  testBox  = simState.box();
128     const Box&                  refBox   = argonSimulationStateBuilder.box();
129     // GTEST does not like the comparison operator in a different namespace
130     const bool compare = (refBox == testBox);
131     EXPECT_TRUE(compare);
132 }
133
134 TEST(NBlibTest, SimulationStateHasCorrectCoordinates)
135 {
136     ArgonSimulationStateBuilder argonSimulationStateBuilder(fftypes::GROMOS43A1);
137     SimulationState             simState = argonSimulationStateBuilder.setupSimulationState();
138     std::vector<Vec3>           test     = simState.coordinates();
139     std::vector<Vec3>           ref      = argonSimulationStateBuilder.coordinates();
140     compareValues(ref, test);
141 }
142
143 TEST(NBlibTest, SimulationStateHasCorrectVelocities)
144 {
145     ArgonSimulationStateBuilder argonSimulationStateBuilder(fftypes::GROMOS43A1);
146     SimulationState             simState = argonSimulationStateBuilder.setupSimulationState();
147     std::vector<Vec3>           test     = simState.velocities();
148     std::vector<Vec3>           ref      = argonSimulationStateBuilder.velocities();
149     compareValues(ref, test);
150 }
151
152 } // namespace
153 } // namespace test
154 } // namespace nblib