#!/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 the recover feature of ph.x "

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

# required executables and pseudopotentials
BIN_LIST="pw.x ph.x"
PSEUDO_LIST="Si.pz-vbc.UPF Ni.pbe-nd-rrkjus.UPF Cu.pbe-kjpaw.UPF Au.rel-pz-kjpaw.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" "$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"
PH_COMMAND="$PARA_PREFIX $BIN_DIR/ph.x $PARA_POSTFIX"
$ECHO
$ECHO "  running pw.x as: $PW_COMMAND"
$ECHO "  running ph.x as: $PH_COMMAND"
$ECHO

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

# self-consistent calculation
cat > si.scf.in << EOF
 &control
    calculation='scf',
    restart_mode='from_scratch',
    prefix='silicon'
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
 /
 &system
    ibrav = 2, celldm(1) =10.20, nat=  2, ntyp= 1,
    ecutwfc = 18.0
 /
 &electrons
    mixing_beta = 0.7
    conv_thr =  1.0d-8
 /
ATOMIC_SPECIES
 Si  28.086  Si.pz-vbc.UPF
ATOMIC_POSITIONS (alat)
 Si 0.00 0.00 0.00
 Si 0.25 0.25 0.25
K_POINTS
  10
   0.1250000  0.1250000  0.1250000   1.00
   0.1250000  0.1250000  0.3750000   3.00
   0.1250000  0.1250000  0.6250000   3.00
   0.1250000  0.1250000  0.8750000   3.00
   0.1250000  0.3750000  0.3750000   3.00
   0.1250000  0.3750000  0.6250000   6.00
   0.1250000  0.3750000  0.8750000   6.00
   0.1250000  0.6250000  0.6250000   3.00
   0.3750000  0.3750000  0.3750000   1.00
   0.3750000  0.3750000  0.6250000   3.00
EOF
$ECHO "  running the scf calculation for NC-Si...\c"
$PW_COMMAND < si.scf.in > si.scf.out
check_failure $?
$ECHO " done"

# phonon calculation at Gamma
cat > si.phG.in1 << EOF
phonons of Si at Gamma
 &inputph
  tr2_ph=1.0d-14,
  prefix='silicon',
  epsil=.true.,
  max_seconds=1,
  amass(1)=28.08,
  outdir='$TMP_DIR/',
  fildyn='si.dynG',
 /
0.0 0.0 0.0
EOF
$ECHO "  running the phonon calculation at Gamma for NC-Si...\c"
$PH_COMMAND < si.phG.in1 > si.phG.out1

# phonon calculation at Gamma
cat > si.phG.in2 << EOF
phonons of Si at Gamma
 &inputph
  tr2_ph=1.0d-14,
  prefix='silicon',
  epsil=.true.,
  recover=.true.,
  amass(1)=28.08,
  outdir='$TMP_DIR/',
  fildyn='si.dynG',
 /
0.0 0.0 0.0
EOF
$ECHO "  recovering the phonon calculation at Gamma for NC-Si...\c"
$PH_COMMAND < si.phG.in2 > si.phG.out2
check_failure $?
$ECHO " done"
$ECHO

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

# self-consistent calculation for Ni with US-PP
cat > ni.scf.in << EOF
 &control
    calculation='scf'
    restart_mode='from_scratch',
    tprnfor = .true.
    prefix='nickel',
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
 /
 &system
    ibrav=2, celldm(1) =6.65, nat=  1, ntyp= 1,
    nspin=2,
    starting_magnetization(1)=0.5,
    degauss=0.02,
    smearing='mv',
    occupations='smearing',
    nr1=27,
    nr2=27,
    nr3=27,
    ecutwfc =27.0
    ecutrho =300.0
 /
 &electrons
    conv_thr =  1.0d-8
    mixing_beta = 0.7
 /
ATOMIC_SPECIES
 Ni  58.6934  Ni.pbe-nd-rrkjus.UPF
ATOMIC_POSITIONS (alat)
 Ni 0.00 0.00 0.00
