[ase-users] atoms sorting
Ondřej Krejčí
Okrejcio at seznam.cz
Tue Nov 26 17:59:28 CET 2019
Dear David and Ask,
thank you for all your suggestions. Here, what I used in one line:
New_atoms = atoms[np.argsort(atoms.positions[:, 2])]
However, it was still harder for me to find this way. What I think would be
ideal in the future is to generalise the `sort` function in `ase.build.
tools` also for positions.
Anyway, once more thank you!
Kind regards,
Ondrej Krejci
---------- Původní e-mail ----------
Od: David Ollodart <davidollodart at gmail.com>
Komu: Ask Hjorth Larsen <asklarsen at gmail.com>
Datum: 26. 11. 2019 18:06:49
Předmět: Re: [ase-users] atoms sorting
"
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@
listserv.fysik.dtu.dk(mailto: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(mailto: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(mailto:ase-users at listserv.fysik.dtu.dk)
> https://listserv.fysik.dtu.dk/mailman/listinfo/ase-users
(https://listserv.fysik.dtu.dk/mailman/listinfo/ase-users)
_______________________________________________
ase-users mailing list
ase-users at listserv.fysik.dtu.dk(mailto:ase-users at listserv.fysik.dtu.dk)
https://listserv.fysik.dtu.dk/mailman/listinfo/ase-users
(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/be0053d0/attachment-0001.html>
More information about the ase-users
mailing list