2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * This implements molecule setup tests
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>
45 #include "nblib/molecules.h"
46 #include "nblib/exception.h"
47 #include "nblib/particletype.h"
48 #include "nblib/tests/testsystems.h"
50 #include "testutils/testasserts.h"
59 TEST(NBlibTest, CanConstructMoleculeWithoutChargeOrResidueName)
62 ParticleType Ar(arAtom.particleTypeName, arAtom.mass);
63 Molecule argon(arAtom.moleculeName);
64 EXPECT_NO_THROW(argon.addParticle(arAtom.particleName, Ar));
67 TEST(NBlibTest, CanConstructMoleculeWithChargeWithoutResidueName)
70 ParticleType Ar(arAtom.particleTypeName, arAtom.mass);
71 Molecule argon(arAtom.moleculeName);
72 EXPECT_NO_THROW(argon.addParticle(arAtom.particleName, Charge(0), Ar));
75 TEST(NBlibTest, CanConstructMoleculeWithoutChargeWithResidueName)
78 ParticleType Ar(arAtom.particleTypeName, arAtom.mass);
79 Molecule argon(arAtom.moleculeName);
80 EXPECT_NO_THROW(argon.addParticle(arAtom.particleName, ResidueName("ar2"), Ar));
83 TEST(NBlibTest, CanConstructMoleculeWithChargeWithResidueName)
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));
91 TEST(NBlibTest, CanGetNumParticlesInMolecule)
93 WaterMoleculeBuilder waterMolecule;
94 Molecule water = waterMolecule.waterMolecule();
95 auto numParticles = water.numParticlesInMolecule();
97 EXPECT_EQ(3, numParticles);
100 TEST(NBlibTest, CanConstructExclusionListFromNames)
102 WaterMoleculeBuilder waterMolecule;
103 Molecule water = waterMolecule.waterMolecule();
105 std::vector<std::tuple<int, int>> exclusions = water.getExclusions();
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 } };
110 ASSERT_EQ(exclusions.size(), 9);
111 for (std::size_t i = 0; i < exclusions.size(); ++i)
113 EXPECT_EQ(exclusions[i], reference[i]);
117 TEST(NBlibTest, CanConstructExclusionListFromNamesAndIndicesMixed)
119 WaterMoleculeBuilder waterMolecule;
120 Molecule water = waterMolecule.waterMoleculeWithoutExclusions();
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"));
127 std::vector<std::tuple<int, int>> exclusions = water.getExclusions();
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 } };
132 ASSERT_EQ(exclusions.size(), 9);
133 for (std::size_t i = 0; i < exclusions.size(); ++i)
135 EXPECT_EQ(exclusions[i], reference[i]);
139 TEST(NBlibTest, AtWorks)
141 WaterMoleculeBuilder waterMolecule;
142 Molecule water = waterMolecule.waterMolecule();
143 EXPECT_NO_THROW(water.at("Ow"));
144 EXPECT_NO_THROW(water.at("H"));
147 TEST(NBlibTest, AtThrows)
149 WaterMoleculeBuilder waterMolecule;
150 Molecule water = waterMolecule.waterMolecule();
151 EXPECT_THROW_GMX(water.at("Hw"), std::out_of_range);
154 TEST(NBlibTest, MoleculeThrowsSameParticleTypeNameDifferentMass)
156 //! User error: Two different ParticleTypes with the same name
157 ParticleType atom1(ParticleTypeName("Atom"), Mass(1));
158 ParticleType atom2(ParticleTypeName("Atom"), Mass(2));
160 Molecule molecule(MoleculeName("UraniumDimer"));
161 EXPECT_NO_THROW(molecule.addParticle(ParticleName("U1"), atom1));
162 EXPECT_THROW(molecule.addParticle(ParticleName("U2"), atom2), InputException);
165 TEST(NBlibTest, MoleculeDontThrowsSameParticleTypeNameDifferentMass)
167 //! User error: Two different ParticleTypes with the same name
168 ParticleType atom1(ParticleTypeName("Atom"), Mass(1));
169 ParticleType atom2(ParticleTypeName("Atom"), Mass(1));
171 Molecule molecule(MoleculeName("UraniumDimer"));
172 EXPECT_NO_THROW(molecule.addParticle(ParticleName("U1"), atom1));
173 EXPECT_NO_THROW(molecule.addParticle(ParticleName("U2"), atom2));
176 TEST(NBlibTest, MoleculeNoThrowsSameParticleTypeName)
178 //! User error: Two different ParticleTypes with the same name
179 ParticleType atom1(ParticleTypeName("Atom"), Mass(1));
180 ParticleType atom2(ParticleTypeName("Atom"), Mass(1));
182 Molecule molecule(MoleculeName("UraniumDimer"));
183 EXPECT_NO_THROW(molecule.addParticle(ParticleName("U1"), atom1));
184 EXPECT_NO_THROW(molecule.addParticle(ParticleName("U2"), atom2));
187 TEST(NBlibTest, CanAddInteractions)
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);
196 HarmonicBondType hb(1, 2);
197 CubicBondType cub(1, 2, 3);
198 HarmonicAngle ang(1, Degrees(1));
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);
205 const auto& interactionData = molecule.interactionData();
208 EXPECT_EQ(pickType<HarmonicBondType>(interactionData).interactions_.size(), 2);
210 EXPECT_EQ(pickType<CubicBondType>(interactionData).interactions_.size(), 1);
211 //! angular interactions
212 EXPECT_EQ(pickType<HarmonicAngle>(interactionData).interactions_.size(), 1);