Accessing variables with PyLammps but having "'NoneType' object is not subscriptable" error

Hi all,

When I was trying to use the following code to extract variables in LAMMPS in PyLammps, running with multiple core using mpirun -np 4 python3 extract_variables.py:

from lammps import PyLammps

L = PyLammps()
L.variable("a equal 1")
a = L.variables["a"]

I receive complain from python on my screen output TypeError: 'NoneType' object is not subscriptable (please see the attached file for full error output error.txt (1.4 KB)). I have tried to change the buffering mode to line buffering as instructed earlier in the mailing list (i.e. using stdbuf -oL), but it doesn’t work. I’m wondering what else can I try to fix this?
Information that might be helpful: I use WSL to run LAMMPS and PyLammps, as instructed by Richard Berger in the documentation. I checkout’ed the master branch a month ago, and my python3 version is 3.8.10.

Many thanks for help in advance! I’ll provide information as needed.

Best regards,
Tianpu

Thanks for your report. This is a bug in the current code. Please try to apply the following modifications:

  diff --git a/python/lammps/pylammps.py b/python/lammps/pylammps.py
  index a89791a02e..b0eeb6c401 100644
  --- a/python/lammps/pylammps.py
  +++ b/python/lammps/pylammps.py
  @@ -643,7 +643,7 @@ class PyLammps(object):
       return [x.strip() for x in value.split('=')]
   
     def _parse_info_system(self, output):
  -    lines = output[6:-2]
  +    lines = output[5:-2]
       system = {}
   
       for line in lines:
  @@ -704,7 +704,7 @@ class PyLammps(object):
       return system
   
     def _parse_info_communication(self, output):
  -    lines = output[6:-3]
  +    lines = output[5:-3]
       comm = {}
   
       for line in lines:
  @@ -725,7 +725,7 @@ class PyLammps(object):
       return comm
   
     def _parse_element_list(self, output):
  -    lines = output[6:-3]
  +    lines = output[5:-3]
       elements = []
   
       for line in lines:
  @@ -737,7 +737,7 @@ class PyLammps(object):
       return elements
   
     def _parse_groups(self, output):
  -    lines = output[6:-3]
  +    lines = output[5:-3]
       groups = []
       group_pattern = re.compile(r"(?P<name>.+) \((?P<type>.+)\)")
   

Hi Axel,

Thanks for your reply! I’ll try this out and report if it’s fixed.

Best,
Tianpu

Hi Tianpu,

Thanks for reporting. Yes, Axel’s updates will fix some of it, but for MPI to work there are still some things missing. The main issue here is that only MPI Rank 0 produces output that can be caught by PyLammps. Hence, the NoneType errors.

I’ll be pushing the necessary changes to an upcoming GitHub PR.

Best,
Richard

Hi Richard,

Many thanks for your reply. Indeed I observed that only when 1 processor is used can this function normally, so I suspected similar things happen. This is very helpful and I’m looking forward to pulling the changes!

Best regards,
Tianpu

Try this change:

That should make it work with MPI too.

Best,
Richard

Hi Axel and Richard,

Thank you both for the updates. These now work very well without trouble with MPI.

Best,
Tianpu