Simplify nblib listed forces type tests
authorJoe Jordan <ejjordan12@gmail.com>
Wed, 18 Aug 2021 09:31:18 +0000 (09:31 +0000)
committerArtem Zhmurov <zhmurov@gmail.com>
Wed, 18 Aug 2021 09:31:18 +0000 (09:31 +0000)
api/nblib/listed_forces/bondtypes.h
api/nblib/listed_forces/tests/listedtesthelpers.h [new file with mode: 0644]
api/nblib/listed_forces/tests/refdata/FourCenter_ListedForcesProperDihedralTest.xml [new file with mode: 0644]
api/nblib/listed_forces/tests/refdata/ThreeCenter_ListedForcesHarmonicAngleTest.xml [new file with mode: 0644]
api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesCubicBondTest.xml [new file with mode: 0644]
api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesFeneBondTest.xml [new file with mode: 0644]
api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesG96BondTest.xml [new file with mode: 0644]
api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesHarmonicBondTest.xml [new file with mode: 0644]
api/nblib/listed_forces/tests/typetests.cpp

index 8e9844f76ab855cb787d9efc4f2ded441d98c19a..51baf103d63da1829218f35b0afe8a323c2bb5fb 100644 (file)
@@ -120,7 +120,16 @@ using HarmonicBondType = TwoParameterInteraction<struct HarmonicBondTypeParamete
  * It represents the interaction of the form
  * V(r, forceConstant, equilDistance) = 0.25 * forceConstant * (r^2 - equilConstant^2)^2
  */
