#!/bin/sh

# 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

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example shows how to use pw.x to calculate propeties of "
$ECHO "isolated systems decoupling periodic images by using "
$ECHO "Martyna-Tuckerman approach with truncated coulomb interaction."
$ECHO
$ECHO "Three simple systems are considered:"
$ECHO "1) a N atom. "
$ECHO "2) a NH4+ ion."
$ECHO "3) a water molecule."
$ECHO
$ECHO "The calculations are performed in a SC cell of dimension 16 bohr"
$ECHO "It is possible to explore convergence of the results w.r.t. box size"
$ECHO "by editing the script and addind/modifying the variable called BOX_SIZE_LIST"
#list of BOX dimesions used in the calculation: modify this list if you wish
BOX_SIZE_LIST=" 16 "
$ECHO

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

# required executables and pseudopotentials
BIN_LIST="pw.x"
PSEUDO_LIST=" H.pbe-kjpaw.UPF N.pbe-kjpaw.UPF O.pbe-kjpaw.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
$ECHO " done"

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

rm -f n.eigenvalues nh4+.eigenvalues h2o.eigenvalues

for a in $BOX_SIZE_LIST ; do
$ECHO "  running tests for a box size =  $a bohr "
$ECHO

# self-consistent calculation
cat > n.in << EOF
&CONTROL
  prefix       = "N",
  pseudo_dir   = "$PSEUDO_DIR",
  outdir       = "$TMP_DIR",
/
&SYSTEM
  ibrav     = 1,
  celldm(1) = $a.0
  nat       = 1,
  ntyp      = 1,
  ecutwfc   = 30.D0,
  ecutrho   = 120.D0,
  nspin = 2, tot_magnetization = 3,
  assume_isolated = 'martyna-tuckerman'
/
&ELECTRONS
  conv_thr    = 1.D-7,
  mixing_beta = 0.7D0,
/
ATOMIC_SPECIES
N  1.00  N.pbe-kjpaw.UPF
ATOMIC_POSITIONS {bohr}
N  0.000  0.0  0.0  0 0 0
K_POINTS Gamma
EOF
$ECHO "  running scf calculation for N atom...\c"
$PW_COMMAND < n.in > n.out-$a
check_failure $?
$ECHO " done"

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

grep -e bands --after=3 n.out-$a| grep -e " -"| tail -2| awk -v a=$a '{print a, $0}' >> n.eigenvalues


# self-consistent calculation
cat > nh4+.in << EOF
&CONTROL
  calculation  = 'relax'
  prefix       = "NH4+",
  pseudo_dir   = "$PSEUDO_DIR",
  outdir       = "$TMP_DIR",
/
&SYSTEM
  ibrav     = 1,
  celldm(1) = $a.0
  nat       = 5,
  ntyp      = 2,
  ecutwfc   = 30.D0,
  ecutrho   = 120.D0,
  tot_charge = +1.0
  nbnd      = 8
  assume_isolated = 'martyna-tuckerman'
/
&ELECTRONS
  conv_thr    = 1.D-7,
  mixing_beta = 0.7D0,
/
&IONS
/
ATOMIC_SPECIES
N  1.00  N.pbe-kjpaw.UPF
H  1.00  H.pbe-kjpaw.UPF
ATOMIC_POSITIONS {bohr}
N  0.0  0.0  0.0  0 0 0
H  1.0  1.0  1.0
H -1.0 -1.0  1.0
H -1.0  1.0 -1.0
H  1.0 -1.0 -1.0
K_POINTS Gamma
EOF
$ECHO "  running relax calculation for NH4+ ion...\c"
$PW_COMMAND < nh4+.in > nh4+.out-$a
check_failure $?
$ECHO " done"
grep -e bands --after=3 nh4+.out-$a| grep -e " -"| tail -1| awk -v a=$a '{print a, $0}' >> nh4+.eigenvalues

# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/NH4+*
$ECHO " done"
# self-consistent calculation
cat > h2o.in << EOF
&CONTROL
  calculation  = 'relax'
  prefix       = "H2O",
  pseudo_dir   = "$PSEUDO_DIR",
  outdir       = "$TMP_DIR",
/
&SYSTEM
  ibrav     = 1,
  celldm(1) = $a.0
  nat       = 3,
  ntyp      = 2,
  ecutwfc   = 30.D0,
  ecutrho   = 120.D0,
  nbnd      = 8
  assume_isolated = 'martyna-tuckerman'
/
&ELECTRONS
  conv_thr    = 1.D-7,
  mixing_beta = 0.7D0,
/
&IONS
/
ATOMIC_SPECIES
O  1.00  O.pbe-kjpaw.UPF
H  1.00  H.pbe-kjpaw.UPF
ATOMIC_POSITIONS {bohr}
O  0.0  0.0  0.0  0 0 0
H  1.0  1.0  1.0
H -1.0 -1.0  1.0
K_POINTS Gamma
EOF
$ECHO "  running relax calculation for H2O molecule...\c"
$PW_COMMAND < h2o.in > h2o.out-$a
check_failure $?
$ECHO " done"
grep -e bands --after=3 h2o.out-$a| grep -e " -"| tail -1| awk -v a=$a '{print a, $0}' >> h2o.eigenvalues

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

done

$ECHO " eigenvalues of N atom"
cat n.eigenvalues
$ECHO 
$ECHO " to be compared with the reference values"
cat ../reference/n.eigenvalues
$ECHO 
$ECHO 
$ECHO " eigenvalues of NH4+ ions"
cat nh4+.eigenvalues
$ECHO 
$ECHO " to be compared with the reference values"
cat ../reference/nh4+.eigenvalues
$ECHO 
$ECHO 
$ECHO " eigenvalues of H2O molecule"
cat h2o.eigenvalues
$ECHO 
$ECHO " to be compared with the reference values"
cat ../reference/h2o.eigenvalues
$ECHO 
$ECHO 
$ECHO "$EXAMPLE_DIR: done"
