Author Topic: Averaging over the frames  (Read 494 times)

Sahari

  • Newbie
  • *
  • Posts: 8
Averaging over the frames
« on: January 04, 2019, 02:22:58 AM »
Dear all,

I need to have the average of the properties such as the charges or the velocities in the scatter plots and histograms over all the frames or some specific frames, but I could not find a way to generate these averages. I think I need to use python, but I am not an expert in python or ovito. I would really appreciate if anyone could help me with this.

Best,

Sahar

Constanze Kalcher

  • Administrator
  • Sr. Member
  • *****
  • Posts: 291
Re: Averaging over the frames
« Reply #1 on: January 04, 2019, 12:07:21 PM »
Dear Sahar,

you're right, the way to achieve this is to use write a short batch script and execute it with ovitos, the python script interpreter that comes with ovito. Please see the documentation about running scripts first
http://ovito.org/manual_testing/python/introduction/running.html

I can also recommend this introduction into the topic that explains the data pipeline concept and how to import data and apply modifiers (analogous to the way you would do it in the graphical user interface).
http://ovito.org/manual_testing/python/introduction/overview.html

As an example, say you have a particle property "My property" and you would like to calculate per-atom values averaged over all frames. In principle, you just need to loop over all frames (or only specific frames) and then sum up your desired property e.g. like this:

Code: [Select]
from ovito.io import import_file
from ovito.modifiers import *
import numpy as np

# Import a sequence of files.                                                                                                                                                                                                         
pipeline = import_file('simulation*.dump')

#Add modifiers here                                                                                                                                                                                                                   
#pipeline.modifiers.append(Name of modifier)                                                                                                                                                                                           

my_prop = np.empty( pipeline.source.data.particles['My Property'].shape )

# Loop over all frames:                                                                                                                                                                                                               
for frame_index in range(pipeline.source.num_frames):
    #The computation results for each frame can be requested using compute()                                                                                                                                                           
    data = pipeline.compute(frame_index)
    my_prop += np.asarray(data.particles['My Property'])

average = my_prop/pipeline.source.num_frames

np.savetxt("average.txt", average)


Hope this will serve you as a good starting point. Let me know if you have further questions.

-Constanze

Constanze Kalcher

  • Administrator
  • Sr. Member
  • *****
  • Posts: 291
Re: Averaging over the frames
« Reply #2 on: January 04, 2019, 02:04:08 PM »
Since you also asked about scatter plots and histograms, maybe there is an easier solution for you. If you're using the latest developer version of OVITO, you could export the time series of scatter plots or histograms you computed as text files (as shown in the attached screenshot).
In a second step, then write a simple python script (you won't need ovitos for this) that reads in all these textfiles files and calculates the average. Let's say you have files that are named according to the following pattern: histo_1.txt, histo_2.txt ...etc.

Then the following python script should do the job:
Code: [Select]
import numpy as np
import glob
Y = []
for file in glob.glob('histo_*.txt'):
    x, y = np.loadtxt(file, unpack = True)
    Y.append(y)
np.savetxt( "average_histo.txt", np.column_stack( (x, np.mean(Y, axis = 0))))

-Constanze

Sahari

  • Newbie
  • *
  • Posts: 8
Re: Averaging over the frames
« Reply #3 on: January 05, 2019, 02:00:35 AM »
Thank you very much for your help. It was absolutely so much useful.

Best,

Sahar