Segmentation Fault- Python- PyArrayObject

Hi,

I am working on lammps implementation of an interatomic potential, the code of which is in python. To quickly test this potential in lammps, I am passing the needed arguments into python code and getting energy and force arrays in return.

To test the validity of the above approach (passing of c++ arrays into python), A simple example was tested on pair_lj_cut.cpp file. I added the following code in its void::compute function. The following addition has been tested successfully on its own.

Py_Initialize();
_import_array();
const int SIZE{10};
npy_intp dimsD[2]{SIZE, SIZE};
const int NDD{ 2 };
long double(c_arr)[SIZE] = new long double[SIZE][SIZE];
PyArrayObject np_arg;
for (int i{}; i < SIZE; i++)
for (int j{}; j < SIZE; j++)
c_arr[i][j] = i * SIZE + j;
np_arg = reinterpret_cast<PyArrayObject
>(PyArray_SimpleNewFromData(NDD, dimsD, NPY_LONGDOUBLE,
reinterpret_cast<void
>(c_arr)));
std::cout<<“DONE CALLING PYARRAY FUNC”<<"\n";

Lammps was compiled with python package and the above addition in the pair_lj_cut.cpp compiled properly. On running a simple lammps script using lj/cut pair style, I get the segmentation fault. Lammps script was tested successfully without the above modification in pair style.

My assumption is that there is some issue with PyArray_SimpleNewFromData() function call. But then it should raise same segmentation fault, when I am running it independently.

I will really appreciate if you can help me with this issue.

Regards,
Gurjot