#!/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 neb.x to calculate the"
$ECHO "minimum energy path (MEP) of the collinear proton transfer reaction:"
$ECHO "  H2+H <==> H+H2, within the Born-Oppenheimer approximation."
$ECHO
$ECHO "!!! Beware: neb.x DOES NOT READ FROM STANDARD INPUT"
$ECHO "run with neb.x -inp input_file_name > output_file_name."
$ECHO
$ECHO "xml format is automatically detected."

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

# required executables and pseudopotentials
BIN_LIST="neb.x"
PSEUDO_LIST="HUSPBE.RRKJ3"

$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 \
                http://www.quantum-espresso.org/pseudo/1.3/UPF/$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
NEB_COMMAND="$PARA_PREFIX $BIN_DIR/neb.x $PARA_POSTFIX"
$ECHO
$ECHO "  running Born-Oppenheimer NEB as: $NEB_COMMAND"
$ECHO

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

# NEB calculation. Automatic choice of the climbing image.
cat > H2+H.in << EOF
BEGIN
BEGIN_PATH_INPUT
&PATH
  restart_mode      = 'from_scratch'
  string_method     = 'neb',
  nstep_path        = 20,
  ds                = 2.D0,
  opt_scheme        = "broyden",
  num_of_images     = 7,
  k_max             = 0.3D0,
  k_min             = 0.2D0,
  CI_scheme         = "auto",
  path_thr          = 0.1D0,
/
END_PATH_INPUT
BEGIN_ENGINE_INPUT
<?xml version="1.0" encoding="UTF-8"?>

<input calculation="scf" prefix="H2+H">
        <cell type="matrix">
                <matrix units="bohr">
                        <real rank="2" n1="3" n2="3">
                            12.0 0.0 0.0
                            0.0 5.0 0.0
                            0.0 0.0 5.0
                        </real>
                </matrix>
        </cell>

        <atomic_species ntyp="1">
                <specie name="H">
                        <property name="mass">
                                <real>1.00794 </real>
                        </property>
                        <property name="pseudofile">
                                <string>HUSPBE.RRKJ3</string>
                        </property>
                       <property name="starting_magnetization">
                                <real>0.5</real>
                        </property>
                </specie>
        </atomic_species>
BEGIN_POSITIONS
FIRST_IMAGE
        <atomic_list units="bohr" nat="3" >
                <atom name="H">
                        <position ifx="1" ify="0" ifz="0">
                                <real rank="1" n1="3">
                                        -4.56670009      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
                <atom name="H">
                        <position ifx="0" ify="0" ifz="0">
                                <real rank="1" n1="3">
                                        0.00000000      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
               <atom name="H">
                        <position ifx="1" ify="0" ifz="0">
                                <real rank="1" n1="3">
                                        1.55776676      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
        </atomic_list>
LAST_IMAGE
        <atomic_list units="bohr" nat="3" >
                <atom name="H">
                        <position>
                                <real rank="1" n1="3">
                                        -1.55776676      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
                <atom name="H">
                        <position>
                                <real rank="1" n1="3">
                                        0.00000000      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
               <atom name="H">
                        <position>
                                <real rank="1" n1="3">
                                        4.56670009      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
        </atomic_list>
END_POSITIONS
        <field name="InputOutput">

                <parameter name="restart_mode">
                        <string>
                                from_scratch
                        </string>
                </parameter>

                <parameter name="pseudo_dir">
                        <string>
                               $PSEUDO_DIR
                        </string>
                </parameter>

                <parameter name="outdir">
                        <string>
                                $TMP_DIR/
                        </string>
                </parameter>

        </field>
        <field name="Numerics">

                <parameter name="ecutwfc">
                        <real>
                                20.0
                        </real>
                </parameter>

                <parameter name="ecutrho">
                        <real>
                                100.0
                        </real>
                </parameter>

                <parameter name="mixing_beta">
                        <real>
                                0.3
                        </real>
                </parameter>

                <parameter name="conv_thr">
                        <real>
                                1.0d-8
                        </real>
                </parameter>

        </field>
        <field name="Options">

                <parameter name="occupations">
                        <string>
                                smearing
                        </string>
                </parameter>

                <parameter name="degauss">
                        <real>
                                0.01
                        </real>
                </parameter>

        </field>
        <field name="Fields">

                <parameter name="nspin">
                        <integer>
                                2
                        </integer>
                </parameter>

        </field>
        <k_points type="gamma">
        </k_points>
