How to delete voronoi cell in python script

I was wondering how to delete specific voronoi cells?

I use:
psel = data.particles_.create_property(“Selection”)
psel[pdel] = 1
data.apply(DeleteSelectedModifier(operate_on ={‘particles’}))
to delete specific particles in the python script. Is there some way similar that I could delete the voronoi cells?

Yes, similar to how you can create properties for particles, you can do that for the vertices, faces, and regions of a SurfaceMesh object, like the one containing the Voronoi polyhedra.

data.surfaces['voronoi-polyhedra_'].regions_.create_property("Selection")

Just a quick tip: You can easily find the Python identifier of an OVITO Data Table or Surface Mesh by opening the Data Inspector in the OVITO Desktop Application and hovering over its name with the mouse. The attached screenshot demonstrates that the regions property container is where you can find additional properties of the Voronoi cells, such as their respective volume or surface area.

You can directly reference those properties in an Expression Selection modifier to select and delete individual Voronoi cells when you choose “Operate on: Voronoi polyhedra → Mesh Region” from the drop-down menu.

If you’re unsure, OVITO Pro’s Python Code generator can assist you in translating your workflow into the corresponding sequence of Python statements.

Great thanks, that helped a lot. I am trying to calculate the surface area of specific faces corresponding to specific bonds. However, once I delete the particles, voronoi cells, and bonds I want, I am still struggling to find a way to match the bonds to the faces they pass through since the “Bond Index” property doesn’t handle the deleted particles, aka the indexing matches the original structure before I deleted particles. Which means that it will index at something like 10000 even if I only have 200 bonds left.

Yes, I see. I think a simple solution would be to create an actual “Bond property” called “Bond Index” containing the original bond indices (before anything is deleted).
That way you can still match Voronoi bonds to the corresponding Voronoi face they pass through even after you delete some bonds.

Feel free to contact me through the Pro support email if you’d like to discuss this in further detail.

One thing I forgot to add yesterday: It actually shouldn’t be necessary to use the Voronoi bond information to find Voronoi faces that separate particle pairs of different types. Each Voronoi face is already associated with the two particles it separates through their Region and Adjacent Cell properties, which essentially are the particle indices. You can use those to look up their respective particle types.

Say you want to find all Voronoi faces separating particle pairs of type 1 and 2. Then, the following modifier function will do the job as long as you do not delete any particles or Voronoi cells in a previous step in your pipeline.

def modify(frame: int, data: DataCollection, type1 = 1, type2 = 2):
    
    mesh = data.surfaces['voronoi-polyhedra_']
    ptypes = data.particles['Particle Type']
    sel = mesh.faces_.create_property("Selection", data = numpy.zeros(mesh.faces.count))
    
    A = ptypes[mesh.faces["Region"]]
    B = ptypes[mesh.faces["Adjacent Cell"]]
      
    sel[:] = numpy.logical_not(numpy.logical_and(A == type1, B == type2)) 
    mesh.delete_faces( mesh.faces['Selection'] == 1) 

Note that for each particle pair (or Voronoi bond for that matter), you will have two faces listed in the surface mesh face property container, i.e., each face has an “opposite face” with the same vertices (belonging to the neighboring Voronoi cell). To find “both sides”, so to say, you could do:

numpy.logical_or( numpy.logical_and(A == type1, B == type2) , numpy.logical_and(A == type2, B == type1) )

Lastly, I’m not sure if you are trying to use this to calculate interface areas.

https://matsci.org/t/cluster-surface-area/54867

You can add up the surface areas of the remaining faces like this: numpy.sum(mesh.faces["Area"]), but I would take that number with a grain of salt. The “interface” in the picture above, for example, looks quite rugged, so it’s likely that you’re overestimating the actual interface area you’re trying to find - or surface area of crystallites, like you mentioned in the other thread. Just something to keep in mind.