Surface mesh of given positions

Hello everyone,

I would like to know if there is a method to calculate the surface mesh of a position array (x_array, y_array, z_array), like so:

pipeline = import_file(positions)
probe_radius = 7.0
pipeline.modifiers.append(ConstructSurfaceModifier(
method=ConstructSurfaceModifier.Method.AlphaShape,
radius=probe_radius,
identify_regions=True))
data = pipeline.compute()
mesh = data.surfaces[‘surface’]

to maybe calculate the volume

volume = data.attributes[‘ConstructSurfaceMesh.filled_volume’]


I know that usually you would put a lammps file like this: pipeline = import_file(f"mgel.dump.0"), but I was wondering if I could also put positions there since I have many molecules (microgels) in my box and I want the volume of each molecule/microgel while properly taking care of the boundary conditions.

Thanks in advance for any help!
Leah

You can use the following a PythonSource to get your positions into an OVITO pipeline:

from ovito.data import DataCollection, ParticleType
from ovito.pipeline import Pipeline, PythonSource
from functools import partial


def create(frame: int, data: DataCollection, positions=[]):
    particles = data.create_particles(count=len(positions))
    particles.create_property("Position", data=positions)

    type_property = particles.create_property("Particle Type")
    if len(type_property.types) == 0:
        type_property.types.append(
            ParticleType(id=1, name="Particle", color=(1, 0, 1), radius=1.0)
        )
    type_property[...] = [1] * len(positions)


positions = [...]
pipeline = Pipeline(source=PythonSource(function=partial(create, positions=positions)))

Afterwards you can add your desired modifiers to the pipeline.

Thank you very much for your fast reply! I get an error: Traceback (most recent call last):
File “ratio_volumina.py”, line 14, in
from ovito.pipeline import Pipeline, PythonSource
ImportError: cannot import name ‘PythonSource’ from ‘ovito.pipeline’ (/home/emanuela/.local/lib/python3.8/site-packages/ovito/pipeline/init.py) is my python to old?

Thank you very much for your fast reply! I get an error: Traceback (most recent call last):
File “ratio_volumina.py”, line 14, in
from ovito.pipeline import Pipeline, PythonSource
ImportError: cannot import name ‘PythonSource’ from ‘ovito.pipeline’ (/home/emanuela/.local/lib/python3.8/site-packages/ovito/pipeline/init .py) is my python to old?

This feature was added in OVITO 3.5.0 in May 2021. You can check your OVITO version using:

import ovito
print(ovito.version)

I managed to fix my problem by using a temporary file:

def calculate_surface_mesh_volume(positions):
“”"
Saves positions to a temporary file and computes the surface mesh volume.
“”"
with tempfile.NamedTemporaryFile(suffix=“.xyz”, delete=False) as tmp:
# Write positions in XYZ format
tmp.write(f"{len(positions)}\n\n".encode()) # XYZ file header
for pos in positions:
tmp.write(f"Particle {pos[0]} {pos[1]} {pos[2]}\n".encode())

    tmp_filename = tmp.name

# Load the temporary file into OVITO
pipeline = import_file(tmp_filename)

# Apply the ConstructSurfaceModifier to compute the surface mesh
probe_radius = 7.0
pipeline.modifiers.append(ConstructSurfaceModifier(
    method=ConstructSurfaceModifier.Method.AlphaShape,
    radius=probe_radius,
    identify_regions=True
))
data = pipeline.compute()

# Retrieve the filled volume attribute
shell_volume = data.attributes['ConstructSurfaceMesh.filled_volume']

# Clean up temporary file
os.remove(tmp_filename)

return shell_volume

thank you so much for your fast help!