### Author Topic: Calculating overall RDF via Python Script  (Read 306 times)

#### benjy

• Newbie
• Posts: 8
##### Calculating overall RDF via Python Script
« on: March 29, 2019, 04:46:32 PM »
Hi all!

I'm a new user to OVITO and have been trying to use it to calculate the overall RDF for a 3 atom simulation that has multiple "frames" (roughly 5000). Since the built in correlation function only looks at single frames I've been trying to build a script that gives the RDF for the entire simulation, using some of the templates on the website and forums as a basis.

So far I've got something that runs and seems to do the RDF calculations for each frame, considering how long it takes, but  the RDF file it outputs is incorrect when compared to what I've gotten from other programs/scripts. Any help on what I'm doing wrong would be greatly appreciated! I've copied the code below:

from ovito.data import *

def modify(frame, input, output):

from ovito.io import import_file
from ovito.modifiers import CoordinationNumberModifier
import numpy

# Load a particle dataset, apply modifier, and evaluate data pipeline.
node = import_file("C:/Python/pos_at.xyz", columns=["Particle Type", "Position.X", "Position.Y", "Position.Z"], multiple_frames = True)

modifier = CoordinationNumberModifier(cutoff = 2.5, number_of_bins = 1000)
node.modifiers.append(modifier)

frames = node.source.num_frames
print("The total number of frames are:",frames)

for frame in range(node.source.num_frames):
output = node.compute(frame)

# Export the computed RDF data to a text file.
numpy.savetxt("C:/Users/Documents/ovito_dat/output_rdf_test.txt", modifier.rdf)

#### Constanze Kalcher

• Sr. Member
• Posts: 301
##### Re: Calculating overall RDF via Python Script
« Reply #1 on: March 29, 2019, 05:23:12 PM »
Hi benjy,

how to calculate a time-averaged RDF is explained in the scripting manual of the latest developer version:
http://www.ovito.org/manual_testing/python/modules/ovito_modifiers.html?highlight=coordination%20analysis#ovito.modifiers.CoordinationAnalysisModifier
You can simply copy the code example and adapt the cutoff and number of bins in the coordination analysis modifier to your needs.
Let me know if you have questions.

If you're not comfortable with the python scripting interface (yet) you could also achieve this with the help of the graphical user interface of OVITO as discussed here:
http://forum.ovito.org/index.php?topic=450.msg1695#msg1695

-Constanze

#### benjy

• Newbie
• Posts: 8
##### Re: Calculating overall RDF via Python Script
« Reply #2 on: March 29, 2019, 09:36:14 PM »
Constanze,

Thank you so much for your assistance! I'm now able to generate global RDF for datasets, however I realized I need to specify periodic boundary conditions for the data sets. I know you can do it in the program on a frame by frame basis but is there a way to force it for each frame in the same Python script for RDF calculation? I'm trying to specify a box from -5 to 5 in the X, Y, and Z directions.

Thanks!

#### Constanze Kalcher

• Sr. Member
• Posts: 301
##### Re: Calculating overall RDF via Python Script
« Reply #3 on: April 01, 2019, 10:57:26 AM »
Hi benjy,

you can define a custom modifier function that takes care of that. Just make sure to add this to your code before the Coordination analysis modifier is added to the pipeline, because the order of the modifiers matters (like in the GUI).

Code: [Select]
`def set_cell(frame, data):    with data.cell_:        data.cell_[:,0] = [10., 0., 0.]        data.cell_[:,1] = [0., 10., 0.]        data.cell_[:,2] = [0., 0., 10]        #cell origin        data.cell_[:,3] = [-5.,   -5.  ,  -5.]        #set periodic boundary conditions        data.cell_.pbc = (True, True, True)node.modifiers.append(set_cell)`
or in short:

Code: [Select]
`def set_cell(frame, data):    with data.cell_:        data.cell_[...] = [[10., 0., 0., -5.], [0., 10., 0., -5.], [0., 0., 10., -5.]]        data.cell_.pbc = (True, True, True)node.modifiers.append(set_cell)`
-Constanze
« Last Edit: April 02, 2019, 11:30:59 AM by Constanze Kalcher »