#!/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 compute the equilibrium geometry"
$ECHO "of a simple molecule, CO, and of an Al (001) slab."
$ECHO "In the latter case the relaxation is performed in two ways:"
$ECHO "1) using the quasi-Newton BFGS algorithm"
$ECHO "2) using a damped dynamics algorithm."

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

# required executables and pseudopotentials
BIN_LIST="pw.x"
PSEUDO_LIST=" O.pz-rrkjus.UPF C.pz-rrkjus.UPF Al.pz-vbc.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 \
                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
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"
$ECHO
$ECHO "  running pw.x as: $PW_COMMAND"
$ECHO

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

# self-consistent calculation
cat > co.rx.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>


<input calculation="relax" prefix="CO">

	<cell type="matrix" sym="cubic" units="angstrom">
		<matrix>
			<real rank="2" n1="3" n2="3">				
				12.0  0.0  0.0
				 0.0 12.0  0.0
				 0.0  0.0 12.0
			</real>
		</matrix>
	</cell>

	<atomic_species ntyp="2">
		<specie name="O">
			<property name="mass">
				<real>1.00</real>
			</property>
			<property name="pseudofile">
				<string>O.pz-rrkjus.UPF</string>
			</property>
		</specie>
		<specie name="C">
			<property name="mass">
				<real>1.00</real>
			</property>
			<property name="pseudofile">
				<string>C.pz-rrkjus.UPF</string>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="bohr" nat="2" >
		<atom name="C">
                    <position>
			<real rank="1" n1="3">
				2.256  0.0  0.0
			</real>
                    </position>
		</atom>	
		<atom name="O">
                    <position ifx="0" ify="0" ifz="0">
			<real rank="1" n1="3">
				0.000  0.0  0.0
			</real>
                    </position>
		</atom>		
	</atomic_list>		
		
	<field name="InputOutput">

		<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>24.D0</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>144.D0</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>1.D-7</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>0.7D0</real>
		</parameter>
	
	</field>
	
	<k_points type="gamma">	
	</k_points>
	
</input>
EOF
$ECHO "  running the geometry relaxation for CO...\c"
$PW_COMMAND < co.rx.xml > co.rx.out
check_failure $?
$ECHO " done"

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

# self-consistent calculation
cat > al001.rx.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

<input calculation="relax">

	<cell type="qecell">
		<qecell ibrav="6" alat="5.3033D0">
			<real rank="1" n1="5">
				0.0 8.D0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Al">
			<property name="mass">
				<real>1.0</real>
			</property>
			<property name="pseudofile">
				<string>Al.pz-vbc.UPF</string>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="7" >
		<atom name="Al">
                    <position>
			<real rank="1" n1="3">
				0.5000000      0.5000000     -2.121320
			</real>
                    </position>
		</atom>
		<atom name="Al">
                    <position>
			<real rank="1" n1="3">
				0.0000000      0.0000000     -1.414213
			</real>
                    </position>
		</atom>
		<atom name="Al">
                    <position>
			<real rank="1" n1="3">
				0.5000000      0.5000000     -0.707107
			</real>
                    </position>
		</atom>
		<atom name="Al">
                    <position>
			<real rank="1" n1="3">
				0.0000000      0.0000000      0.000000
			</real>
                    </position>
		</atom>						
		<atom name="Al">
                    <position>
			<real rank="1" n1="3">
				0.5000000      0.5000000      0.707107
			</real>
                    </position>
		</atom>	
		<atom name="Al">
                    <position>
			<real rank="1" n1="3">
				0.0000000      0.0000000      1.414213
			</real>
                    </position>
		</atom>	
		<atom name="Al">
                    <position>
			<real rank="1" n1="3">
				0.5000000      0.5000000      2.121320
			</real>
                    </position>
		</atom>							
	</atomic_list>		
	
	
	<field name="InputOutput">

		<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>12.D0</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>1.D-6</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>0.3D0</real>
		</parameter>
	
	</field>
	
	<field name="Options">

		<parameter name="occupations">
			<string>smearing</string>
		</parameter>
		
		<parameter name="smearing">
			<string>methfessel-paxton</string>
		</parameter>
		
		<parameter name="degauss">
			<real>0.05D0</real>
		</parameter>		
	
	</field>
	
	<field name="RELAX">
	
		<parameter name="bfgs_ndim">
			<integer>3</integer>
		</parameter>
	
	</field>
	
	<k_points type="tpiba">
		<mesh npoints="3">
			<real rank="2" n1="4" n2="3">
				 0.125  0.125  0.0  1.0
				 0.125  0.375  0.0  2.0
				 0.375  0.375  0.0  1.0
			</real>
		</mesh>
	</k_points>