</input>
END_ENGINE_INPUT
END
EOF
$ECHO "  running Born-Oppenheimer NEB calculation for H2+H => H+H2...\c"
$NEB_COMMAND -inp H2+H.in  > H2+H.out
check_failure $?
$ECHO " done"

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

# NEB calculation. Climbing image is not used
cat > symmetric_H2+H.in << EOF
BEGIN
BEGIN_PATH_INPUT
&PATH
  restart_mode      = 'from_scratch'
  string_method     = 'neb',
  nstep_path        = 20, 
  ds                = 2.D0,
  opt_scheme        = "broyden",
  num_of_images     = 8,
  k_max             = 0.3D0,
  k_min             = 0.2D0,
  path_thr          = 0.2D0,
/
END_PATH_INPUT
BEGIN_ENGINE_INPUT
<?xml version="1.0" encoding="UTF-8"?>

<input calculation="scf" prefix="symmetric_H2+H">
        <cell type="matrix">
                <matrix units="bohr">
                        <real rank="2" n1="3" n2="3">
                            12.0 0.0 0.0
                            0.0 5.0 0.0
                            0.0 0.0 5.0
                        </real>
                </matrix>
        </cell>

        <atomic_species ntyp="1">
                <specie name="H">
                        <property name="mass">
                                <real>1.00794 </real>
                        </property>
                        <property name="pseudofile">
                                <string>HUSPBE.RRKJ3</string>
                        </property>
                       <property name="starting_magnetization">
                                <real>0.5</real>
                        </property>
                </specie>
        </atomic_species>
BEGIN_POSITIONS
FIRST_IMAGE
        <atomic_list units="bohr" nat="3" >
                <atom name="H">
                        <position ifx="1" ify="0" ifz="0">
                                <real rank="1" n1="3">
                                        -4.56670009      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
                <atom name="H">
                        <position ifx="0" ify="0" ifz="0">
                                <real rank="1" n1="3">
                                        0.00000000      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
               <atom name="H">
                        <position ifx="1" ify="0" ifz="0">
                                <real rank="1" n1="3">
                                        1.55776676      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
        </atomic_list>
LAST_IMAGE
        <atomic_list units="bohr" nat="3" >
                <atom name="H">
                        <position>
                                <real rank="1" n1="3">
                                        -1.55776676      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
                <atom name="H">
                        <position>
                                <real rank="1" n1="3">
                                        0.00000000      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
               <atom name="H">
                        <position>
                                <real rank="1" n1="3">
                                        4.56670009      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
        </atomic_list>
END_POSITIONS
        <field name="InputOutput">

                <parameter name="restart_mode">
                        <string>
                                from_scratch
                        </string>
                </parameter>

                <parameter name="pseudo_dir">
                        <string>
                               $PSEUDO_DIR
                        </string>
                </parameter>

                <parameter name="outdir">
                        <string>
                                $TMP_DIR/
                        </string>
                </parameter>

        </field>
        <field name="Numerics">

                <parameter name="ecutwfc">
                        <real>
                                20.0
                        </real>
                </parameter>

                <parameter name="ecutrho">
                        <real>
                                100.0
                        </real>
                </parameter>

                <parameter name="mixing_beta">
                        <real>
                                0.3
                        </real>
                </parameter>

                <parameter name="conv_thr">
                        <real>
                                1.0d-8
                        </real>
                </parameter>

        </field>
        <field name="Options">

                <parameter name="occupations">
                        <string>
                                smearing
                        </string>
                </parameter>

                <parameter name="degauss">
                        <real>
                                0.01
                        </real>
                </parameter>

        </field>
        <field name="Fields">

                <parameter name="nspin">
                        <integer>
                                2
                        </integer>
                </parameter>

        </field>
        <k_points type="gamma">
        </k_points>
</input>
END_ENGINE_INPUT
END
EOF
$ECHO "  running Born-Oppenheimer NEB calculation for symmetric H2+H => H+H2...\c"
$NEB_COMMAND -inp symmetric_H2+H.in  > symmetric_H2+H.out
check_failure $?
$ECHO " done"

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

