#!/bin/sh

###
### Initial file from QE modified by
### G. Stenuit (06/08/2009)
###

# run from directory where this script is
cd `echo $0 | sed 's/\(.*\)\/.*/\1/'` # extract pathname
EXAMPLE_DIR=`pwd`

# check whether ECHO has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

# function to test the exit status of a job
. ../../check_failure.sh

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example shows how to use pw.x, head.x, pw4gww.x and gww.x to compute"
$ECHO "the quasi-particle energies in an extended system such as bulk Si"

# set the needed environment variables
cd ../
. ../environment_variables
cd $EXAMPLE_DIR

# required executables and pseudopotentials
BIN_LIST="pw.x head.x pw4gww.x gww.x"
PSEUDO_LIST="Si.pz-rrkj.UPF"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO
$ECHO "  checking that needed directories and files exist...\c"

# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done

for DIR in "$TMP_DIR" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done

# check for executables
for FILE in $BIN_LIST ; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done

# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"

# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"
HEAD_COMMAND="$PARA_PREFIX $BIN_DIR/head.x $PARA_POSTFIX"
PW4GWW_COMMAND="$PARA_PREFIX $BIN_DIR/pw4gww.x $PARA_POSTFIX"
GWW_COMMAND="$PARA_PREFIX $BIN_DIR/gww.x $PARA_POSTFIX"
$ECHO
$ECHO "  running pw.x as: $PW_COMMAND"
$ECHO "  running pw4gww.x as: $PW4GWW_COMMAND"
$ECHO "  running gww.x as: $GWW_COMMAND"
$ECHO

# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR , $EXAMPLE_DIR/HEAD , $EXAMPLE_DIR/EXCHANGE, $EXAMPLE_DIR/WANNIER \c"
rm -rf $TMP_DIR/*
rm -rf $EXAMPLE_DIR/HEAD
rm -rf $EXAMPLE_DIR/EXCHANGE
rm -rf $EXAMPLE_DIR/WANNIER
$ECHO " done"

### HEAD part...
for DIR in "$EXAMPLE_DIR/HEAD" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/HEAD

# self-consistent calculation at Gamma
cat > si_scf_head.in << EOF
 &control
    calculation='scf'
    restart_mode='from_scratch',
    pseudo_dir = '$PSEUDO_DIR/',
    prefix='si'
    outdir='$TMP_DIR/'
/
 &system
    ibrav= 2, celldm(1)= 10.26,celldm(2)= 1, celldm(3)=1,
    nat=  2, ntyp= 1,
    ecutwfc = 18.0, nosym=.false.
 /
 &electrons
    diagonalization='david',
    conv_thr =  1.0d-10,
    mixing_beta = 0.5,
    startingwfc='random',
 /
ATOMIC_SPECIES
 Si  1. Si.pz-rrkj.UPF 
ATOMIC_POSITIONS (crystal)
Si 0.00000 0.00000 0.00000
Si 0.25000 0.25000 0.25000
K_POINTS (automatic)
3 3 3 1 1 1
EOF
$ECHO "  running the scf calculation for bulk Si (HEAD part)...\c"
$PW_COMMAND < si_scf_head.in > si_scf_head.out 
check_failure $?
$ECHO " done"

# ph calculation at Gamma
cat > si_ph_head.in << EOF
phonons of Si at Gamma
 &inputph
  tr2_ph=1.d-4,
  prefix='si',
  outdir='$TMP_DIR/',
  epsil=.false.,
  amass(1)=28.08,
  fildyn='si.dynG',
  recover=.false.
  trans=.false.
  l_head=.true.
  omega_gauss=20.
  n_gauss=79
  grid_type=2
  niter_ph=1
 /
0.0 0.0 0.0
EOF
$ECHO "  running the phonon calculation at Gamma for computing the head of bulk Si...\c"
$HEAD_COMMAND < si_ph_head.in > si_ph_head.out 
$ECHO " done"

cd $EXAMPLE_DIR
### HEAD part...
for DIR in "$EXAMPLE_DIR/EXCHANGE" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/EXCHANGE

# self-consistent calculation at Gamma
cat > si_scf_exchange.in << EOF
 &control
    calculation='scf'
    restart_mode='from_scratch',
    pseudo_dir='$PSEUDO_DIR/',
    prefix='si'
    outdir='$TMP_DIR/'
/
 &system
    ibrav= 2, celldm(1)=10.26, celldm(2)= 1, celldm(3)=1,
    nat= 2, ntyp= 1,
    ecutwfc = 18.0, nbnd = 96, nosym=.true.
 /
 &electrons
    diagonalization='cg',
    conv_thr =  1.0d-8,
    mixing_beta = 0.5,
    startingwfc='random',
 /
ATOMIC_SPECIES
 Si  1. Si.pz-rrkj.UPF 
ATOMIC_POSITIONS (crystal)
Si 0.00000 0.00000 0.00000
Si 0.25000 0.25000 0.25000
K_POINTS (automatic)
3 3 3 0 0 0
EOF
$ECHO "  running the scf calculation for bulk Si (EXCHANGE part)...\c"
$PW_COMMAND < si_scf_exchange.in > si_scf_exchange.out
check_failure $?
$ECHO " done"

# pw4gww calculation at Gamma for computing the exchange energies
cat > si_exchange.in << EOF
&inputpw4gww
    prefix='si'
    outdir='$TMP_DIR/',
    lwannier=.false.
    l_exchange=.true.
    num_nbndv=4
    num_nbnds=20
    l_truncated_coulomb=.true.
    truncation_radius=20.52
    omega_gauss=20.
    tau_gauss=10.d0
    n_gauss=79
    grid_type=2
    restart_gww=-1
/
EOF
$ECHO "  running the pw4gww calculation at Gamma for computing the exchange energies of bulk Si...\c"
$PW4GWW_COMMAND < si_exchange.in > si_exchange.out
$ECHO " done"

cd $EXAMPLE_DIR
### HEAD part...
for DIR in "$EXAMPLE_DIR/WANNIER" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/WANNIER

# self-consistent calculation with kpoints 
cat > si_scf_kpt_wannier.in << EOF
 &control
    calculation='scf'
    restart_mode='from_scratch',
    pseudo_dir='$PSEUDO_DIR/',
    prefix='si'
    outdir='$TMP_DIR/'
/
 &system
    ibrav= 2, celldm(1)=10.26, celldm(2)= 1, celldm(3)=1,
    nat= 2, ntyp= 1,
    ecutwfc = 18.0, nosym=.true.
 /
 &electrons
    diagonalization='cg',
    conv_thr =  1.0d-8,
    mixing_beta = 0.5,
    startingwfc='random',
 /
ATOMIC_SPECIES
 Si  1. Si.pz-rrkj.UPF 
ATOMIC_POSITIONS (crystal)
Si 0.00000 0.00000 0.00000
Si 0.25000 0.25000 0.25000
K_POINTS (automatic)
3 3 3 1 1 1 
EOF
$ECHO "  running the scf calculation with kpoints for bulk Si (WANNIER part)...\c"
#$PW_COMMAND < si_scf_kpt_wannier.in > si_scf_kpt_wannier.out 
check_failure $?
$ECHO " done"

# non self-consistent calculation at GAMMA 
cat > si_nscf_gamma_wannier.in << EOF
 &control
    calculation='nscf'
    restart_mode='restart',
    pseudo_dir='$PSEUDO_DIR/',
    prefix='si'
    outdir='$TMP_DIR/'
/
 &system
    ibrav= 2, celldm(1)=10.26, celldm(2)= 1, celldm(3)=1,
    nat= 2, ntyp= 1,
    ecutwfc = 18.0, nbnd = 96, nosym=.true.
 /
 &electrons
    diagonalization='cg',
    conv_thr =  1.0d-8,
    mixing_beta = 0.5,
 /
ATOMIC_SPECIES
 Si  1. Si.pz-rrkj.UPF 
ATOMIC_POSITIONS (crystal)
Si 0.00000 0.00000 0.00000
Si 0.25000 0.25000 0.25000
K_POINTS (gamma)
EOF
$ECHO "  running the nscf calculation at GAMMA for bulk Si (WANNIER part)...\c"
$PW_COMMAND < si_nscf_gamma_wannier.in > si_nscf_gamma_wannier.out
check_failure $?
$ECHO " done"

# copy the wing info from HEAD calculations:
$ECHO " "
$ECHO " copy ../HEAD/si.e_head into the current directory ."
cp ../HEAD/si.e_head .

# wannier calculations...
cat > si_wannier.in << EOF
 &inputpw4gww
    prefix='si'
    outdir='$TMP_DIR/'
    lwannier=.true.,
    cutoff_wpr_vc = 0.1d0
    num_nbnd_first = 50
    num_nbndv=4
    num_nbnds=20
    l_truncated_coulomb=.false.
    remainder=-1
    restart_gww=-1
    numw_prod=1
    n_gauss=79
    omega_gauss=20.d0
    tau_gauss=10.d0
    l_zero=.true.
    l_wing=.true.
    cprim_first=1
    cprim_last=20
    cutoff_products=0.01d0
    l_polarization_analysis=.true.
    cutoff_polarization=0.01d0
/
EOF
$ECHO "  running the pw4gww calculation for bulk Si (WANNIER part)...\c"
$PW4GWW_COMMAND < si_wannier.in > si_wannier.out
check_failure $?
$ECHO " done"

# copy the wing info from HEAD calculations:
$ECHO " "
$ECHO " copy ../HEAD/si.e_head, ../HEAD/si.head, ../EXCHANGE/si.exchange and ../EXCHANGE/si.gv_time"
$ECHO " into the current directory ."

cp ../HEAD/si.e_head .
cp ../HEAD/si.head .
cp ../EXCHANGE/si.exchange .
cp ../EXCHANGE/si.gv_time .

# GWW calculation 
cat > inputgww << EOF
&inputgww
ggwin%n=79,
ggwin%n_fit=79,
ggwin%tau=10.,
ggwin%max_i=20,
ggwin%prefix='si'
ggwin%num_rows=96
ggwin%starting_point=1
ggwin%ending_point=7
ggwin%lnonorthogonal=.false.
ggwin%lconduction=.true.
ggwin%grid_time=2
ggwin%grid_freq=2
ggwin%omega=20
ggwin%omega_fit=20
ggwin%n_grid_fit=240
ggwin%i_min=1
ggwin%i_max=20
ggwin%l_head_epsilon=.true.
ggwin%w_divergence=2
ggwin%l_wing_epsilon=.true.
/
EOF

$ECHO "  running the GWW calculation for bulk Si...\c"
$GWW_COMMAND < inputgww | tee out
#check_failure $?
$ECHO " done"


# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/*
$ECHO " done"

$ECHO
$ECHO "$EXAMPLE_DIR: done"
