#!/bin/sh

###############################################################################
##
##  HIGH VERBOSITY 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 pmw.x to generate better projectors for
$ECHO  LDA+U calculation on FeO. Read file README for more details"

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

# required executables and pseudopotentials
BIN_LIST="pw.x pmw.x"
PSEUDO_LIST="O.pz-rrkjus.UPF Fe.pz-nd-rrkjus.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

# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"
PMW_COMMAND="$PARA_PREFIX $BIN_DIR/pmw.x $PARA_POSTFIX"
$ECHO
$ECHO "  running pw.x as: $PW_COMMAND"
$ECHO "  running pmw.x as: $PMW_COMMAND"
$ECHO

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

# self-consistent calculation with standard LDA
cat > feo_LDA.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

<!-- FeO
FeO Wustite in LDA -->

<input calculation="scf" prefix="feo_af">

	<cell type="matrix" sym="cubic">
		<matrix units="alat" alat="8.19">
			<real rank="2" n1="3" n2="3">				
				0.50 0.50 1.00
				0.50 1.00 0.50
				1.00 0.50 0.50
			</real>
		</matrix>
	</cell>

	<atomic_species ntyp="3">
		<specie name="O1">
			<property name="mass">
				<real>1.0</real>
			</property>
			<property name="pseudofile">
				<string>O.pz-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.0</real>
			</property>
			<property name="hubbard_u">
				<real>0.0</real>
			</property>		
		</specie>
		<specie name="Fe1">
			<property name="mass">
				<real>1.0</real>
			</property>
			<property name="pseudofile">
				<string>Fe.pz-nd-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5</real>
			</property>			
			<property name="hubbard_u">
				<real>1.d-8</real>
			</property>
		</specie>
		<specie name="Fe2">
			<property name="mass">
				<real>1.0</real>
			</property>
			<property name="pseudofile">
				<string>Fe.pz-nd-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>-0.5</real>
			</property>			
			<property name="hubbard_u">
				<real>1.d-8</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="crystal" nat="4" >
		<atom name="O1">
			<position>
				<real rank="1" n1="3">
					0.25 0.25 0.25
				</real>
			</position>
		</atom>	
		<atom name="O1">
			<position>
				<real rank="1" n1="3">
					0.75 0.75 0.75
				</real>
			</position>
		</atom>	
		<atom name="Fe1">
			<position>
				<real rank="1" n1="3">
					0.0  0.0  0.0
				</real>
			</position>
		</atom>	
		<atom name="Fe2">
			<position>
				<real rank="1" n1="3">
					0.5  0.5  0.5
				</real>
			</position>
		</atom>					
	</atomic_list>		
	
	
	<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>
		
		<parameter name="tprnfor">
			<logical>
				true
			</logical>
		</parameter>

		<parameter name="tstress">
			<logical>
				true
			</logical>
		</parameter>
		
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				30.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				240.0
			</real>
		</parameter>
		
		<parameter name="mixing_mode">
			<string>
				plain
			</string>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.3
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0d-6
			</real>
		</parameter>
		
	</field>
	
	<field name="XC_Functional">

		<parameter name="lda_plus_u">
			<logical>
				true
			</logical>
		</parameter>
		
		<parameter name="mixing_fixed_ns">
			<integer>
				0
			</integer>
		</parameter>
		
	</field>	
	
	<field name="Options">

		<parameter name="occupations">
			<string>
				smearing
			</string>
		</parameter>
		
		<parameter name="smearing">
			<string>
				gauss
			</string>
		</parameter>
		
		<parameter name="degauss">
			<real>
				0.01
			</real>
		</parameter>
		
		<parameter name="nbnd">
			<integer>
				20
			</integer>
		</parameter>
		
	</field>	
	
	<field name="Fields">
		
		<parameter name="nspin">
			<integer>
				 2
			</integer>
		</parameter>
		
	</field>
	
	<k_points type="automatic">
		<mesh>
			<integer rank="1" n1="6">
				2 2 2 0 0 0
			</integer>
		</mesh> 
	</k_points>
</input>
EOF
$ECHO "  1) running scf for FeO in LDA ...\c"
$PW_COMMAND < feo_LDA.xml > feo_LDA.out
check_failure $?
$ECHO " done"

