Allow fix python to only execute every N steps

This commit is contained in:
Richard Berger
2017-05-12 00:29:58 -04:00
parent 93f6033061
commit c4d0f07093
3 changed files with 18 additions and 12 deletions

View File

@ -10,13 +10,14 @@ fix python command :h3
[Syntax:] [Syntax:]
fix ID group-ID python callback function_name :pre fix ID group-ID python N callback function_name :pre
ID, group-ID are ignored by this fix :ulb,l ID, group-ID are ignored by this fix :ulb,l
python = style name of this fix command :l python = style name of this fix command :l
N = execute every N steps :l
callback = {post_force} or {end_of_step} :l callback = {post_force} or {end_of_step} :l
{post_force} = callback after force computations on atoms {post_force} = callback after force computations on atoms every N time steps
{end_of_step} = callback after each time step :pre {end_of_step} = callback after each N time steps :pre
:ule :ule
[Examples:] [Examples:]
@ -35,14 +36,14 @@ def end_of_step_callback(lammps_ptr):
# access LAMMPS state using Python interface # access LAMMPS state using Python interface
""" :pre """ :pre
fix pf all python post_force post_force_callback fix pf all python 50 post_force post_force_callback
fix eos all python end_of_step end_of_step_callback :pre fix eos all python 50 end_of_step end_of_step_callback :pre
[Description:] [Description:]
This fix allows you to call a Python function during a simulation run. This fix allows you to call a Python function during a simulation run.
The callback is either executed after forces have been applied to atoms The callback is either executed after forces have been applied to atoms
or at the end of each time step. or at the end of every N time steps.
Callback functions must be declared in the global scope of the Callback functions must be declared in the global scope of the
active Python interpreter. This can either be done by defining it active Python interpreter. This can either be done by defining it

View File

@ -33,8 +33,8 @@ def post_force_callback(lmp, v):
""" """
fix 1 all nve fix 1 all nve
fix 2 all python end_of_step end_of_step_callback fix 2 all python 50 end_of_step end_of_step_callback
fix 3 all python post_force post_force_callback fix 3 all python 50 post_force post_force_callback
#dump id all atom 50 dump.melt #dump id all atom 50 dump.melt

View File

@ -36,14 +36,17 @@ using namespace FixConst;
FixPython::FixPython(LAMMPS *lmp, int narg, char **arg) : FixPython::FixPython(LAMMPS *lmp, int narg, char **arg) :
Fix(lmp, narg, arg) Fix(lmp, narg, arg)
{ {
if (narg != 5) error->all(FLERR,"Illegal fix python command"); if (narg != 6) error->all(FLERR,"Illegal fix python command");
nevery = force->inumeric(FLERR,arg[3]);
if (nevery <= 0) error->all(FLERR,"Illegal fix python command");
// ensure Python interpreter is initialized // ensure Python interpreter is initialized
python->init(); python->init();
if (strcmp(arg[3],"post_force") == 0) { if (strcmp(arg[4],"post_force") == 0) {
selected_callback = POST_FORCE; selected_callback = POST_FORCE;
} else if (strcmp(arg[3],"end_of_step") == 0) { } else if (strcmp(arg[4],"end_of_step") == 0) {
selected_callback = END_OF_STEP; selected_callback = END_OF_STEP;
} }
@ -57,7 +60,7 @@ FixPython::FixPython(LAMMPS *lmp, int narg, char **arg) :
error->all(FLERR,"Could not initialize embedded Python"); error->all(FLERR,"Could not initialize embedded Python");
} }
char * fname = arg[4]; char * fname = arg[5];
pFunc = PyObject_GetAttrString(pyMain, fname); pFunc = PyObject_GetAttrString(pyMain, fname);
if (!pFunc) { if (!pFunc) {
@ -94,6 +97,8 @@ void FixPython::end_of_step()
void FixPython::post_force(int vflag) void FixPython::post_force(int vflag)
{ {
if (update->ntimestep % nevery != 0) return;
PyGILState_STATE gstate = PyGILState_Ensure(); PyGILState_STATE gstate = PyGILState_Ensure();
PyObject * ptr = PY_VOID_POINTER(lmp); PyObject * ptr = PY_VOID_POINTER(lmp);