Filtering particles based on user defined property

Hello,
I am trying to read LAMMPS dump files using the OVITO python library and then add two user-defined properties (‘cluster_id_i’ and ‘cluster_id_f’). These properties are just two numpy arrays. Based on these two properties I am selecting a particles and then computing the mean square displacement of those particles. Here is the code

import numpy as np
from ovito.io import import_file, export_file
from ovito.modifiers import ExpressionSelectionModifier, CalculateDisplacementsModifier
from ovito.modifiers import DeleteSelectedModifier

timesteps = np.arange(200000,240000,400,dtype=int)
data_dir = 'dump_files_2/'

cluster_path = 'case_N800K/cluster_ids_2/'
npz = np.load(cluster_path + 'cluster_ids_{}.npz'.format(timesteps[0]))
cluster_id_i = npz['cluster_id']

npz = np.load(cluster_path + 'cluster_ids_{}.npz'.format(timesteps[-1]))
cluster_id_f = npz['cluster_id']

file_list = []
for time_id in timesteps:
    name = data_dir + 'pos.atom.' + str(time_id)
    file_list.append(name)

pipeline = import_file(file_list)
pipeline.modifiers.append(ExpressionSelectionModifier(expression = 'ParticleType == 2'))
pipeline.modifiers.append(DeleteSelectedModifier())

data = pipeline.compute()
data.particles_.create_property('cluster_id_i',data=cluster_id_i)
data.particles_.create_property('cluster_id_f',data=cluster_id_f)

pipeline.modifiers.append(ExpressionSelectionModifier(expression = 'cluster_id_i > -1 && cluster_id_f > -1'))
pipeline.modifiers.append(DeleteSelectedModifier())

def calculate_msd(frame, data):
    displacement_magnitude = data.particles['Displacement Magnitude']

    msd = np.sum(displacement_magnitude**2) / len(displacement_magnitude)

    data.attributes["MSD"] = msd

pipeline.modifiers.append(calculate_msd)

export_file(pipeline, "msd_gas.txt",format="txt/attr",
        columns=["Timestep","MSD"],
        multiple_frames=True)

Unfortunately, I am getting the following error

$ python compute_msd_gas.py 
Traceback (most recent call last):
  File "/home/arijit/Desktop/SLAC_CO2/compute_msd_gas.py", line 43, in <module>
    export_file(pipeline, "msd_gas.txt",format="txt/attr",
  File "/home/arijit/anaconda3/envs/ovito_env/lib/python3.11/site-packages/ovito/io/_export_file_func.py", line 210, in export_file
    exporter.do_export()
RuntimeError: Export of frame 0 failed, because data pipeline evaluation has failed.
Unexpected token "cluster_id_i" found at position 0.

I am not sure why the user defined property is not recognized. Can anyone please suggest what I am doing wrong?

Thanks
Arijit

Hi Arijit,

To fix this error, you need to wrap these two lines in a user-defined modifier function (just like your calculate_msd() function):

data.particles_.create_property('cluster_id_i',data=cluster_id_i)
data.particles_.create_property('cluster_id_f',data=cluster_id_f)

Only then will the ExpressionSelectionModifier in the pipeline “see” the two new properties. For the reason why, please read the 2nd Attention box in the documentation of Pipeline.compute().

1 Like