-using G96BondType = TwoParameterInteraction<struct G96BondTypeParameter>;
+class G96BondType : public TwoParameterInteraction<struct G96BondTypeParameter>
+{
+public:
+    G96BondType() = default;
+    //! \brief Store square of equilibrium distance
+    G96BondType(ForceConstant f, EquilConstant equilConstant) :
+        TwoParameterInteraction<struct G96BondTypeParameter>{ f, equilConstant * equilConstant }
+    {
+    }
+};
 
 
 /*! \brief FENE bond type
diff --git a/api/nblib/listed_forces/tests/listedtesthelpers.h b/api/nblib/listed_forces/tests/listedtesthelpers.h
new file mode 100644 (file)
index 0000000..a8d62ab
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * This file is part of the GROMACS molecular simulation package.
+ *
+ * Copyright (c) 2021, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
+ *
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
+ *
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
+ *
+ * To help us fund GROMACS development, we humbly ask that you cite
+ * the research papers on the package. Check out http://www.gromacs.org.
+ */
+/*! \internal \file
+ * \brief
+ * A collection of helper utilities that allow setting up both Nblib and
+ * GROMACS fixtures for computing listed interactions given sets of parameters
+ * and coordinates
+ *
+ * \author Victor Holanda <victor.holanda@cscs.ch>
+ * \author Joe Jordan <ejjordan@kth.se>
+ * \author Prashanth Kanduri <kanduri@cscs.ch>
+ * \author Sebastian Keller <keller@cscs.ch>
+ */
+
+#ifndef NBLIB_LISTEDFORCES_LISTEDTESTHELPERS_H
+#define NBLIB_LISTEDFORCES_LISTEDTESTHELPERS_H
+
+#include "nblib/listed_forces/definitions.h"
+
+namespace nblib
+{
+class Box;
+
+//! \brief Creates a default vector of indices for two-centered interactions
+template<class Interaction, std::enable_if_t<Contains<Interaction, SupportedTwoCenterTypes>{}>* = nullptr>
+std::vector<InteractionIndex<Interaction>> indexVector()
+{
+    return { { 0, 1, 0 } };
+}
+
+//! \brief Creates a default vector of indices for three-centered interactions
+template<class Interaction, std::enable_if_t<Contains<Interaction, SupportedThreeCenterTypes>{}>* = nullptr>
+std::vector<InteractionIndex<Interaction>> indexVector()
+{
+    return { { 0, 1, 2, 0 } };
+}
+
+//! \brief Creates a default vector of indices for four-centered interactions
+template<class Interaction, std::enable_if_t<Contains<Interaction, SupportedFourCenterTypes>{}>* = nullptr>
+std::vector<InteractionIndex<Interaction>> indexVector()
+{
+    return { { 0, 1, 2, 3, 0 } };
+}
+
+} // namespace nblib
+
+#endif // NBLIB_LISTEDFORCES_LISTEDTESTHELPERS_H
diff --git a/api/nblib/listed_forces/tests/refdata/FourCenter_ListedForcesProperDihedralTest.xml b/api/nblib/listed_forces/tests/refdata/FourCenter_ListedForcesProperDihedralTest.xml
new file mode 100644 (file)
index 0000000..0c97923
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/xsl" href="referencedata.xsl"?>
+<ReferenceData>
+  <Real Name="Epot">15.644075054627638</Real>
+  <Sequence Name="forces">
+    <Int Name="Length">4</Int>
+    <Vector>
+      <Real Name="X">0</Real>
+      <Real Name="Y">3000.9773757382213</Real>
+      <Real Name="Z">0</Real>
+    </Vector>
+    <Vector>
+      <Real Name="X">-294.15662928338577</Real>
+      <Real Name="Y">2953.652678308878</Real>
+      <Real Name="Z">-14.707831464169288</Real>
+    </Vector>
+    <Vector>
+      <Real Name="X">589.93598821678029</Real>
+      <Real Name="Y">-5922.0943245644257</Real>
+      <Real Name="Z">29.49679941083901</Real>
+    </Vector>
+    <Vector>
+      <Real Name="X">-295.77935893339446</Real>
+      <Real Name="Y">-32.535729482673389</Real>
+      <Real Name="Z">-14.788967946669722</Real>
+    </Vector>
+  </Sequence>
+</ReferenceData>
diff --git a/api/nblib/listed_forces/tests/refdata/ThreeCenter_ListedForcesHarmonicAngleTest.xml b/api/nblib/listed_forces/tests/refdata/ThreeCenter_ListedForcesHarmonicAngleTest.xml
new file mode 100644 (file)
index 0000000..0a6d6ff
--- /dev/null
@@ -0,0 +1,23 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/xsl" href="referencedata.xsl"?>
+<ReferenceData>
+  <Real Name="Epot">0.94993062982878551</Real>
+  <Sequence Name="forces">
+    <Int Name="Length">3</Int>
+    <Vector>
+      <Real Name="X">21.211573973968179</Real>
+      <Real Name="Y">-58.969435807718355</Real>
+      <Real Name="Z">11.550849737392344</Real>
+    </Vector>
+    <Vector>
+      <Real Name="X">-68.126432793403268</Real>
+      <Real Name="Y">74.829490902945352</Real>
+      <Real Name="Z">28.581851681412015</Real>
+    </Vector>
+    <Vector>
+      <Real Name="X">46.914858819435082</Real>
+      <Real Name="Y">-15.860055095226993</Real>
+      <Real Name="Z">-40.132701418804359</Real>
+    </Vector>
+  </Sequence>
+</ReferenceData>
diff --git a/api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesCubicBondTest.xml b/api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesCubicBondTest.xml
new file mode 100644 (file)
index 0000000..e7c441b
--- /dev/null
@@ -0,0 +1,18 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/xsl" href="referencedata.xsl"?>
+<ReferenceData>
+  <Real Name="Epot">0.0024516697933710625</Real>
+  <Sequence Name="forces">
+    <Int Name="Length">2</Int>
+    <Vector>
+      <Real Name="X">0.45585011220943999</Real>
+      <Real Name="Y">0.063187144266655115</Real>
+      <Real Name="Z">-0.51452388902847734</Real>
+    </Vector>
+    <Vector>
+      <Real Name="X">-0.45585011220943999</Real>
+      <Real Name="Y">-0.063187144266655115</Real>
+      <Real Name="Z">0.51452388902847734</Real>
+    </Vector>
+  </Sequence>
+</ReferenceData>
diff --git a/api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesFeneBondTest.xml b/api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesFeneBondTest.xml
new file mode 100644 (file)
index 0000000..766b33d
--- /dev/null
@@ -0,0 +1,18 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/xsl" href="referencedata.xsl"?>
+<ReferenceData>
+  <Real Name="Epot">0.063226249960230213</Real>
+  <Sequence Name="forces">
+    <Int Name="Length">2</Int>
+    <Vector>
+      <Real Name="X">-0.59147774272182241</Real>
+      <Real Name="Y">-0.081987013842628936</Real>
+      <Real Name="Z">0.66760854128997849</Real>
+    </Vector>
+    <Vector>
+      <Real Name="X">0.59147774272182241</Real>
+      <Real Name="Y">0.081987013842628936</Real>
+      <Real Name="Z">-0.66760854128997849</Real>
+    </Vector>
+  </Sequence>
+</ReferenceData>
diff --git a/api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesG96BondTest.xml b/api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesG96BondTest.xml
new file mode 100644 (file)
index 0000000..03490e3
--- /dev/null
@@ -0,0 +1,18 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/xsl" href="referencedata.xsl"?>
+<ReferenceData>
+  <Real Name="Epot">0.0000099681125000004196</Real>
+  <Sequence Name="forces">
+    <Int Name="Length">2</Int>
+    <Vector>
+      <Real Name="X">-0.0045096500000000941</Real>
+      <Real Name="Y">-0.00062510000000001373</Real>
+      <Real Name="Z">0.0050901000000001112</Real>
+    </Vector>
+    <Vector>
+      <Real Name="X">0.0045096500000000941</Real>
+      <Real Name="Y">0.00062510000000001373</Real>
+      <Real Name="Z">-0.0050901000000001112</Real>
+    </Vector>
+  </Sequence>
+</ReferenceData>
diff --git a/api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesHarmonicBondTest.xml b/api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesHarmonicBondTest.xml
new file mode 100644 (file)
index 0000000..0e92652
--- /dev/null
@@ -0,0 +1,18 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/xsl" href="referencedata.xsl"?>
+<ReferenceData>
+  <Real Name="Epot">0.002172238952435819</Real>
+  <Sequence Name="forces">
+    <Int Name="Length">2</Int>
+    <Vector>
+      <Real Name="X">-0.97326747891234377</Real>
+      <Real Name="Y">-0.13490836341359236</Real>
+      <Real Name="Z">1.0985395306535377</Real>
+    </Vector>
+    <Vector>
+      <Real Name="X">0.97326747891234377</Real>
+      <Real Name="Y">0.13490836341359236</Real>
+      <Real Name="Z">-1.0985395306535377</Real>
+    </Vector>
+  </Sequence>
+</ReferenceData>
index 0d1121fe647c3ba6d5d2b2d166240c0faa77dc6d..af26692cb61a048645a903291bef9d5316fb7e8d 100644 (file)
  * \author Prashanth Kanduri <kanduri@cscs.ch>
  * \author Sebastian Keller <keller@cscs.ch>
  */
