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

Expression selection - select only maximum value of each frame


I'm relatively new to OVITO. I am trying to isolate a single atom in my system that has the highest potential energy in each frame. My potential energy variable is c_3, so I tried using the expression selection with boolean expression c_3 == max(c3), but this still selects all of the atoms instead of the one with the maximum energy. Is there another way to do this?



Uploaded files:
  • You need to login to have access to uploads.

Hi Nadia,

First, regarding the Expression Selection modifier: This modifier only supports expressions that involve local properties of the current particle. Its doesn't support reduction operations that take into account the properties of all other particles. And the max() function is not as smart as you might think (or wish). It only returns the largest of its arguments, which must all be scalar numbers. If you give it a single argument, it will simply return that argument, which is a useless behavior and explains why all particles get selected in your case.

OVITO provides other selection tools, for example the Histogram modifier, letting you select those particles with property values in the upper range of a distribution. But the selection threshold must be specified manually, which renders them useless if you want to dynamically identify the particle having the highest value in each simulation frame.

The last resort in such a situation is to write a Python modifier function to solve your problem. In this case, it's also the preferable solution, I would say. Selecting the particle with the largest c_3 value is straightforward:

import numpy

def modify(frame, data):
    c_3 = data.particles['c_3']
    selection = data.particles_.create_property('Selection')
    selection[numpy.argmax(c_3)] = 1

The function creates the Selection particle property (default initialized to 0). It then determines the index of the particle having the highest c_3 value and changes its Selection property to 1.

Let me know if you have any further questions.