Author Topic: Changing Particle Shape Automatically  (Read 344 times)

ellipsoid

  • Newbie
  • *
  • Posts: 2
Changing Particle Shape Automatically
« on: March 20, 2018, 09:35:34 PM »
Hello,

I've been trying to automatically change the shape of my simulation from sphere to ellipsoids. I understand one can interactively load the trajectory then change the property type through the Python script modifier. However I'd like for OVITO to do this automatically by simplying passing the script file, or loading it completely from batch with the --gui flag. In the interactive mode I can edit the python script as follows and it works:

----------------------------------------------------------------------
from ovito.data import *

def modify(frame, input, output):
        output.create_particle_property(ParticleProperty.Type.AsphericalShape, [0.5,2.,0.5])
        output['Aspherical Shape'].changed()
----------------------------------------------------------------------

But when I tried to load it from the command line with the --script flag or the menu from Scripting -> run script file it doesn't work (does nothing).

I've then tried doing it directly from a batch script as follows:

----------------------------------------------------------------------
from ovito.io import *
from ovito.vis import *
from ovito.data import *
from ovito.modifiers import *
import numpy

#----------------Begin script
#Import file
filename="trajectory.gsd"
node = import_file(filename, multiple_frames = True)

def modify(frame, input, output):
    output.create_particle_property(ParticleProperty.Type.AsphericalShape, [0.5,2.,0.5])
    output['Aspherical Shape'].changed()

node.modifiers.append((PythonScriptModifier(function = modify )))

node.add_to_scene()
node.compute()
----------------------------------------------------------------------

But when I run it it returns a python script error claiming that 'ParticleProperty' is not defined. There are other things I've tried and the best that I've been able to achieve is to display a single frame with the right shapes only to reset and give the same error when I run the rest of the trajectory. It seems like it should be a fairly simple thing to implement but I've run out of things to try.

I appreciate any and all help.



Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 512
Re: Changing Particle Shape Automatically
« Reply #1 on: March 21, 2018, 06:44:02 AM »
Hi,

What is the best strategy in this case depends on whether the data for the "Aspherical Shape" property should be loaded from the input file or is to be generated on the fly after loading. From the scripts you posted I read that you want to assign the same aspherical shape (0.5,2.,0.5) to all particles. Is that right?

Let's assume you want to set the aspherical shape property for all particles to the same uniform value. In this case, the Compute Property modifier is your best choice. You can use it to set the component values of the "Aspherical Shape" particle property to specific values such as 0.5, 2.0 and 0.5.

There are different ways to apply this modifier: You could do it by hand within the GUI and enter the values for the three components. But then you would have to do this every time you are loading a new simulation file. One way to simplify this step is to define a modifier preset. Then setting the aspherical shape takes just one or two clicks.

The third option is to use scripting commands. Since the Compute Property modifier is already doing the job of setting the property for you, you won't need to write a user-defined Python modifier. You only need to insert an instance of the ComputePropertyModifier into the current data pipeline.

When you run ovito (not ovitos) from the terminal with the --help command line option, you will see that it supports the --exec option, which lets you execute arbitrary Python commands after program startup. We can use it to insert the ComputePropertyModifier after the simulation file has been loaded:

Code: [Select]
ovito --exec "import ovito; from ovito.modifiers import ComputePropertyModifier; ovito.dataset.selected_node.modifiers.append(ComputePropertyModifier(output_property='Aspherical Shape', expressions=['0.5','2.0','0.5']))" <datafile>

Alternatively, you can put these script commands into a .py file and use the --script command line option to execute the script file on program startup. The effect is the same as if you would manually choose Run Script File from the menu in the GUI.

-Alex

ellipsoid

  • Newbie
  • *
  • Posts: 2
Re: Changing Particle Shape Automatically
« Reply #2 on: March 22, 2018, 01:17:06 AM »
Works great! I knew there had to be a simple solution. However, I admit, based on documentation, it would have been difficult for me to figure this out on my own. Are there any particular baby-step tutorials that you'd recommend? The ones provided on the website such as the Examples entry in the manual seem to make a lot of implicit assumptions about what is understood and not and are not too helpful.

Either way, thank you for your help Alex!