How to use Python script to batch output information I need?

Hello everyone! My OVITO version is 2.9.0. I first use “atom strain” and than use “bin and reduce” to get the average value of strain in specific area. In this way I have a strain array in one frame. But I can only save one frame of data. I want to output all frmaes of data in a single dat or txt file. How to write Python script to achieve this? My core question in code is how to define the variable I need? What is the exact form?

For example, I try to define timestep in the MSD example like below:

import numpy
def modify(frame, input, output):

    # Access the per-particle displacement magnitudes computed by an existing 
    # Displacement Vectors modifier that precedes this custom modifier in the 
    # data pipeline:
	displacement_magnitudes = input.particle_properties.displacement_magnitude.array
	
    # Compute MSD:
	msd = numpy.sum(displacement_magnitudes ** 2) / len(displacement_magnitudes)
	step = input.particle_properties.timestep.array
    # Output MSD value as a global attribute: 
	output.attributes["MSD"] = msd 
  
	current_frame = input.attributes['SourceFrame']
	print("In frame {}, the vaule of MSD is {}".format(current_frame, msd))
	
	with open('E:\MD simulation of interfacial friction (20221229-)\modified Morse potential model\ForceRateLoading\Rc122E0001\Rate1e-9\MSD.dat','a+') as f:
		f.write("{} {}\n".format(timestep, msd))

The error is:

The Python script has exited with an error.
Traceback (most recent call last):
  File "<string>", line 13, in modify
  File "D:\Ovito2.9.0\plugins\python\ovito\data\particles\__init__.py", line 82, in __getattr__
    raise AttributeError("DataCollection does not contain the particle property '%s'." % name)
AttributeError: DataCollection does not contain the particle property 'timestep'.

Generally speaking, I do not know how to get the right form of defining timestep in Python script. Where can I get relevant information? I did not find the detailed description in OVITO’s manual.

Any help will be appreciated and thanks advance!

Hi, is there a specific reason you’re using such an outdated version of OVITO?
You can find a detailed introduction into how to use the OVITO Python API to output computational results here: https://www.ovito.org/docs/current/python/introduction/custom_modifiers.html#how-to-output-computational-results
More specifically, the documentation of the Spatial Binning modifier contains code examples that show you how to output the resulting bin grid: https://www.ovito.org/docs/current/python/modules/ovito_modifiers.html#ovito.modifiers.SpatialBinningModifier

1 Like

Thanks for your reply!
Actually I use this old version because it is free to use “PythonScript” function and “Bin and Reduce” function. In the latest version, this two functions and several other are only available in Pro version which requires payment.
Thanks again and I will read the mannual.

You should have a look at the OVITO Python module, which can be used free of charge (MIT license).

Thank you! Do you mean I can leave OVITO’s user graphical interface alone and write python script to help me process data?

Yes, you can execute your non-interactive automation scripts via an external Python interpreter. More details here: https://docs.ovito.org/python/introduction/examples.html#code-examples