Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions regtest/basic/rt-make-settimestep-rounding/COLVAR.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! FIELDS time c
20000.000000 1.000000
#! FIELDS time c
20000.000000 1.000000
1 change: 1 addition & 0 deletions regtest/basic/rt-make-settimestep-rounding/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../scripts/test.make
1 change: 1 addition & 0 deletions regtest/basic/rt-make-settimestep-rounding/config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type=make
53 changes: 53 additions & 0 deletions regtest/basic/rt-make-settimestep-rounding/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <vector>
#include <fstream>
#include "plumed/wrapper/Plumed.h"
#include <iostream>

using namespace PLMD;

template<typename T>
void run(){

auto natoms=10;
std::vector<T> masses;
std::vector<std::array<T,3>> positions;
std::vector<std::array<T,3>> 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<natoms;i++) for(unsigned j=0;j<3;j++) positions[i][j]=0.0;
for(unsigned i=0;i<natoms;i++) for(unsigned j=0;j<3;j++) forces[i][j]=0.0;
for(unsigned i=0;i<natoms;i++) masses[i]=1.0;

p.cmd("setStep",10000000);
p.cmd("setMasses",&masses[0]);
p.cmd("setPositions",&positions[0][0]);
p.cmd("setForces",&forces[0][0]);

p.cmd("setBox",&box[0][0]);
p.cmd("setVirial",&virial[0][0]);
p.cmd("calc");

}

int main(){
run<double>();
run<float>();
}
11 changes: 11 additions & 0 deletions src/core/DataPassingObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ void DataPassingObjectTyped<T>::saveValueAsDouble( const TypesafePtr & val ) {
bvalue=double(val.template get<T>());
}

template <>
void DataPassingObjectTyped<float>::saveValueAsDouble( const TypesafePtr & val ) {
hasbackup=true;
bvalue=double(val.template get<float>());
// 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 <class T>
void DataPassingObjectTyped<T>::setValuePointer( const TypesafePtr & val, const std::vector<unsigned>& shape, const bool& isconst ) {
if( shape.size()==0 ) {
Expand Down
8 changes: 0 additions & 8 deletions src/core/PlumedMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading