#!/bin/bash
# This bash scrpit is for the interface between ATAT and Quantum-ESPRESSO v5.0
# The final result will be in eV unit
# Produced by GAO Zhe, Mater. Sci & Eng, Seoul Nat'l Univ., S. Korea
# email to gaozhe1983(a)snu.ac.kr

# Set the k-points density in reciprocal space
KPPRA=1000

# Set the command to run pw.x, for example,
# if you wanna run it with 4 cores/cpus, set it
# as "$MPI_PATH/mpirun -n 4 $QE_PATH/pw.x"
PWCOMMAND="pw.x"

# Set some calculation parameters here, which contents:
# cut-off energy for wave-functions and charge density;
# gaussian broadening parameter for smearing;
# especially, the directory where restore your pseudo-potential files.
# Definitely, you can directly set these and some other parameters
# in the input file-creating part.
########################################################################
# Recommand from the official manual of Quantum-ESPRESSO:              #
# for Norm-Conserving pseudo-potential, the cut-off for charge density #
# does not need to be setted (or, 4 times of ecutwfc).                 #
# But for ultrasoft pseudo-potential, set the cut-off for charge       #
# density to 8~10 times of cut-off for wavefunctions.                  #
########################################################################
PseudoDIR=/usr/local/pseudo
CUT_WFC=
CUT_CHG=
DEGAUSS=

# Try to find the parameter setting file, which contents the 
# information for species of elements
# If this file does not exist, the script will stop
ParamFile=qe.param
if [ ! -e $ParamFile ]; then
  ParamFile="../qe.param"
  if [ ! -e $ParamFile ]; then
    ParamFile="../../qe.param"
    if [ ! -e $ParamFile ]; then
     echo " Cannot find the param file qe.param, stop the code!"
     exit 1
    fi
  fi
fi

# Create the fist part of input file for QE v5.0
cat > pwscf.in <<EOF
&control
   calculation = 'vc-relax' ,
   prefix = 'pwscf' ,
   outdir = './temp/' ,
   pseudo_dir = '$PseudoDIR/' ,
   disk_io = 'none' ,
   etot_conv_thr = 1.0d-10 ,
   forc_conv_thr = 1.0d-5 ,
/
&system
   ibrav = 0 ,
   celldm(1) = 1.889725989 ,
   ecutwfc = ${CUT_WFC} ,
   ecutrho = ${CUT_CHG} ,
   occupations = 'smearing' ,
   smearing = 'mv' ,
   degauss = ${DEGAUSS} ,
EOF

# Find the atom number and elements number
# Then, write them into the input file
NAT=`cellcvrt -f < str.out | tail -n +7 | wc -l`
NTYPE=`cellcvrt -f < str.out | tail -n +7 | awk '{print $4}' | sort -u | wc -l`
echo "   nat = $NAT , " >> pwscf.in
echo "   ntyp = $NTYPE , " >> pwscf.in

# Create the temp input file, which corresponds to
# the second big part. After creating, it will be 
# attached follow the former one and deleted.
cat > pwscf.tmp <<EOF
/
&electrons
   conv_thr = 1.0d-10 ,
   diagonalization = 'david' ,
   mixing_mode = 'plain' ,
   startingpot = 'atomic' ,
   startingwfc = 'atomic+random' ,
   mixing_beta = 0.6 ,
/
&ions
   ion_dynamics = 'bfgs' ,
/
&cell
   cell_dynamics = 'bfgs' ,
   cell_factor = 2.0 ,
   press = 0.0 ,
   press_conv_thr = 1.0d-3 ,
/
CELL_PARAMETERS alat
EOF
cat pwscf.tmp >> pwscf.in
rm -f pwscf.tmp

# Find and write the lattice vector information
cat str.out | cellcvrt -c -sig=9 | tail -n +4 | head -3 >> pwscf.in

# Find and write the information for element type, mass
# and pseudo-potential files' name
echo "ATOMIC_SPECIES" >> pwscf.in
INT=1
while [ "$INT" -le "$NTYPE" ]; do
  KIND=pp`cellcvrt -f < str.out | tail -n +7 | awk '{print $4}' | sort -u | head -${INT} | tail -1`
  grep "$KIND" $ParamFile | awk '{print "  " $2, $3, $4}' >> pwscf.in
  let "$((INT++))"
done

# Find and write the atoms' position to input file
echo "ATOMIC_POSITIONS angstrom" >> pwscf.in
cat str.out | cellcvrt -c -sig=9 | tail -n +7 | awk '{print "  " $4,  $1,  $2,  $3 }' >> pwscf.in

# Find the k-points mesh, the mesh will be created
# by Monkhorst-Pack method
echo "K_POINTS automatic" >> pwscf.in
nbat=`cellcvrt -pn < str.out`
echo "  " `( echo $KPPRA $nbat | awk '{print $1/$2}' ; cat str.out | cellcvrt -f | head -3 ) | kmesh -q -r`   0  0  0 >> pwscf.in

# Run pw.x for calculation
$PWCOMMAND < pwscf.in > pwscf.out

# Abstract the final total energy to "energy" file
# in the unit of eV
FERy=`grep '!' pwscf.out | tail -1 | cut -c 35-50`
FEeV=`echo "scale=15; $FERy*13.605698066" | bc -l`
echo "$FEeV" > energy

#thanks to "pepe" for contributing this:
cat pwscf.out | getlines -jbt "Begin final coordinates" "End final coordinates" > pw.tmp
echo "1 0 0" >  str_relax.tmp
echo "0 1 0" >> str_relax.tmp
echo "0 0 1" >> str_relax.tmp
cat pw.tmp | getlines -jaf "CELL_PARAMETERS" | head -n 3 | awk '{print $1,$2,$3}' >> str_relax.tmp
cat pw.tmp | getlines -jaf "ATOMIC_POSITIONS" | awk '{print $2,$3,$4,$1}' >> str_relax.tmp
cat str_relax.tmp | cellcvrt -f -sig=9 > str_relax.out
rm pw.tmp str_relax.tmp
