From 0bedff1ce0e47d8c173701b0250d1126f0d87c59 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 3 May 2022 13:15:52 -0400 Subject: [PATCH] implement set_time command --- doc/src/Commands_all.rst | 1 + doc/src/commands_list.rst | 1 + doc/src/set_time.rst | 44 ++++++++++++++++++++++ src/input.cpp | 11 ++++++ src/input.h | 1 + src/lmprestart.h | 5 ++- src/read_restart.cpp | 29 +++++++------- src/update.h | 2 +- src/write_restart.cpp | 20 +++++----- unittest/commands/test_simple_commands.cpp | 34 +++++++++++++++++ 10 files changed, 121 insertions(+), 27 deletions(-) create mode 100644 doc/src/set_time.rst diff --git a/doc/src/Commands_all.rst b/doc/src/Commands_all.rst index bc25d0c0e0..7b972af8f0 100644 --- a/doc/src/Commands_all.rst +++ b/doc/src/Commands_all.rst @@ -105,6 +105,7 @@ An alphabetic list of all general LAMMPS commands. * :doc:`run ` * :doc:`run_style ` * :doc:`set ` + * :doc:`set_time ` * :doc:`shell ` * :doc:`special_bonds ` * :doc:`suffix ` diff --git a/doc/src/commands_list.rst b/doc/src/commands_list.rst index fdfb6d1d59..69ed07cb45 100644 --- a/doc/src/commands_list.rst +++ b/doc/src/commands_list.rst @@ -96,6 +96,7 @@ Commands run run_style set + set_time shell special_bonds suffix diff --git a/doc/src/set_time.rst b/doc/src/set_time.rst new file mode 100644 index 0000000000..0c0a113dbf --- /dev/null +++ b/doc/src/set_time.rst @@ -0,0 +1,44 @@ +.. index:: set_time + +set_time command +================ + +Syntax +"""""" + +.. parsed-literal:: + + set_time t + +* t = current simulation time (time units) + +Examples +"""""""" + +.. code-block:: LAMMPS + + time 0.0 + time 10.5 + +Description +""""""""""" + +Set the current accumulated simulation time for subsequent molecular +dynamics simulations. See the :doc:`units ` command for the time +units associated with each choice of units that LAMMPS supports. + + +Restrictions +"""""""""""" + none + +Related commands +"""""""""""""""" + +:doc:`reset_timestep `, :doc:`timestep `, +:doc:`fix dt/reset `, :doc:`units ` + +Default +""""""" + +0.0 at the beginning of the first run. diff --git a/src/input.cpp b/src/input.cpp index a28bd48296..c0ca056a7f 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -773,6 +773,7 @@ int Input::execute_command() else if (!strcmp(command,"reset_timestep")) reset_timestep(); else if (!strcmp(command,"restart")) restart(); else if (!strcmp(command,"run_style")) run_style(); + else if (!strcmp(command,"set_time")) set_time(); else if (!strcmp(command,"special_bonds")) special_bonds(); else if (!strcmp(command,"suffix")) suffix(); else if (!strcmp(command,"thermo")) thermo(); @@ -1772,6 +1773,16 @@ void Input::run_style() /* ---------------------------------------------------------------------- */ +void Input::set_time() +{ + if (narg != 1) error->all(FLERR,"Illegal set_time command"); + + update->atime = utils::numeric(FLERR,arg[0],false,lmp); + update->atimestep = update->ntimestep; +} + +/* ---------------------------------------------------------------------- */ + void Input::special_bonds() { // store 1-3,1-4 and dihedral/extra flag values before change diff --git a/src/input.h b/src/input.h index f54d2ea4e5..5fe8c64c49 100644 --- a/src/input.h +++ b/src/input.h @@ -131,6 +131,7 @@ class Input : protected Pointers { void reset_timestep(); void restart(); void run_style(); + void set_time(); void special_bonds(); void suffix(); void thermo(); diff --git a/src/lmprestart.h b/src/lmprestart.h index 526b9676bf..7dc9271755 100644 --- a/src/lmprestart.h +++ b/src/lmprestart.h @@ -17,7 +17,7 @@ #define MAGIC_STRING "LammpS RestartT" #define ENDIAN 0x0001 #define ENDIANSWAP 0x1000 -#define FORMAT_REVISION 2 +#define FORMAT_REVISION 3 enum{VERSION,SMALLINT,TAGINT,BIGINT, UNITS,NTIMESTEP,DIMENSION,NPROCS,PROCGRID, @@ -37,7 +37,8 @@ enum{VERSION,SMALLINT,TAGINT,BIGINT, COMM_MODE,COMM_CUTOFF,COMM_VEL,NO_PAIR, EXTRA_BOND_PER_ATOM,EXTRA_ANGLE_PER_ATOM,EXTRA_DIHEDRAL_PER_ATOM, EXTRA_IMPROPER_PER_ATOM,EXTRA_SPECIAL_PER_ATOM,ATOM_MAXSPECIAL, - NELLIPSOIDS,NLINES,NTRIS,NBODIES}; + NELLIPSOIDS,NLINES,NTRIS,NBODIES, + ATIME,ATIMESTEP}; #define LB_FACTOR 1.1 diff --git a/src/read_restart.cpp b/src/read_restart.cpp index 645f0c1d6d..b42dcce1ca 100644 --- a/src/read_restart.cpp +++ b/src/read_restart.cpp @@ -594,21 +594,18 @@ void ReadRestart::header() if (flag == VERSION) { char *version = read_string(); if (me == 0) - utils::logmesg(lmp," restart file = {}, LAMMPS = {}\n", - version,lmp->version); + utils::logmesg(lmp," restart file = {}, LAMMPS = {}\n", version, lmp->version); delete[] version; // we have no forward compatibility, thus exit with error if (revision > FORMAT_REVISION) - error->all(FLERR,"Restart file format revision incompatible " - "with current LAMMPS version"); + error->all(FLERR,"Restart file format revision incompatible with current LAMMPS version"); // warn when attempting to read older format revision if ((me == 0) && (revision < FORMAT_REVISION)) - error->warning(FLERR,"Old restart file format revision. " - "Switching to compatibility mode."); + error->warning(FLERR,"Old restart file format revision. Switching to compatibility mode."); // check lmptype.h sizes, error if different @@ -654,8 +651,8 @@ void ReadRestart::header() } else if (flag == NPROCS) { nprocs_file = read_int(); if (nprocs_file != comm->nprocs && me == 0) - error->warning(FLERR,"Restart file used different # of processors: " - "{} vs. {}",nprocs_file,comm->nprocs); + error->warning(FLERR,"Restart file used different # of processors: {} vs. {}", + nprocs_file,comm->nprocs); // don't set procgrid, warn if different @@ -681,16 +678,14 @@ void ReadRestart::header() int newton_pair_file = read_int(); if (force->newton_pair != 1) { if (newton_pair_file != force->newton_pair && me == 0) - error->warning(FLERR, - "Restart file used different newton pair setting, " + error->warning(FLERR, "Restart file used different newton pair setting, " "using input script value"); } } else if (flag == NEWTON_BOND) { int newton_bond_file = read_int(); if (force->newton_bond != 1) { if (newton_bond_file != force->newton_bond && me == 0) - error->warning(FLERR, - "Restart file used different newton bond setting, " + error->warning(FLERR, "Restart file used different newton bond setting, " "using restart file value"); } force->newton_bond = newton_bond_file; @@ -721,8 +716,7 @@ void ReadRestart::header() boundary[2][0] != domain->boundary[2][0] || boundary[2][1] != domain->boundary[2][1]) { if (me == 0) - error->warning(FLERR, - "Restart file used different boundary settings, " + error->warning(FLERR, "Restart file used different boundary settings, " "using restart file values"); } } @@ -864,6 +858,13 @@ void ReadRestart::header() } else if (flag == NBODIES) { atom->nbodies = read_bigint(); + } else if (flag == ATIMESTEP) { + update->atimestep = read_bigint(); + } else if (flag == ATIME) { + update->atime = read_double(); + + // set dimension from restart file + // for backward compatibility } else if (flag == EXTRA_SPECIAL_PER_ATOM) { force->special_extra = read_int(); diff --git a/src/update.h b/src/update.h index 8df82eece3..b88e70134c 100644 --- a/src/update.h +++ b/src/update.h @@ -27,7 +27,7 @@ class Update : protected Pointers { bigint ntimestep; // current step (dynamics or min iterations) int nsteps; // # of steps to run (dynamics or min iter) int whichflag; // 0 for unset, 1 for dynamics, 2 for min - double atime; // simulation time at atime_step + double atime; // simulation time at atimestep bigint atimestep; // last timestep atime was updated bigint firststep, laststep; // 1st & last step of this run bigint beginstep, endstep; // 1st and last step of multiple runs diff --git a/src/write_restart.cpp b/src/write_restart.cpp index 41532feaf3..bd21f65a3f 100644 --- a/src/write_restart.cpp +++ b/src/write_restart.cpp @@ -118,8 +118,7 @@ void WriteRestart::command(int narg, char **arg) /* ---------------------------------------------------------------------- */ -void WriteRestart::multiproc_options(int multiproc_caller, int mpiioflag_caller, - int narg, char **arg) +void WriteRestart::multiproc_options(int multiproc_caller, int mpiioflag_caller, int narg, char **arg) { multiproc = multiproc_caller; mpiioflag = mpiioflag_caller; @@ -127,14 +126,12 @@ void WriteRestart::multiproc_options(int multiproc_caller, int mpiioflag_caller, // error checks if (multiproc && mpiioflag) - error->all(FLERR, - "Restart file MPI-IO output not allowed with % in filename"); + error->all(FLERR,"Restart file MPI-IO output not allowed with % in filename"); if (mpiioflag) { mpiio = new RestartMPIIO(lmp); if (!mpiio->mpiio_exists) - error->all(FLERR,"Writing to MPI-IO filename when " - "MPIIO package is not installed"); + error->all(FLERR,"Writing to MPI-IO filename when MPIIO package is not installed"); } // defaults for multiproc file writing @@ -158,8 +155,7 @@ void WriteRestart::multiproc_options(int multiproc_caller, int mpiioflag_caller, if (strcmp(arg[iarg],"fileper") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal write_restart command"); if (!multiproc) - error->all(FLERR,"Cannot use write_restart fileper " - "without % in restart file name"); + error->all(FLERR,"Cannot use write_restart fileper without % in restart file name"); int nper = utils::inumeric(FLERR,arg[iarg+1],false,lmp); if (nper <= 0) error->all(FLERR,"Illegal write_restart command"); @@ -176,8 +172,7 @@ void WriteRestart::multiproc_options(int multiproc_caller, int mpiioflag_caller, } else if (strcmp(arg[iarg],"nfile") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal write_restart command"); if (!multiproc) - error->all(FLERR,"Cannot use write_restart nfile " - "without % in restart file name"); + error->all(FLERR,"Cannot use write_restart nfile without % in restart file name"); int nfile = utils::inumeric(FLERR,arg[iarg+1],false,lmp); if (nfile <= 0) error->all(FLERR,"Illegal write_restart command"); nfile = MIN(nfile,nprocs); @@ -510,6 +505,11 @@ void WriteRestart::header() write_bigint(NTRIS,atom->ntris); write_bigint(NBODIES,atom->nbodies); + // write out current simulation time. added 3 May 2022 + + write_bigint(ATIMESTEP,update->atimestep); + write_double(ATIME,update->atime); + // -1 flag signals end of header int flag = -1; diff --git a/unittest/commands/test_simple_commands.cpp b/unittest/commands/test_simple_commands.cpp index acbade5ad8..a407da32a2 100644 --- a/unittest/commands/test_simple_commands.cpp +++ b/unittest/commands/test_simple_commands.cpp @@ -241,6 +241,40 @@ TEST_F(SimpleCommandsTest, ResetTimestep) TEST_FAILURE(".*ERROR: Expected integer .*", command("reset_timestep xxx");); } +TEST_F(SimpleCommandsTest, SetTime) +{ + ASSERT_EQ(lmp->update->ntimestep, 0); + ASSERT_EQ(lmp->update->atimestep, 0); + ASSERT_DOUBLE_EQ(lmp->update->atime, 0.0); + + BEGIN_HIDE_OUTPUT(); + command("set_time 10.0"); + END_HIDE_OUTPUT(); + ASSERT_EQ(lmp->update->atimestep, 0); + ASSERT_DOUBLE_EQ(lmp->update->atime, 10.0); + + BEGIN_HIDE_OUTPUT(); + command("reset_timestep 10"); + command("set_time 10.0"); + END_HIDE_OUTPUT(); + ASSERT_EQ(lmp->update->ntimestep, 10); + ASSERT_EQ(lmp->update->atimestep, 10); + ASSERT_DOUBLE_EQ(lmp->update->atime, 10.0); + + BEGIN_HIDE_OUTPUT(); + command("reset_timestep 0"); + command("set_time 10.0"); + command("reset_timestep 10"); + END_HIDE_OUTPUT(); + ASSERT_EQ(lmp->update->ntimestep, 10); + ASSERT_EQ(lmp->update->atimestep, 10); + ASSERT_DOUBLE_EQ(lmp->update->atime, 10.0 + lmp->update->dt * 10); + + TEST_FAILURE(".*ERROR: Illegal set_time .*", command("set_time");); + TEST_FAILURE(".*ERROR: Illegal set_time .*", command("set_time 10 10");); + TEST_FAILURE(".*ERROR: Expected floating .*", command("set_time xxx");); +} + TEST_F(SimpleCommandsTest, Suffix) { ASSERT_EQ(lmp->suffix_enable, 0);