From 479f13a647c911dac513fdd130f1e8508d5d8884 Mon Sep 17 00:00:00 2001 From: Giovanni Bussi Date: Tue, 19 May 2026 16:56:36 +0200 Subject: [PATCH 1/2] added test for digits in time with single precision --- .../COLVAR.reference | 4 ++ .../rt-make-settimestep-rounding/Makefile | 1 + .../basic/rt-make-settimestep-rounding/config | 1 + .../rt-make-settimestep-rounding/main.cpp | 53 +++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 regtest/basic/rt-make-settimestep-rounding/COLVAR.reference create mode 100644 regtest/basic/rt-make-settimestep-rounding/Makefile create mode 100644 regtest/basic/rt-make-settimestep-rounding/config create mode 100644 regtest/basic/rt-make-settimestep-rounding/main.cpp diff --git a/regtest/basic/rt-make-settimestep-rounding/COLVAR.reference b/regtest/basic/rt-make-settimestep-rounding/COLVAR.reference new file mode 100644 index 0000000000..110e53892d --- /dev/null +++ b/regtest/basic/rt-make-settimestep-rounding/COLVAR.reference @@ -0,0 +1,4 @@ +#! FIELDS time c + 20000.000000 1.000000 +#! FIELDS time c + 20000.000000 1.000000 diff --git a/regtest/basic/rt-make-settimestep-rounding/Makefile b/regtest/basic/rt-make-settimestep-rounding/Makefile new file mode 100644 index 0000000000..3703b27cea --- /dev/null +++ b/regtest/basic/rt-make-settimestep-rounding/Makefile @@ -0,0 +1 @@ +include ../../scripts/test.make diff --git a/regtest/basic/rt-make-settimestep-rounding/config b/regtest/basic/rt-make-settimestep-rounding/config new file mode 100644 index 0000000000..df1f95bf3e --- /dev/null +++ b/regtest/basic/rt-make-settimestep-rounding/config @@ -0,0 +1 @@ +type=make diff --git a/regtest/basic/rt-make-settimestep-rounding/main.cpp b/regtest/basic/rt-make-settimestep-rounding/main.cpp new file mode 100644 index 0000000000..187ce283d3 --- /dev/null +++ b/regtest/basic/rt-make-settimestep-rounding/main.cpp @@ -0,0 +1,53 @@ +#include +#include +#include "plumed/wrapper/Plumed.h" +#include + +using namespace PLMD; + +template +void run(){ + + auto natoms=10; + std::vector masses; + std::vector> positions; + std::vector> forces; + T box[3][3]; + T virial[3][3]; + + Plumed p; + + p.cmd("setRealPrecision",int(sizeof(T))); + p.cmd("setNatoms",natoms); + p.cmd("setTimestep",(T)0.002); + p.cmd("init"); + + p.cmd("readInputLines", + "c: CONSTANT VALUE=1.0 \n" + "PRINT ARG=c FILE=COLVAR RESTART=YES\n" + ); + + // dummy settings, not really used + positions.resize(natoms); + masses.resize(natoms); + forces.resize(natoms); + + for(unsigned i=0;i(); + run(); +} From 1ea9df0c518bc49ac29b62cbe1dee83ec431a376 Mon Sep 17 00:00:00 2001 From: Giovanni Bussi Date: Tue, 19 May 2026 17:29:26 +0200 Subject: [PATCH 2/2] Tentative fix for #1417 --- src/core/Action.cpp | 5 +---- src/core/PlumedMain.cpp | 8 +++++++- src/core/PlumedMain.h | 8 ++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/core/Action.cpp b/src/core/Action.cpp index 1cf9ac7780..30ea57f0a4 100644 --- a/src/core/Action.cpp +++ b/src/core/Action.cpp @@ -137,10 +137,7 @@ Action::Action(const ActionOptions&ao): } void Action::resetStoredTimestep() { - ActionWithValue* ts = plumed.getActionSet().selectWithLabel("timestep"); - if( ts ) { - timestep = (ts->copyOutput(0))->get(); - } + timestep = plumed.getTimestep(); } Action::~Action() { diff --git a/src/core/PlumedMain.cpp b/src/core/PlumedMain.cpp index 21ebfcfa7a..d921c9534b 100644 --- a/src/core/PlumedMain.cpp +++ b/src/core/PlumedMain.cpp @@ -737,14 +737,20 @@ void PlumedMain::cmd(std::string_view word,const TypesafePtr & val) { if( !ts->setValuePointer("timestep", val ) ) { plumed_error(); } + // store a cached double precision value + if( ts ) { + timestep = (ts->copyOutput(0))->get(); + } + // The following is to avoid extra digits in case the MD code uses floats // e.g.: float f=0.002 when converted to double becomes 0.002000000094995 // To avoid this, we keep only up to 6 significant digits after first one if( getRealPrecision()<=4 ) { Value* tstepv = ts->copyOutput(0); double magnitude=std::pow(10,std::floor(std::log10(tstepv->get()))); - tstepv->set( std::round(tstepv->get()/magnitude*1e6)/1e6*magnitude ); + timestep = std::round(tstepv->get()/magnitude*1e6)/1e6*magnitude; } + ts->updateUnits( passtools.get() ); } break; diff --git a/src/core/PlumedMain.h b/src/core/PlumedMain.h index 902ecfe10e..0ff0056186 100644 --- a/src/core/PlumedMain.h +++ b/src/core/PlumedMain.h @@ -231,6 +231,8 @@ class PlumedMain: /// Flag for parse only mode -- basically just forces restart to turn off bool doParseOnly=false; +/// Timestep saved as double + double timestep=0.0; private: /// Forward declaration. ForwardDecl stopFlag_fwd; @@ -533,6 +535,8 @@ class PlumedMain: double MDQuantityToPLUMED( const std::string& unit, const TypesafePtr & m) const ; /// Get the keywords for a particular action void getKeywordsForAction( const std::string& action, Keywords& keys ) const ; +/// Return timestep + double getTimestep() const ; }; ///// @@ -618,6 +622,10 @@ bool PlumedMain::callErrorHandler(int code,const char* msg)const { } } +inline +double PlumedMain::getTimestep() const { + return timestep; +} }