# poly.sh to get positions, scf, and nscf files for the polymorphous structure
#!/bin/bash
#
declare -i i
#
sed -i '/disk_io/d' ZG-scf_poly_iter_00.in
cp ZG-scf_poly_iter_00.in ZG-nscf_poly_iter_00.in
#
cat ZG-nscf_poly_iter_00.in Kpoints.txt > tmp
mv tmp ZG-nscf_poly_iter_00.in
#
head -n -40 ZG-scf_poly_iter_00.in > ZG-scf_poly_iter_00
tail -40 ZG-scf_poly_iter_00.in > poly_pos.dat
#
cp equil_pos.dat tmp
sed -i '1,2d' tmp
array=(430 450 470 500) 
for i in ${array[@]}
do
  # capture displacements by taking the difference between 
  # ZG coordinates and equil structure
  cp ZG-configuration_"$i".00K.dat tmpZG
  sed -i '1,2d' tmpZG
  paste tmpZG tmp > oo
  awk '{print $1, $2 - $6, $3 - $7, $4 - $8}' oo > displ_"$i"K.txt 
  # add displacements on the polymorphous configuration
  paste poly_pos.dat displ_"$i"K.txt > oo
  awk '{print $1, $2 + $6, $3 + $7, $4 + $8}' oo > ZG_poly-configuration_"$i".00K.dat
  cat ZG-scf_poly_iter_00 ZG_poly-configuration_"$i".00K.dat > ZG-scf_poly_"$i".00K.in 
  sed -i 's/ZG-scf/ZG-'$i'.00-poly-scf/' ZG-scf_poly_"$i".00K.in
  cp ZG-scf_poly_"$i".00K.in ZG-nscf_poly_"$i".00K.in
done

# prepare nscf files with empty bands and remove uniform grid info
sed -i 's/scf/nscf/g' *-nscf_poly*
sed -i 's/-nscf/-scf/g' *-nscf_poly*
sed -i 's/ecutwfc/nosym = .true., nbnd = 200, ecutwfc/g' *-nscf_poly*
sed -i '/K_P/d' *-nscf_poly*
sed -i '/2   2   2/d' *-nscf_poly*
   
# add correct K-point at the end of the nscf files
for i in ${array[@]}
do
  cat ZG-nscf_poly_"$i".00K.in Kpoints.txt > tmp
  mv tmp ZG-nscf_poly_"$i".00K.in
done