K_POINTS AUTOMATIC
4 4 4 1 1 1
EOF
$ECHO "  running the scf calculation for US-Ni...\c"
$PW_COMMAND < ni.scf.in > ni.scf.out
check_failure $?
$ECHO " done"

# phonon calculation at X
cat > ni.phX.in1 << EOF
phonons of Ni at X
 &inputph
  tr2_ph=1.0d-14,
  prefix='nickel',
  amass(1)=58.6934,
  max_seconds=6,
  fildyn='nix.dyn',
  outdir='$TMP_DIR/',
 /
0.0 0.0 1.0
EOF
$ECHO "  running the phonon calculation at X for US-Ni...\c"
$PH_COMMAND < ni.phX.in1 > ni.phX.out1
$ECHO " done"

# phonon calculation at X
cat > ni.phX.in2 << EOF
phonons of Ni at X
 &inputph
  tr2_ph=1.0d-14,
  prefix='nickel',
  amass(1)=58.6934,
  recover=.true.,
  fildyn='nix.dyn',
  outdir='$TMP_DIR/',
 /
0.0 0.0 1.0
EOF
$ECHO "  recovering the phonon calculation at X for US-Ni...\c"
$PH_COMMAND < ni.phX.in2 > ni.phX.out2
check_failure $?
$ECHO " done"
$ECHO

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

# self-consistent calculation for PAW Cu PBE with PAW-PP
cat > Cu.scf_pbe.in << EOF
 &control
    calculation='scf',
    restart_mode='from_scratch',
    prefix='copper',
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
 /
 &system
    ibrav = 2, celldm(1) =6.90, nat=  1, ntyp= 1,
    occupations='smearing',
    smearing='mv',
    degauss=0.01,
    ecutwfc = 30.0
    ecutrho = 700.0
 /
 &electrons
    startingwfc='atomic'
    mixing_beta = 0.7
    conv_thr =  1.0d-9
 /
ATOMIC_SPECIES
Cu   0.0  Cu.pbe-kjpaw.UPF
ATOMIC_POSITIONS (alat)
 Cu  0.00 0.00 0.00
K_POINTS AUTOMATIC
4 4 4 1 1 1
EOF
$ECHO "  running the scf calculation for PAW-Cu with GGA-PBE...\c"
$PW_COMMAND < Cu.scf_pbe.in > Cu.scf_pbe.out
check_failure $?
$ECHO " done"

# phonon calculation at G
cat > Cu.phG_pbe.in1 << EOF
phonons of Cu at Gamma
 &inputph
  tr2_ph=1.0d-14,
  prefix='copper',
  max_seconds=5,
  fildyn='Cug.dyn',
  outdir='$TMP_DIR/'
 /
0.0 0.0 0.0
EOF
$ECHO "  running the phonon calculation for PAW-Cu at Gamma...\c"
$PH_COMMAND < Cu.phG_pbe.in1 > Cu.phG_pbe.out1

# phonon calculation at G
cat > Cu.phG_pbe.in2 << EOF
phonons of Cu at Gamma
 &inputph
  tr2_ph=1.0d-14,
  prefix='copper',
  recover=.true.,
  fildyn='Cug.dyn',
  outdir='$TMP_DIR/'
 /
0.0 0.0 0.0
EOF
$ECHO "  recovering the phonon calculation for PAW-Cu at Gamma...\c"
$PH_COMMAND < Cu.phG_pbe.in2 > Cu.phG_pbe.out2
check_failure $?
$ECHO " done"
$ECHO

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

# self-consistent calculation for PAW Au with spin-orbit
cat > Au.scf_rel.in << EOF
 &control
    calculation='scf',
    restart_mode='from_scratch',
    prefix='gold',
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
 /
 &system
    ibrav = 2, celldm(1) =7.666, nat=  1, ntyp= 1,
    noncolin=.true.,
    lspinorb=.true.,
    occupations='smearing',
    smearing='mv',
    degauss=0.04,
    ecutwfc = 35.0
    ecutrho = 400.0
 /
 &electrons
    mixing_beta = 0.7
    conv_thr =  1.0d-9
 /
