# script.sh for computing anharmonic phonons in PbTe
cd $PWD

QE=path_to_qe_bin

# download pseudos from pseudo-dojo library
wget http://www.pseudo-dojo.org/pseudos/nc-sr-04_pbesol_standard/Pb.upf.gz
wget http://www.pseudo-dojo.org/pseudos/nc-sr-04_pbesol_standard/Te.upf.gz
gunzip Pb.upf.gz
gunzip Te.upf.gz

# copy inputs
cp -r ../inputs/* .
# run sch and ph to obtain harmonic phonons
ibrun -np 48 $QE/pw.x -nk 12 < scf.in > scf.out
ibrun -np 48 $QE/ph.x -nk 12 < ph.in > ph.out
ibrun -np 1 $QE/q2r.x < q2r.in > q2r.out
ibrun -np 1 $QE/matdyn.x < matdyn.in > matdyn.out
rm -rf _ph0 *wfc* *save/*wfc*
#
# set temperature in ZG files
T=300
sed -i 's/AAA/'$T'/g' ZG*in
sed -i 's/AAA/'$T'/g' matdyn_*in

# first iteration starting from harmonic phonons
ibrun -np 1 $QE/ZG.x < ZG_1.in > ZG_1.out

file=ZG-scf_300.00K_iter_01
mkdir fd_forces/
for i in {0001..0096}; do
  mkdir $i
  cd $i
  cp ../Pb.upf .
  cp ../Te.upf .
  mv ../"$file"_"$i".in .
## We have copied the "fd_forces" folder from inputs which includes all ASDM calculations.
## This is done for spead up purposes. Otherwise, calculate and copy forces files by 
## uncommenting the two lines below.
#  ibrun -np 48 $QE/pw.x -nk 12 < "$file"_"$i".in > "$file"_"$i".out
#  mv "$file"_"$i".out ../fd_forces/.
  rm Pb.upf Te.upf
  cd ../
done

ibrun -np 1 $QE/ZG.x < ZG_2.in > ZG_2.out
ibrun -np 1 $QE/matdyn.x < matdyn_01.in > matdyn_01.out
# 
# second iteration
ibrun -np 1 $QE/ZG.x < ZG_3.in > ZG_3.out

file=ZG-scf_300.00K_iter_02
mkdir fd_forces/
for i in {0001..0096}; do
  mkdir $i
  cd $i
  cp ../Pb.upf .
  cp ../Te.upf .
  mv ../"$file"_"$i".in .
## We have copied the "fd_forces" folder from inputs which includes all ASDM calculations.
## This is done for spead up purposes. Otherwise, calculate and copy forces files by 
## uncommenting the two lines below.
#  ibrun -np 48 $QE/pw.x -nk 12 < "$file"_"$i".in > "$file"_"$i".out
#  mv "$file"_"$i".out ../fd_forces/.
  rm Pb.upf Te.upf
  cd ../
done

ibrun -np 1 $QE/ZG.x < ZG_4.in > ZG_4.out
ibrun -np 1 $QE/matdyn.x < matdyn_02.in > matdyn_02.out

# third iteration
ibrun -np 1 $QE/ZG.x < ZG_5.in > ZG_5.out

file=ZG-scf_300.00K_iter_03
mkdir fd_forces/
for i in {0001..0096}; do
  mkdir $i
  cd $i
  cp ../Pb.upf .
  cp ../Te.upf .
  mv ../"$file"_"$i".in .
## We have copied the "fd_forces" folder from inputs which includes all ASDM calculations.
## This is done for spead up purposes. Otherwise, calculate and copy forces files by 
## uncommenting the two lines below.
#  ibrun -np 48 $QE/pw.x -nk 12 < "$file"_"$i".in > "$file"_"$i".out
#  mv "$file"_"$i".out ../fd_forces/.
  rm Pb.upf Te.upf
  cd ../
done

ibrun -np 1 $QE/ZG.x < ZG_6.in > ZG_6.out
ibrun -np 1 $QE/matdyn.x < matdyn_03.in > matdyn_03.out

