# how to use compute property?

Dear all,

first of all, thanks to developers of OVITO, for providing this wonderful software.

I calculate potential energy like below, but it is a hard procedure (the bold lines), therefore, I wounder there is another way, for example, by using compute property.

1- I insert Voronoi Analysis, 2-  expression selection (VoronoiIndex.4==1 && VoronoiIndex.5==10 && VoronoiIndex.6==5 && Coordination==16 && ParticleType==2), 3- expand selection (N=16), 4 and 5- invert and delete selected particles, 6- scatter plot, to show energy of each atoms.

Now, I have to save the output data in a txt file for each dump. Then, copy and paste data to Excel. In the next, I have to subtract the basic energy according to the particle type, for instance, -3 from the first type and -6 from the second type.

If I want to do this for 200 dumps, containing more than 10000 atom, it take a long time. Is there any way?

regard

Hi,

if I understand you correctly you're asking how to subtract an energy value from your selection of particles depending on their type? Yes, this can be done using a Compute property modifier:

Let's say you want to subtract -1.5 eV from the potential energy of all particles that are type 1 and -3 eV of all particles that are type 2. Then the syntax would be:

Output property: "Potential Energy" or whatever you named the property that contains the potential energy before import, e.g. "c_epot"
Expression: `(ParticleType == 1) ? c_epot - 1.5 : c_epot - 3`

see section "Selective and conditional property assignment" in the manual: https://www.ovito.org/docs/current/particles.modifiers.compute_property.php.

-Constanze

Dear Constanze,

so many thanks for helping. It is absolutely what I wanted. But, I have another question. how can I take average from all of obtained potential energy. I used this "avg(c_pe_energy_after_subtraction)" in Expression of other compute property , but it does not work. thanks in advance.

Happy new year

Happy New Year to you too!
For that you will need to write a short python script modifier function, see e.g.
http://www.ovito.org/docs/current/particles.modifiers.python_script.php and http://www.ovito.org/docs/current/python/introduction/custom_modifiers.php#.

```from ovito.data import *
import numpy as np

def modify(frame, data):
epot = data.particles["Potential Energy"]
data.attributes["Epot_avg"] = np.mean(epot)```

Here is an example of a such modifier function that computes the average of a particle property called "Potential Energy" and stores that as a global attribute "Epot_avg". This global attribute will appear in the Data inspector as well and its time evolution can be exported to a txt-file using OVITO's file export function "Table of values".

-Constanze

Dear Dr. Constanze Kalcher

So many thanks for your help.

regard

Dear Dr. Constanze Kalcher

I wrote the below script to calculate the number of atoms before and after expansion, as well as calculate the "average potential energy", as you kindly wrote in your previous post. the below script can calculate the number of atoms, but could not work out the "average potential energy". I have two question, it would be appreciated if you would help me.

1- how can i modify the script to determine "average potential energy"?

2- the script, firstly, figures out the icosahedrons, in the next, expands to 12 nearest neighbors. therefore, when it wants to compute the "average potential energy", considers just the present atoms. but it seems unsuitable, because it is the energy of all atoms, and not the average of energy of clusters.

for example, lets consider we have 3 isolated icosahedrons in a sample, when we use this expansion, we should have 3*12+3=39 atoms. but sometime, as you know, some icosahedrons are connected to each other, namely face-sharing, then the number of atoms shown after expansion is less than 39 atoms. how can i modify the script to determine "average potential energy" for all 39 atoms? OVITO can do it? or, at least gives me the id of each icosahedron + id of surrounding atoms?

regard,

ll

from ovito.io import import_file
from ovito.modifiers import *
import numpy as np
def modify(frame, pipeline):
pipeline = import_file('1.0e*_pe.lammpsTrj')
atom_types = pipeline.source.data.particles['Particle Type'].types
pipeline.modifiers.append(ComputePropertyModifier(expressions = ['(ParticleType == 1) ? c_eng +3.2831162 : c_eng +6.4688'],output_property="PotentialEnergy" ))
energy=pipeline.compute()

epot = pipeline.source.data.particles['Potential Energy']
energy.attributes["Epot_avg"] = np.mean(epot)
energy=pipeline.compute()

pipeline.modifiers.append(SelectExpressionModifier(expression = 'VoronoiIndex.5==12 && Coordination==12 &&ParticleType==1'))
before_expand=pipeline.compute()
pipeline.modifiers.append(ExpandSelectionModifier(mode=ExpandSelectionModifier.ExpansionMode.Nearest,num_neighbors=12))
pipeline.modifiers.append(InvertSelectionModifier())
pipeline.modifiers.append(DeleteSelectedParticlesModifier())
after_expand=pipeline.compute()

print("before expand", before_expand.attributes['SelectExpression.num_selected'])
print("after expand: %i" % (after_expand.number_of_particles))
print("average energy" , energy.attributes["Epot_avg"])

1. From your "Compute Property Modifier" I can see that you called the particle property which contains the potential energy "c_eng". Then you modify that property and store it in a property container "Potential Energy". Since this property was not present in the data source yet, you cannot call `pipeline.source.data.particles["Potential Energy"]` but need to do this:
```data = pipeline.compute()
epot = data.particles['Potential Energy']
data.attributes["Epot_avg"] = np.mean(epot)```

If you meant to average the original potential energy, then you can look it up like this:

`epot = pipeline.source.data.particles["c_eng"]`

2. I'm not sure I understand your question correctly, but if you have activated the option "Generate neighbor bonds" in the Voronoi Analysis modifier, you can make use of these bonds to look up all atoms that are part of a coordination polyhedron. First, you would select the central atom. Then you can use an Expand Selection modifier with the option "bonded to a selected particle". The selection than contains the central atom and all neighbor atoms that are part of the coordination polyhedron. Finally, you can use the particle ID's present in your selection to look up the corresponding potential energies and compute an average value, for example.

-Constanze

Dear Dr. Constanze Kalcher

I want to select atoms having the largest 10% of the potential energy in my model. What I am doing is:

1- With “color coding” I secure the range of potential energy in my system, for example, it is from 0 to 10.

2- In "expression selection", I write "potential_energy>9"

If I want to do it for legions of snapshots, it takes a lot of time to calculate the 10% of potential energy with “color coding” for each snapshot one by one. Could I ask you to show me another easy way?

Best Regards,

Lee

Hi,

you can use a short Python script modifier function to do that, e.g.

```from ovito.data import *
from ovito.modifiers import ExpressionSelectionModifier
import numpy as np
def modify(frame, data):
epot = data.particles["Potential Energy"]
data.apply( ExpressionSelectionModifier(expression = 'PotentialEnergy >= {:e}'.format(np.min(epot) + 0.9*(np.max(epot)-np.min(epot)))))```