Commit c4d0f070 authored by Richard Berger's avatar Richard Berger
Browse files

Allow fix python to only execute every N steps

parent 93f60330
Loading
Loading
Loading
Loading
+7 −6
Original line number Diff line number Diff line
@@ -10,13 +10,14 @@ fix python command :h3

[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
python = style name of this fix command :l
N = execute every N steps :l
callback = {post_force} or {end_of_step} :l
  {post_force} = callback after force computations on atoms
  {end_of_step} = callback after each time step :pre
  {post_force} = callback after force computations on atoms every N time steps
  {end_of_step} = callback after each N time steps :pre
:ule

[Examples:]
@@ -35,14 +36,14 @@ def end_of_step_callback(lammps_ptr):
    # access LAMMPS state using Python interface
""" :pre

fix pf  all python post_force post_force_callback
fix eos all python end_of_step end_of_step_callback :pre
fix pf  all python 50 post_force post_force_callback
fix eos all python 50 end_of_step end_of_step_callback :pre

[Description:]

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
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
active Python interpreter. This can either be done by defining it
+2 −2
Original line number Diff line number Diff line
@@ -33,8 +33,8 @@ def post_force_callback(lmp, v):
"""

fix		1 all nve
fix     2 all python end_of_step end_of_step_callback
fix     3 all python post_force post_force_callback
fix     2 all python 50 end_of_step end_of_step_callback
fix     3 all python 50 post_force post_force_callback

#dump		id all atom 50 dump.melt

+9 −4
Original line number Diff line number Diff line
@@ -36,14 +36,17 @@ using namespace FixConst;
FixPython::FixPython(LAMMPS *lmp, int narg, char **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
  python->init();

  if (strcmp(arg[3],"post_force") == 0) {
  if (strcmp(arg[4],"post_force") == 0) {
    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;
  }

@@ -57,7 +60,7 @@ FixPython::FixPython(LAMMPS *lmp, int narg, char **arg) :
    error->all(FLERR,"Could not initialize embedded Python");
  }

  char * fname = arg[4];
  char * fname = arg[5];
  pFunc = PyObject_GetAttrString(pyMain, fname);

  if (!pFunc) {
@@ -94,6 +97,8 @@ void FixPython::end_of_step()

void FixPython::post_force(int vflag)
{
  if (update->ntimestep % nevery != 0) return;

  PyGILState_STATE gstate = PyGILState_Ensure();

  PyObject * ptr = PY_VOID_POINTER(lmp);