Author Topic: How to determine the mean value of a particle property of a selection of atoms?  (Read 1800 times)

Constanze Kalcher

  • Administrator
  • Full Member
  • *****
  • Posts: 110
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, Select particle type, Slice or Manual selection to select the atoms of interest.

Then, insert a Python script modifier 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 as Calculation Results Text File output file format.

Constanze Kalcher

  • Administrator
  • Full Member
  • *****
  • Posts: 110
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] )