Author Topic: call modifiers before import  (Read 135 times)

movaffaq1985

  • Newbie
  • *
  • Posts: 2
call modifiers before import
« on: April 07, 2019, 10:07:02 PM »
Dear Alexander and Constanze

I have a question regarding loops which I couldn't find its answer in the older posts. I have an outer loop that calls different trajectories and an inner loop for frames inside each trajectory. I think my script is heavy because I call modifiers over and over. Is there any way to call modifiers outside both loops when I'm importing trajectory in the outer loop? Any suggestion for the increased performance would be appreciated. Below is a simplified version of the script.

Kind regards,
Movaffaq Kateb, PhD
University of Iceland

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

shape = "cub"
step =        10
num_bins =    1200
range_start=  0.01
range_end=    12.01
bin_size=     (range_end-range_start)/num_bins
cutoff = 12

for shell in range(2,202,2):
    finput = "dump.%d%s" % (shell, shape)
    print(finput)
    # Import a sequence of files.
    node = import_file(finput, columns=["Particle Identifier", "Particle Type", "Position.X", "Position.Y", "Position.Z", "Potential Energy", "Displacement Magnitude"], multiple_frames = True)

    create_bonds_modifier = CreateBondsModifier(cutoff=cutoff, mode=CreateBondsModifier.Mode.Pairwise)
    node.modifiers.append(create_bonds_modifier)

    create_bonds_modifier.set_pairwise_cutoff('Type 1', 'Type 1', cutoff)
    node.modifiers.append(ComputeBondLengthsModifier())

    hist_modifier = HistogramModifier(bin_count = num_bins,particle_property = 'Potential Energy',fix_xrange = True,xrange_start = range_start,xrange_end = range_end)
    node.modifiers.append(hist_modifier)

    # Loop over all/some frames
    for frame in range(0,node.source.num_frames,step):
        print('Current frame: ',frame)
        output = node.compute(frame)
        hist, bin_edges = np.histogram(output.bond_properties.length.array, bins=num_bins,range = (range_start,range_end))
       
        rho = output.number_of_particles / output.cell.volume
        factor = 4./3. * np.pi * rho * output.number_of_particles
   
        radii = bin_edges[:-1]
        radii_right = bin_edges[1:]
        rdf = hist / (factor * (radii_right**3 - radii**3))
   
        result = np.column_stack((radii,rdf))
   
        foutput = "./u3/%d-%s%04d.txt" %(shell,shape,frame)
        np.savetxt(foutput, result,fmt=['%.2f', '%.4f'],header="RDF 0-%.1f(bin size: %.3f, rho: %.10f" %(cutoff,bin_size,rho))

print("Fin!")
« Last Edit: April 08, 2019, 08:59:46 AM by Constanze Kalcher »

Constanze Kalcher

  • Administrator
  • Sr. Member
  • *****
  • Posts: 277
Re: call modifiers before import
« Reply #1 on: April 08, 2019, 12:00:20 PM »
Dear Movaffaq,

I checked your script and it seems fine to me. Whenever you import a new dump file you set up a new pipeline and thus also have to append the modifiers, so everything you did is correct. One thing I noticed though, is that you calculate a histogram of the potential energy which is not used or exported in the following, so you could remove these two lines if you don't need them.

Is your goal to calculate partial RDFS? Note that the latest developer version ovito.3.0.0-dev has a built-in modifier function for that.
http://ovito.org/manual_testing/particles.modifiers.coordination_analysis.html
https://ovito.org/manual/python/modules/ovito_modifiers.html?highlight=rdf#ovito.modifiers.CoordinationNumberModifier.rdf
so you can skip all these extra steps where you first create bonds and then create a histogram etc, which will probably bring quite some speed up.

Let me know if you have questions.

-Constanze

« Last Edit: April 08, 2019, 12:10:17 PM by Constanze Kalcher »

movaffaq1985

  • Newbie
  • *
  • Posts: 2
Re: call modifiers before import
« Reply #2 on: April 08, 2019, 04:30:43 PM »
Dear Constanze
Thanks for your response and suggestion.
Kind regards,
MK