[lammps-users] TypeError on gather_atoms in Python examples


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
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
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!



You need this bugfix: https://github.com/lammps/lammps/commit/232ae5e8568a413cc7f3524ca718d0d5b72c7914


Indeed after changing those lines from ‘type’ to ‘dtype’ in core.py the “get_atoms” commands work without error.
Thanks for the quick and helpful response!



If you look at the changes to the LAMMPS source code since the version you are using was released, there are quite a few bugfixes that you are missing by not upgrading to the latest released version. at least the latest stable version with the bugfixes patch applied. We have changed the ways we check the code and thus I believe that for several years now we are consistently removing more bugs than we introduced (which are mostly in completely new packages/features). Where possible, identified bugs are then covered by unit or regression tests to keep them from coming back. Considering the large size of LAMMPS there is probably still a significant amount of bugs left, but they are increasingly obscure and hard to hit. However, the python module support is one part that is still missing some refactoring and systematic testing, so the probability of yet undetected bugs is higher.

Hi Axel - That makes sense. I didn’t realize there were many things that changed since the version I have - I’ll go ahead and upgrade to the latest version.