Skip to content
Closed
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>();
}
5 changes: 1 addition & 4 deletions src/core/Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ Action::Action(const ActionOptions&ao):
}

void Action::resetStoredTimestep() {
ActionWithValue* ts = plumed.getActionSet().selectWithLabel<ActionWithValue*>("timestep");
if( ts ) {
timestep = (ts->copyOutput(0))->get();
}
timestep = plumed.getTimestep();
}

Action::~Action() {
Expand Down
8 changes: 7 additions & 1 deletion src/core/PlumedMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/core/PlumedMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypesafePtr> stopFlag_fwd;
Expand Down Expand Up @@ -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 ;
};

/////
Expand Down Expand Up @@ -618,6 +622,10 @@ bool PlumedMain::callErrorHandler(int code,const char* msg)const {
}
}

inline
double PlumedMain::getTimestep() const {
return timestep;
}

}

Expand Down
Loading