#!/usr/bin/python3 -u
import os
import time
from aegon.libutils    import readxyzs, writexyzs, rename, sort_by_energy, cutter_energy
from aegon.libposcar   import molecule2poscar
from aegon.libcodegulp import calculator_all_gulp, display_info
from aegon.libgrowpal  import growpal_parallel
from aegon.libcalc_lj  import parallel_opt_LJ
from       libdiscusr  import comparator_usr_batch
from       libselbyclus import selectbyclustering_comb_prom
#------------------------------------------------------------------------------------------------
scratch_folder='/Users/fortiz/installdir/github/growpal/growpal/test_with_usr_batch/scratch/'
nproc = 14
growatom='Mo'
threshold=0.99
ecut=50.0
#------------------------------------------------------------------------------------------
if __name__ == "__main__":
    for iii in range(4, 70+1):
        base_name = 'LJ'+str(iii).zfill(3)
        if os.path.isfile(base_name+'.xyz'):
            print ("%s exists" %(base_name))
            molecu0 = readxyzs(base_name+'.xyz')
            continue
        molecu0 = rename(molecu0, base_name, 5)
        molecu1 = growpal_parallel(molecu0, growatom, nproc, scratch_folder=scratch_folder, base_name='tmp')
        #--------------------------
        for i in range(2):
            poscar1=parallel_opt_LJ(molecu1, nproc)
            #writexyzs(poscar1, base_name+'ANTES.xyz')
            molecu1=comparator_usr_batch(poscar1, batch_size=100, tols=0.99, tole=0.1, mono=True)
            #writexyzs(molecu1, base_name+'DESPUES.xyz')
        #--------------------------------------------
        dicc_term = {iposcar.info['i']: iposcar.info['c'] for iposcar in poscar1}
        molecu0=cutter_energy(molecu1, ecut)
        #--------------------------------------------
        #writexyzs(molecu0, base_name+'ALL.xyz')
        molecu0=selectbyclustering_comb_prom(molecu0, [150, 150, 150, 150, 150, 100, 100, 50], "MBTR_dis", "MBTR_cos")
        display_info(molecu0[0:10], base_name, dicc_term)
        writexyzs(molecu0, base_name+'.xyz')
