Add nblib backend: Part 1 of 2
[alexxy/gromacs.git] / api / nblib / tests / molecules.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/molecules.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, CanConstructMoleculeWithoutChargeOrResidueName)
60 {
61     ArAtom       arAtom;
62     ParticleType Ar(arAtom.particleTypeName, arAtom.mass);
63     Molecule     argon(arAtom.moleculeName);
64     EXPECT_NO_THROW(argon.addParticle(arAtom.particleName, Ar));
65 }
66
67 TEST(NBlibTest, CanConstructMoleculeWithChargeWithoutResidueName)
68 {
69     ArAtom       arAtom;
70     ParticleType Ar(arAtom.particleTypeName, arAtom.mass);
71     Molecule     argon(arAtom.moleculeName);
72     EXPECT_NO_THROW(argon.addParticle(arAtom.particleName, Charge(0), Ar));
73 }
74
75 TEST(NBlibTest, CanConstructMoleculeWithoutChargeWithResidueName)
76 {
77     ArAtom       arAtom;
78     ParticleType Ar(arAtom.particleTypeName, arAtom.mass);
79     Molecule     argon(arAtom.moleculeName);
80     EXPECT_NO_THROW(argon.addParticle(arAtom.particleName, ResidueName("ar2"), Ar));
81 }
82
83 TEST(NBlibTest, CanConstructMoleculeWithChargeWithResidueName)
84 {
85     ArAtom       arAtom;
86     ParticleType Ar(arAtom.particleTypeName, arAtom.mass);
87     Molecule     argon(arAtom.moleculeName);
88     EXPECT_NO_THROW(argon.addParticle(arAtom.particleName, ResidueName("ar2"), Charge(0), Ar));
89 }
90
91 TEST(NBlibTest, CanGetNumParticlesInMolecule)
92 {
93     WaterMoleculeBuilder waterMolecule;
94     Molecule             water        = waterMolecule.waterMolecule();
95     auto                 numParticles = water.numParticlesInMolecule();
96
97     EXPECT_EQ(3, numParticles);
98 }
99
100 TEST(NBlibTest, CanConstructExclusionListFromNames)
101 {
102     WaterMoleculeBuilder waterMolecule;
103     Molecule             water = waterMolecule.waterMolecule();
104
105     std::vector<std::tuple<int, int>> exclusions = water.getExclusions();
106
107     std::vector<std::tuple<int, int>> reference{ { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 0 }, { 1, 1 },
108                                                  { 1, 2 }, { 2, 0 }, { 2, 1 }, { 2, 2 } };
109
110     ASSERT_EQ(exclusions.size(), 9);
111     for (std::size_t i = 0; i < exclusions.size(); ++i)
112     {
113         EXPECT_EQ(exclusions[i], reference[i]);
114     }
115 }
116
117 TEST(NBlibTest, CanConstructExclusionListFromIndices)
118 {
119     WaterMoleculeBuilder waterMolecule;
120     Molecule             water = waterMolecule.waterMoleculeWithoutExclusions();
121
122     //! Add the exclusions
123     water.addExclusion(1, 0);
124     water.addExclusion(2, 0);
125     water.addExclusion(1, 2);
126
127     std::vector<std::tuple<int, int>> exclusions = water.getExclusions();
128
129     std::vector<std::tuple<int, int>> reference{ { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 0 }, { 1, 1 },
130                                                  { 1, 2 }, { 2, 0 }, { 2, 1 }, { 2, 2 } };
131
132     ASSERT_EQ(exclusions.size(), 9);
133     for (std::size_t i = 0; i < exclusions.size(); ++i)
134     {
135         EXPECT_EQ(exclusions[i], reference[i]);
136     }
137 }
138
139 TEST(NBlibTest, CanConstructExclusionListFromNamesAndIndicesMixed)
140 {
141     WaterMoleculeBuilder waterMolecule;
142     Molecule             water = waterMolecule.waterMoleculeWithoutExclusions();
143
144     //! Add the exclusions
145     water.addExclusion("H1", "Oxygen");
146     water.addExclusion("H2", "Oxygen");
147     water.addExclusion(1, 2);
148
149     std::vector<std::tuple<int, int>> exclusions = water.getExclusions();
150
151     std::vector<std::tuple<int, int>> reference{ { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 0 }, { 1, 1 },
152                                                  { 1, 2 }, { 2, 0 }, { 2, 1 }, { 2, 2 } };
153
154     ASSERT_EQ(exclusions.size(), 9);
155     for (std::size_t i = 0; i < exclusions.size(); ++i)
156     {
157         EXPECT_EQ(exclusions[i], reference[i]);
158     }
159 }
160
161 TEST(NBlibTest, AtWorks)
162 {
163     WaterMoleculeBuilder waterMolecule;
164     Molecule             water = waterMolecule.waterMolecule();
165     EXPECT_NO_THROW(water.at("Ow"));
166     EXPECT_NO_THROW(water.at("H"));
167 }
168
169 TEST(NBlibTest, AtThrows)
170 {
171     WaterMoleculeBuilder waterMolecule;
172     Molecule             water = waterMolecule.waterMolecule();
173     EXPECT_THROW_GMX(water.at("Hw"), std::out_of_range);
174 }
175
176 TEST(NBlibTest, MoleculeThrowsSameParticleTypeNameDifferentMass)
177 {
178     //! User error: Two different ParticleTypes with the same name
179     ParticleType atom1(ParticleTypeName("Atom"), Mass(1));
180     ParticleType atom2(ParticleTypeName("Atom"), Mass(2));
181
182     Molecule molecule(MoleculeName("UraniumDimer"));
183     EXPECT_NO_THROW(molecule.addParticle(ParticleName("U1"), atom1));
184     EXPECT_THROW(molecule.addParticle(ParticleName("U2"), atom2), InputException);
185 }
186
187 TEST(NBlibTest, MoleculeDontThrowsSameParticleTypeNameDifferentMass)
188 {
189     //! User error: Two different ParticleTypes with the same name
190     ParticleType atom1(ParticleTypeName("Atom"), Mass(1));
191     ParticleType atom2(ParticleTypeName("Atom"), Mass(1));
192
193     Molecule molecule(MoleculeName("UraniumDimer"));
194     EXPECT_NO_THROW(molecule.addParticle(ParticleName("U1"), atom1));
195     EXPECT_NO_THROW(molecule.addParticle(ParticleName("U2"), atom2));
196 }
197
198 TEST(NBlibTest, MoleculeNoThrowsSameParticleTypeName)
199 {
200     //! User error: Two different ParticleTypes with the same name
201     ParticleType atom1(ParticleTypeName("Atom"), Mass(1));
202     ParticleType atom2(ParticleTypeName("Atom"), Mass(1));
203
204     Molecule molecule(MoleculeName("UraniumDimer"));
205     EXPECT_NO_THROW(molecule.addParticle(ParticleName("U1"), atom1));
206     EXPECT_NO_THROW(molecule.addParticle(ParticleName("U2"), atom2));
207 }
208
209 } // namespace
210 } // namespace test
211 } // namespace nblib