#!/bin/sh

# run from directory where this script is
cd `dirname $0`
EXAMPLE_DIR=`pwd`

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

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This examples shows how to use pp.x to plot the squared modulus
$ECHO  of the Hubbard projector functions of DFT+U"

# set the needed environment variables
. ../../../environment_variables

# required executables and pseudopotentials
BIN_LIST="pw.x pp.x"
PSEUDO_LIST="Ti.pbesol-spn-rrkjus_psl.1.0.0.UPF O.pbesol-n-rrkjus_psl.1.0.0.UPF"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$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" "$EXAMPLE_DIR/results" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/results

# 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 "Downloading $FILE to $PSEUDO_DIR...\c"
            $WGET $PSEUDO_DIR/$FILE $NETWORK_PSEUDO/$FILE 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done

# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"
PP_COMMAND="$PARA_PREFIX $BIN_DIR/pp.x $PARA_POSTFIX"
$ECHO
$ECHO "  running pw.x as: $PW_COMMAND"
$ECHO "  running pp.x as: $PP_COMMAND"
$ECHO

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

# self-consistent calculation
cat > TiO2.scf.in << EOF
 &control
    calculation = 'scf'
    restart_mode='from_scratch',
    prefix='TiO2',
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
    disk_io = 'medium'
 /
 &system
    ibrav = 0,
    nat = 6,
    ntyp = 2,
    ecutwfc = 50,
    ecutrho = 500,
    occupations = 'fixed',
    nosym = .true.
    noinv = .true.
 /
 &electrons
    mixing_mode = 'plain'
    mixing_beta = 0.3
    conv_thr =  1.0d-8
 /
ATOMIC_SPECIES
 Ti 47.867  Ti.pbesol-spn-rrkjus_psl.1.0.0.UPF
 O  15.999  O.pbesol-n-rrkjus_psl.1.0.0.UPF 
ATOMIC_POSITIONS {crystal}
Ti  0.000000000   0.000000000   0.000000000
Ti  0.500000000   0.500000000   0.500000000
O   0.305700016   0.305700016   0.000000000
O   0.805699990   0.194299997   0.500000000
O   0.194299997   0.805699990   0.500000000
O   0.694300010   0.694300010   0.000000000
CELL_PARAMETERS {angstrom}
   4.5940999985    0.0000000000     0.0000000000
   0.0000000000    4.5940999985     0.0000000000
   0.0000000000    0.0000000000     2.9588999748
K_POINTS {automatic}
 2 2 2 0 0 0
HUBBARD {ortho-atomic}
U Ti-3d 4.0
EOF
$ECHO "  Running scf for TiO2 using PBEsol...\c"
$PW_COMMAND < TiO2.scf.in > TiO2.scf.out
$ECHO " done"


# Post-processing
cat > TiO2.pp.in << EOF
 &INPUTPP
   prefix = 'TiO2'
   outdir = '$TMP_DIR/',
   filplot = 'hub'
   plot_num = 25
   nc(1) = 2
   nc(2) = 2
   nc(3) = 2
   n0(1) = 1
   n0(2) = 1
   n0(3) = 1
 /
 &PLOT
   iflag = 3
   output_format = 5
   fileout = 'plot.xsf'
/
EOF
$ECHO "  Running the post-processing for TiO2...\c"
$PP_COMMAND < TiO2.pp.in > TiO2.pp.out
$ECHO " done"

$ECHO
$ECHO "$EXAMPLE_DIR : done"
