#!/bin/sh
# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi
EXAMPLE_DIR=`pwd`
mkdir $EXAMPLE_DIR/results 
cd $EXAMPLE_DIR/results

PARA_PREFIX="mpirun -np 24"
PARA_POSTFIX="-nk 4"
BIN_DIR="/home/uqmhong2/qe-6.0/bin"
TMP_DIR=$EXAMPLE_DIR/temp
PSEUDO_DIR=$EXAMPLE_DIR
PREFIX='AgSbTe2'

# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"
DOS_COMMAND="$PARA_PREFIX $BIN_DIR/dos.x $PARA_POSTFIX"
PDOS_COMMAND="$PARA_PREFIX $BIN_DIR/projwfc.x $PARA_POSTFIX"
BANDS_COMMAND="$PARA_PREFIX $BIN_DIR/bands.x $PARA_POSTFIX"
PH_COMMAND="$PARA_PREFIX $BIN_DIR/ph.x $PARA_POSTFIX"
MATDYN_COMMAND="$PARA_PREFIX $BIN_DIR/matdyn.x $PARA_POSTFIX"
Q2R_COMMAND="$PARA_PREFIX $BIN_DIR/q2r.x $PARA_POSTFIX"

# self-consistent calculation
    cat > AgSbTe2.scf.in << EOF
&CONTROL
  calculation = 'scf'
  restart_mode='from_scratch',
  tstress = .true.
  tprnfor = .true.
  outdir='$TMP_DIR/'
  pseudo_dir = '$PSEUDO_DIR/',
  prefix='$PREFIX',
/
&SYSTEM
  ibrav = 0
  nat = 16
  ntyp = 3
  nbnd = 65
  ecutwfc = 50
  occupations = 'fixed'
/
&ELECTRONS
  conv_thr = 1.D-10
/
CELL_PARAMETERS (angstrom)
   0.00000000000000   6.17833734212762   6.17833734212762
   6.17833734212762   0.00000000000000   6.17833734212762
   6.17833734212762   6.17833734212762   0.00000000000000
ATOMIC_SPECIES
  Ag  107.86820  Ag.pbe-n-kjpaw_psl.1.0.0.UPF
  Sb  121.76000  Sb.pbe-n-kjpaw_psl.1.0.0.UPF
  Te  127.60000  Te.pbe-n-kjpaw_psl.1.0.0.UPF
ATOMIC_POSITIONS {crystal}
Ag      -0.000000000  -0.000000000   0.000000000
Ag       0.500000000   0.500000000   0.000000000
Ag       0.500000000  -0.000000000   0.500000000
Ag       0.500000000   0.000000000   0.000000000
Sb       0.500000000   0.500000000   0.500000000
Sb       0.000000000   0.000000000   0.500000000
Sb       0.000000000   0.500000000   0.000000000
Sb      -0.000000000   0.500000000   0.500000000
Te       0.253188819   0.753188698   0.240433784
Te       0.746811181   0.246811302   0.759566216
Te       0.746811181   0.246811302   0.246811302
Te       0.253188819   0.753188698   0.753188698
Te       0.746811181   0.759566216   0.246811302
Te       0.253188819   0.240433784   0.753188698
Te       0.740433747   0.753188751   0.753188751
Te       0.259566253   0.246811249   0.246811249
K_POINTS automatic
   5 5 5  0 0 0
EOF
    $ECHO "running the scf calculation for AgSbTe2...\c"
    $PW_COMMAND < AgSbTe2.scf.in > AgSbTe2.scf.out
    $ECHO " done"

# Initial calculation, only band structure.
cat > AgSbTe2.ph.wfc.in << EOF
phonons of AgSbTe2
 &inputph
  tr2_ph=1.0d-15,
  prefix='$PREFIX',
  ldisp=.true.,
  nq1=4, nq2=4, nq3=4
  alpha_mix(1) = 0.1,
  only_wfc=.true.,
  lqdir=.true.,
  outdir='$TMP_DIR/'
  fildyn='$PREFIX.dyn.xml',
  recover = .true.
  search_sym = .false.
 /
EOF
$ECHO "  running the band structure calculation ...\c"
 $PH_COMMAND < AgSbTe2.ph.wfc.in > AgSbTe2.ph.wfc.out
check_failure $?
$ECHO " done"

# Initial calculation, initial part of the dynamical matrix
cat > AgSbTe2.ph.in0 << EOF
phonons of AgSbTe2
 &inputph
  tr2_ph=1.0d-15,
  prefix='$PREFIX',
  ldisp=.true.,
  nq1=4, nq2=4, nq3=4
  alpha_mix(1) = 0.1,
  only_init=.true.,
  recover=.true.
  lqdir=.true.,
  outdir='$TMP_DIR/'
  fildyn='$PREFIX.dyn.xml',
  search_sym=.false.
 /
EOF
$ECHO "  running the initialization phonon calculation ...\c"
$PH_COMMAND < AgSbTe2.ph.in0 > AgSbTe2.ph.out0
check_failure $?
$ECHO " done"



