Author Topic: Loop over the frames calculation  (Read 194 times)

akhoursh

  • Newbie
  • *
  • Posts: 18
Loop over the frames calculation
« on: February 04, 2019, 10:34:47 PM »
Dear Ovito Users,
Hi,

Is there any way I can average a specific property with looping over all the frames using Python modifier ? If so, I wonder if you could please upload a sample code showing the procedure. I'm using ovito 3 dev 234, and I guess I should use " for file in sorted ... "  command.
Thanks,
Ali
« Last Edit: February 05, 2019, 03:28:00 AM by akhoursh »

Constanze Kalcher

  • Administrator
  • Full Member
  • *****
  • Posts: 233
Re: Loop over the frames calculation
« Reply #1 on: February 05, 2019, 10:19:10 AM »
Dear Ali,

you can do it using a small batch script that you need to execute with ovitos.  Say you're calculating a system average of a particle property "My Property" and then also want to average that over different frames. This can be done like this for example:
Code: [Select]
pipeline = import_file("simulation*.dump")
sum = 0.0
for frame in range(pipeline.source.num_frames):
    data = pipeline.compute(frame)
    sum += numpy.average( data.particles["My Property"] )
print( sum/pipeline.source.num_frames )
also see here
http://www.ovito.org/manual_testing/python/introduction/file_io.html

Does that solve your question?

-Constanze
« Last Edit: February 05, 2019, 10:24:26 AM by Constanze Kalcher »

akhoursh

  • Newbie
  • *
  • Posts: 18
Re: Loop over the frames calculation
« Reply #2 on: February 05, 2019, 03:55:32 PM »
Hi Constanze,

Thanks for the response. One more question: What if instead of loading the file in the python, I want to use the files that I've already loaded in the software via load file under file tab, i.e. graphical user tab, ? What I'm saying is that I'm loading the file in the software. Then, I will use python scripting modifier to calculate the average over the frames. In that case, I'm loading before python.
Best,
Ali
« Last Edit: February 05, 2019, 04:16:31 PM by akhoursh »

Constanze Kalcher

  • Administrator
  • Full Member
  • *****
  • Posts: 233
Re: Loop over the frames calculation
« Reply #3 on: February 05, 2019, 05:18:44 PM »
Hi Ali,

that's not possible at the moment, since the python script modifier in the GUI is part of the pipeline which is evaluated for each frame.
It can't manipulate the pipeline itself that it is part of (e.g. adding/removing modifiers) or otherwise change the global program state.
For more details, I'll refer you to the documentation on custom modifiers:
https://ovito.org/manual_testing/python/introduction/custom_modifiers.html

-Constanze

akhoursh

  • Newbie
  • *
  • Posts: 18
Re: Loop over the frames calculation
« Reply #4 on: February 05, 2019, 07:50:30 PM »
Hi Constanze,

I see. So, suppose that I have a series of the files as " dump.tensile.*.cfg ". How can I calculate the average of atomic shear strain for each frame and export it beside the frame number in a text file ?
Thanks,
Ali
« Last Edit: February 05, 2019, 09:28:10 PM by akhoursh »

Constanze Kalcher

  • Administrator
  • Full Member
  • *****
  • Posts: 233
Re: Loop over the frames calculation
« Reply #5 on: February 05, 2019, 09:34:16 PM »
Hi Ali,

to answer your first question, if you simply want to calculate an average of a particle property for each frame you can do it using a custom python script modifier function in the GUI as often discussed in this Forum and explained in the FAQ. The following script (OVITO 3) will calculate the average atomic shear strain, print and save that information as a global attribute for each frame. Global attributes can conveniently be exported to a text file using OVITO's file export function, see Manual https://ovito.org/manual_testing/data_inspector.attributes.html
Code: [Select]
from ovito.data import *
import numpy as np

def modify(frame, data):
    avg_shear_strain = np.mean(data.particles["Shear Strain"])
    print(avg_shear_strain)
    output.attributes["Average shear strain"] = avg_shear_strain

As for your second question, the previous example is a batch script. It's meant to be executed with ovitos (OVITO's script interpreter) from the command line. See the Scripting Manual for more details.
https://ovito.org/manual_testing/python/introduction/running.html
In short, if you have installed OVITO in C:\ProgramFiles, you execute it like this:
Code: [Select]
"C:\Program Files\Ovito\ovitos.exe"or you add that folder to your path variable
Code: [Select]
set PATH=%PATH%;"C:\Program Files\Ovito\"so you don't need to type the whole path every time.
Also don't forget to import the numpy and the ovito.io modules.

-Constanze