cd $PWD

QE=/path_to/q-e/bin

# download pseudos from pseudo-dojo library
wget http://www.pseudo-dojo.org/pseudos/nc-sr-04_pbesol_standard/Cs.upf.gz
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/Br.upf.gz
gunzip Cs.upf.gz Pb.upf.gz Br.upf.gz

# calculate harmonic phonons  
cp ../inputs/scf.in .
ibrun -np 48 $QE/pw.x -nk 8 < scf.in > scf.out
cp ../inputs/ph.in .
ibrun -np 48 $QE/ph.x -nk 8 < ph.in > ph.out
rm -r _ph0 *wfc* *save/*wfc*
cp ../inputs/q2r.in .
ibrun -np 1 $QE/q2r.x < q2r.in > q2r.out
cp ../inputs/matdyn.in .
ibrun -np 1 $QE/matdyn.x < matdyn.in > matdyn.out

# compute the polymorphous structure
rm ZG-relax.out
cp ../inputs/ZG_*in .
# generate ZG-relax.in
ibrun -np 1 $QE/ZG.x < ZG_1.in > ZG_1.out
cp ../inputs/ZG-relax.out .
## to speed up the process we copy ZG-relax.out from inputs. Uncomment the line below if you want to perform the relaxation.
#ibrun -np 50 $QE/pw.x -nk 10 < ZG-relax.in > ZG-relax.out
ibrun -np 1 $QE/ZG.x < ZG_2.in > ZG_2.out

# compute the phonons of the polymorphous structure
## to speed up the process we copy scf output files from inputs. Comment the line below if you want to perform the calculations.
cp -r ../inputs/fd_forces .
#
file=ZG-scf_poly_iter_00
mkdir fd_forces/
for i in {0001..0240}; do
  mkdir $i
  cd $i
  cp ../*.upf .
  mv ../"$file"_"$i".in .
## to speed up the process outputs ZG-scf_poly_iter_00*out are given in fd_forces.
## Uncomment the two lines below to perform the scf calculations.  
#  ibrun -np 48 $QE/pw.x -nk 8 < "$file"_"$i".in > "$file"_"$i".out
#  mv "$file"_"$i".out ../fd_forces/.
  rm *.upf
  cd ../
done
ibrun -np 1 $QE/ZG.x < ZG_3.in > ZG_3.out
cp ../inputs/matdyn_poly.in .
ibrun -np 1 $QE/matdyn.x < matdyn_poly.in > matdyn_poly.out

# compute the phonons at 450 K: first iteration
ibrun -np 1 $QE/ZG.x < ZG_4.in > ZG_4.out
#
file=ZG-scf_450.00K_iter_01
for i in {0001..0240}; do
  mkdir $i
  cd $i
  cp ../*.upf .
  mv ../"$file"_"$i".in .
## to speed up the process outputs ZG-scf_450.00K_iter_01*out are given in fd_forces.
## Uncomment the two lines below to perform the scf calculations.  
#  ibrun -np 48 $QE/pw.x -nk 8 < "$file"_"$i".in > "$file"_"$i".out
#  mv "$file"_"$i".out ../fd_forces/.
  rm *.upf
  cd ../
done
ibrun -np 1 $QE/ZG.x < ZG_5.in > ZG_5.out
cp ../inputs/matdyn_01.in .
ibrun -np 1 $QE/matdyn.x < matdyn_01.in > matdyn_01.out

# compute the phonons at 450 K: second iteration
ibrun -np 1 $QE/ZG.x < ZG_6.in > ZG_6.out
#
file=ZG-scf_450.00K_iter_02
for i in {0001..0240}; do
  mkdir $i
  cd $i
  cp ../*.upf .
  mv ../"$file"_"$i".in .
## to speed up the process outputs ZG-scf_450.00K_iter_02*out are given in fd_forces.
## Uncomment the two lines below to perform the scf calculations.  
#  ibrun -np 48 $QE/pw.x -nk 8 < "$file"_"$i".in > "$file"_"$i".out
#  mv "$file"_"$i".out ../fd_forces/.
  rm *.upf
  cd ../
done
ibrun -np 1 $QE/ZG.x < ZG_7.in > ZG_7.out
cp ../inputs/matdyn_02.in .
ibrun -np 1 $QE/matdyn.x < matdyn_02.in > matdyn_02.out

exit
