Author Topic: Applying ConstructSurfaceModifier to multiple selected clusters.  (Read 403 times)

shanis

  • Newbie
  • *
  • Posts: 11
Dear ovito users,

If I have 10 clusters, out of which I want to select only 5 of them and apply the ConstructSurfaceModifier to each of them to obtain volume of each cluster, how could I achieve this ? Currently I can only select one cluster and apply the modifier. I can't use python variable to change the SelectExpressionModifier so I am thinking of an alternative way. Following is a code snippet that I am using to construct the surface.

Code: [Select]
# select cluster
clusterselect_modifier = SelectExpressionModifier(expression = 'Cluster == 1')
pipeline.modifiers.append(clusterselect_modifier)
data = pipeline.compute()

# apply surface mesh on selected cluster
pipeline.modifiers.append(ConstructSurfaceModifier(only_selected = True))
data = pipeline.compute()

volume = pipeline.output.attributes['ConstructSurfaceMesh.solid_volume']

Thanks a lot !





« Last Edit: May 06, 2019, 11:16:55 AM by Constanze Kalcher »

Constanze Kalcher

  • Administrator
  • Sr. Member
  • *****
  • Posts: 301
Re: Applying ConstructSurfaceModifier to multiple selected clusters.
« Reply #1 on: May 06, 2019, 11:16:24 AM »
Dear shanis,

in order to help you, I need to know what makes the five clusters you're trying to select different from the others? For example, if you wanted to get the 5 biggest clusters out of those 10 you could activate the option "Sort clusters by size"
https://www.ovito.org/manual_testing/particles.modifiers.cluster_analysis.html
and then create a selection based on their cluster ID's
Code: [Select]
clusterselect_modifier = SelectExpressionModifier(expression = 'Cluster <= 5')]Just let me know a little bit more about your system.

-Constanze

shanis

  • Newbie
  • *
  • Posts: 11
Re: Applying ConstructSurfaceModifier to multiple selected clusters.
« Reply #2 on: May 08, 2019, 05:42:12 PM »
Thanks Constanze,

Yes, I am sorting the clusters based on size, and I can see that using the relational operator <= , as you suggested computes the surface mesh on all selected clusters. However, I also want to output the surface volume values as a global attribute for all of the selected clusters, but currently I only get one value for surface mesh using the above code. Alternatively, I can repeat the code for each cluster separately and get the volumes for each clusters that way, but this is not very efficient when there are many clusters. Let me know if you have other suggestions.

Best,

Constanze Kalcher

  • Administrator
  • Sr. Member
  • *****
  • Posts: 301
Re: Applying ConstructSurfaceModifier to multiple selected clusters.
« Reply #3 on: May 10, 2019, 11:48:25 AM »
Dear shanis,

ah okay, sorry I somehow missed that you would like to get the individual cluster volumes instead. You're right, at the moment this is not possible yet by only applying the Surface mesh modifier once, but it is very likely a feature of the next release. So for now, you will have to use a little workaround, like you already suggested in your previous post, e.g. by looping over the Cluster-ID's and then applying the Surface mesh modifier to each individual cluster, e.g. :

Code: [Select]
   
...
clusterselect_modifier = SelectExpressionModifier()
pipeline.modifiers.append(clusterselect_modifier)
pipeline.modifiers.append(ConstructSurfaceModifier(only_selected = True))
for clusterID in range(1,6):
    my_expression = "Cluster == {}".format(clusterID)
    clusterselect_modifier.expression = my_expression
    data = pipeline.compute()
    print(pipeline.output.attributes['ConstructSurfaceMesh.solid_volume'])

-Constanze

shanis

  • Newbie
  • *
  • Posts: 11
Re: Applying ConstructSurfaceModifier to multiple selected clusters.
« Reply #4 on: May 20, 2019, 08:45:51 PM »
Thanks Constanze. This works for a single frame analysis. Is there a way I can use it to analyze multiple frames/all frames of a trajectory ? -

Constanze Kalcher

  • Administrator
  • Sr. Member
  • *****
  • Posts: 301
