# script_poly_gap.sh for computing band gap renormalization of polymorphous CsPbBr3 

cd $PWD

QE=/path_to/q-e/bin

# apply ZG displacements and generate, scf, and nscf files for calculations on the polymorphous structure
cp ../inputs/poly.sh .
./poly.sh
#
# static-equilibrium and ZG configurations of the polymorphous structure
# scf calculation for static-equilibrium polymorphous structure
ibrun -np 48 $QE/pw.x -nk 8 < ZG-scf_poly_iter_00.in > ZG-scf_poly_iter_00.out
ibrun -np 48 $QE/pw.x < ZG-nscf_poly_iter_00.in > ZG-nscf_poly_iter_00.out
rm *wfc* *.save/*wfc* 
poly_gap=$(grep highest ZG-nscf_poly_iter_00.out | awk '{print $8-$7}')
echo $poly_gap > poly_gap.dat
#
# run scf and nscf calculations 
array=(430 450 470 500)
for j in ${array[@]}
do
  ibrun -np 48 $QE/pw.x -nk 8  < ZG-scf_poly_"$j".00K.in > ZG-scf_poly_"$j".00K.out
  ibrun -np 48 $QE/pw.x < ZG-nscf_poly_"$j".00K.in > ZG-nscf_poly_"$j".00K.out
  rm ZG-"$j".00-poly-scf.save/*wfc*
  rm ZG-"$j".00-poly-scf.save/*upf
  rm *wfc*
done
# calculate the band gap renormalization
grep highest ZG-nscf_poly_*K.out > tmp
awk '{print $1, $8, $9, $9-$8, $9-$8-'$poly_gap'}' tmp | sed 's/ZG-nscf_poly_//g' | sed 's/K.out://g' > Renorm_poly_T.dat

exit
