[ase-users] atoms sorting

David Ollodart davidollodart at gmail.com
Tue Nov 26 17:06:09 CET 2019


Dear Ondrej,

There is already the `sort` function in `ase.build.tools`. If you want
hierarchical sorting you can use the tuple comparison. Below is a script
which allows for sorting by chemical symbol and positions, in ascending or
descending order (for scalars, multiply by -1, for symbols, change the
ordering numbers). It is shown sorting by atomic symbol first carbon then
hydrogen, then z, y, and x positions in descending order:

from ase.build import molecule
atoms=molecule('CH4')
atoms.rotate(45,'x')
atoms.rotate(45,'y') #to make asymmetric coordinates for more interesting
sort order
natoms=len(atoms)
pos=atoms.positions
from numpy import arange
pnt=arange(natoms)
symbol_dct={'C':1,'H':2}

pnt=sorted(pnt,key=lambda x:(
    symbol_dct[atoms[x].symbol],
    -1*round(pos[x][2],4), #to make similar values equal in comparison
    -1*round(pos[x][1],4),
    -1*round(pos[x][0],4)
    ))
atoms=atoms[pnt]
for atom in atoms:
    print(atom)

Best,

David

On Tue, Nov 26, 2019 at 7:47 AM Ask Hjorth Larsen via ase-users <
ase-users at listserv.fysik.dtu.dk> wrote:

> Dear Ondřej,
>
> Am Di., 26. Nov. 2019 um 10:11 Uhr schrieb Ondřej Krejčí via ase-users
> <ase-users at listserv.fysik.dtu.dk>:
> >
> > Dear ase users and developers,
> >
> > I'm really happy that ASE can work with so many DFT codes and their
> inputs and outputs.
> > The only thing, that I'm missing is some simple build-in function for
> sorting atoms (I know they are possibilities outside of atoms, however that
> is not convenient and especially for new users hard to work with as well as
> it is not written in the ASE help). Sorting by x,y,z and elements ascending
> and descending (not because of DFT codes, but some other codes, that are
> working on top of DFT) would make life easier for some of the ASE users.
> Maybe in some future release?
>
> I suppose one can always add shortcut methods, butin this case I think
> it would be just two lines:
>
> args = np.argsort(atoms.numbers)
> sorted_atoms = atoms[args]
>
> Best regards
> Ask
>
> >
> > Thank you!
> > Kind regards,
> > Ondrej Krejci
> > _______________________________________________
> > ase-users mailing list
> > ase-users at listserv.fysik.dtu.dk
> > https://listserv.fysik.dtu.dk/mailman/listinfo/ase-users
>
> _______________________________________________
> ase-users mailing list
> ase-users at listserv.fysik.dtu.dk
> https://listserv.fysik.dtu.dk/mailman/listinfo/ase-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listserv.fysik.dtu.dk/pipermail/ase-users/attachments/20191126/208b5ae6/attachment.html>


More information about the ase-users mailing list