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 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 
TEST(NBlibTest,CanConstructMoleculeWithoutChargeOrResidueName)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 
TEST(NBlibTest,CanConstructMoleculeWithChargeWithoutResidueName)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 
TEST(NBlibTest,CanConstructMoleculeWithoutChargeWithResidueName)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 
TEST(NBlibTest,CanConstructMoleculeWithChargeWithResidueName)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 
TEST(NBlibTest,CanGetNumParticlesInMolecule)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 
TEST(NBlibTest,CanConstructExclusionListFromNames)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 
TEST(NBlibTest,CanConstructExclusionListFromNamesAndIndicesMixed)117 TEST(NBlibTest, CanConstructExclusionListFromNamesAndIndicesMixed)
118 {
119     WaterMoleculeBuilder waterMolecule;
120     Molecule             water = waterMolecule.waterMoleculeWithoutExclusions();
121 
122     //! Add the exclusions
123     water.addExclusion(ParticleName("H1"), ParticleName("Oxygen"));
124     water.addExclusion(ParticleName("H2"), ParticleName("Oxygen"));
125     water.addExclusion(ParticleName("H1"), ParticleName("H2"));
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 
TEST(NBlibTest,AtWorks)139 TEST(NBlibTest, AtWorks)
140 {
141     WaterMoleculeBuilder waterMolecule;
142     Molecule             water = waterMolecule.waterMolecule();
143     EXPECT_NO_THROW(water.at("Ow"));
144     EXPECT_NO_THROW(water.at("H"));
145 }
146 
TEST(NBlibTest,AtThrows)147 TEST(NBlibTest, AtThrows)
148 {
149     WaterMoleculeBuilder waterMolecule;
150     Molecule             water = waterMolecule.waterMolecule();
151     EXPECT_THROW_GMX(water.at("Hw"), std::out_of_range);
152 }
153 
TEST(NBlibTest,MoleculeThrowsSameParticleTypeNameDifferentMass)154 TEST(NBlibTest, MoleculeThrowsSameParticleTypeNameDifferentMass)
155 {
156     //! User error: Two different ParticleTypes with the same name
157     ParticleType atom1(ParticleTypeName("Atom"), Mass(1));
158     ParticleType atom2(ParticleTypeName("Atom"), Mass(2));
159 
160     Molecule molecule(MoleculeName("UraniumDimer"));
161     EXPECT_NO_THROW(molecule.addParticle(ParticleName("U1"), atom1));
162     EXPECT_THROW(molecule.addParticle(ParticleName("U2"), atom2), InputException);
163 }
164 
TEST(NBlibTest,MoleculeDontThrowsSameParticleTypeNameDifferentMass)165 TEST(NBlibTest, MoleculeDontThrowsSameParticleTypeNameDifferentMass)
166 {
167     //! User error: Two different ParticleTypes with the same name
168     ParticleType atom1(ParticleTypeName("Atom"), Mass(1));
169     ParticleType atom2(ParticleTypeName("Atom"), Mass(1));
170 
171     Molecule molecule(MoleculeName("UraniumDimer"));
172     EXPECT_NO_THROW(molecule.addParticle(ParticleName("U1"), atom1));
173     EXPECT_NO_THROW(molecule.addParticle(ParticleName("U2"), atom2));
174 }
175 
TEST(NBlibTest,MoleculeNoThrowsSameParticleTypeName)176 TEST(NBlibTest, MoleculeNoThrowsSameParticleTypeName)
177 {
178     //! User error: Two different ParticleTypes with the same name
179     ParticleType atom1(ParticleTypeName("Atom"), Mass(1));
180     ParticleType atom2(ParticleTypeName("Atom"), Mass(1));
181 
182     Molecule molecule(MoleculeName("UraniumDimer"));
183     EXPECT_NO_THROW(molecule.addParticle(ParticleName("U1"), atom1));
184     EXPECT_NO_THROW(molecule.addParticle(ParticleName("U2"), atom2));
185 }
186 
TEST(NBlibTest,CanAddInteractions)187 TEST(NBlibTest, CanAddInteractions)
188 {
189     Molecule     molecule(MoleculeName("BondTest"));
190     ParticleType O(ParticleTypeName("Ow"), Mass(1));
191     ParticleType H(ParticleTypeName("Hw"), Mass(1));
192     molecule.addParticle(ParticleName("O"), O);
193     molecule.addParticle(ParticleName("H1"), H);
194     molecule.addParticle(ParticleName("H2"), H);
195 
196     HarmonicBondType  hb(1, 2);
197     CubicBondType     cub(1, 2, 3);
198     HarmonicAngleType ang(Degrees(1), 1);
199 
200     molecule.addInteraction(ParticleName("O"), ParticleName("H1"), hb);
201     molecule.addInteraction(ParticleName("O"), ParticleName("H2"), hb);
202     molecule.addInteraction(ParticleName("H1"), ParticleName("H2"), cub);
203     molecule.addInteraction(ParticleName("H1"), ParticleName("O"), ParticleName("H2"), ang);
204 
205     const auto& interactionData = molecule.interactionData();
206 
207     //! harmonic bonds
208     EXPECT_EQ(pickType<HarmonicBondType>(interactionData).interactions_.size(), 2);
209     //! cubic bonds
210     EXPECT_EQ(pickType<CubicBondType>(interactionData).interactions_.size(), 1);
211     //! angular interactions
212     EXPECT_EQ(pickType<HarmonicAngleType>(interactionData).interactions_.size(), 1);
213 }
214 
215 } // namespace
216 } // namespace test
217 } // namespace nblib
218