[lammps-users] Segmentation fault while using pair_style python using pylammps


I have been using Pylammps to run lammps code.

Here I am trying to call piar_style python inside the Pylammps functions as follows

granular = granular_lammps_module()

granular.command(“pair_style python 2.5”)

granular.command(“pair_coeff * * py_pot.LJCutMelt lj”)

2d_4.lmp (4.8 KB)

script_lammps.py (5.02 KB)

py_pot.py (1.15 KB)

please add the following changes to the LAMMPS source code file src/PYTHON/pair_python.cpp, recompile and try again:

diff --git a/src/PYTHON/pair_python.cpp b/src/PYTHON/pair_python.cpp
index eb54d1d4e5…32cf26641b 100644
— a/src/PYTHON/pair_python.cpp
+++ b/src/PYTHON/pair_python.cpp
@@ -49,6 +49,7 @@ PairPython::PairPython(LAMMPS *lmp) : Pair(lmp) {

// add current directory to PYTHONPATH

  • PyGILState_STATE gstate = PyGILState_Ensure();
    PyObject * py_path = PySys_GetObject((char *)“path”);
    PyList_Append(py_path, PY_STRING_FROM_STRING("."));

@@ -57,6 +58,7 @@ PairPython::PairPython(LAMMPS *lmp) : Pair(lmp) {
if (potentials_path != nullptr) {
PyList_Append(py_path, PY_STRING_FROM_STRING(potentials_path));

  • PyGILState_Release(gstate);

/* ---------------------------------------------------------------------- */

looks like you forgot to do “make package-update” before compiling


Hi Axel,

Thanks for the reply. I did as you suggested but I am still getting Segmentation fault (core dumped)

I made changes as shown in the attached file.

Then I executed the following command

make clean-all
make mode=shared serial

and then in the virtual environment

pair_python.cpp (14.1 KB)