</input>
EOF
$ECHO "  running the geometry relaxation for Al (001) using BFGS...\c"
$PW_COMMAND < al001.rx.xml > al001.rx.out
check_failure $?
$ECHO " done"

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

# self-consistent calculation
rm -f e eal ave p avec tv
cat > al001.mm.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>


<input calculation="relax">

	<cell type="qecell">
		<qecell ibrav="6" alat="5.3033D0">
			<real rank="1" n1="5">
				0.0 8.D0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Al">
			<property name="mass">
				<real>1.D0</real>
			</property>
			<property name="pseudofile">
				<string>Al.pz-vbc.UPF</string>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="7" >
		<atom name="Al">
                    <position>
			<real rank="1" n1="3">
				0.5000000      0.5000000     -2.121320
			</real>
                    </position>
		</atom>	
		<atom name="Al">
                    <position>
			<real rank="1" n1="3">
				0.0000000      0.0000000     -1.414213
			</real>
                    </position>
		</atom>	
		<atom name="Al">
                    <position>
			<real rank="1" n1="3">
				0.5000000      0.5000000     -0.707107
			</real>
                    </position>
		</atom>	
		<atom name="Al">
                    <position>
			<real rank="1" n1="3">
				0.0000000      0.0000000      0.000000
			</real>
                    </position>
		</atom>	
		<atom name="Al">
                    <position>
			<real rank="1" n1="3">
				0.5000000      0.5000000      0.707107
			</real>
                    </position>
		</atom>	
		<atom name="Al">
                    <position>
			<real rank="1" n1="3">
				0.0000000      0.0000000      1.414213
			</real>
                    </position>
		</atom>	
		<atom name="Al">
                    <position>
			<real rank="1" n1="3">
				0.5000000      0.5000000      2.121320
			</real>
                    </position>
		</atom>		
	</atomic_list>		
	
	<field name="InputOutput">

		<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>12.D0</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>1.D-7</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>0.3D0</real>
		</parameter>
	
	</field>
	
	<field name="Options">

		<parameter name="occupations">
			<string>smearing</string>
		</parameter>
		
		<parameter name="smearing">
			<string>methfessel-paxton</string>
		</parameter>
		
		<parameter name="degauss">
			<real>0.05D0</real>
		</parameter>		
	
	</field>
		
	<field name="RELAX">
	
		<parameter name="ion_dynamics">
			<string>damp</string>
		</parameter>

                <parameter name="wfc_extrapolation">
                        <string>second_order</string>
                </parameter>

                <parameter name="pot_extrapolation">
                        <string>second_order</string>
                </parameter>

		<parameter name="dt">
		   <real>30.D0</real>
		</parameter>
	
	</field>
	
	<k_points type="tpiba">
		<mesh npoints="3">
			<real rank="2" n1="4" n2="3">
				   	 0.125  0.125  0.0  1.0
					 0.125  0.375  0.0  2.0
					 0.375  0.375  0.0  1.0
			</real>
		</mesh>
	</k_points>
</input>
EOF
$ECHO "  running the geometry relaxation for Al (001) using damped MD...\c"
$PW_COMMAND < al001.mm.xml > al001.mm.out
check_failure $?
$ECHO " done"

$ECHO
$ECHO "$EXAMPLE_DIR: done"