+#include "gromacs/utility/arrayref.h"
 #include "nblib/listed_forces/dataflow.hpp"
+#include "nblib/listed_forces/tests/listedtesthelpers.h"
+#include "nblib/tests/testhelpers.h"
 
 #include "testutils/refdata.h"
 #include "testutils/testasserts.h"
 namespace nblib
 {
 
-//! Number of atoms used in these tests.
-constexpr int c_numAtoms = 4;
+//! Coordinates for testing
+static const std::vector<gmx::RVec> c_coordinatesForDihTests = { { 0.0, 0.0, 0.0 },
+                                                                 { 0.0, 0.0, 0.2 },
+                                                                 { 0.005, 0.0, 0.1 },
+                                                                 { -0.001, 0.1, 0.0 } };
 
-namespace
-{
+//! Coordinates for testing angles
+static const std::vector<gmx::RVec> c_coordinatesForAngleTests = { { 1.382, 1.573, 1.482 },
+                                                                   { 1.281, 1.559, 1.596 },
+                                                                   { 1.292, 1.422, 1.663 } };
 
-//! Coordinates for testing
-std::vector<std::vector<gmx::RVec>> c_coordinatesForTests = {
-    { { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.2 }, { 0.005, 0.0, 0.1 }, { -0.001, 0.1, 0.0 } },
-    { { 0.5, 0.0, 0.0 }, { 0.5, 0.0, 0.15 }, { 0.5, 0.07, 0.22 }, { 0.5, 0.18, 0.22 } },
-    { { -0.1143, -0.0282, 0.0 }, { 0.0, 0.0434, 0.0 }, { 0.1185, -0.0138, 0.0 }, { -0.0195, 0.1498, 0.0 } }
-};
-// Parameters for harmonic bonds
-std::vector<InteractionIndex<HarmonicBondType>> c_HarmonicBondIndices{ { 0, 1, 0 }, { 1, 2, 0 }, { 2, 3, 0 } };
-std::vector<std::vector<HarmonicBondType>> c_InputHarmonicBond = { { HarmonicBondType(500, 0.15) } };
-
-// Parameters for harmonic angles
-std::vector<InteractionIndex<HarmonicAngle>> c_HarmonicAngleIndices{ { 0, 1, 2, 0 }, { 1, 2, 3, 0 } };
-std::vector<std::vector<HarmonicAngle>> c_InputHarmonicAngle = { { HarmonicAngle(50.0, Degrees(100)) } };
-
-//! Function types for testing dihedrals. Add new terms at the end.
-std::vector<std::vector<ProperDihedral>> c_InputDihs = { { { ProperDihedral(Degrees(-105.0), 15.0, 2) } } /*, { ImproperDihedral(100.0, 50.0) }*/ };
-// Todo: update test setup to allow more than one interaction type and add the following to the inputs
-// std::vector<std::vector<RyckaertBellemanDihedral>> c_InputDihs = { { RyckaertBellemanDihedral({ -7.35, 13.6, 8.4, -16.7, 1.3, 12.4 }) } };
-
-} // namespace
-
-template<class Interaction>
-class ListedForcesBase
-{
-public:
-    std::vector<Interaction>                   input_;
-    std::vector<gmx::RVec>                     x_;
-    std::vector<InteractionIndex<Interaction>> indices_;
-    PbcHolder                                  pbcHolder_;
-    gmx::test::TestReferenceData               refData_;
-    gmx::test::TestReferenceChecker            checker_;
-    std::vector<gmx::RVec>                     forces_;
-    real                                       energy_;
-
-    ListedForcesBase(std::vector<Interaction>                   input,
-                     std::vector<gmx::RVec>                     coordinates,
-                     std::vector<InteractionIndex<Interaction>> indices) :
-        input_(std::move(input)),
-        x_(std::move(coordinates)),
-        indices_(std::move(indices)),
-        pbcHolder_(PbcType::Xyz, Box(1.5)),
-        checker_(refData_.rootChecker()),
-        forces_(c_numAtoms, gmx::RVec{ 0, 0, 0 })
-    {
-        energy_ = computeForces(gmx::ArrayRef<const InteractionIndex<Interaction>>(indices_),
-                                gmx::ArrayRef<const Interaction>(input_),
-                                x_,
-                                &forces_,
-                                pbcHolder_);
-    }
-
-    void checkForcesAndEnergies()
-    {
-        // We need quite specific tolerances here since angle functions
-        // etc. are not very precise and reproducible.
-        auto tolerances = gmx::test::FloatingPointTolerance(
-                singleAbsoluteTolerance_, 1.0e-12, singleRelativeTolerance_, 1.0e-12, 1000, 100, false);
-        checker_.setDefaultTolerance(tolerances);
-        checker_.checkReal(energy_, "Epot");
-        checker_.checkSequence(std::begin(forces_), std::end(forces_), "forces");
-    }
-
-    void setSingleTolerance(float relative, float absolute)
-    {
-        singleRelativeTolerance_ = relative;
-        singleAbsoluteTolerance_ = absolute;
-    }
-
-private:
-    float singleRelativeTolerance_ = 1.0e-12;
-    float singleAbsoluteTolerance_ = 1.0e-12;
-};
-
-class ProperDihedralTest :
-    public ListedForcesBase<ProperDihedral>,
-    public testing::TestWithParam<std::tuple<std::vector<ProperDihedral>, std::vector<gmx::RVec>>>
-{
-    using Base = ListedForcesBase<ProperDihedral>;
+//! Coordinates for testing bonds
+static const std::vector<gmx::RVec> c_coordinatesForBondTests = { { 1.382, 1.573, 1.482 },
+                                                                  { 1.281, 1.559, 1.596 } };
 
-public:
-    ProperDihedralTest() :
-        Base(std::get<0>(GetParam()), std::get<1>(GetParam()), { { 0, 1, 2, 3, 0 } })
-    {
-    }
-};
+//! Function types for testing Harmonic bonds
+static const std::vector<HarmonicBondType> c_InputHarmonicBonds = { { HarmonicBondType(500.0, 0.15) } };
 
-TEST_P(ProperDihedralTest, CheckListed)
-{
-    checkForcesAndEnergies();
-}
+//! Function types for testing G96 bonds
+static const std::vector<G96BondType> c_InputG96Bonds = { { G96BondType(50.0, 0.15) } };
 
-INSTANTIATE_TEST_SUITE_P(FourCenter,
-                         ProperDihedralTest,
-                         ::testing::Combine(::testing::ValuesIn(c_InputDihs),
-                                            ::testing::ValuesIn(c_coordinatesForTests)));
+//! Function types for testing cubic bonds
+static const std::vector<CubicBondType> c_InputCubicBonds = { { CubicBondType(50.0, 2.0, 0.16) } };
 
-class HarmonicBondTest :
-    public ListedForcesBase<HarmonicBondType>,
-    public testing::TestWithParam<std::tuple<std::vector<HarmonicBondType>, std::vector<gmx::RVec>>>
-{
-    using Base = ListedForcesBase<HarmonicBondType>;
+//! Function types for testing FENE bonds
+static const std::vector<FENEBondType> c_InputFeneBonds = { { FENEBondType(5.0, 0.4) } };
 
-public:
-    HarmonicBondTest() :
-        Base(std::get<0>(GetParam()), std::get<1>(GetParam()), c_HarmonicBondIndices)
-    {
-    }
-};
+//! Function types for testing Harmonic angles
+static const std::vector<HarmonicAngle> c_InputHarmonicAngles = { { HarmonicAngle(50.0, Degrees(100)) } };
 
-TEST_P(HarmonicBondTest, CheckListed)
+//! Function types for testing dihedrals
+static const std::vector<ProperDihedral> c_InputDihs = { { ProperDihedral(Degrees(-105.0), 15.0, 2) } };
+
+template<class Interaction, std::enable_if_t<Contains<Interaction, SupportedListedTypes>{}>* = nullptr>
+void checkForcesAndEnergiesWithRefData(std::vector<Interaction> input, gmx::ArrayRef<const gmx::RVec> x)
 {
-    checkForcesAndEnergies();
+    auto                   indices = indexVector<Interaction>();
+    PbcHolder              pbcHolder(PbcType::Xyz, Box(1.5));
+    test::RefDataChecker   refDataChecker(1e-4);
+    std::vector<gmx::RVec> forces(x.size(), gmx::RVec{ 0, 0, 0 });
+
+    auto energy = computeForces(gmx::ArrayRef<const InteractionIndex<Interaction>>(indices),
+                                gmx::ArrayRef<const Interaction>(input),
+                                x,
+                                &forces,
+                                pbcHolder);
+
+    refDataChecker.testReal(energy, "Epot");
+    refDataChecker.testArrays<gmx::RVec>(forces, "forces");
 }
 
-INSTANTIATE_TEST_SUITE_P(TwoCenter,
-                         HarmonicBondTest,
-                         ::testing::Combine(::testing::ValuesIn(c_InputHarmonicBond),
-                                            ::testing::ValuesIn(c_coordinatesForTests)));
+TEST(FourCenter, ListedForcesProperDihedralTest)
+{
+    checkForcesAndEnergiesWithRefData(c_InputDihs, c_coordinatesForDihTests);
+}
 
-class HarmonicAngleTest :
-    public ListedForcesBase<HarmonicAngle>,
-    public testing::TestWithParam<std::tuple<std::vector<HarmonicAngle>, std::vector<gmx::RVec>>>
+TEST(ThreeCenter, ListedForcesHarmonicAngleTest)
 {
-    using Base = ListedForcesBase<HarmonicAngle>;
+    checkForcesAndEnergiesWithRefData(c_InputHarmonicAngles, c_coordinatesForAngleTests);
+}
 
-public:
-    HarmonicAngleTest() :
-        Base(std::get<0>(GetParam()), std::get<1>(GetParam()), c_HarmonicAngleIndices)
-    {
-    }
-};
+TEST(TwoCenter, ListedForcesHarmonicBondTest)
+{
+    checkForcesAndEnergiesWithRefData(c_InputHarmonicBonds, c_coordinatesForBondTests);
+}
 
-TEST_P(HarmonicAngleTest, CheckListed)
+TEST(TwoCenter, ListedForcesG96BondTest)
 {
-    setSingleTolerance(1e-12, 2e-3);
-    checkForcesAndEnergies();
+    checkForcesAndEnergiesWithRefData(c_InputG96Bonds, c_coordinatesForBondTests);
 }
 
-INSTANTIATE_TEST_SUITE_P(ThreeCenter,
-                         HarmonicAngleTest,
-                         ::testing::Combine(::testing::ValuesIn(c_InputHarmonicAngle),
-                                            ::testing::ValuesIn(c_coordinatesForTests)));
+TEST(TwoCenter, ListedForcesCubicBondTest)
+{
+    checkForcesAndEnergiesWithRefData(c_InputCubicBonds, c_coordinatesForBondTests);
+}
+
+TEST(TwoCenter, ListedForcesFeneBondTest)
+{
+    checkForcesAndEnergiesWithRefData(c_InputFeneBonds, c_coordinatesForBondTests);
+}
 
 } // namespace nblib