#!/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 with vdw-DF functional. In the"
$ECHO "first part a cell relaxation of graphite will be calculated and"
$ECHO "then the energy of two water molecules far apart will be computed."
$ECHO "Optionally, at the end, you can see how to set up a force relaxation"
$ECHO "of an Argon dimer, not activated by default in the distribution."

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

# required executables and pseudopotentials
BIN_LIST="pw.x generate_vdW_kernel_table.x"
PSEUDO_LIST="C.pbe-rrkjus.UPF O.pbe-rrkjus.UPF H.pbe-rrkjus.UPF"
VDW_TABLE="vdW_kernel_table"

$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"
GEN_COMMAND="$PARA_PREFIX $BIN_DIR/generate_vdW_kernel_table.x $PARA_POSTFIX"

# check for vdw kernel table
if test ! -r $PSEUDO_DIR/$VDW_TABLE ; then
	$ECHO " "
        $ECHO " "
	$ECHO "  WARNING: $PSEUDO_DIR/$VDW_TABLE not existent or not readable"
	$ECHO "  WARNING: a new table will be generated, this process will"
	$ECHO "  WARNING: probably take about 20 mins (depending on your cpu"
	$ECHO "  WARNING: power and configuration)."
	$ECHO
	$ECHO "  Generating $VDW_TABLE...\c"
	if $GEN_COMMAND ; then
		if test ! -r $VDW_TABLE ; then
			$ECHO "  ERROR: cannot generate vdW_kernel_table !!"
			exit 1
		fi
		$ECHO "done ! Table moved to $PSEUDO_DIR"	
		mv $VDW_TABLE $PSEUDO_DIR
	fi
fi 
$ECHO " done"

# Print how we run executables
$ECHO
$ECHO "  running pw.x as: $PW_COMMAND"
$ECHO

#
# Graphite cell relaxation
#
cat > graphite.scf.in << EOF
&control
   calculation = 'vc-relax'
   restart_mode='from_scratch',
   prefix='graphite',
   tstress = .true.
   tprnfor = .true.
   pseudo_dir = '$PSEUDO_DIR',
   outdir='$TMP_DIR'

   forc_conv_thr = 1.0D-3

/

&system
    ibrav           = 4 
    celldm(1)       = 4.6411700000
    celldm(3)       = 2.7264000000
    
    nat             = 4 
    ntyp            = 1

    occupations     = 'fixed'
    smearing        = 'methfessel-paxton'
    degauss         = 0.02

    ecutwfc         = 30.0
    ecutrho         = 180.0

    input_dft  	    = 'vdW-DF'
/

&electrons
    conv_thr        = 1.0d-8
/
&ions
/
&cell
    press_conv_thr  = 0.5D0
    press           = 0.D0
    cell_dynamics   = 'bfgs'
    cell_dofree     = 'z'
/

ATOMIC_SPECIES
C   12.00   C.pbe-rrkjus.UPF

ATOMIC_POSITIONS {alat}
C      0.0000000000 0.0000000000 0.0000000000 
C      0.0000000000 0.5773502692 0.0000000000
C      0.0000000000 0.0000000000 1.3632000000
C      0.5000000000 0.2886751346 1.3632000000

K_POINTS automatic
4 4 4 1 1 1
EOF

$ECHO "  running the graphite cell relaxation...\c"
$PW_COMMAND < graphite.scf.in > graphite.scf.out
check_failure $?
$ECHO " done"

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

#
# self-consistent calculation
# for water molecules 
#
cat > water.scf.in << EOF
&control
   calculation = 'scf'
   restart_mode='from_scratch',
   prefix='water_vdw',
   tstress = .true.
   tprnfor = .true.
   pseudo_dir = '$PSEUDO_DIR/',
   outdir='$TMP_DIR/'
   verbosity = 'high'
/

&system
    ibrav           = 8
    celldm(1)       = 15 
    celldm(2)       = 0.954545454545455
    celldm(3)       = 1.22727272727273
    nat             = 6
    ntyp            = 2
    occupations     = 'fixed'
    ecutwfc         = 30.0
    ecutrho         = 180.0
    input_dft       = 'vdW-DF'
/

&electrons
    conv_thr        = 1.0d-8
/


ATOMIC_SPECIES
   O   15.9994    O.pbe-rrkjus.UPF
   H   1.00794    H.pbe-rrkjus.UPF
 
ATOMIC_POSITIONS {angstrom}
O     -0.000000     0.013129    -0.057535
H     -0.000000     0.779069    -0.656064
H      0.000000     0.389646     0.845802
O      0.000000     0.887109     2.818248
H     -0.774530     0.521469     3.280767
H      0.774530     0.521469     3.280767

K_POINTS gamma

EOF

$ECHO "  running the scf calculation for water molecules...\c"
$PW_COMMAND < water.scf.in > water.scf.out
check_failure $?
$ECHO " done"

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

#
# self-consistent calculation
# for Argon dimer 
#
cat > Ar.scf.in << EOF
&control
   calculation = 'scf'
   restart_mode='from_scratch',
   prefix='Ar_vdw',
   tstress = .true.
   tprnfor = .true.
   pseudo_dir = '$PSEUDO_DIR/',
   outdir='$TMP_DIR/'
   verbosity = 'high'
   forc_conv_thr   = 1.0d-4
/

&system
    ibrav           = 8
    celldm(1)       = 19
    celldm(2)       = 1
    celldm(3)       = 1.47368421052632
    nat             = 2
    ntyp            = 1
    occupations     = 'fixed'
    ecutwfc         = 80.0
    input_dft  	    = 'vdW-DF'
/

&electrons
    conv_thr        = 1.0d-11
/

&ions
    ion_dynamics    = 'bfgs'
/

ATOMIC_SPECIES
    Ar   36.00    Ar.pz-rrkj.UPF

ATOMIC_POSITIONS {angstrom}
Ar      0.000000     0.000000     0.000000
Ar      0.000000     0.000000     4.500000

K_POINTS gamma 

EOF

$ECHO "  running the scf calculation for argon dimer...\c"
#$PW_COMMAND < Ar.scf.in > Ar.scf.out
#check_failure $?
#$ECHO " done"

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

$ECHO
$ECHO "$EXAMPLE_DIR : done"
