Forum Navigation
You need to log in to create posts and topics.

How to get the length of the box in the X direction

Dear all,

I want to use  the length of the box in the X direction, but it shows the program is wrong. Could someone help me?

Thank you!


Uploaded files:
  • Python.PNG

Hi Shuai,

To query the size of the simulation cell after loading a simulation file use the following expression:

x =[0,0]

Note that cell[0,0] refers to the x-component of the first cell vector.  See the SimulationCell documentation for further information. To position the slice plane in the center of the cell, for example, you may have to additionally add the x-coordinate of the cell's origin unless it it located at (0,0,0) anyway.

Furthermore, note that this will give you only the size of the simulation cell at the beginning of the loaded trajectory. If you want to dynamically position the slicing plane based on a varying simulation box size, you have to take a different approach:

from import *
from ovito.modifiers import *

pipeline = import_file(...)

def slice_center(frame, data):
    x = data.cell[0,0] / 2 + data.cell[3,0]
    data.apply(SliceModifier(distance=x, normal=(1,0,0)), frame)

This script inserts a user-defined modifier function into the data pipeline, which calls the SliceModifier as a subroutine with dynamically computed parameters (see DataCollection.apply() method).


Hi Alex,

Thank you for your help!