# the name of the restart file is changed in order to conform to the
# prefix of the new run
# the restart file asymmetric_H2+H.neb is modified (second row)
# since a new simulation (from the old path) is started
cat symmetric_H2+H.path | \
    awk '{if(NR==2){printf"   0\n"}; if(NR!=2){print}}' > asymmetric_H2+H.path

# NEB calculation. The image that has to climb is manually chosen
cat > asymmetric_H2+H.in << EOF
BEGIN
BEGIN_PATH_INPUT
&PATH
  restart_mode      = 'restart'
  string_method     = 'neb',
  nstep_path        = 20, 
  ds                = 2.D0,
  opt_scheme        = "broyden",
  num_of_images     = 8,
  k_max             = 0.3D0,
  k_min             = 0.2D0,
  path_thr          = 0.05D0,
  CI_scheme         = "manual"
/
CLIMBING_IMAGES
 5
END_PATH_INPUT
BEGIN_ENGINE_INPUT
<?xml version="1.0" encoding="UTF-8"?>

<input calculation="scf" prefix="asymmetric_H2+H">
        <cell type="matrix">
                <matrix units="bohr">
                        <real rank="2" n1="3" n2="3">
                            12.0 0.0 0.0
                            0.0 5.0 0.0
                            0.0 0.0 5.0
                        </real>
                </matrix>
        </cell>

        <atomic_species ntyp="1">
                <specie name="H">
                        <property name="mass">
                                <real>1.00794 </real>
                        </property>
                        <property name="pseudofile">
                                <string>HUSPBE.RRKJ3</string>
                        </property>
                       <property name="starting_magnetization">
                                <real>0.5</real>
                        </property>
                </specie>
        </atomic_species>
BEGIN_POSITIONS
FIRST_IMAGE
        <atomic_list units="bohr" nat="3" >
                <atom name="H">
                        <position ifx="1" ify="0" ifz="0">
                                <real rank="1" n1="3">
                                        -4.56670009      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
                <atom name="H">
                        <position ifx="0" ify="0" ifz="0">
                                <real rank="1" n1="3">
                                        0.00000000      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
               <atom name="H">
                        <position ifx="1" ify="0" ifz="0">
                                <real rank="1" n1="3">
                                        1.55776676      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
        </atomic_list>
LAST_IMAGE
        <atomic_list units="bohr" nat="3" >
                <atom name="H">
                        <position>
                                <real rank="1" n1="3">
                                        -1.55776676      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
                <atom name="H">
                        <position>
                                <real rank="1" n1="3">
                                        0.00000000      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
               <atom name="H">
                        <position>
                                <real rank="1" n1="3">
                                        4.56670009      0.00000000      0.00000000
                                </real>
                        </position>
                </atom>
        </atomic_list>
END_POSITIONS
        <field name="InputOutput">

                <parameter name="restart_mode">
                        <string>
                                from_scratch
                        </string>
                </parameter>

                <parameter name="pseudo_dir">
                        <string>
                               $PSEUDO_DIR
                        </string>
                </parameter>

                <parameter name="outdir">
                        <string>
                                $TMP_DIR/
                        </string>
                </parameter>

        </field>
        <field name="Numerics">

                <parameter name="ecutwfc">
                        <real>
                                20.0
                        </real>
                </parameter>

                <parameter name="ecutrho">
                        <real>
                                100.0
                        </real>
                </parameter>

                <parameter name="mixing_beta">
                        <real>
                                0.3
                        </real>
                </parameter>

                <parameter name="conv_thr">
                        <real>
                                1.0d-8
                        </real>
                </parameter>

        </field>
        <field name="Options">

                <parameter name="occupations">
                        <string>
                                smearing
                        </string>
                </parameter>

                <parameter name="degauss">
                        <real>
                                0.01
                        </real>
                </parameter>

        </field>
        <field name="Fields">

                <parameter name="nspin">
                        <integer>
                                2
                        </integer>
                </parameter>

        </field>
        <k_points type="gamma">
        </k_points>
</input>
END_ENGINE_INPUT
END
EOF
$ECHO "  running Born-Oppenheimer NEB calculation for asymmetric H2+H => H+H2...\c"
$NEB_COMMAND -inp asymmetric_H2+H.in > asymmetric_H2+H.out
check_failure $?
$ECHO " done"

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