OVITO Forum

OVITO => Frequently Asked Questions (FAQ) => Topic started by: Constanze Kalcher on September 14, 2018, 04:18:12 PM

Title: How to determine the mean value of a particle property of a selection of atoms?
Post by: Constanze Kalcher on September 14, 2018, 04:18:12 PM
Let's say you want to calculate the average value of a particle property for only a subset of atoms, for example the shear strain.
First you may want to use one of the Selection modifiers, e.g., Expression select (http://ovito.org/manual/particles.modifiers.expression_select.html), Select particle type (http://ovito.org/manual/particles.modifiers.select_particle_type.html), Slice (http://ovito.org/manual/particles.modifiers.slice.html) or Manual selection (http://ovito.org/manual/particles.modifiers.slice.html) to select the atoms of interest.

Then, insert a Python script modifier (http://ovito.org/manual/particles.modifiers.python_script.html) that calculates the mean value of the per-atom shear strain of your selection and saves it as a global attribute.
Code: [Select]
from ovito.data import *
import numpy as np

def modify(frame, input, output):
    shear_strain = input.particle_properties["Shear Strain"].array
    selection = input.particle_properties["Selection"].array
    output.attributes["Average shear strain"] = np.mean( shear_strain[selection == 1] )

Global attributes can be exported (http://www.ovito.org/manual/usage.export.html) as Calculation Results Text File output file format.
Title: Re: How to determine the mean value of a particle property of a selection of atoms?
Post by: Constanze Kalcher on September 14, 2018, 04:20:40 PM
The OVITO 3 equivalent of the above Python script modifier would be :
Code: [Select]
from ovito.data import *
import numpy as np

def modify(frame, input, output):
    shear_strain = input.particles["Shear Strain"]
    selection = input.particles["Selection"]
    output.attributes["Average shear strain"] = np.mean( shear_strain[selection == 1] )