#!/bin/sh

###############################################################################
##
##  gate EXAMPLE
##
###############################################################################

# 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 a gate field to simulate"
$ECHO "a charged surface slab"
$ECHO

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

# required executables and pseudopotentials
BIN_LIST="pw.x"
PSEUDO_LIST="Mo.pz-spn-rrkjus_psl.0.2.UPF  S.pz-n-rrkjus_psl.0.1.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

# scf calculation for -0.10 electron charges
cat > single_-0.10.in  << EOF
&control
 calculation   = 'relax',
 restart_mode  = 'from_scratch',
 pseudo_dir    = '$PSEUDO_DIR',
 prefix        = 'single_-0.10',
 outdir        = '$TMP_DIR/',
 nstep         = 300,
 gate          = .true.,
 tefield       = .true.,
 dipfield      = .true.,
/
&system
 ibrav       = 4,
 celldm(1)   = 5.9716,
 celldm(3)   = 12,
 nat         = 3,
 ntyp        = 2,
 ecutwfc     = 50,
 ecutrho     = 410,
 occupations = 'smearing',
 degauss     = 0.002,
 smearing    = 'gauss',
 tot_charge  = -0.10,
 relaxz      = .true.,
 edir        = 3,
 zgate       = 0.011,
 emaxpos     = 0.000001,
 eopreg      = 0.01,
 eamp        = 0.0,
 block       = .true.,
 block_1     = 0.000001,
 block_2     = 0.1,
 block_height= 1.0,
/
&electrons
 conv_thr = 1.0d-9,
/
&IONS
 ion_dynamics='bfgs',
/
ATOMIC_SPECIES
 Mo  95.94    Mo.pz-spn-rrkjus_psl.0.2.UPF
 S   32.066   S.pz-n-rrkjus_psl.0.1.UPF
ATOMIC_POSITIONS (alat)
S        0.500000000   0.288675130   1.974192764
Mo       0.000000000   0.577350270   2.462038339
S        0.000000000  -0.577350270   2.950837559
K_POINTS automatic
16 16 1 0 0 0
EOF
$ECHO "  running the calculation for -0.1 electron charges...\c"
$PW_COMMAND < single_-0.10.in > single_-0.10.out
check_failure $?
$ECHO " done"

# scf calculation for +0.10 electron charges
cat > single_+0.10.in  << EOF
&control
 calculation   = 'relax',
 restart_mode  = 'from_scratch',
 pseudo_dir    = '$PSEUDO_DIR',
 prefix        = 'single_+0.10',
 outdir        = '$TMP_DIR/',
 nstep         = 300,
 gate          = .true.,
 tefield       = .true.,
 dipfield      = .true.,
/
&system
 ibrav       = 4,
 celldm(1)   = 5.9716,
 celldm(3)   = 12,
 nat         = 3,
 ntyp        = 2,
 ecutwfc     = 50,
 ecutrho     = 410,
 occupations = 'smearing',
 degauss     = 0.002,
 smearing    = 'gauss',
 tot_charge  = +0.10,
 relaxz      = .true.,
 edir        = 3,
 zgate       = 0.011,
 emaxpos     = 0.000001,
 eopreg      = 0.01,
 eamp        = 0.0,
 block       = .true.,
 block_1     = 0.000001,
 block_2     = 0.1,
 block_height= 1.0,
/
&electrons
 conv_thr = 1.0d-9,
/
&IONS
 ion_dynamics='bfgs',
/
ATOMIC_SPECIES
 Mo  95.94    Mo.pz-spn-rrkjus_psl.0.2.UPF
 S   32.066   S.pz-n-rrkjus_psl.0.1.UPF
ATOMIC_POSITIONS (alat)
S        0.500000000   0.288675130   1.863316950
Mo       0.000000000   0.577350270   2.350404949
S        0.000000000  -0.577350270   2.838203782
K_POINTS automatic
16 16 1 0 0 0
EOF
$ECHO "  running the calculation for +0.1 electron charges...\c"
$PW_COMMAND < single_+0.10.in > single_+0.10.out
check_failure $?
$ECHO " done"

# bands calculation for +0.10 electron charges
cat > single_+0.10.bands.in  << EOF
&control
 calculation   = 'bands',
 restart_mode  = 'from_scratch',
 pseudo_dir    = '$PSEUDO_DIR',
 prefix        = 'single_+0.10',
 outdir        = '$TMP_DIR/',
 nstep         = 300,
 gate          = .true.,
 tefield       = .true.,
 dipfield      = .true.,
/
&system
 ibrav       = 4,
 celldm(1)   = 5.9716,
 celldm(3)   = 12,
 nat         = 3,
 ntyp        = 2,
 ecutwfc     = 50,
 ecutrho     = 410,
 occupations = 'smearing',
 degauss     = 0.002,
 smearing    = 'gauss',
 tot_charge  = +0.10,
 relaxz      = .true.,
 edir        = 3,
 zgate       = 0.011,
 emaxpos     = 0.000001,
 eopreg      = 0.01,
 eamp        = 0.0,
 block       = .true.,
 block_1     = 0.000001,
 block_2     = 0.1,
 block_height= 1.0,
