#!/bin/bash

mv -f ./bands.dat ./bands.dat.OLD 2>/dev/null

# scan these values for the lattice constant
a_values="
4.04100
4.24100
4.44100
4.64100
4.84100
5.04100
5.24100
5.44100
5.64100
5.84100
6.04100
6.04100
8.04100
10.04100
12.04100
14.04100
"

counter=-1
#for a_val in $(seq 4.04100 2.0 14.1)
# for a_val in $(seq 4.04100 2.0 8.)
for a_val in ${a_values}
do :

counter=$(printf "%02d" $(echo "${counter}+1"|bc))
# echo ${counter}

tag="a=${a_val}"
ecutwfc=$(echo "scale=4;55.*4.04100/${a_val}"|bc -l)
ecutrho=$(echo "scale=4;8.*${ecutwfc}"|bc -l)

# echo ${counter}
# echo ${ecutwfc}
# echo ${ecutrho}

# run the scf calc
sed -E \
    -e's|@A@|'${a_val}'|' \
    -e's|@ECUTWFC@|'${ecutwfc}'|' \
    -e's|@ECUTRHO@|'${ecutrho}'|' \
    scf.in.in > scf.in
rm -rf ./tmp
mpirun pw.x < scf.in 2>&1 |tee  "${counter}-scf-${tag}.out"

# run the bands calc
sed -E \
    -e's|@A@|'${a_val}'|' \
    -e's|@ECUTWFC@|'${ecutwfc}'|' \
    -e's|@ECUTRHO@|'${ecutrho}'|' \
    bands.in.in > bands.in
mpirun pw.x < bands.in 2>&1 |tee  "${counter}-bands-${tag}.out"

# catch the eigenvalues (add fermi energy as the last column)
awk '
/^ *k =/{
getline;getline;
print $0
}
' ./"${counter}-bands-${tag}.out" > "${counter}-bands-${tag}.dat"

done # for a_val...
exit
