Author Topic: Python Script Loop  (Read 30 times)

jhart

  • Newbie
  • *
  • Posts: 19
Python Script Loop
« on: September 12, 2018, 07:23:06 AM »
Hello,

I have the script below. Notice that I read in a file that is 200eV.dump and it creates a single column of data, the number of vacancies at each frame. I actually have a lot of dump files, that start at 002eV and increase up to 200eV with no repeating pattern (i.e. 002eV.dump, 0.005eV.dump, 0.010eV.dump etc).

How can I loop through all of these files without having to manually read in each dump file for each energy?

The script writes out the number of defects at each timestep for each defect type

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

node = import_file("../200eV.dump",multiple_frames = True)

# Perform Wigner-Seitz analysis:
ws = WignerSeitzAnalysisModifier(
    per_type_occupancies = True,
    eliminate_cell_deformation = True)
ws.reference.load("../../../WS_Ref/ws.dump")
node.modifiers.append(ws)

# Define a modifier function that selects sites of type A=1 which
# are occupied by exactly one atom of type B=2.
def modify(frame, input, output):

    # Retrieve the two-dimensional Numpy array with the site occupancy numbers.
    occupancies = input.particle_properties['Occupancy'].array
   
    # Get the site types as additional input:
    site_type = input.particle_properties.particle_type.array

    # Calculate total occupancy of every site:
    total_occupancy = np.sum(occupancies, axis=1)

    # Set up a particle selection by creating the Selection property:

    selection1 = (site_type == 1) & (occupancies[:,0] == 0) & (occupancies[:,1] == 0)
   
    output.attributes['Ca_Vac'] = np.count_nonzero(selection1)


# Insert Python modifier into the data pipeline.
node.modifiers.append(PythonScriptModifier(function = modify))

# Let OVITO do the computation and export the number of identified
# antisites as a function of simulation time to a text file:
export_file(node, "defects_200.txt", "txt",
    columns = ['Timestep', 'Ca_Vac'],
    multiple_frames = True)


Alexander Stukowski

  • Administrator
  • Sr. Member
  • *****
  • Posts: 464
Re: Python Script Loop
« Reply #1 on: September 12, 2018, 09:57:41 AM »
Hi,

Here is how it is typically done: Put the existing call to export_file() into a Python for-loop and repeatedly use FileSource.load() to replace the input of the data pipeline with a new file:

Code: [Select]
import os

# ... do the pipeline setup here

for filename in files_to_process:

    # Set a new input file for the current data pipeline:
    node.source.load(filename, multiple_frames = True)

    # Derive the name of the output txt file from the input dump file:
    output_filename = os.path.splitext(filename)[0] + ".txt"

    # Let OVITO do the processing:
    export_file(node, output_filename, "txt",
        columns = ['Timestep', 'Ca_Vac'],
        multiple_frames = True)

The list files_to_process should contain the names of the dump files you want to process.