$ECHO "  2) running poormanwannier post-processing to build Wannier projectors ...\c"
cat > pmw.in << EOF
 &inputpp
    outdir='$TMP_DIR/',
    prefix='feo_af',
    first_band=9, last_band=20,
 /
EOF
$PMW_COMMAND < pmw.in > pmw.out
check_failure $?
$ECHO " done"

# self-consistent calculation with user defined ns + Wannier
cat > feo_wannier.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

<!-- FeO
FeO Wustite whithin LDA+U with user defined ns + Wannier -->

<input calculation="scf" prefix="feo_af">

	<cell type="matrix" sym="cubic">
		<matrix units="alat" alat="8.19">
			<real rank="2" n1="3" n2="3">				
				0.50 0.50 1.00
				0.50 1.00 0.50
				1.00 0.50 0.50
			</real>
		</matrix>
	</cell>

	<atomic_species ntyp="3">
		<specie name="O1">
			<property name="mass">
				<real>1.0</real>
			</property>
			<property name="pseudofile">
				<string>O.pz-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.0</real>
			</property>				
		</specie>
		<specie name="Fe1">
			<property name="mass">
				<real>1.0</real>
			</property>
			<property name="pseudofile">
				<string>Fe.pz-nd-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5</real>
			</property>			
			<property name="hubbard_u">
				<real>4.3</real>
			</property>
			<property name="starting_ns_eigenvalue" ispin="2" ns="3">
				<real>1.d0</real>
			</property>	
		</specie>
		<specie name="Fe2">
			<property name="mass">
				<real>1.0</real>
			</property>
			<property name="pseudofile">
				<string>Fe.pz-nd-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>-0.5</real>
			</property>			
			<property name="hubbard_u">
				<real>4.3</real>
			</property>
			<property name="starting_ns_eigenvalue" ispin="1" ns="3">
				<real>1.d0</real>
			</property>	
		</specie>
	</atomic_species>

	<atomic_list units="crystal" nat="4" >
		<atom name="O1">
			<position>
				<real rank="1" n1="3">
					0.25 0.25 0.25
				</real>
			</position>
		</atom>	
		<atom name="O1">
			<position>
				<real rank="1" n1="3">
					0.75 0.75 0.75
				</real>
			</position>
		</atom>	
		<atom name="Fe1">
			<position>
				<real rank="1" n1="3">
					0.0  0.0  0.0
				</real>
			</position>
		</atom>	
		<atom name="Fe2">
			<position>
				<real rank="1" n1="3">
					0.5  0.5  0.5
				</real>
			</position>
		</atom>					
	</atomic_list>		
	
	
	<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>
				30.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				240.0
			</real>
		</parameter>
		
		<parameter name="mixing_mode">
			<string>
				plain
			</string>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.3
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0d-6
			</real>
		</parameter>
		
	</field>
	
	<field name="XC_Functional">

		<parameter name="lda_plus_u">
			<logical>
				true
			</logical>
		</parameter>
		
		<parameter name="U_projection_type">
			<string>
				file
			</string>
		</parameter>
		
		<parameter name="mixing_fixed_ns">
			<integer>
				0
			</integer>
		</parameter>
		
	</field>	
	
	<field name="Options">

		<parameter name="occupations">
			<string>
				smearing
			</string>
		</parameter>
		
		<parameter name="smearing">
			<string>
				gauss
			</string>
		</parameter>
		
		<parameter name="degauss">
			<real>
				0.01
			</real>
		</parameter>
		
		<parameter name="nbnd">
			<integer>
				20
			</integer>
		</parameter>
		
	</field>	
	
	<field name="Fields">
	
		<parameter name="nspin">
			<integer>
				 2
			</integer>
		</parameter>
		
	</field>
	
	<k_points type="automatic">
		<mesh>
			<integer rank="1" n1="6">
				2 2 2 0 0 0
			</integer>
		</mesh> 
	</k_points>
</input>
EOF
$ECHO "  3) running scf for FeO in LDA+U using Wannier projectors...\c"
$PW_COMMAND < feo_wannier.xml > feo_wannier.out
check_failure $?
$ECHO " done"

$ECHO
$ECHO "$EXAMPLE_DIR : done"
