From e1220bd61c3508eaac7f9d44bd4e16e95aed2142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20H=C3=A9nin?= Date: Tue, 17 Sep 2024 11:28:54 +0200 Subject: [PATCH] Import fixes and changes from GROMACS repo Limited to the gromacs/applied_forces directory, not the library --- .../applied_forces/colvars/CMakeLists.txt | 7 +- .../colvars/colvarproxygromacs.cpp | 10 ++- .../colvars/colvarproxygromacs.h | 7 ++ .../colvars/colvarsMDModule.cpp | 10 +++ .../colvars/colvarsMDModule_stub.cpp | 34 ++++++++-- .../colvars/colvarsforceprovider.cpp | 18 +++++ .../colvars/colvarsforceprovider.h | 18 +++++ .../applied_forces/colvars/colvarsoptions.cpp | 44 +++++++++--- .../applied_forces/colvars/colvarsoptions.h | 11 ++- .../colvars/colvarspreprocessor.cpp | 11 ++- .../colvars/colvarspreprocessor.h | 9 +++ .../colvars/colvarssimulationsparameters.cpp | 2 + .../colvars/colvarssimulationsparameters.h | 5 ++ .../colvars/tests/CMakeLists.txt | 21 ++++-- .../applied_forces/colvars/tests/colvars.cpp | 41 ++++++++++- .../colvars/tests/colvarsforceprovider.cpp | 39 +++++++---- .../colvars/tests/colvarsoptions.cpp | 68 +++++++++++++------ .../colvars/tests/colvarspreprocessor.cpp | 27 +++++--- ...ionsTest_OutputDefaultValuesWhenActive.xml | 2 +- 19 files changed, 310 insertions(+), 74 deletions(-) diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/CMakeLists.txt b/gromacs/gromacs-mdmodules/applied_forces/colvars/CMakeLists.txt index 29a9e7c3b..3dc6ebeed 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/CMakeLists.txt +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/CMakeLists.txt @@ -36,6 +36,7 @@ if(NOT GMX_USE_COLVARS STREQUAL "INTERNAL") gmx_add_libgromacs_sources( colvarsMDModule_stub.cpp + colvarsoptions.cpp ) else() gmx_add_libgromacs_sources( @@ -46,8 +47,8 @@ else() colvarsforceprovider.cpp colvarproxygromacs.cpp ) +endif() - if (BUILD_TESTING) - add_subdirectory(tests) - endif() +if (BUILD_TESTING) + add_subdirectory(tests) endif() diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarproxygromacs.cpp b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarproxygromacs.cpp index 728b2ebf4..67bd56a80 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarproxygromacs.cpp +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarproxygromacs.cpp @@ -41,13 +41,21 @@ #include "colvarproxygromacs.h" +#include + #include +#include +#include "gromacs/math/vectypes.h" +#include "gromacs/random/seed.h" +#include "gromacs/utility/exceptions.h" #include "gromacs/utility/futil.h" #include "gromacs/utility/path.h" #include "colvarproxy_gromacs_version.h" +enum class PbcType : int; + namespace gmx { @@ -180,7 +188,7 @@ int ColvarProxyGromacs::backup_file(char const* filename) } else { - // General backup provedure + // General backup procedure make_backup(filename); } return COLVARS_OK; diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarproxygromacs.h b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarproxygromacs.h index 3a6633480..0e7fa471e 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarproxygromacs.h +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarproxygromacs.h @@ -42,10 +42,15 @@ #ifndef GMX_APPLIED_FORCES_COLVARPROXYGROMACS_H #define GMX_APPLIED_FORCES_COLVARPROXYGROMACS_H +#include +#include + // NOLINTBEGIN // Disabling clang-tidy checks on Colvars library code that is not called directly by GROMACS, // or is not even used at all (e.g. code used by NAMD or VMD interfaces) #include "external/colvars/colvarproxy.h" + +#include "gromacs/utility/real.h" // NOLINTEND #include "gromacs/pbcutil/pbc.h" @@ -54,6 +59,8 @@ #include "gromacs/topology/atoms.h" #include "gromacs/utility/logger.h" +enum class PbcType : int; + namespace gmx { diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsMDModule.cpp b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsMDModule.cpp index 46f8bd4b1..8d0625289 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsMDModule.cpp +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsMDModule.cpp @@ -42,12 +42,15 @@ #include "colvarsMDModule.h" +#include #include #include #include "gromacs/domdec/localatomsetmanager.h" #include "gromacs/fileio/checkpoint.h" +#include "gromacs/mdrunutility/mdmodulesnotifiers.h" #include "gromacs/mdtypes/commrec.h" +#include "gromacs/mdtypes/iforceprovider.h" #include "gromacs/mdtypes/imdmodule.h" #include "gromacs/utility/keyvaluetreebuilder.h" @@ -55,9 +58,16 @@ #include "colvarsoptions.h" #include "colvarssimulationsparameters.h" +enum class PbcType : int; +struct gmx_mtop_t; + namespace gmx { +class IMDOutputProvider; +class IMdpOptionProvider; +class KeyValueTreeObject; +class MDLogger; namespace { diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsMDModule_stub.cpp b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsMDModule_stub.cpp index 4f30fc542..af5dd97d2 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsMDModule_stub.cpp +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsMDModule_stub.cpp @@ -48,9 +48,11 @@ #include "gromacs/fileio/checkpoint.h" #include "gromacs/mdtypes/commrec.h" #include "gromacs/mdtypes/imdmodule.h" +#include "gromacs/mdtypes/imdpoptionprovider.h" #include "gromacs/utility/keyvaluetreebuilder.h" #include "colvarsMDModule.h" +#include "colvarsoptions.h" namespace gmx @@ -62,7 +64,7 @@ namespace /*! \internal * \brief Colvars module * - * Stub Implementation + * Stub Implementation in case Colvars library is not compiled */ class ColvarsMDModule final : public IMDModule { @@ -71,15 +73,39 @@ class ColvarsMDModule final : public IMDModule explicit ColvarsMDModule() = default; - void subscribeToPreProcessingNotifications(MDModulesNotifiers* /*notifier*/) override {} + void subscribeToPreProcessingNotifications(MDModulesNotifiers* /*notifier*/) override + { + // Proper exit when Colvars is activated from the mdp but has not been compiled along with GROMACS. + // In this case, Colvars config file cannot be parsed & verified and the tpr created could be flawed. + if (colvarsOptionsStub_.isActive()) + { + GMX_THROW(InternalError( + "Colvars module is activated but GROMACS has not been compiled with Colvars, " + "Colvars simulation is not possible.\n" + "Please, reconfigure GROMACS with -DGMX_USE_COLVARS=internal.\n")); + } + } void subscribeToSimulationSetupNotifications(MDModulesNotifiers* /*notifier*/) override {} - IMdpOptionProvider* mdpOptionProvider() override { return nullptr; } + IMdpOptionProvider* mdpOptionProvider() override { return &colvarsOptionsStub_; } IMDOutputProvider* outputProvider() override { return nullptr; } - void initForceProviders(ForceProviders* /*forceProviders*/) override {} + void initForceProviders(ForceProviders* /*forceProviders*/) override + { + // Proper exit when Colvars is activated from the tpr but has not been compiled along with GROMACS. + if (colvarsOptionsStub_.isActive()) + { + GMX_THROW(InternalError( + "Colvars module is activated but GROMACS has not been compiled with Colvars, " + "Colvars simulation is not possible.\n" + "Please, reconfigure GROMACS with -DGMX_USE_COLVARS=internal.\n")); + } + } + +private: + ColvarsOptions colvarsOptionsStub_; }; diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsforceprovider.cpp b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsforceprovider.cpp index fe4018501..be8434572 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsforceprovider.cpp +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsforceprovider.cpp @@ -41,22 +41,40 @@ #include "colvarsforceprovider.h" +#include +#include + +#include #include #include "external/colvars/colvars_memstream.h" +#include "gromacs/applied_forces/colvars/colvarproxygromacs.h" +#include "gromacs/compat/pointers.h" #include "gromacs/domdec/localatomsetmanager.h" #include "gromacs/fileio/checkpoint.h" #include "gromacs/gmxlib/network.h" +#include "gromacs/math/vec.h" #include "gromacs/mdlib/broadcaststructs.h" #include "gromacs/mdlib/groupcoord.h" #include "gromacs/mdtypes/commrec.h" #include "gromacs/mdtypes/enerdata.h" #include "gromacs/mdtypes/forceoutput.h" +#include "gromacs/pbcutil/pbc.h" +#include "gromacs/topology/ifunc.h" +#include "gromacs/utility/arrayref.h" +#include "gromacs/utility/basedefinitions.h" +#include "gromacs/utility/exceptions.h" +#include "gromacs/utility/keyvaluetree.h" +#include "gromacs/utility/keyvaluetreebuilder.h" +#include "gromacs/utility/smalloc.h" + +enum class PbcType : int; namespace gmx { +class MDLogger; /******************************************************************** * ColvarsForceProviderState diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsforceprovider.h b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsforceprovider.h index 67f6bed16..eb7666cba 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsforceprovider.h +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsforceprovider.h @@ -43,15 +43,33 @@ #define GMX_APPLIED_FORCES_COLVARSFORCEPROVIDER_H +#include + +#include +#include +#include +#include + #include "gromacs/domdec/localatomset.h" +#include "gromacs/math/vectypes.h" #include "gromacs/mdrunutility/mdmodulesnotifiers.h" #include "gromacs/mdtypes/iforceprovider.h" +#include "gromacs/topology/atoms.h" +#include "gromacs/utility/real.h" #include "colvarproxygromacs.h" +enum class PbcType : int; +struct t_commrec; + namespace gmx { +class KeyValueTreeObject; +class KeyValueTreeObjectBuilder; +class LocalAtomSetManager; +class MDLogger; +struct MDModulesWriteCheckpointData; /*! \internal diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsoptions.cpp b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsoptions.cpp index 6e53b5726..b6a4dc6d7 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsoptions.cpp +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsoptions.cpp @@ -34,25 +34,43 @@ /*! \internal \file * \brief * Implements options for Colvars. + * Keep a minimal ColvarsOptions implementation in the case Colvars is not activated + * to ensure compatbility (with gmx tools for example). */ #include "gmxpre.h" #include "colvarsoptions.h" +#include "config.h" + +#include + +#include #include +#include +#include "gromacs/math/arrayrefwithpadding.h" #include "gromacs/math/vec.h" #include "gromacs/options/basicoptions.h" +#include "gromacs/options/ioptionscontainerwithsections.h" #include "gromacs/options/optionsection.h" #include "gromacs/topology/mtop_util.h" #include "gromacs/utility/arrayref.h" +#include "gromacs/utility/exceptions.h" +#include "gromacs/utility/gmxassert.h" +#include "gromacs/utility/keyvaluetree.h" #include "gromacs/utility/keyvaluetreebuilder.h" #include "gromacs/utility/keyvaluetreetransform.h" #include "gromacs/utility/path.h" #include "gromacs/utility/strconvert.h" #include "gromacs/utility/textreader.h" -#include "colvarspreprocessor.h" +#if GMX_HAVE_COLVARS +# include "colvarspreprocessor.h" +#endif + +enum class PbcType : int; +struct gmx_mtop_t; namespace gmx @@ -127,11 +145,23 @@ void ColvarsOptions::initMdpOptions(IOptionsContainerWithSections* options) } +bool ColvarsOptions::isActive() const +{ + return active_; +} + + +#if GMX_HAVE_COLVARS + + void ColvarsOptions::writeInternalParametersToKvt(KeyValueTreeObjectBuilder treeBuilder) { // Copy the content of the colvars input file into a string for latter save in KVT - colvarsConfigString_ = TextReader::readFileToString(colvarsFileName_); + if (!colvarsFileName_.empty()) + { + colvarsConfigString_ = TextReader::readFileToString(colvarsFileName_); + } // Write colvars input file as a string treeBuilder.addValue(c_colvarsModuleName + "-" + c_configStringTag_, colvarsConfigString_); @@ -270,8 +300,7 @@ void ColvarsOptions::processEdrFilename(const EdrOutputFilename& filename) // Provided name should not be empty GMX_RELEASE_ASSERT(!filename.edrOutputFilename_.empty(), "Empty name for the *.edr output file"); - outputPrefix_ = - stripExtension(std::filesystem::path(filename.edrOutputFilename_).filename()).u8string(); + outputPrefix_ = stripExtension(std::filesystem::path(filename.edrOutputFilename_)).string(); } @@ -287,11 +316,6 @@ void ColvarsOptions::processTemperature(const EnsembleTemperature& temp) } } -bool ColvarsOptions::isActive() const -{ - return active_; -} - const std::string& ColvarsOptions::colvarsFileName() const { return colvarsFileName_; @@ -344,4 +368,6 @@ void ColvarsOptions::setParameters(const std::string& colvarsfile, } +#endif // GMX_HAVE_COLVARS + } // namespace gmx diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsoptions.h b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsoptions.h index 0dff277d8..91fb52eb8 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsoptions.h +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarsoptions.h @@ -50,9 +50,13 @@ #include "gromacs/mdrunutility/mdmodulesnotifiers.h" #include "gromacs/mdtypes/imdpoptionprovider.h" #include "gromacs/topology/atoms.h" +#include "gromacs/utility/arrayref.h" #include "gromacs/utility/logger.h" #include "gromacs/utility/real.h" +enum class PbcType : int; +struct gmx_mtop_t; + namespace gmx { @@ -61,6 +65,9 @@ class KeyValueTreeObject; class KeyValueTreeBuilder; struct CoordinatesAndBoxPreprocessed; struct MdRunInputFilename; +class IKeyValueTreeTransformRules; +class IOptionsContainerWithSections; +class KeyValueTreeObjectBuilder; //! Tag with name of the Colvars MDModule @@ -185,8 +192,8 @@ class ColvarsOptions final : public IMdpOptionProvider //! \} - //! Colvars config filename, default colvars.dat - std::string colvarsFileName_ = "colvars.dat"; + //! Colvars config filename + std::string colvarsFileName_; //! Colvars seed for Langevin integrator diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarspreprocessor.cpp b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarspreprocessor.cpp index f7e3c1f97..baf873603 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarspreprocessor.cpp +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarspreprocessor.cpp @@ -42,10 +42,19 @@ #include "colvarspreprocessor.h" +#include +#include +#include #include +#include "gromacs/applied_forces/colvars/colvarproxygromacs.h" +#include "gromacs/pbcutil/pbc.h" + +enum class PbcType : int; + namespace gmx { +class MDLogger; ColvarsPreProcessor::ColvarsPreProcessor(const std::string& colvarsConfigString, t_atoms atoms, @@ -77,7 +86,7 @@ std::vector ColvarsPreProcessor::getColvarsCoords() { std::vector colvarsCoords; - + colvarsCoords.reserve(atoms_ids.size()); for (const auto& atom_id : atoms_ids) { colvarsCoords.push_back(x_[atom_id]); diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarspreprocessor.h b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarspreprocessor.h index eef67412a..7c9783880 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarspreprocessor.h +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarspreprocessor.h @@ -42,14 +42,23 @@ #define GMX_APPLIED_FORCES_COLVARSPREPROCESSOR_H +#include +#include + +#include "gromacs/math/vectypes.h" +#include "gromacs/topology/atoms.h" #include "gromacs/utility/arrayref.h" #include "gromacs/utility/keyvaluetreebuilder.h" +#include "gromacs/utility/real.h" #include "colvarproxygromacs.h" +enum class PbcType : int; + namespace gmx { +class MDLogger; /*! \internal \brief * Class that read a colvars configuration file during pre-processing and diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarssimulationsparameters.cpp b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarssimulationsparameters.cpp index b6a703a2f..f0e5e825a 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarssimulationsparameters.cpp +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarssimulationsparameters.cpp @@ -43,6 +43,8 @@ #include "gromacs/topology/mtop_util.h" #include "gromacs/utility/exceptions.h" +struct gmx_mtop_t; + namespace gmx { diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarssimulationsparameters.h b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarssimulationsparameters.h index 0d8966f6a..8d6a227d7 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarssimulationsparameters.h +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/colvarssimulationsparameters.h @@ -42,12 +42,17 @@ #ifndef GMX_APPLIED_FORCES_COLVARSIMULATIONSPARAMETERS_H #define GMX_APPLIED_FORCES_COLVARSIMULATIONSPARAMETERS_H +#include + #include "gromacs/domdec/localatomsetmanager.h" #include "gromacs/mdtypes/commrec.h" +#include "gromacs/mdtypes/md_enums.h" #include "gromacs/pbcutil/pbc.h" #include "gromacs/topology/atoms.h" #include "gromacs/utility/logger.h" +struct gmx_mtop_t; + namespace gmx { diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/CMakeLists.txt b/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/CMakeLists.txt index 2e7eddcf4..c47edb16e 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/CMakeLists.txt +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/CMakeLists.txt @@ -31,14 +31,23 @@ # To help us fund GROMACS development, we humbly ask that you cite # the research papers on the package. Check out https://www.gromacs.org. -gmx_add_unit_test(ColvarsAppliedForcesUnitTest colvars_applied_forces-test - CPP_SOURCE_FILES - colvarspreprocessor.cpp - colvars.cpp - colvarsoptions.cpp - colvarsforceprovider.cpp +if(GMX_USE_COLVARS STREQUAL "INTERNAL") + gmx_add_unit_test(ColvarsAppliedForcesUnitTest colvars_applied_forces-test + CPP_SOURCE_FILES + colvars.cpp + colvarsoptions.cpp + colvarspreprocessor.cpp + colvarsforceprovider.cpp + ) +else() + gmx_add_unit_test(ColvarsAppliedForcesUnitTest colvars_applied_forces-test + CPP_SOURCE_FILES + colvars.cpp + colvarsoptions.cpp ) +endif() + target_link_libraries(colvars_applied_forces-test PRIVATE applied_forces math topology mdlib mdtypes) diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvars.cpp b/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvars.cpp index bc8a286f6..77ed904a4 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvars.cpp +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvars.cpp @@ -40,6 +40,11 @@ */ #include "gmxpre.h" +#include "config.h" + +#include +#include + #include #include "gromacs/applied_forces/colvars/colvarsMDModule.h" @@ -55,6 +60,7 @@ #include "gromacs/mdtypes/mdatom.h" #include "gromacs/options/options.h" #include "gromacs/options/treesupport.h" +#include "gromacs/utility/keyvaluetree.h" #include "gromacs/utility/keyvaluetreebuilder.h" #include "gromacs/utility/keyvaluetreetransform.h" #include "gromacs/utility/real.h" @@ -76,8 +82,7 @@ class ColvarsTest : public ::testing::Test public: void addMdpOptionColvarsActive() { - mdpValueBuilder_.rootObject().addValue("colvars-active", std::string("yes")); - mdpValueBuilder_.rootObject().addValue("colvars-filename", std::string("colvars.dat")); + mdpValueBuilder_.rootObject().addValue("colvars-active", std::string("true")); } //! build an mdp options tree that sets the options for the Colvars module @@ -109,6 +114,8 @@ class ColvarsTest : public ::testing::Test std::unique_ptr ColvarsModule_; }; +#if GMX_HAVE_COLVARS + TEST_F(ColvarsTest, ForceProviderLackingInputThrows) { // Prepare MDP inputs @@ -121,6 +128,36 @@ TEST_F(ColvarsTest, ForceProviderLackingInputThrows) EXPECT_ANY_THROW(intializeForceProviders()); } +#else + + +TEST_F(ColvarsTest, ForceProviderWithoutColvars) +{ + // Prepare MDP inputs + addMdpOptionColvarsActive(); + + // Initialize with default options + makeColvarsModuleWithSetOptions(); + + // Build the force provider without colvars + EXPECT_ANY_THROW(intializeForceProviders()); +} + +TEST_F(ColvarsTest, PreProcessingWithoutColvars) +{ + // Prepare MDP inputs + addMdpOptionColvarsActive(); + + // Initialize with default options + makeColvarsModuleWithSetOptions(); + + // Try to subscribe to notifications without colvars + MDModulesNotifiers notifiers_; + EXPECT_ANY_THROW(ColvarsModule_->subscribeToPreProcessingNotifications(¬ifiers_)); +} + +#endif // GMX_HAVE_COLVARS + } // namespace } // namespace gmx diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvarsforceprovider.cpp b/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvarsforceprovider.cpp index 2a4d3b4ab..fbf3452d2 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvarsforceprovider.cpp +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvarsforceprovider.cpp @@ -42,6 +42,10 @@ #include "gromacs/applied_forces/colvars/colvarsforceprovider.h" +#include +#include +#include +#include #include #include @@ -52,16 +56,22 @@ #include "gromacs/gmxpreprocess/grompp.h" #include "gromacs/math/paddedvector.h" #include "gromacs/math/vec.h" +#include "gromacs/math/vectypes.h" #include "gromacs/mdlib/forcerec.h" #include "gromacs/mdtypes/commrec.h" #include "gromacs/mdtypes/enerdata.h" #include "gromacs/mdtypes/forceoutput.h" #include "gromacs/mdtypes/iforceprovider.h" #include "gromacs/pbcutil/pbc.h" +#include "gromacs/topology/atoms.h" +#include "gromacs/topology/ifunc.h" #include "gromacs/topology/mtop_lookup.h" #include "gromacs/topology/mtop_util.h" #include "gromacs/topology/topology.h" #include "gromacs/utility/arrayref.h" +#include "gromacs/utility/logger.h" +#include "gromacs/utility/real.h" +#include "gromacs/utility/smalloc.h" #include "gromacs/utility/textreader.h" #include "gromacs/utility/textwriter.h" @@ -70,6 +80,8 @@ #include "testutils/testasserts.h" #include "testutils/testfilemanager.h" +enum class PbcType : int; + namespace gmx { @@ -79,27 +91,23 @@ class ColvarsForceProviderTest : public ::testing::Test void PrepareInputForceProvider(const std::string& fileName) { - gmx::test::TestFileManager fileManager_; - const std::string simData = - gmx::test::TestFileManager::getTestSimulationDatabaseDirectory().u8string(); + gmx::test::TestFileManager fileManager_; + const std::filesystem::path simData = + gmx::test::TestFileManager::getTestSimulationDatabaseDirectory(); // Generate empty mdp file const std::string mdpInputFileName = - fileManager_.getTemporaryFilePath(fileName + ".mdp").u8string(); + fileManager_.getTemporaryFilePath(fileName + ".mdp").string(); gmx::TextWriter::writeFileFromString(mdpInputFileName, ""); // Generate tpr file - const std::string tprName = fileManager_.getTemporaryFilePath(fileName + ".tpr").u8string(); + const std::string tprName = fileManager_.getTemporaryFilePath(fileName + ".tpr").string(); { gmx::test::CommandLine caller; caller.append("grompp"); caller.addOption("-f", mdpInputFileName); - caller.addOption( - "-p", - std::filesystem::path(simData).append(fileName).replace_extension(".top").u8string()); - caller.addOption( - "-c", - std::filesystem::path(simData).append(fileName).replace_extension(".gro").u8string()); + caller.addOption("-p", (simData / fileName).replace_extension(".top").string()); + caller.addOption("-c", (simData / fileName).replace_extension(".gro").string()); caller.addOption("-o", tprName); ASSERT_EQ(0, gmx_grompp(caller.argc(), caller.argv())); } @@ -116,7 +124,7 @@ class ColvarsForceProviderTest : public ::testing::Test void ColvarsConfigStringFromFile(const std::string& filename) { // Path to the sample colvars input file - std::string colvarsInputFile = gmx::test::TestFileManager::getInputFilePath(filename).u8string(); + std::filesystem::path colvarsInputFile = gmx::test::TestFileManager::getInputFilePath(filename); colvarsConfigString_ = TextReader::readFileToString(colvarsInputFile); } @@ -146,8 +154,8 @@ class ColvarsForceProviderTest : public ::testing::Test void IncorrectColvarsConfigString() { // Path to the sample colvars input file - std::string colvarsInputFile = - gmx::test::TestFileManager::getInputFilePath("colvars_sample.dat").u8string(); + std::filesystem::path colvarsInputFile = + gmx::test::TestFileManager::getInputFilePath("colvars_sample.dat"); colvarsConfigString_ = TextReader::readFileToString(colvarsInputFile); } @@ -226,7 +234,8 @@ TEST_F(ColvarsForceProviderTest, SimpleInputs) // Re-use the PreProcessorTest since the ForceProvider recalls colvars initilization and the input are identicals. - gmx::test::TestReferenceData data("ColvarsPreProcessorTest_CheckValuesFourWaters.xml"); + gmx::test::TestReferenceData data( + std::filesystem::path{ "ColvarsPreProcessorTest_CheckValuesFourWaters.xml" }); gmx::test::TestReferenceChecker checker(data.rootChecker()); // Check colvars & atoms values are correctly read diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvarsoptions.cpp b/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvarsoptions.cpp index 3d3960a3d..47b0c5799 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvarsoptions.cpp +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvarsoptions.cpp @@ -42,6 +42,9 @@ #include "gromacs/applied_forces/colvars/colvarsoptions.h" +#include "config.h" + +#include #include #include @@ -50,14 +53,18 @@ #include "gromacs/fileio/confio.h" #include "gromacs/gmxpreprocess/grompp.h" #include "gromacs/math/paddedvector.h" +#include "gromacs/math/vectypes.h" #include "gromacs/mdrunutility/mdmodulesnotifiers.h" #include "gromacs/mdtypes/inputrec.h" #include "gromacs/options/options.h" #include "gromacs/options/treesupport.h" #include "gromacs/selection/indexutil.h" +#include "gromacs/topology/atoms.h" #include "gromacs/topology/index.h" #include "gromacs/topology/mtop_util.h" #include "gromacs/topology/topology.h" +#include "gromacs/utility/arrayref.h" +#include "gromacs/utility/keyvaluetree.h" #include "gromacs/utility/keyvaluetreebuilder.h" #include "gromacs/utility/keyvaluetreemdpwriter.h" #include "gromacs/utility/keyvaluetreetransform.h" @@ -72,6 +79,8 @@ #include "testutils/testfilemanager.h" #include "testutils/testmatchers.h" +enum class PbcType : int; + namespace gmx { @@ -115,34 +124,32 @@ class ColvarsOptionsTest : public ::testing::Test return mdpValueBuilder.build(); } +#if GMX_HAVE_COLVARS + void PrepareInputColvarsPreProcessor(const std::string& fileName) { // Path to the sample colvars input file std::string colvarsConfigFile = - gmx::test::TestFileManager::getInputFilePath("colvars_sample.dat").u8string(); + gmx::test::TestFileManager::getInputFilePath("colvars_sample.dat").string(); - gmx::test::TestFileManager fileManager_; - const std::string simData = - gmx::test::TestFileManager::getTestSimulationDatabaseDirectory().u8string(); + gmx::test::TestFileManager fileManager_; + const std::filesystem::path simData = + gmx::test::TestFileManager::getTestSimulationDatabaseDirectory(); // Generate empty mdp file const std::string mdpInputFileName = - fileManager_.getTemporaryFilePath(fileName + ".mdp").u8string(); + fileManager_.getTemporaryFilePath(fileName + ".mdp").string(); gmx::TextWriter::writeFileFromString(mdpInputFileName, ""); // Generate tpr file - const std::string tprName = fileManager_.getTemporaryFilePath(fileName + ".tpr").u8string(); + const std::string tprName = fileManager_.getTemporaryFilePath(fileName + ".tpr").string(); { gmx::test::CommandLine caller; caller.append("grompp"); caller.addOption("-f", mdpInputFileName); - caller.addOption( - "-p", - std::filesystem::path(simData).append(fileName).replace_extension(".top").u8string()); - caller.addOption( - "-c", - std::filesystem::path(simData).append(fileName).replace_extension(".gro").u8string()); + caller.addOption("-p", (simData / fileName).replace_extension(".top").string()); + caller.addOption("-c", (simData / fileName).replace_extension(".gro").string()); caller.addOption("-o", tprName); ASSERT_EQ(0, gmx_grompp(caller.argc(), caller.argv())); } @@ -170,6 +177,7 @@ class ColvarsOptionsTest : public ::testing::Test done_atom(&atoms); } +#endif // GMX_HAVE_COLVARS protected: rvec* coords; @@ -178,13 +186,9 @@ class ColvarsOptionsTest : public ::testing::Test }; -TEST_F(ColvarsOptionsTest, OptionSetsActive) -{ - EXPECT_FALSE(colvarsOptions_.isActive()); - setFromMdpValues(ColvarsBuildDefaulMdpValues()); - EXPECT_TRUE(colvarsOptions_.isActive()); -} - +// The following tests should work either with or without Colvars compiled +// because GROMACS without colvars should still be able to read a tpr file with colvars mdp keywords. +// (For example, gmx dump should be able to output colvars values even without Colvars compiled.) TEST_F(ColvarsOptionsTest, OutputNoDefaultValuesWhenInactive) { // Test buildMdpOutput() @@ -253,6 +257,15 @@ TEST_F(ColvarsOptionsTest, OutputValuesWhenActive) checker.checkString(stream.toString(), "Mdp output"); } +#if GMX_HAVE_COLVARS + +TEST_F(ColvarsOptionsTest, OptionSetsActive) +{ + EXPECT_FALSE(colvarsOptions_.isActive()); + setFromMdpValues(ColvarsBuildDefaulMdpValues()); + EXPECT_TRUE(colvarsOptions_.isActive()); +} + TEST_F(ColvarsOptionsTest, InternalsToKvtAndBack) { @@ -292,4 +305,21 @@ TEST_F(ColvarsOptionsTest, InternalsToKvtAndBack) deleteInputColvarsPreProcessor(); } + +TEST_F(ColvarsOptionsTest, RetrieveEdrFilename) +{ + // Activate colvars + setFromMdpValues(ColvarsBuildInputMdpValues()); + + std::string refEdrFilename = "output/ener.edr"; + colvarsOptions_.processEdrFilename(EdrOutputFilename{ refEdrFilename }); + const std::string ref = std::filesystem::path("output/ener").make_preferred().string(); + EXPECT_EQ(ref, colvarsOptions_.colvarsOutputPrefix()); + + refEdrFilename = "sim.part1.edr"; + colvarsOptions_.processEdrFilename(EdrOutputFilename{ refEdrFilename }); + EXPECT_EQ("sim.part1", colvarsOptions_.colvarsOutputPrefix()); +} +#endif // GMX_HAVE_COLVARS + } // namespace gmx diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvarspreprocessor.cpp b/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvarspreprocessor.cpp index ea400ed5f..2210ba085 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvarspreprocessor.cpp +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/colvarspreprocessor.cpp @@ -42,7 +42,12 @@ #include "gromacs/applied_forces/colvars/colvarspreprocessor.h" +#include + +#include #include +#include +#include #include #include @@ -50,6 +55,7 @@ #include "gromacs/fileio/confio.h" #include "gromacs/gmxpreprocess/grompp.h" #include "gromacs/math/vec.h" +#include "gromacs/math/vectypes.h" #include "gromacs/pbcutil/pbc.h" #include "gromacs/topology/atoms.h" #include "gromacs/topology/mtop_lookup.h" @@ -57,6 +63,7 @@ #include "gromacs/topology/topology.h" #include "gromacs/utility/arrayref.h" #include "gromacs/utility/path.h" +#include "gromacs/utility/smalloc.h" #include "gromacs/utility/textwriter.h" #include "testutils/cmdlinetest.h" @@ -64,6 +71,8 @@ #include "testutils/testasserts.h" #include "testutils/testfilemanager.h" +enum class PbcType : int; + namespace gmx { @@ -75,26 +84,22 @@ class ColvarsPreProcessorTest : public ::testing::Test */ void makeMtopFromFile(const std::string& fileName, const std::string& mdpContent) { - const std::string simData = - gmx::test::TestFileManager::getTestSimulationDatabaseDirectory().u8string(); + const std::filesystem::path simData = + gmx::test::TestFileManager::getTestSimulationDatabaseDirectory(); // Generate empty mdp file const std::string mdpInputFileName = - fileManager_.getTemporaryFilePath(fileName + ".mdp").u8string(); + fileManager_.getTemporaryFilePath(fileName + ".mdp").string(); gmx::TextWriter::writeFileFromString(mdpInputFileName, mdpContent); // Generate tpr file - const std::string tprName = fileManager_.getTemporaryFilePath(fileName + ".tpr").u8string(); + const std::string tprName = fileManager_.getTemporaryFilePath(fileName + ".tpr").string(); { gmx::test::CommandLine caller; caller.append("grompp"); caller.addOption("-f", mdpInputFileName); - caller.addOption( - "-p", - std::filesystem::path(simData).append(fileName).replace_extension(".top").u8string()); - caller.addOption( - "-c", - std::filesystem::path(simData).append(fileName).replace_extension(".gro").u8string()); + caller.addOption("-p", (simData / fileName).replace_extension(".top").string()); + caller.addOption("-c", (simData / fileName).replace_extension(".gro").string()); caller.addOption("-o", tprName); ASSERT_EQ(0, gmx_grompp(caller.argc(), caller.argv())); } @@ -200,7 +205,7 @@ TEST_F(ColvarsPreProcessorTest, CheckNestedInputFiles) })"; // Replace index.ndx by its absolute path so Colvars can parse it. - std::string pathIndex = gmx::test::TestFileManager::getInputFilePath("index.ndx").u8string(); + std::string pathIndex = gmx::test::TestFileManager::getInputFilePath("index.ndx").string(); size_t index = colvarsInput.find(""); colvarsInput.replace(index, std::string("").length(), pathIndex); diff --git a/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/refdata/ColvarsOptionsTest_OutputDefaultValuesWhenActive.xml b/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/refdata/ColvarsOptionsTest_OutputDefaultValuesWhenActive.xml index 53f0d9c09..f41ac246a 100644 --- a/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/refdata/ColvarsOptionsTest_OutputDefaultValuesWhenActive.xml +++ b/gromacs/gromacs-mdmodules/applied_forces/colvars/tests/refdata/ColvarsOptionsTest_OutputDefaultValuesWhenActive.xml @@ -5,7 +5,7 @@ ; Colvars bias colvars-active = true ; colvars config file -colvars-configfile = colvars.dat +colvars-configfile = ; Colvars seed colvars-seed = -1