Python script that is calculating Temperature incorrectly

Dear OVITO Team,

I hope you are well.

I am writing to request your help with a Python script that is calculating Temperature incorrectly. When I use the manual expression feature in OVITO, I get the correct value. However, when I run my Python script on the same data, it produces a different and incorrect value.

Could you please review my script and help me find the cause of this error?

Thank you very much for your time and assistance.

Best regards,
Abdo
crystal

#Import necessary OVITO libraries
from ovito.io import import_file
from ovito.modifiers import VoronoiAnalysisModifier, ExpressionSelectionModifier
import numpy as np

#This is the list of temperatures to be used for each frame.
temperatures = [
300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000,
1050, 1100, 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500, 1550, 1600, 1650,
1700, 1750, 1800, 1850, 1900, 1950, 2000, 2050, 2100, 2150, 2200, 2250, 2300,
2350, 2400, 2450, 2500
]

#Load your simulation trajectory file.
pipeline = import_file(“…/cooling.lammpstrj”)

#1. Perform Voronoi tessellation analysis.
pipeline.modifiers.append(VoronoiAnalysisModifier(compute_indices=True))

#2. Define the selection expression based on your Voronoi index criteria.
selection_expression = “(VoronoiIndex.3 == 0 && VoronoiIndex.4 == 4 && VoronoiIndex.5 == 4 && VoronoiIndex.6 == 7) ||
(VoronoiIndex.3 == 0 && VoronoiIndex.4 == 4 && VoronoiIndex.5 == 4 && VoronoiIndex.6 == 6) ||
(VoronoiIndex.3 == 0 && VoronoiIndex.4 == 4 && VoronoiIndex.5 == 4 && VoronoiIndex.6 == 5)”

#3. Apply the selection modifier to tag the matching atoms.
pipeline.modifiers.append(ExpressionSelectionModifier(expression=selection_expression))

#Print the header for the output data
print(“Temperature(K) Fraction_Voronoi(%)”)

#Loop through each frame (timestep) of the simulation
#The loop will stop if it runs out of frames or temperatures.
num_steps_to_process = min(pipeline.source.num_frames, len(temperatures))
for frame in range(num_steps_to_process):
# Process the data pipeline for the current frame
data = pipeline.compute(frame)

# Get the temperature from the predefined list using the frame index.
temperature = temperatures[frame]

# Get the total number of atoms
total_atoms = data.particles.count

if total_atoms > 0:
    # --- MODIFIED SECTION ---
    # Instead of reading a summary attribute, we will count the selected particles manually.
    # The 'ExpressionSelectionModifier' creates a 'Selection' property for each particle (1=selected, 0=not).
    
    # Get the 'Selection' property array from all particles.
    selection_property = data.particles['Selection']
    
    # Count how many particles are non-zero (i.e., selected).
    num_selected = np.count_nonzero(selection_property)
    # --- END OF MODIFIED SECTION ---

    # Calculate the fraction as a percentage
    fraction_percentage = (num_selected / total_atoms) * 100

    # Print the temperature and the calculated fraction
    print("{:<15.2f} {:<20.2f}".format(temperature, fraction_percentage))

To make it easier for others to read, understand, and debug your code, please ensure it is properly formatted. More information here: Posting code or preformatted text - Using Discourse - Discourse Meta

1 Like

Thanks so much for getting back to me. I really appreciate it.

I’m new to this, so I was hoping you could please take a look at my Python script (attached) . Sorry in advance if it’s a bit messy!
attached:
Voronoi.txt (2.5 KB)

Thanks for your time and any help you can provide.

Abdo