28533b42f2ea299b9e066161bc4b958bd4f7775a
[alexxy/gromacs.git] / src / gromacs / math / tests / coordinatetransformation.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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 /*! \internal \file
36  * \brief
37  * Tests structure similarity measures rmsd and size-independent rho factor.
38  *
39  * \author Christian Blau <cblau@gwdg.de>
40  * \ingroup module_math
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/math/coordinatetransformation.h"
45
46 #include <array>
47
48 #include <gmock/gmock.h>
49 #include <gtest/gtest.h>
50
51 #include "testutils/testasserts.h"
52 #include "testutils/testmatchers.h"
53
54 namespace gmx
55 {
56
57 namespace test
58 {
59
60 using ::testing::Pointwise;
61 class TranslateAndScaleTest : public ::testing::Test
62 {
63 protected:
64     RVec              identityScale_       = { 1, 1, 1 };
65     RVec              identityTranslation_ = { 0, 0, 0 };
66     std::vector<RVec> testVectors_         = { { 0, 0, 0 },
67                                        { 1, 0, 0 },
68                                        { 0, -1, -1 },
69                                        { 1e10, 1e1, 1e-2 },
70                                        { 3, -6, 2.5 } };
71 };
72
73 TEST_F(TranslateAndScaleTest, identityTransformation)
74 {
75     TranslateAndScale translateAndScale(identityScale_, identityTranslation_);
76     auto              toBeTransformed = testVectors_;
77     translateAndScale(toBeTransformed);
78     EXPECT_THAT(testVectors_, Pointwise(RVecEq(defaultFloatTolerance()), toBeTransformed));
79 }
80
81 TEST_F(TranslateAndScaleTest, translationWithIdentityScaling)
82 {
83     TranslateAndScale translateAndScale(identityScale_, { 1, -1, 2 });
84     translateAndScale(testVectors_);
85     std::vector<RVec> expected = {
86         { 1, -1, 2 }, { 2, -1, 2 }, { 1, -2, 1 }, { 1e10 + 1, 1e1 - 1, 1e-2 + 2 }, { 4, -7, 4.5 }
87     };
88     EXPECT_THAT(expected, Pointwise(RVecEq(defaultFloatTolerance()), testVectors_));
89 }
90
91 TEST_F(TranslateAndScaleTest, scalingWithZeroTranslation)
92 {
93     TranslateAndScale translateAndScale({ -1e10, 2, 0 }, identityTranslation_);
94     translateAndScale(testVectors_);
95     std::vector<RVec> expected = {
96         { 0, 0, 0 }, { -1e10, 0, 0 }, { 0, -2, 0 }, { -1e20, 2e1, 0 }, { -3e10, -12, 0 }
97     };
98     EXPECT_THAT(expected, Pointwise(RVecEq(defaultFloatTolerance()), testVectors_));
99 }
100
101 TEST_F(TranslateAndScaleTest, translationAndScalingNonTrivial)
102 {
103     TranslateAndScale translateAndScale({ -1e10, 2, 0 }, { 1, -1, 2 });
104     translateAndScale(testVectors_);
105     std::vector<RVec> expected = {
106         { -1e+10, -2, 0 }, { -2e+10, -2, 0 }, { -1e+10, -4, 0 }, { -1e+20, 18, 0 }, { -4e+10, -14, 0 }
107     };
108     EXPECT_THAT(expected, Pointwise(RVecEq(defaultFloatTolerance()), testVectors_));
109 }
110
111 TEST_F(TranslateAndScaleTest, scalingIdentity)
112 {
113     ScaleCoordinates scale(identityScale_);
114     auto             expected = testVectors_;
115     scale(testVectors_);
116     EXPECT_THAT(expected, Pointwise(RVecEq(defaultFloatTolerance()), testVectors_));
117 }
118
119 TEST_F(TranslateAndScaleTest, scalingNonTrivial)
120 {
121     ScaleCoordinates  scale({ -100, 0.1, 0 });
122     std::vector<RVec> expected = {
123         { 0, 0, 0 }, { -100, 0, 0 }, { 0, -0.1, 0 }, { -1e12, 1e0, 0 }, { -300, -0.6, 0 }
124     };
125     scale(testVectors_);
126     EXPECT_THAT(expected, Pointwise(RVecEq(defaultFloatTolerance()), testVectors_));
127 }
128
129 TEST_F(TranslateAndScaleTest, scalingInverseNoZero)
130 {
131     ScaleCoordinates scale({ -100, 0.1, 3 });
132     auto             expected = testVectors_;
133     scale(testVectors_);
134     scale.inverseIgnoringZeroScale(testVectors_);
135     EXPECT_THAT(expected, Pointwise(RVecEq(defaultFloatTolerance()), testVectors_));
136 }
137
138 TEST_F(TranslateAndScaleTest, scalingInverseWithOneScaleDimensionZero)
139 {
140     ScaleCoordinates scale({ -100, 0.1, 0 });
141     std::vector<RVec> expected = { { 0, 0, 0 }, { 1, 0, 0 }, { 0, -1, 0 }, { 1e10, 1e1, 0 }, { 3, -6, 0 } };
142     scale(testVectors_);
143     scale.inverseIgnoringZeroScale(testVectors_);
144     EXPECT_THAT(expected, Pointwise(RVecEq(defaultFloatTolerance()), testVectors_));
145 }
146
147 } // namespace test
148 } // namespace gmx