/
&electrons
 conv_thr = 1.0d-9,
/
ATOMIC_SPECIES
 Mo  95.94    Mo.pz-spn-rrkjus_psl.0.2.UPF
 S   32.066   S.pz-n-rrkjus_psl.0.1.UPF
ATOMIC_POSITIONS (alat)
S        0.500000000   0.288675130   1.863316950
Mo       0.000000000   0.577350270   2.350404949
S        0.000000000  -0.577350270   2.838203782
K_POINTS crystal_b
4
 0.000000  0.000000  0.000000  33
 0.500000  0.000000  0.000000  25
 0.666666 -0.333333  0.000000  50
 0.000000  0.000000  0.000000  1
EOF
$ECHO "  running the calculation of bands for +0.1 electron charges...\c"
$PW_COMMAND < single_+0.10.bands.in > single_+0.10.bands.out
check_failure $?
$ECHO " done"

# scf calculation for +0.10 electron charges (no electron spilling barrier)
cat > single_+0.10_nobarrier.in  << EOF
&control
 calculation   = 'relax',
 restart_mode  = 'from_scratch',
 pseudo_dir    = '$PSEUDO_DIR',
 prefix        = 'single_+0.10_nob',
 outdir        = '$TMP_DIR/',
 nstep         = 300,
 gate          = .true.,
 tefield       = .true.,
 dipfield      = .true.,
/
&system
 ibrav       = 4,
 celldm(1)   = 5.9716,
 celldm(3)   = 12,
 nat         = 3,
 ntyp        = 2,
 ecutwfc     = 50,
 ecutrho     = 410,
 occupations = 'smearing',
 degauss     = 0.002,
 smearing    = 'gauss',
 tot_charge  = +0.10,
 relaxz      = .true.,
 edir        = 3,
 zgate       = 0.011,
 emaxpos     = 0.000001,
 eopreg      = 0.01,
 eamp        = 0.0,
/
&electrons
 conv_thr = 1.0d-9,
/
&IONS
 ion_dynamics='bfgs',
/
ATOMIC_SPECIES
 Mo  95.94    Mo.pz-spn-rrkjus_psl.0.2.UPF
 S   32.066   S.pz-n-rrkjus_psl.0.1.UPF
ATOMIC_POSITIONS (alat)
S        0.500000000   0.288675130   2.500000000    0   0   0
Mo       0.000000000   0.577350270   2.987083433
S        0.000000000  -0.577350270   3.474835516
K_POINTS automatic
16 16 1 0 0 0
EOF
$ECHO "  running the calculation for +0.1 electron charges (no electron spilling barrier)...\c"
$PW_COMMAND < single_+0.10_nobarrier.in > single_+0.10_nobarrier.out
check_failure $?
$ECHO " done"

# bands calculation for +0.10 electron charges (no electron spilling barrier)
cat > single_+0.10_nobarrier.bands.in  << EOF
&control
 calculation   = 'bands',
 restart_mode  = 'from_scratch',
 pseudo_dir    = '$PSEUDO_DIR',
 prefix        = 'single_+0.10_nob',
 outdir        = '$TMP_DIR/',
 nstep         = 300,
 gate          = .true.,
 tefield       = .true.,
 dipfield      = .true.,
/
&system
 ibrav       = 4,
 celldm(1)   = 5.9716,
 celldm(3)   = 12,
 nat         = 3,
 ntyp        = 2,
 ecutwfc     = 50,
 ecutrho     = 410,
 occupations = 'smearing',
 degauss     = 0.002,
 smearing    = 'gauss',
 tot_charge  = +0.10,
 relaxz      = .true.,
 edir        = 3,
 zgate       = 0.011,
 emaxpos     = 0.000001,
 eopreg      = 0.01,
 eamp        = 0.0,
/
&electrons
 conv_thr = 1.0d-9,
/
ATOMIC_SPECIES
 Mo  95.94    Mo.pz-spn-rrkjus_psl.0.2.UPF
 S   32.066   S.pz-n-rrkjus_psl.0.1.UPF
ATOMIC_POSITIONS (alat)
S        0.500000000   0.288675130   2.500000000    0   0   0
Mo       0.000000000   0.577350270   2.987083433
S        0.000000000  -0.577350270   3.474835516
K_POINTS crystal_b
4
 0.000000  0.000000  0.000000  33
 0.500000  0.000000  0.000000  25
 0.666666 -0.333333  0.000000  50
 0.000000  0.000000  0.000000  1
EOF
$ECHO "  running the calculation of bands for +0.1 electron charges (no electron spilling barrier)...\c"
$PW_COMMAND < single_+0.10_nobarrier.bands.in > single_+0.10_nobarrier.bands.out
check_failure $?
$ECHO " done"




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

$ECHO
$ECHO "$EXAMPLE_DIR: done"