Re: Applying ConstructSurfaceModifier to multiple selected clusters.
« Reply #5 on: May 22, 2019, 03:04:14 PM »
Hi,

yes, have a look at the manual for the python scripting interface:
http://ovito.org/manual/python/introduction/file_io.html
or
http://ovito.org/manual_testing/python/introduction/file_io.html if your using the latest developer version.
In general, you would load your file(-sequence) and set up your pipeline by adding all the modifiers. Then, you can just
loop over all the frames of your loaded animation sequence like this:
Code: [Select]
# Load a sequence of simulation files 'frame0.dump', 'frame1000.dump', etc.
node = import_file("simulation*.dump")

# Set up data pipeline, apply modifiers as needed, e.g.
#node.modifiers.append(...)

for frame in range(node.source.num_frames):

    # This loads the input data for the current frame and
    # evaluates the applied modifiers:
    output = node.compute(frame)

    # Work with the computation results
    ...

-Constanze

shanis

  • Newbie
  • *
  • Posts: 11
Re: Applying ConstructSurfaceModifier to multiple selected clusters.
« Reply #6 on: May 28, 2019, 09:06:02 PM »
Thanks Constanze ! Your suggestions have been very useful. I am coming back with two problems now.

1. Using the for loop for frames doesn't work with the work around you suggested for obtaining the mesh.

2. When I use the more primitive way,  and try to export (for all frames using the GUI) the volumes as the global attribute, in the middle of the saving process I get the errror: ERROR: The global attribute 'ConstructSurfaceMesh.solid_volume.47' to be exported is not available at animation frame 50.
--when I look at the frame, looks like ovito wasn't able to construct mesh for a cluster. The error message: Cannot construct mesh for this input dataset. Adjacent cell face not found.

Please let me know if there is a solution to this problem, or if I just have to skip those problematic frames (which is not ideal when I have large number of frames to process as I won't know before hand which frame will give me an error).

Best,

.....
clusterselect = SelectExpressionModifier(expression = 'Cluster == 40')
pipeline.modifiers.append(clusterselect)
pipeline.modifiers.append(ConstructSurfaceModifier(radius=2.0, only_selected = True))
data = pipeline.compute()
.
.
clusterselect_modifier = SelectExpressionModifier(expression = 'Cluster == 47')
pipeline.modifiers.append(clusterselect_modifier)
pipeline.modifiers.append(ConstructSurfaceModifier(radius=2.0, only_selected = True))
data = pipeline.compute()

Constanze Kalcher

  • Administrator
  • Sr. Member
  • *****
  • Posts: 301
Re: Applying ConstructSurfaceModifier to multiple selected clusters.
« Reply #7 on: June 04, 2019, 10:45:37 AM »
Hello shanis,

1. can you explain why it does not work for you? In principle you should be able to loop over all frames and clusters without any problem.
2. If you like you can upload that problematic frame for us here so we can try to understand the problem. For now, in order to avoid your analysis script to crash you could use a try/except python statement.

-Constanze

shanis

  • Newbie
  • *
  • Posts: 11
Re: Applying ConstructSurfaceModifier to multiple selected clusters.
« Reply #8 on: June 06, 2019, 08:24:36 PM »
Hi Constanze,

1. By not working I mean, that the number of clusters that I want to get the surface of is different for each frame. So the value of the range in the for loop below should itself be a variable that depends on the output of the cluster analysis modifier, and this I am not able to think of how to do properly.

for clusterID in range(1,6):
    my_expression = "Cluster == {}".format(clusterID)
    clusterselect_modifier.expression = my_expression
    data = pipeline.compute()
    print(pipeline.output.attributes['ConstructSurfaceMesh.solid_volume'])

Best,

Constanze Kalcher

  • Administrator
  • Sr. Member
  • *****
  • Posts: 301
Re: Applying ConstructSurfaceModifier to multiple selected clusters.
« Reply #9 on: June 07, 2019, 11:47:03 AM »
Hi shanis,

yes, the number of clusters can of course vary in every frame. Which clusters would you like to further examine? From your first post I assumed that it's the biggest five. Seems that that's not the case.

 
-Constanze