Sorting a vector in LAMMPS?

Hi everyone,

I want to create a fix halt that would trigger when 1st and 2nd biggest clusters in my system are > some N. I can do this for just the 1st bigger cluster by

# Define clusters based on the proximity of the core atoms:
group           coreatoms type  1 2 3

compute         cluster coreatoms cluster/atom 1.5
# assigns each atom in the group an ID=cluster_ID

compute         cc1 coreatoms chunk/atom c_cluster compress yes
# remove redundant cluster_ID-s

compute         clustersize coreatoms property/chunk cc1 count
# count number of atoms in each chunk

variable        maxclustersize equal max(c_clustersize)

...

fix		        halt_maxcl all halt 1000 v_maxclustersize > 500 error continue

I want something like

...
compute         clustersize coreatoms property/chunk cc1 count
compute_modify  clustersize sort decreasing
...
fix		        halt_maxcl2 all halt 1000 ((c_clustersize[1] > 500) && (c_clustersize[2] > 500)) error continue

but I did not find anything like this in compute_modify or any mentions of sorting for property/chunk computes.

Is there an easy way to do this?

How feasible is it (compared to e.g. implementing a custom bond_style which I’ve done) to implement a custom function max2nd(c_ID) to then do

variable        cs1 equal max(c_clustersize)
variable        cs2 equal max2nd(c_clustersize)

Or maybe I could somehow create a new group without the biggest cluster atoms and find its maximum cluster; Something like:

group           coreatoms2 ((type  1 2 3) && (c_cluster != v_maxclustersize))
...
variable        maxclustersize2 equal max(c_clustersize2)

To the best of my knowledge there is currently no simple way to sort a vector in LAMMPS input scripting.

However, I just implemented a sort() and rsort() function for vector style variables to sort in ascending or descending order, respectively. This is now submitted for inclusion into LAMMPS as a pull request: Add special variable functions sort() and rsort() for sorting vectors by value by akohlmey · Pull Request #4171 · lammps/lammps · GitHub

With these functions you could be doing something like this:

variable sorted vector rsort(c_clustersize)      # sort in descending order
variable max2 equal v_sorted[2]                  # grab the second largest value

[...]

fix		        halt_maxcl all halt 1000 v_max2 > 500 error continue
2 Likes

Thank you! I copied committed differences and it all works for me now.

1 Like

Thanks for reporting.