Forum Navigation
You need to log in to create posts and topics.

Mean Square displacement for specific zone

Dear Sir

I am doing MD simulation on core-shell particle. I want to see the MSD for shell and core separately. I have used your example for measuring the MSD. then I have added small part for counting MSD only at the shell. But it is not working at all. can you help me.

 

from ovito.io import import_file, export_file
from ovito.modifiers import (CalculateDisplacementsModifier, ExpressionSelectionModifier)
import numpy

# Load input data and create a data pipeline.
pipeline = import_file("./test_interior1.dump")

# Select shell atoms:
pipeline.modifiers.append(ExpressionSelectionModifier(expression = '(Position.X - 75.83)^2 + (Position.Y - 75.83)^2 + (Position.Z - 75.83)^2 <= 51^2'))
data = pipeline.compute()

# Calculate per-particle displacements with respect to initial simulation frame:
pipeline.modifiers.append(CalculateDisplacementsModifier())

# Define the custom modifier function:
def modify(frame, data):
    
    # Access the per-particle displacement magnitudes computed by the
    # CalculateDisplacementsModifier that precedes this user-defined modifier in the
    # data pipeline:
    displacement_magnitudes = data.particles['Displacement Magnitude']
    
    # Compute MSD:
    msd = numpy.sum(displacement_magnitudes ** 2) / len(displacement_magnitudes)

    # Output MSD value as a global attribute:
    data.attributes["MSD"] = msd

# Insert user-defined modifier function into the data pipeline.
pipeline.modifiers.append(modify)

# Export calculated MSD value to a text file and let OVITO's data pipeline do the rest:
export_file(pipeline, "./msd_data_shell.txt", format = "txt/attr", columns = ["Timestep", "MSD"], multiple_frames = True)

 

 

Hi,

in line 9 you're using the Expression Selection Modifier to create a selection of atoms, but you don't use this information anywhere.
You can modify line 24 to

msd = numpy.mean(displacement_magnitudes[data.particles.selection] ** 2)

to take into account the length of the displacement vectors of the selected atoms only. If you are not familiar with this syntax yet, you might find this numpy docs entry helpful: https://numpy.org/devdocs/user/basics.indexing.html

Also, line 10 is obsolete, you can remove it. The pipeline is automatically evaluated when you call export_file() in line 33.

Dear Sir

Thank you very much for your comment.  I have changed the line 24

and got below error

Traceback (most recent call last):
File "msd_shell.py", line 24, in modify
msd = numpy.mean(displacement_magnitudes[selection] ** 2)
NameError: name 'selection' is not defined
Traceback (most recent call last):
File "msd_shell.py", line 32, in <module>
export_file(pipeline, "./msd_data_shell.txt",
File "/home/zahid/anaconda3/lib/python3.8/site-packages/ovito/io/export_file.py", line 185, in export_file
exporter.do_export()
RuntimeError: Export of animation frame 0 failed, because data pipeline evaluation did not succeed. Status message: Modifier 'Python script' reported: The Python script has exited with an error.
ERROR: The Python script has exited with an error.

You can either look up the particle property selection like this,

selection = data.particles["Selection"]

or like this

selection = data.particles.selection