From e77e1f6b8ea690b7e551907d27157d20b185e21b Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 14 Oct 2016 19:40:51 -0400 Subject: [PATCH 1/6] replace complex solution to implement a soft exit with simpler one --- src/timer.cpp | 10 ---------- src/timer.h | 5 ++--- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/src/timer.cpp b/src/timer.cpp index 583c6a2b3f..8711231889 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -98,7 +98,6 @@ Timer::Timer(LAMMPS *lmp) : Pointers(lmp) _timeout = -1.0; _checkfreq = 10; _nextcheck = -1; - _laststep = -1; this->_stamp(RESET); } @@ -216,7 +215,6 @@ void Timer::set_wall(enum ttype which, double newtime) void Timer::init_timeout() { - _laststep = -1; if (_timeout < 0) _nextcheck = -1; else @@ -249,14 +247,6 @@ void Timer::print_timeout(FILE *fp) /* ---------------------------------------------------------------------- */ -void Timer::force_timeout() -{ - _timeout = 0.0; - _nextcheck = _laststep + 1; -} - -/* ---------------------------------------------------------------------- */ - bool Timer::_check_timeout() { double walltime = MPI_Wtime() - timeout_start; diff --git a/src/timer.h b/src/timer.h index ff9c66db7c..9afe6d531d 100644 --- a/src/timer.h +++ b/src/timer.h @@ -64,7 +64,7 @@ class Timer : protected Pointers { void init_timeout(); // trigger enforced timeout - void force_timeout(); + void force_timeout() { _timeout = 0.0; } // get remaining time in seconds. 0.0 if inactive, negative if expired double get_timeout_remain(); @@ -75,7 +75,7 @@ class Timer : protected Pointers { // check for timeout. inline wrapper around internal // function to reduce overhead in case there is no check. bool check_timeout(int step) { - _laststep = step; + if (_timeout == 0.0) return true; if (_nextcheck != step) return false; else return _check_timeout(); } @@ -93,7 +93,6 @@ class Timer : protected Pointers { int _timeout; // max allowed wall time in seconds. infinity if negative int _checkfreq; // frequency of timeout checking int _nextcheck; // loop number of next timeout check - int _laststep; // loop number of last iteration // update one specific timer array void _stamp(enum ttype); From e7825ba21a0bd6dd0a6e60f04544481de08cce0b Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sat, 15 Oct 2016 07:30:07 -0400 Subject: [PATCH 2/6] import fix halt prototype code with corrections for soft exit --- src/fix_halt.cpp | 177 +++++++++++++++++++++++++++++++++++++++++++++++ src/fix_halt.h | 61 ++++++++++++++++ 2 files changed, 238 insertions(+) create mode 100644 src/fix_halt.cpp create mode 100644 src/fix_halt.h diff --git a/src/fix_halt.cpp b/src/fix_halt.cpp new file mode 100644 index 0000000000..68fd0645d1 --- /dev/null +++ b/src/fix_halt.cpp @@ -0,0 +1,177 @@ +/* ---------------------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + http://lammps.sandia.gov, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +#include +#include +#include "fix_halt.h" +#include "update.h" +#include "force.h" +#include "input.h" +#include "modify.h" +#include "comm.h" +#include "timer.h" +#include "variable.h" +#include "error.h" + +using namespace LAMMPS_NS; +using namespace FixConst; + +enum{BONDMAX,VARIABLE}; +enum{LT,LE,GT,GE,EQ,NEQ,XOR}; +enum{SOFT,HARD}; + +/* ---------------------------------------------------------------------- */ + +FixHalt::FixHalt(LAMMPS *lmp, int narg, char **arg) : + Fix(lmp, narg, arg), idvar(NULL) +{ + if (narg < 7) error->all(FLERR,"Illegal fix halt command"); + nevery = force->inumeric(FLERR,arg[3]); + if (nevery <= 0) error->all(FLERR,"Illegal fix halt command"); + + // comparison args + + idvar = NULL; + + if (strcmp(arg[4],"bondmax") == 0) attribute = BONDMAX; + else if (strncmp(arg[4],"v_",2) == 0) { + attribute = VARIABLE; + int n = strlen(arg[4]); + idvar = new char[n]; + strcpy(idvar,&arg[4][2]); + ivar = input->variable->find(idvar); + if (ivar < 0) error->all(FLERR,"Could not find fix halt variable name"); + if (input->variable->equalstyle(ivar) == 0) + error->all(FLERR,"Fix halt variable is not equal-style variable"); + } else error->all(FLERR,"Invalid fix halt attribute"); + + if (strcmp(arg[5],"<") == 0) operation = LT; + else if (strcmp(arg[5],"<=") == 0) operation = LE; + else if (strcmp(arg[5],">") == 0) operation = GT; + else if (strcmp(arg[5],">=") == 0) operation = GE; + else if (strcmp(arg[5],"==") == 0) operation = EQ; + else if (strcmp(arg[5],"!=") == 0) operation = NEQ; + else if (strcmp(arg[5],"|^") == 0) operation = XOR; + else error->all(FLERR,"Invalid fix halt operator"); + + value = force->numeric(FLERR,arg[6]); + + // parse optional args + + eflag = SOFT; + + int iarg = 7; + while (iarg < narg) { + if (strcmp(arg[iarg],"error") == 0) { + if (iarg+2 > narg) error->all(FLERR,"Illegal fix halt command"); + if (strcmp(arg[iarg+1],"soft") == 0) eflag = SOFT; + else if (strcmp(arg[iarg+1],"hard") == 0) eflag = HARD; + else error->all(FLERR,"Illegal fix halt command"); + iarg += 2; + } else error->all(FLERR,"Illegal fix halt command"); + } + + // add nfirst to all computes that store invocation times + // since don't know a priori which are invoked via variables by this fix + // once in end_of_step() can set timestep for ones actually invoked + + if (attribute == VARIABLE) { + const bigint nfirst = (update->ntimestep/nevery)*nevery + nevery; + modify->addstep_compute_all(nfirst); + } +} + +/* ---------------------------------------------------------------------- */ + +FixHalt::~FixHalt() +{ + delete [] idvar; +} + +/* ---------------------------------------------------------------------- */ + +int FixHalt::setmask() +{ + int mask = 0; + mask |= END_OF_STEP; + return mask; +} + +/* ---------------------------------------------------------------------- */ + +void FixHalt::init() +{ + // set ivar from current variable list + + ivar = input->variable->find(idvar); + if (ivar < 0) error->all(FLERR,"Could not find fix halt variable name"); + if (input->variable->equalstyle(ivar) == 0) + error->all(FLERR,"Fix halt variable is not equal-style variable"); +} + +/* ---------------------------------------------------------------------- */ + +void FixHalt::end_of_step() +{ + // make a copy of string to work on + // substitute for $ variables (no printing) + // append a newline and print final copy + // variable evaluation may invoke computes so wrap with clear/add + + double attvalue; + + if (attribute == BONDMAX) attvalue = bondmax(); + else { + modify->clearstep_compute(); + attvalue = input->variable->compute_equal(ivar); + modify->addstep_compute(update->ntimestep + nevery); + } + + // check if halt is triggered, else just return + + if (operation == LT) { + if (attvalue >= value) return; + } else if (operation == LE) { + if (attvalue > value) return; + } else if (operation == GT) { + if (attvalue <= value) return; + } else if (operation == GE) { + if (attvalue < value) return; + } else if (operation == EQ) { + if (attvalue != value) return; + } else if (operation == NEQ) { + if (attvalue == value) return; + } else if (operation == XOR) { + if ((attvalue == 0.0 && value == 0.0) || + (attvalue != 0.0 && value != 0.0)) return; + } + + // hard halt -> exit LAMMPS + // also print ID of fix halt in case multiple ? + + if (eflag == HARD) error->all(FLERR,"Fix halt condition triggered"); + else { // eflag == SOFT + if (comm->me == 0) error->message(FLERR,"Fix halt condition triggered"); + timer->force_timeout(); + } +} + +/* ---------------------------------------------------------------------- */ + +double FixHalt::bondmax() +{ + // could add custom keyword methods + // e.g. one to compute max bond length, with no sqrt() + + return 0.0; +} diff --git a/src/fix_halt.h b/src/fix_halt.h new file mode 100644 index 0000000000..8173be1a6b --- /dev/null +++ b/src/fix_halt.h @@ -0,0 +1,61 @@ +/* -*- c++ -*- ---------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + http://lammps.sandia.gov, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +#ifdef FIX_CLASS + +FixStyle(halt,FixHalt) + +#else + +#ifndef LMP_FIX_HALT_H +#define LMP_FIX_HALT_H + +#include +#include "fix.h" + +namespace LAMMPS_NS { + +class FixHalt : public Fix { + public: + FixHalt(class LAMMPS *, int, char **); + ~FixHalt(); + int setmask(); + void init(); + void end_of_step(); + + private: + int attribute,operation,eflag,ivar; + double attvalue,value; + char *idvar; + + double bondmax(); +}; + +} + +#endif +#endif + +/* ERROR/WARNING messages: + +E: Illegal ... command + +Self-explanatory. Check the input script syntax and compare to the +documentation for the command. You can use -echo screen as a +command-line option when running LAMMPS to see the offending line. + +E: Cannot open fix print file %s + +The output file generated by the fix print command cannot be opened + +*/ From 954d536826ee293a1245ffd96e1360c75530cefb Mon Sep 17 00:00:00 2001 From: Steve Plimpton Date: Tue, 18 Oct 2016 09:27:01 -0600 Subject: [PATCH 3/6] added bondmax method to fix halt --- src/fix_halt.cpp | 74 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/src/fix_halt.cpp b/src/fix_halt.cpp index 68fd0645d1..6a64e4ccd1 100644 --- a/src/fix_halt.cpp +++ b/src/fix_halt.cpp @@ -11,16 +11,20 @@ See the README file in the top-level LAMMPS directory. ------------------------------------------------------------------------- */ +#include #include #include #include "fix_halt.h" #include "update.h" #include "force.h" +#include "update.h" #include "input.h" +#include "variable.h" +#include "atom.h" +#include "neighbor.h" #include "modify.h" #include "comm.h" #include "timer.h" -#include "variable.h" #include "error.h" using namespace LAMMPS_NS; @@ -28,7 +32,7 @@ using namespace FixConst; enum{BONDMAX,VARIABLE}; enum{LT,LE,GT,GE,EQ,NEQ,XOR}; -enum{SOFT,HARD}; +enum{HARD,SOFT,CONTINUE}; /* ---------------------------------------------------------------------- */ @@ -74,8 +78,9 @@ FixHalt::FixHalt(LAMMPS *lmp, int narg, char **arg) : while (iarg < narg) { if (strcmp(arg[iarg],"error") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix halt command"); - if (strcmp(arg[iarg+1],"soft") == 0) eflag = SOFT; - else if (strcmp(arg[iarg+1],"hard") == 0) eflag = HARD; + if (strcmp(arg[iarg+1],"hard") == 0) eflag = HARD; + else if (strcmp(arg[iarg+1],"soft") == 0) eflag = SOFT; + else if (strcmp(arg[iarg+1],"continue") == 0) eflag = CONTINUE; else error->all(FLERR,"Illegal fix halt command"); iarg += 2; } else error->all(FLERR,"Illegal fix halt command"); @@ -113,19 +118,18 @@ void FixHalt::init() { // set ivar from current variable list - ivar = input->variable->find(idvar); - if (ivar < 0) error->all(FLERR,"Could not find fix halt variable name"); - if (input->variable->equalstyle(ivar) == 0) - error->all(FLERR,"Fix halt variable is not equal-style variable"); + if (attribute == VARIABLE) { + ivar = input->variable->find(idvar); + if (ivar < 0) error->all(FLERR,"Could not find fix halt variable name"); + if (input->variable->equalstyle(ivar) == 0) + error->all(FLERR,"Fix halt variable is not equal-style variable"); + } } /* ---------------------------------------------------------------------- */ void FixHalt::end_of_step() { - // make a copy of string to work on - // substitute for $ variables (no printing) - // append a newline and print final copy // variable evaluation may invoke computes so wrap with clear/add double attvalue; @@ -157,21 +161,53 @@ void FixHalt::end_of_step() } // hard halt -> exit LAMMPS - // also print ID of fix halt in case multiple ? + // soft halt -> trigger timer to break from run loop + // continue halt -> trigger time to exit only this run loop + // print message with ID of fix halt in case multiple instances - if (eflag == HARD) error->all(FLERR,"Fix halt condition triggered"); - else { // eflag == SOFT - if (comm->me == 0) error->message(FLERR,"Fix halt condition triggered"); + char str[128]; + sprintf(str,"Fix halt %s condition met on step %ld with value %g", + id,update->ntimestep,attvalue); + + if (eflag == HARD) error->all(FLERR,str); + else if (eflag == SOFT) { + if (comm->me == 0) error->message(FLERR,str); timer->force_timeout(); + } else if (eflag == CONTINUE) { + // exit this run, but not subsequent ones } } -/* ---------------------------------------------------------------------- */ +/* ---------------------------------------------------------------------- + compute max length of any bond using Neighbor bondlist for each proc +------------------------------------------------------------------------- */ double FixHalt::bondmax() { - // could add custom keyword methods - // e.g. one to compute max bond length, with no sqrt() + double **x = atom->x; + double **f = atom->f; + int **bondlist = neighbor->bondlist; + int nbondlist = neighbor->nbondlist; + int nlocal = atom->nlocal; - return 0.0; + int i1,i2; + double delx,dely,delz,rsq; + double maxone = 0.0; + + for (int n = 0; n < nbondlist; n++) { + i1 = bondlist[n][0]; + i2 = bondlist[n][1]; + + delx = x[i1][0] - x[i2][0]; + dely = x[i1][1] - x[i2][1]; + delz = x[i1][2] - x[i2][2]; + + rsq = delx*delx + dely*dely + delz*delz; + maxone = MAX(rsq,maxone); + } + + double maxall; + MPI_Allreduce(&maxone,&maxall,1,MPI_DOUBLE,MPI_MAX,world); + + return sqrt(maxall); } From 394c3bb504530684331c4df0094621922a62c3e6 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 18 Oct 2016 12:20:49 -0400 Subject: [PATCH 4/6] add Timer::reset_timeout() API, that allows restoring the previous timeout setting after a forced timeout --- src/timer.cpp | 4 +++- src/timer.h | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/timer.cpp b/src/timer.cpp index 8711231889..9c1aa87943 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -95,7 +95,8 @@ Timer::Timer(LAMMPS *lmp) : Pointers(lmp) { _level = NORMAL; _sync = OFF; - _timeout = -1.0; + _timeout = -1; + _s_timeout = -1; _checkfreq = 10; _nextcheck = -1; this->_stamp(RESET); @@ -215,6 +216,7 @@ void Timer::set_wall(enum ttype which, double newtime) void Timer::init_timeout() { + _s_timeout = _timeout; if (_timeout < 0) _nextcheck = -1; else diff --git a/src/timer.h b/src/timer.h index 9afe6d531d..6159180faf 100644 --- a/src/timer.h +++ b/src/timer.h @@ -66,6 +66,9 @@ class Timer : protected Pointers { // trigger enforced timeout void force_timeout() { _timeout = 0.0; } + // restore original timeout setting after enforce timeout + void reset_timeout() { _timeout = _s_timeout; } + // get remaining time in seconds. 0.0 if inactive, negative if expired double get_timeout_remain(); @@ -91,6 +94,7 @@ class Timer : protected Pointers { int _level; // level of detail: off=0,loop=1,normal=2,full=3 int _sync; // if nonzero, synchronize tasks before setting the timer int _timeout; // max allowed wall time in seconds. infinity if negative + int _s_timeout; // copy of timeout for restoring after a forced timeout int _checkfreq; // frequency of timeout checking int _nextcheck; // loop number of next timeout check From 1f1c87235a07fdd57d20e9553295fb9fe867729e Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 18 Oct 2016 12:21:27 -0400 Subject: [PATCH 5/6] add support for "error continue" option, which resets the timer timeout setting after a soft exit --- src/fix_halt.cpp | 14 +++++++++++--- src/fix_halt.h | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/fix_halt.cpp b/src/fix_halt.cpp index 6a64e4ccd1..b6a5b0efdf 100644 --- a/src/fix_halt.cpp +++ b/src/fix_halt.cpp @@ -109,6 +109,7 @@ int FixHalt::setmask() { int mask = 0; mask |= END_OF_STEP; + mask |= POST_RUN; return mask; } @@ -170,14 +171,21 @@ void FixHalt::end_of_step() id,update->ntimestep,attvalue); if (eflag == HARD) error->all(FLERR,str); - else if (eflag == SOFT) { + else if (eflag == SOFT || eflag == CONTINUE) { if (comm->me == 0) error->message(FLERR,str); timer->force_timeout(); - } else if (eflag == CONTINUE) { - // exit this run, but not subsequent ones } } +/* ---------------------------------------------------------------------- + reset expired timer setting to original value, if requested +------------------------------------------------------------------------- */ + +void FixHalt::post_run() +{ + if (eflag == CONTINUE) timer->reset_timeout(); +} + /* ---------------------------------------------------------------------- compute max length of any bond using Neighbor bondlist for each proc ------------------------------------------------------------------------- */ diff --git a/src/fix_halt.h b/src/fix_halt.h index 8173be1a6b..bc7f0055a5 100644 --- a/src/fix_halt.h +++ b/src/fix_halt.h @@ -32,6 +32,7 @@ class FixHalt : public Fix { int setmask(); void init(); void end_of_step(); + void post_run(); private: int attribute,operation,eflag,ivar; From 003581c6a80c7ced3e869bcb6354006ecdfc7db2 Mon Sep 17 00:00:00 2001 From: Steve Plimpton Date: Tue, 18 Oct 2016 11:16:28 -0600 Subject: [PATCH 6/6] final changes to fix halt --- src/fix_halt.cpp | 5 +++-- src/timer.cpp | 14 +++++++++++--- src/timer.h | 9 +++------ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/fix_halt.cpp b/src/fix_halt.cpp index b6a5b0efdf..301e678e38 100644 --- a/src/fix_halt.cpp +++ b/src/fix_halt.cpp @@ -162,8 +162,7 @@ void FixHalt::end_of_step() } // hard halt -> exit LAMMPS - // soft halt -> trigger timer to break from run loop - // continue halt -> trigger time to exit only this run loop + // soft/continue halt -> trigger timer to break from run loop // print message with ID of fix halt in case multiple instances char str[128]; @@ -183,6 +182,8 @@ void FixHalt::end_of_step() void FixHalt::post_run() { + // continue halt -> subsequent runs are allowd + if (eflag == CONTINUE) timer->reset_timeout(); } diff --git a/src/timer.cpp b/src/timer.cpp index 9c1aa87943..583c6a2b3f 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -95,10 +95,10 @@ Timer::Timer(LAMMPS *lmp) : Pointers(lmp) { _level = NORMAL; _sync = OFF; - _timeout = -1; - _s_timeout = -1; + _timeout = -1.0; _checkfreq = 10; _nextcheck = -1; + _laststep = -1; this->_stamp(RESET); } @@ -216,7 +216,7 @@ void Timer::set_wall(enum ttype which, double newtime) void Timer::init_timeout() { - _s_timeout = _timeout; + _laststep = -1; if (_timeout < 0) _nextcheck = -1; else @@ -249,6 +249,14 @@ void Timer::print_timeout(FILE *fp) /* ---------------------------------------------------------------------- */ +void Timer::force_timeout() +{ + _timeout = 0.0; + _nextcheck = _laststep + 1; +} + +/* ---------------------------------------------------------------------- */ + bool Timer::_check_timeout() { double walltime = MPI_Wtime() - timeout_start; diff --git a/src/timer.h b/src/timer.h index 6159180faf..ff9c66db7c 100644 --- a/src/timer.h +++ b/src/timer.h @@ -64,10 +64,7 @@ class Timer : protected Pointers { void init_timeout(); // trigger enforced timeout - void force_timeout() { _timeout = 0.0; } - - // restore original timeout setting after enforce timeout - void reset_timeout() { _timeout = _s_timeout; } + void force_timeout(); // get remaining time in seconds. 0.0 if inactive, negative if expired double get_timeout_remain(); @@ -78,7 +75,7 @@ class Timer : protected Pointers { // check for timeout. inline wrapper around internal // function to reduce overhead in case there is no check. bool check_timeout(int step) { - if (_timeout == 0.0) return true; + _laststep = step; if (_nextcheck != step) return false; else return _check_timeout(); } @@ -94,9 +91,9 @@ class Timer : protected Pointers { int _level; // level of detail: off=0,loop=1,normal=2,full=3 int _sync; // if nonzero, synchronize tasks before setting the timer int _timeout; // max allowed wall time in seconds. infinity if negative - int _s_timeout; // copy of timeout for restoring after a forced timeout int _checkfreq; // frequency of timeout checking int _nextcheck; // loop number of next timeout check + int _laststep; // loop number of last iteration // update one specific timer array void _stamp(enum ttype);