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

Output the center-of-mass of clusters in a loop

Hi,

I want to output the center-of-mass of each cluster. The script (uploaded below) worked out well if  I replace ClusterNum in

pipeline.modifiers.append(ExpressionSelectionModifier(expression = 'Cluster == ClusterNum')) with a specific number, such as 1.  But it seems that variable is not supported in the argument. I might use a shell sed command to run the script and output the data in a loop, but I wonder if there is a better solution.

Thanks

LLL

 

Uploaded files:

Hi Liuliu,

note that the expression is a string, so you would have to do the following:

pipeline.modifiers.append(ExpressionSelectionModifier(expression='Cluster=={}'.format(ClusterNum)))

For more details, have a look at any tutorial that explains string formatting in python, e.g. https://pyformat.info/. In order for your script to work, you will have to edit the ExpressionSelectionModifier for every ClusterNum, though, and also call pipeline.compute() again every time.

However, since you're using a python script there is an easier way. You can directly use the particle property "Cluster" which contains the Cluster IDs (computed by the ClusterAnalysisModifier) to look up the positions of all particles belonging to the same cluster, i.e.

#ovito.3.0.0.
#Center of mass of cluster
from ovito.io import *
from ovito.data import *
from ovito.modifiers import ExpressionSelectionModifier, DeleteSelectedModifier, ClusterAnalysisModifier
from ovito.pipeline import *
import numpy as np

pipeline = import_file('dump.restart7.750000')
#delete the particle type 1
pipeline.modifiers.append(ExpressionSelectionModifier(expression = 'ParticleType == 1'))
pipeline.modifiers.append(DeleteSelectedModifier())
#analysis the cluster of type 2
pipeline.modifiers.append(ClusterAnalysisModifier(cutoff = 2.48, sort_by_size = True))

data = pipeline.compute()
pos = data.particles.position
cluster = data.particles.cluster   

for ClusterNum in range(data.attributes['ClusterAnalysis.cluster_count']):  
    print(ClusterNum, np.mean(pos[cluster == ClusterNum], axis = 0) ])

-Constanze

 

Hi Constanze,

It's enlightening. Thanks a lot for your nice help!

LLL