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(); +} diff --git a/src/core/DataPassingObject.cpp b/src/core/DataPassingObject.cpp index a92202004b..d1bb720e47 100644 --- a/src/core/DataPassingObject.cpp +++ b/src/core/DataPassingObject.cpp @@ -94,6 +94,17 @@ void DataPassingObjectTyped::saveValueAsDouble( const TypesafePtr & val ) { bvalue=double(val.template get()); } +template <> +void DataPassingObjectTyped::saveValueAsDouble( const TypesafePtr & val ) { + hasbackup=true; + bvalue=double(val.template 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 + double magnitude=std::pow(10,std::floor(std::log10(bvalue))); + bvalue = std::round(bvalue/magnitude*1e6)/1e6*magnitude; +} + template void DataPassingObjectTyped::setValuePointer( const TypesafePtr & val, const std::vector& shape, const bool& isconst ) { if( shape.size()==0 ) { diff --git a/src/core/PlumedMain.cpp b/src/core/PlumedMain.cpp index 21ebfcfa7a..af72978504 100644 --- a/src/core/PlumedMain.cpp +++ b/src/core/PlumedMain.cpp @@ -737,14 +737,6 @@ void PlumedMain::cmd(std::string_view word,const TypesafePtr & val) { if( !ts->setValuePointer("timestep", val ) ) { plumed_error(); } - // 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 ); - } ts->updateUnits( passtools.get() ); } break;