#!/usr/bin/env python3

from ase import io, Atoms
import numpy as np



def doit(inAtoms, resort):
    resortIdxs = [x[0] for x in resort]
    newAtoms = []
    i = 0
    for iAtom in range(len(inAtoms)):
        if iAtom in resortIdxs:
            tpl = resort[resortIdxs.index(iAtom)]
            newAtoms.append(inAtoms[tpl[1]])
        else:
            newAtoms.append(inAtoms[i])
            i += 1
        if len(newAtoms) == len(inAtoms):
            break
    return Atoms(symbols=[a.symbol for a in newAtoms], positions=[a.position for a in newAtoms], cell=inAtoms.cell, pbc=inAtoms.pbc)

inAtoms = io.read('POSCAR_end_presorted')
resort = [(81,-4), (150,-1), (107, -2),(106,-3) ]
final = doit(inAtoms, resort)
final.write('POSCAR_end_sorted')


one = io.read('POSCAR_start')
two = io.read('POSCAR_end_sorted')
final = one.copy()
final.positions -= two.positions
final.write('diff.xyz')
dists = [np.linalg.norm(pos) for pos in final.positions]
#print(dists)