ATOMIC_SPECIES
Au   0.0  Au.rel-pz-kjpaw.UPF
ATOMIC_POSITIONS (alat)
Au  0.00 0.00 0.00
K_POINTS AUTOMATIC
4 4 4 1 1 1
EOF
$ECHO "  running the scf calculation for PAW-Au with spin-orbit...\c"
$PW_COMMAND < Au.scf_rel.in > Au.scf_rel.out
check_failure $?
$ECHO " done"

# phonon calculation at G
cat > Au.phX_rel.in1 << EOF
phonons of Au at X
 &inputph
  tr2_ph=1.0d-14,
  prefix='gold',
  max_seconds=15,
  fildyn='AuX.dyn',
  outdir='$TMP_DIR/'
 /
1.0 0.0 0.0
EOF
$ECHO "  running the phonon calculation for PAW-Au at X...\c"
$PH_COMMAND < Au.phX_rel.in1 > Au.phX_rel.out1

# phonon calculation at X
cat > Au.phX_rel.in2 << EOF
phonons of Au at X
 &inputph
  tr2_ph=1.0d-14,
  prefix='gold',
  recover=.true.,
  fildyn='AuX.dyn',
  outdir='$TMP_DIR/'
 /
1.0 0.0 0.0
EOF
$ECHO "  recovering the phonon calculation for PAW-Au at X...\c"
$PH_COMMAND < Au.phX_rel.in2 > Au.phX_rel.out2
check_failure $?
$ECHO " done"

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

# self-consistent calculation for PAW Cu PBE with PAW-PP
cat > Cu.scf_pbe.in << EOF
 &control
    calculation='scf',
    restart_mode='from_scratch',
    prefix='copper',
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
 /
 &system
    ibrav = 2, celldm(1) =6.90, nat=  1, ntyp= 1,
    occupations='smearing',
    smearing='mv',
    degauss=0.01,
    ecutwfc = 30.0
    ecutrho = 700.0
 /
 &electrons
    mixing_beta = 0.7
    conv_thr =  1.0d-9
 /
ATOMIC_SPECIES
Cu   0.0  Cu.pbe-kjpaw.UPF
ATOMIC_POSITIONS (alat)
 Cu  0.00 0.00 0.00
K_POINTS AUTOMATIC
4 4 4 1 1 1
EOF
$ECHO "  running the scf calculation for PAW-Cu with GGA-PBE...\c"
$PW_COMMAND < Cu.scf_pbe.in > Cu.scf_pbe.out
check_failure $?
$ECHO " done"

# single mode phonon calculation at X
cat > Cu.phX_pbe_one_mode.in1 << EOF
phonons of Cu at X
 &inputph
  tr2_ph=1.0d-14,
  prefix='copper',
  max_seconds=12,
  modenum=2,
  fildyn='Cux.dyn',
  outdir='$TMP_DIR/'
 /
1.0 0.0 0.0
EOF
$ECHO "  running the phonon calculation for one mode PAW-Cu at X...\c"
$PH_COMMAND < Cu.phX_pbe_one_mode.in1 > Cu.phX_pbe_one_mode.out1
$ECHO " done"

# single mode phonon calculation at X
cat > Cu.phX_pbe_one_mode.in2 << EOF
phonons of Cu at X
 &inputph
  tr2_ph=1.0d-14,
  prefix='copper',
  modenum=2,
  recover=.true.,
  fildyn='Cux.dyn',
  outdir='$TMP_DIR/'
 /
1.0 0.0 0.0
EOF
$ECHO "  recovering the phonon calculation for one mode PAW-Cu at X...\c"
$PH_COMMAND < Cu.phX_pbe_one_mode.in2 > Cu.phX_pbe_one_mode.out2
check_failure $?
$ECHO " done"
$ECHO

$ECHO
$ECHO "$EXAMPLE_DIR: done"

