Hello,
I am trying to get set up using Python to drive LAMMPS. I would like to access the atom positions, forces, etc., but I get a “TypeError” whenever the “gather_atoms” commands are called.
After compiling as a shared library and installing mpi4py, I can successfully run a python script with mpi such as the “test.py” file suggested in the documentation (https://docs.lammps.org/Python_launch.html). I then tried to run several of the examples in Python/Examples, such as demo.py and simple.py. In both of these examples, I get an error when trying to execute the “gather_atoms” commands.The line “xc = lmp.gather_atoms(“x”,1,3)” from demo.py gives the following error:
File “demo.py”, line 50, in
xc = lmp.gather_atoms(“x”,1,3)
File “/home/ejohnson/.local/lib/python3.8/site-packages/lammps/core.py”, line 1140, in gather_atoms
self.lib.lammps_gather_atoms(self.lmp,name,type,count,data)
ctypes.ArgumentError: argument 3: <class ‘TypeError’>: wrong type
Similarly, the line “ids = lmp.gather_atoms_concat(“id”,0,1)” in the simple.py example gives the error:
ids = lmp.gather_atoms_concat(“id”,0,1)
File “/home/ejohnson/.local/lib/python3.8/site-packages/lammps/core.py”, line 1153, in gather_atoms_concat
self.lib.lammps_gather_atoms_concat(self.lmp,name,type,count,data)
ctypes.ArgumentError: argument 3: <class ‘TypeError’>: wrong type
The other commands in demo.py are working as expected, such as get_natoms, extract_variable, get_thermo, and extract_atom.
From the error message it seems that something is wrong in the “type” argument, which should be 0 for an int, or 1 for a double. The arguments used in the examples seem to match the documentation and the comments in library.cpp and core.py, so I can’t see anything obvious that’s causing the error (though this is my first time working with these python wrappers so I could be making a rookie mistake!). My lammps version is May 27, 2021.
I see there was a bug fix in the Nov 30, 2020 release for lammps_gather_atoms (https://github.com/lammps/lammps/releases/tag/patch_30Nov2020, related to issue https://github.com/lammps/lammps/pull/2472), but it seems my lammps version should be ok as it was a subsequent release.
Do you have any ideas on why I would get this “TypeError” when running the examples? Any thoughts would be much appreciated!
Thanks,
Evan