### Author Topic: Averaging the values of a property  (Read 897 times)

#### Sam

• Newbie
• Posts: 5
##### Averaging the values of a property
« on: July 03, 2018, 09:02:55 PM »
Dear Alex,

Would you please possibly tell me how to calculate the average values of a particular property, e.g., atomic stress, for some specific atoms?

Best,
Sam

#### Constanze Kalcher

• Full Member
• Posts: 197
##### Re: Averaging the values of a property
« Reply #1 on: July 04, 2018, 10:29:56 AM »
Dear Sam,

one way to do this in the graphical user interface would be to first use one of the Selection modifiers, e.g., Expression selection, Select type or Manual selection to select the atoms of interest.
Then, add the Python script modifier at the top of the modification pipeline and write your own modifier function, e.g.

Code: [Select]
`from ovito.data import *import numpy as npdef modify(frame, input, output):    my_property = input.particles[“My Property”]    selection = input.particles[“Selection”]    output.attributes["Mean"] = np.mean( my_property[np.nonzero(selection)] )`
This function outputs the result as a global attribute which will appear on the Attributes page in the Data Inspector (only available in OVITO3.0).
Data Inspector
In OVITO2.9 you could make use of the Export function instead.
File Export
Alternatively you can of course simply add a print statement to your modifier function.

Constanze

#### Sam

• Newbie
• Posts: 5
##### Re: Averaging the values of a property
« Reply #2 on: July 08, 2018, 01:02:53 PM »
Dear Constanze,

I am new to Python scripting in Ovito. Let's imagine the desired property is Shear Strain. So the script should be:

Code: [Select]
`from ovito.data import *import numpy as npdef modify(frame, input, output):my_property = input.particles["Shear Strain"]selection = input.particles["Selection"]output.attributes["Mean"] = np.mean( my_property[np.nonzero(selection)] )`
Do we need to define ParticleProperty.Type.StrainTensor and load a reference configuration file (e.g., reference.load("simulation.0.dump")) in the script? If so, would you please tell me how to do this?
Following my previous question, i wonder if you could tell me whether it is possible to use the the graphical user interface to calculate strain and displacement, then import them to the python script in order to calculate the average values for the selected atoms? If so, can you please explain how to perform this?

Best,
Sam

#### Constanze Kalcher

• Full Member
• Posts: 197
##### Re: Averaging the values of a property
« Reply #3 on: July 09, 2018, 11:59:47 AM »
Dear Sam,

in the graphical user interface of OVITO you would first add the Atomic Strain modifier to your pipeline,
then a Selection modifier of your choice and finally the Python script modifier. You don't need to implement the calculation of the von Mises shear strain in your user defined function. Similar to the selection modifier, which creates a particle property "Selection", the Atomic Strain modifier will create a particle property "Shear Strain" which is available to you as input.particles["Shear Strain"]. You already described that correctly in your previous post.
Note, however, that indentation matters in python, so you need to adapt your script accordingly (see code below).

If you also want to know the average atomic displacement of your selected atoms, simply add the Displacement vectors modifier to your pipeline (below the Python Script modifier). This modifier also calculates the length of the displacement vectors which are stored as particle property "Displacement Magnitude".

Code: [Select]
`from ovito.data import *import numpy as npdef modify(frame, input, output):    my_property = input.particles["Shear Strain"]    selection = input.particles["Selection"]    displ = input.particles["Displacement Magnitude"]    output.attributes["Mean Shear Strain"] = np.mean( my_property[np.nonzero(selection)] )    print(output.attributes["Mean Shear Strain"])    output.attributes["Mean Displacement"] = np.mean( displ[np.nonzero(selection)] )    print(output.attributes["Mean Displacement"])`
Let me know if that worked for you.

Constanze
« Last Edit: July 09, 2018, 12:06:31 PM by Constanze Kalcher »

#### Sam

• Newbie
• Posts: 5
##### Re: Averaging the values of a property
« Reply #4 on: July 09, 2018, 04:17:03 PM »
Dear Constanze,

Thanks so much for the detailed explanations. I got "nan" for both the properties. Please see the attachment.

Best,
Sam

#### Constanze Kalcher

• Full Member
• Posts: 197
##### Re: Averaging the values of a property
« Reply #5 on: July 09, 2018, 04:29:44 PM »
Are you using OVITO3.0? Also, can you show me your selection modifier please.

#### Constanze Kalcher

• Full Member
• Posts: 197
##### Re: Averaging the values of a property
« Reply #6 on: July 09, 2018, 04:43:55 PM »
Also note that the order of the modifiers in the pipeline is important. If you first added your Selection modifier and then the Atomic strain modifier and therein activated the option Select invalid particles you might have overwritten your previous selection of atoms with a selection that contains no (invalid) atoms. This will lead to the result you got.
« Last Edit: July 09, 2018, 04:45:40 PM by Constanze Kalcher »

#### Sam

• Newbie
• Posts: 5
##### Re: Averaging the values of a property
« Reply #7 on: July 09, 2018, 04:59:12 PM »
Dear Constanze,

Many thanks indeed ! It works now in OVITO3.0. I did not know that the order of the modifiers in the pipeline is crucial.

Best,
Sam

#### Sam

• Newbie
• Posts: 5
##### Re: Averaging the values of a property
« Reply #8 on: July 10, 2018, 11:28:28 AM »
Dear Constanze,

I have outputted atomic stress, e.g. Stress Tensor.XX, in the dump file. I can visualize the atomic stress using color coding, however, i am not sure how to introduce stress tensor to the python script to calculate mean values. Would you please help me with this?

Best,
Sam

#### Constanze Kalcher

`stress_xx = input.particles['Stress Tensor'][:,0]stress_yy = input.particles['Stress Tensor'][:,1]stress_zz = input.particles['Stress Tensor'][:,2]`