bb93f5dfd3d7f5493e134ace3de65dfd2b2dcd6a
[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), nbfp.getC6(atom1.name(), atom2.name()),
91                        gmx::test::defaultRealTolerance());
92     //! + symmetric pair
93     EXPECT_REAL_EQ_TOL(std::sqrt(c6_1 * c6_2), nbfp.getC6(atom2.name(), atom1.name()),
94                        gmx::test::defaultRealTolerance());
95
96     //! geometric comb rule for c12
97     EXPECT_REAL_EQ_TOL(std::sqrt(c12_1 * c12_2), nbfp.getC12(atom1.name(), atom2.name()),
98                        gmx::test::defaultRealTolerance());
99
100     //! + symmetric par
101     EXPECT_REAL_EQ_TOL(std::sqrt(c12_1 * c12_2), nbfp.getC12(atom2.name(), atom1.name()),
102                        gmx::test::defaultRealTolerance());
103
104     //! explicit pairwise interaction c6
105     EXPECT_REAL_EQ_TOL(c6comb, nbfp.getC6(atom2.name(), atom3.name()), gmx::test::defaultRealTolerance());
106     EXPECT_REAL_EQ_TOL(c6comb, nbfp.getC6(atom3.name(), atom2.name()), gmx::test::defaultRealTolerance());
107
108     //! explicit pairwise interaction c12
109     EXPECT_REAL_EQ_TOL(c12comb, nbfp.getC12(atom2.name(), atom3.name()),
110                        gmx::test::defaultRealTolerance());
111     EXPECT_REAL_EQ_TOL(c12comb, nbfp.getC12(atom3.name(), atom2.name()),
112                        gmx::test::defaultRealTolerance());
113
114     ParticleType atom4(ParticleTypeName("a4"), Mass(1));
115     interactions.add(atom3.name(), atom4.name(), C6(1), C12(2));
116     //! need to have self-interaction for all particles
117     EXPECT_THROW(auto interactionsTable = interactions.generateTable(), InputException);
118 }
119
120 TEST(NBlibTest, CanMergeInteractions)
121 {
122     ParticleType atom1(ParticleTypeName("a1"), Mass(1));
123     ParticleType atom2(ParticleTypeName("a2"), Mass(1));
124     ParticleType atom3(ParticleTypeName("a3"), Mass(1));
125
126     ParticleTypesInteractions interactions;
127
128     auto c6_1  = C6(1.6);
129     auto c12_1 = C12(1.12);
130     C6   c6_2{ 2.6 };
131     C12  c12_2{ 2.12 };
132     C6   c6_3  = C6(3.6);
133     C12  c12_3 = C12(3.12);
134
135     auto c6comb  = C6{ 40 };
136     auto c12comb = C12{ 41 };
137
138     interactions.add(atom1.name(), c6_1, c12_1);
139     interactions.add(atom2.name(), c6_2, c12_2);
140     interactions.add(atom3.name(), c6_3, c12_3);
141     interactions.add(atom2.name(), atom3.name(), c6comb, c12comb);
142
143     ParticleType atom4(ParticleTypeName("a4"), Mass(1));
144     ParticleType atom5(ParticleTypeName("a5"), Mass(1));
145     auto         c6_4  = C6{ 4.6 };
146     auto         c12_4 = C12{ 4.12 };
147
148     C6  c6_5{ 5.6 };
149     C12 c12_5{ 5.12 };
150
151     C6  c6_override  = C6(45.6);
152     C12 c12_override = C12(45.12);
153
154     ParticleTypesInteractions otherInteractions;
155     otherInteractions.add(atom4.name(), c6_4, c12_4);
156     otherInteractions.add(atom5.name(), c6_5, c12_5);
157     otherInteractions.add(atom4.name(), atom5.name(), c6_override, c12_override);
158
159     interactions.merge(otherInteractions);
160
161     auto nbfp = interactions.generateTable();
162
163     EXPECT_REAL_EQ_TOL(std::sqrt(c6_3 * c6_4), nbfp.getC6(atom3.name(), atom4.name()),
164                        gmx::test::defaultRealTolerance());
165     EXPECT_REAL_EQ_TOL(std::sqrt(c12_3 * c12_4), nbfp.getC12(atom3.name(), atom4.name()),
166                        gmx::test::defaultRealTolerance());
167     EXPECT_REAL_EQ_TOL(c6_override, nbfp.getC6(atom4.name(), atom5.name()),
168                        gmx::test::defaultRealTolerance());
169     EXPECT_REAL_EQ_TOL(c12_override, nbfp.getC12(atom4.name(), atom5.name()),
170                        gmx::test::defaultRealTolerance());
171 }
172
173 } // namespace
174 } // namespace test
175 } // namespace nblib