Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / api / nblib / tests / interactions.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020, 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 molecule setup 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  * \author Artem Zhmurov <zhmurov@gmail.com>
44  */
45 #include "nblib/interactions.h"
46 #include "nblib/exception.h"
47 #include "nblib/particletype.h"
48 #include "nblib/tests/testsystems.h"
49
50 #include "testutils/testasserts.h"
51
52 namespace nblib
53 {
54 namespace test
55 {
56 namespace
57 {
58
59 TEST(NBlibTest, NonBondedForceParamsCorrect)
60 {
61     ParticleType atom1(ParticleTypeName("a1"), Mass(1));
62     ParticleType atom2(ParticleTypeName("a2"), Mass(1));
63     ParticleType atom3(ParticleTypeName("a3"), Mass(1));
64
65     ParticleTypesInteractions interactions;
66
67     auto c6_1  = C6(1.6);
68     auto c12_1 = C12(1.12);
69     C6   c6_2{ 2.6 };
70     C12  c12_2{ 2.12 };
71     C6   c6_3  = C6(3.6);
72     C12  c12_3 = C12(3.12);
73
74     auto c6comb  = C6{ 40 };
75     auto c12comb = C12{ 41 };
76
77     interactions.add(atom1.name(), c6_1, c12_1);
78     interactions.add(atom2.name(), c6_2, c12_2);
79     interactions.add(atom3.name(), c6_3, c12_3);
80     interactions.add(atom2.name(), atom3.name(), c6comb, c12comb);
81
82     auto nbfp = interactions.generateTable();
83
84     //! self interaction for c6
85     EXPECT_REAL_EQ_TOL(c6_1, nbfp.getC6(atom1.name(), atom1.name()), gmx::test::defaultRealTolerance());
86     //! + symmetric pair
87     EXPECT_REAL_EQ_TOL(c12_1, nbfp.getC12(atom1.name(), atom1.name()), gmx::test::defaultRealTolerance());
88
89     //! geometric comb rule for c6
90     EXPECT_REAL_EQ_TOL(std::sqrt(c6_1 * c6_2),
91                        nbfp.getC6(atom1.name(), atom2.name()),
92                        gmx::test::defaultRealTolerance());
93     //! + symmetric pair
94     EXPECT_REAL_EQ_TOL(std::sqrt(c6_1 * c6_2),
95                        nbfp.getC6(atom2.name(), atom1.name()),
96                        gmx::test::defaultRealTolerance());
97
98     //! geometric comb rule for c12
99     EXPECT_REAL_EQ_TOL(std::sqrt(c12_1 * c12_2),
100                        nbfp.getC12(atom1.name(), atom2.name()),
101                        gmx::test::defaultRealTolerance());
102
103     //! + symmetric par
104     EXPECT_REAL_EQ_TOL(std::sqrt(c12_1 * c12_2),
105                        nbfp.getC12(atom2.name(), atom1.name()),
106                        gmx::test::defaultRealTolerance());
107
108     //! explicit pairwise interaction c6
109     EXPECT_REAL_EQ_TOL(c6comb, nbfp.getC6(atom2.name(), atom3.name()), gmx::test::defaultRealTolerance());
110     EXPECT_REAL_EQ_TOL(c6comb, nbfp.getC6(atom3.name(), atom2.name()), gmx::test::defaultRealTolerance());
111
112     //! explicit pairwise interaction c12
113     EXPECT_REAL_EQ_TOL(
114             c12comb, nbfp.getC12(atom2.name(), atom3.name()), gmx::test::defaultRealTolerance());
115     EXPECT_REAL_EQ_TOL(
116             c12comb, nbfp.getC12(atom3.name(), atom2.name()), gmx::test::defaultRealTolerance());
117
118     ParticleType atom4(ParticleTypeName("a4"), Mass(1));
119     interactions.add(atom3.name(), atom4.name(), C6(1), C12(2));
120     //! need to have self-interaction for all particles
121     EXPECT_THROW(auto interactionsTable = interactions.generateTable(), InputException);
122 }
123
124 TEST(NBlibTest, CanMergeInteractions)
125 {
126     ParticleType atom1(ParticleTypeName("a1"), Mass(1));
127     ParticleType atom2(ParticleTypeName("a2"), Mass(1));
128     ParticleType atom3(ParticleTypeName("a3"), Mass(1));
129
130     ParticleTypesInteractions interactions;
131
132     auto c6_1  = C6(1.6);
133     auto c12_1 = C12(1.12);
134     C6   c6_2{ 2.6 };
135     C12  c12_2{ 2.12 };
136     C6   c6_3  = C6(3.6);
137     C12  c12_3 = C12(3.12);
138
139     auto c6comb  = C6{ 40 };
140     auto c12comb = C12{ 41 };
141
142     interactions.add(atom1.name(), c6_1, c12_1);
143     interactions.add(atom2.name(), c6_2, c12_2);
144     interactions.add(atom3.name(), c6_3, c12_3);
145     interactions.add(atom2.name(), atom3.name(), c6comb, c12comb);
146
147     ParticleType atom4(ParticleTypeName("a4"), Mass(1));
148     ParticleType atom5(ParticleTypeName("a5"), Mass(1));
149     auto         c6_4  = C6{ 4.6 };
150     auto         c12_4 = C12{ 4.12 };
151
152     C6  c6_5{ 5.6 };
153     C12 c12_5{ 5.12 };
154
155     C6  c6_override  = C6(45.6);
156     C12 c12_override = C12(45.12);
157
158     ParticleTypesInteractions otherInteractions;
159     otherInteractions.add(atom4.name(), c6_4, c12_4);
160     otherInteractions.add(atom5.name(), c6_5, c12_5);
161     otherInteractions.add(atom4.name(), atom5.name(), c6_override, c12_override);
162
163     interactions.merge(otherInteractions);
164
165     auto nbfp = interactions.generateTable();
166
167     EXPECT_REAL_EQ_TOL(std::sqrt(c6_3 * c6_4),
168                        nbfp.getC6(atom3.name(), atom4.name()),
169                        gmx::test::defaultRealTolerance());
170     EXPECT_REAL_EQ_TOL(std::sqrt(c12_3 * c12_4),
171                        nbfp.getC12(atom3.name(), atom4.name()),
172                        gmx::test::defaultRealTolerance());
173     EXPECT_REAL_EQ_TOL(
174             c6_override, nbfp.getC6(atom4.name(), atom5.name()), gmx::test::defaultRealTolerance());
175     EXPECT_REAL_EQ_TOL(
176             c12_override, nbfp.getC12(atom4.name(), atom5.name()), gmx::test::defaultRealTolerance());
177 }
178
179 } // namespace
180 } // namespace test
181 } // namespace nblib