Issue with ovito.io.lammps

Hello Community Readers,

I was testing out how to call ovito from a running lammps simulation using the “ovito.io.lammps” in ovito and “fix python” functionality in lammps. I have a custom python function to compute the FCC fraction at certain timesteps. But it runs into an Attribute not found error. It refers to the attribute “rank”. I checked to see if the lammps simulation state was correctly imported in the prior step and it was being imported fine.

The details of the code and error are provided below. If anyone can shed some light on how I go forward from here, it will be really helpful.

Python Function

from lammps import lammps
from ovito.io.lammps import lammps_to_ovito
from ovito.modifiers import PolyhedralTemplateMatchingModifier

def compute_fcc_fraction(lammps_ptr):

lmp = lammps(ptr=lammps_ptr)  # Access the LAMMPS simulation state
data = lammps_to_ovito(lmp)  # Convert to OVITO data representation

# Skip analysis on non-master MPI ranks
if data is None:
    return

# Apply the Polyhedral Template Matching modifier
ptm = PolyhedralTemplateMatchingModifier(rmsd_cutoff=0.155)
data.apply(ptm)

# Calculate FCC fraction
fcc_count = data.attributes['PolyhedralTemplateMatching.counts.FCC']
total_atoms = data.particles.count
fcc_fraction = fcc_count / total_atoms

# Save the result to a file
timestep = int(lmp.get_thermo('step'))
with open('fcc_fraction.txt', 'a') as f:  # Append to the file
    f.write(f'{timestep} {fcc_fraction:.2f}\n')

Lammps fix python part

python source fcc_fraction.py
fix fcc_analysis all python/invoke 1 end_of_step compute_fcc_fraction

Error

Traceback (most recent call last):
File “fcc_fraction.py”, line 8, in compute_fcc_fraction
data = lammps_to_ovito(lmp) # Convert to OVITO data representation
^^^^^^^^^^^^^^^^^^^^
File “/work/07691/aswamy96/miniconda/envs/lammps-python/lib/python3.11/site-packages/ovito/io/lammps/init.py”, line 91, in lammps_to_ovito
if lmp.get_mpi_comm().rank != 0:
^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: ‘NoneType’ object has no attribute ‘rank’
ERROR: Fix python/invoke end_of_step() method failed (…/fix_python_invoke.cpp:100)
Last command: run 1

OVITO calls the lammps.get_mpi_comm() method, which fails if LAMMPS has been built without MPI support. You should be able to solve this by enabling MPI in the LAMMPS build (CMake option BUILD_MPI=ON).

We’ll add an extra check in the next release of OVITO to make this also work if MPI is disabled in LAMMPS.

Hi Alex,
I do have MPI support in lammps. I’m able to run non-python scripts with mpi in lammps.

Then you should probably install the mpi4py package in your Python interpreter. I think another reason why get_mpi_comm() returns None may be that this package isn’t available.

Thank you. That fixed the issue.