Author Topic: Python Modifier Script  (Read 1365 times)

dekoning

  • Newbie
  • *
  • Posts: 9
Python Modifier Script
« on: November 22, 2016, 07:25:53 PM »
I'm trying to use the Python script option from the Modifier Dropdown menu. The first thing I tried was to use the example in
the documentation that explains the use of the DataCollection.copy_if_needed() method. It is copied below.
My problem is that the script does not get past the first assert test, whether the cell of the input and output DataObjects are the same.
So apparently they are different. Any idea what' going on?

def modify(frame, input, output):

    # Original simulation cell is passed through by default.
    # Output simulation cell is just a reference to the input cell.
    assert(output.cell is input.cell)

    # Make a copy of the simulation cell:
    cell = output.copy_if_needed(output.cell)

    # copy_if_needed() made a deep copy of the simulation cell object.
    # Now the the input and output each point to different objects.
    assert(cell is output.cell)
    assert(cell is not input.cell)

    # Now it's safe to modify the object copy:
    cell.pbc = (False, False, False)

Alexander Stukowski

  • Administrator
  • Sr. Member
  • *****
  • Posts: 465
Re: Python Modifier Script
« Reply #1 on: November 23, 2016, 07:57:13 AM »
Unfortunately, the version of Ovito that you are using (I assume it's 2.7.1) contains a bug in the implementation of the "is" comparison operator, which always evaluates to false. That is why the assertion from the example fails even though the original assumption still holds: input and output cell both point to the same internal object.

Please remove the assert() statements and proceed with writing your own modifier, or install the current development release of Ovito, which comes with a completely new implementation of the Python bindings.

Let me know if you need further help with writing the modifier script. The next steps depend on what you would like your modifier to do.

dekoning

  • Newbie
  • *
  • Posts: 9
Re: Python Modifier Script
« Reply #2 on: November 23, 2016, 12:15:41 PM »
Thanks Alexander,

Yes, I am using version 2.7.1.

The modifier I would like to write should allow one to select Molecules (not atoms) according to some positional information. For instance, considering a cell containing water molecules, I would like to select those molecules with the z-coordinates of the oxygens within certain limits.

Any suggestions?

Alexander Stukowski

  • Administrator
  • Sr. Member
  • *****
  • Posts: 465
Re: Python Modifier Script
« Reply #3 on: November 26, 2016, 05:07:38 PM »
I leave it to you to work out an actual algorithm to select the molecules. But here is the boilerplate code you need to access the positions, types and molecule IDs of atoms, and to create the output selection property. Set the corresponding entries in the selection array to 1 for atoms that you would like to select.

Code: [Select]
from ovito.data import *

def modify(frame, input, output):
    position = input.particle_properties.position.array
    type = input.particle_properties.particle_type.array
    molecule_id = input.particle_properties.molecule_identifier.array
    selection = output.create_particle_property(ParticleProperty.Type.Selection).marray
    ...
« Last Edit: November 26, 2016, 05:11:32 PM by Alexander Stukowski »

dekoning

  • Newbie
  • *
  • Posts: 9
Re: Python Modifier Script
« Reply #4 on: December 08, 2016, 12:48:33 PM »
Thanks, that worked.