Segmentation fault when using extract_compute() in python wrapper

Hi all,

I am encountering a Segmentation fault while implementing the extract_compute() command from the fix python/invoke command.

The idea is to run a python script within a lammps script. The former extracts data from the latter and returns information to the latter, all during the run.
A simpler idea, of extracting global variables has been implemented in in.fix_python_invoke (an example script from /lammps/examples/python dir).

To demonstrate the problem, I have attached a script which is a modified version of the above mentioned example. Please note that the modifications are in lines #20, and #27-32. Offending line: #29.
I have passed a SELF pointer to the while defining the function, with the understanding that the python wrapper in fix python/invoke now references to the already running lammps script.

If the above idea is indeed correct, could you please also tell me if there is a way to implement "flag = lmp.set_variable(name,value) " for vectors?

The input scripts, and the output have been attached for your perusal.

Thanks a lot for your help,
Praneeth

output (2.92 KB)

in.test (1.32 KB)

in.fix_python_invoke (1.03 KB)

Hi all,

I am encountering a Segmentation fault while implementing the
extract_compute() command from the fix python/invoke command.

The idea is to run a python script within a lammps script. The former
extracts data from the latter and returns information to the latter, all
during the run.
A simpler idea, of extracting global variables has been implemented in
in.fix_python_invoke (an example script from /lammps/examples/python dir).
To demonstrate the problem, I have attached a script which is a modified
version of the above mentioned example. Please note that the modifications
are in lines #20, and #27-32. Offending line: #29.
I have passed a SELF pointer to the while defining the function, with the
understanding that the python wrapper in fix python/invoke now references
to the already running lammps script.

If the above idea is indeed correct, could you please also tell me if
there is a way to implement "flag = lmp.set_variable(name,value) " for
vectors?

​there is a serious problem with your script:

your compute cluster/atom command is using too long a cutoff and thus the
compute instance is never created and any reference to it will cause a
segfault.
that said, it is a *very* bad idea to define computes and fixes *while*
doing a run. those need to be defined outside, so that proper
initialization protocol is followed.
i've never imagined that somebody would think of su​ch a thing inside a
step, but then again, it is only the python wrapping that makes this
possible in the first place.

i don't understand what you are asking about with respect to variables.

if you modify the script to have this (check out what happens, if you
reduce the pair style cutoff back to 2.5), it should not segfault:

pair_style lj/cut 3.5
pair_coeff 1 1 1.0 1.0 3.5

neighbor 0.3 bin
neigh_modify every 20 delay 0 check no
compute clu all cluster/atom 3.5
fix clf all ave/atom 10 3 60 c_clu

python end_of_step_callback input 1 SELF format p here """
from __future__ import print_function
from lammps import lammps

def end_of_step_callback(lmp):
  L = lammps(ptr=lmp)

  cvec = L.extract_compute("clu",1,1) #cluster/atom gives a per
atom vector. Pg 233, PDF docs 5Feb18

  t = L.extract_global("ntimestep", 0)
  print("### END OF STEP ###", t)

def post_force_callback(lmp, v):
  L = lammps(ptr=lmp)
  t = L.extract_global("ntimestep", 0)
  print("### POST_FORCE ###", t)
"""

​axel.​