From 9280a30e7bdbdca81e567b9ea888f95bd2b33d93 Mon Sep 17 00:00:00 2001 From: Joe Jordan Date: Wed, 18 Aug 2021 09:31:18 +0000 Subject: [PATCH] Simplify nblib listed forces type tests --- api/nblib/listed_forces/bondtypes.h | 11 +- .../listed_forces/tests/listedtesthelpers.h | 79 +++++++ ...rCenter_ListedForcesProperDihedralTest.xml | 28 +++ ...eeCenter_ListedForcesHarmonicAngleTest.xml | 23 +++ .../TwoCenter_ListedForcesCubicBondTest.xml | 18 ++ .../TwoCenter_ListedForcesFeneBondTest.xml | 18 ++ .../TwoCenter_ListedForcesG96BondTest.xml | 18 ++ ...TwoCenter_ListedForcesHarmonicBondTest.xml | 18 ++ api/nblib/listed_forces/tests/typetests.cpp | 195 ++++++------------ 9 files changed, 277 insertions(+), 131 deletions(-) create mode 100644 api/nblib/listed_forces/tests/listedtesthelpers.h create mode 100644 api/nblib/listed_forces/tests/refdata/FourCenter_ListedForcesProperDihedralTest.xml create mode 100644 api/nblib/listed_forces/tests/refdata/ThreeCenter_ListedForcesHarmonicAngleTest.xml create mode 100644 api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesCubicBondTest.xml create mode 100644 api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesFeneBondTest.xml create mode 100644 api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesG96BondTest.xml create mode 100644 api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesHarmonicBondTest.xml diff --git a/api/nblib/listed_forces/bondtypes.h b/api/nblib/listed_forces/bondtypes.h index 8e9844f76a..51baf103d6 100644 --- a/api/nblib/listed_forces/bondtypes.h +++ b/api/nblib/listed_forces/bondtypes.h @@ -120,7 +120,16 @@ using HarmonicBondType = TwoParameterInteraction; +class G96BondType : public TwoParameterInteraction +{ +public: + G96BondType() = default; + //! \brief Store square of equilibrium distance + G96BondType(ForceConstant f, EquilConstant equilConstant) : + TwoParameterInteraction{ 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 index 0000000000..a8d62ab36f --- /dev/null +++ b/api/nblib/listed_forces/tests/listedtesthelpers.h @@ -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 + * \author Joe Jordan + * \author Prashanth Kanduri + * \author Sebastian Keller + */ + +#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{}>* = nullptr> +std::vector> indexVector() +{ + return { { 0, 1, 0 } }; +} + +//! \brief Creates a default vector of indices for three-centered interactions +template{}>* = nullptr> +std::vector> indexVector() +{ + return { { 0, 1, 2, 0 } }; +} + +//! \brief Creates a default vector of indices for four-centered interactions +template{}>* = nullptr> +std::vector> 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 index 0000000000..0c97923ca0 --- /dev/null +++ b/api/nblib/listed_forces/tests/refdata/FourCenter_ListedForcesProperDihedralTest.xml @@ -0,0 +1,28 @@ + + + + 15.644075054627638 + + 4 + + 0 + 3000.9773757382213 + 0 + + + -294.15662928338577 + 2953.652678308878 + -14.707831464169288 + + + 589.93598821678029 + -5922.0943245644257 + 29.49679941083901 + + + -295.77935893339446 + -32.535729482673389 + -14.788967946669722 + + + 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 index 0000000000..0a6d6ff593 --- /dev/null +++ b/api/nblib/listed_forces/tests/refdata/ThreeCenter_ListedForcesHarmonicAngleTest.xml @@ -0,0 +1,23 @@ + + + + 0.94993062982878551 + + 3 + + 21.211573973968179 + -58.969435807718355 + 11.550849737392344 + + + -68.126432793403268 + 74.829490902945352 + 28.581851681412015 + + + 46.914858819435082 + -15.860055095226993 + -40.132701418804359 + + + 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 index 0000000000..e7c441bbcd --- /dev/null +++ b/api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesCubicBondTest.xml @@ -0,0 +1,18 @@ + + + + 0.0024516697933710625 + + 2 + + 0.45585011220943999 + 0.063187144266655115 + -0.51452388902847734 + + + -0.45585011220943999 + -0.063187144266655115 + 0.51452388902847734 + + + 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 index 0000000000..766b33d6d3 --- /dev/null +++ b/api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesFeneBondTest.xml @@ -0,0 +1,18 @@ + + + + 0.063226249960230213 + + 2 + + -0.59147774272182241 + -0.081987013842628936 + 0.66760854128997849 + + + 0.59147774272182241 + 0.081987013842628936 + -0.66760854128997849 + + + 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 index 0000000000..03490e33f7 --- /dev/null +++ b/api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesG96BondTest.xml @@ -0,0 +1,18 @@ + + + + 0.0000099681125000004196 + + 2 + + -0.0045096500000000941 + -0.00062510000000001373 + 0.0050901000000001112 + + + 0.0045096500000000941 + 0.00062510000000001373 + -0.0050901000000001112 + + + 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 index 0000000000..0e92652864 --- /dev/null +++ b/api/nblib/listed_forces/tests/refdata/TwoCenter_ListedForcesHarmonicBondTest.xml @@ -0,0 +1,18 @@ + + + + 0.002172238952435819 + + 2 + + -0.97326747891234377 + -0.13490836341359236 + 1.0985395306535377 + + + 0.97326747891234377 + 0.13490836341359236 + -1.0985395306535377 + + + diff --git a/api/nblib/listed_forces/tests/typetests.cpp b/api/nblib/listed_forces/tests/typetests.cpp index 0d1121fe64..af26692cb6 100644 --- a/api/nblib/listed_forces/tests/typetests.cpp +++ b/api/nblib/listed_forces/tests/typetests.cpp @@ -41,7 +41,10 @@ * \author Prashanth Kanduri * \author Sebastian Keller */ +#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" @@ -49,153 +52,85 @@ namespace nblib { -//! Number of atoms used in these tests. -constexpr int c_numAtoms = 4; +//! Coordinates for testing +static const std::vector 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 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> 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> c_HarmonicBondIndices{ { 0, 1, 0 }, { 1, 2, 0 }, { 2, 3, 0 } }; -std::vector> c_InputHarmonicBond = { { HarmonicBondType(500, 0.15) } }; - -// Parameters for harmonic angles -std::vector> c_HarmonicAngleIndices{ { 0, 1, 2, 0 }, { 1, 2, 3, 0 } }; -std::vector> c_InputHarmonicAngle = { { HarmonicAngle(50.0, Degrees(100)) } }; - -//! Function types for testing dihedrals. Add new terms at the end. -std::vector> 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> c_InputDihs = { { RyckaertBellemanDihedral({ -7.35, 13.6, 8.4, -16.7, 1.3, 12.4 }) } }; - -} // namespace - -template -class ListedForcesBase -{ -public: - std::vector input_; - std::vector x_; - std::vector> indices_; - PbcHolder pbcHolder_; - gmx::test::TestReferenceData refData_; - gmx::test::TestReferenceChecker checker_; - std::vector forces_; - real energy_; - - ListedForcesBase(std::vector input, - std::vector coordinates, - std::vector> 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>(indices_), - gmx::ArrayRef(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, - public testing::TestWithParam, std::vector>> -{ - using Base = ListedForcesBase; +//! Coordinates for testing bonds +static const std::vector 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 c_InputHarmonicBonds = { { HarmonicBondType(500.0, 0.15) } }; -TEST_P(ProperDihedralTest, CheckListed) -{ - checkForcesAndEnergies(); -} +//! Function types for testing G96 bonds +static const std::vector 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 c_InputCubicBonds = { { CubicBondType(50.0, 2.0, 0.16) } }; -class HarmonicBondTest : - public ListedForcesBase, - public testing::TestWithParam, std::vector>> -{ - using Base = ListedForcesBase; +//! Function types for testing FENE bonds +static const std::vector 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 c_InputHarmonicAngles = { { HarmonicAngle(50.0, Degrees(100)) } }; -TEST_P(HarmonicBondTest, CheckListed) +//! Function types for testing dihedrals +static const std::vector c_InputDihs = { { ProperDihedral(Degrees(-105.0), 15.0, 2) } }; + +template{}>* = nullptr> +void checkForcesAndEnergiesWithRefData(std::vector input, gmx::ArrayRef x) { - checkForcesAndEnergies(); + auto indices = indexVector(); + PbcHolder pbcHolder(PbcType::Xyz, Box(1.5)); + test::RefDataChecker refDataChecker(1e-4); + std::vector forces(x.size(), gmx::RVec{ 0, 0, 0 }); + + auto energy = computeForces(gmx::ArrayRef>(indices), + gmx::ArrayRef(input), + x, + &forces, + pbcHolder); + + refDataChecker.testReal(energy, "Epot"); + refDataChecker.testArrays(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, - public testing::TestWithParam, std::vector>> +TEST(ThreeCenter, ListedForcesHarmonicAngleTest) { - using Base = ListedForcesBase; + 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 -- 2.22.0