#!/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 calculate the total energy and"
$ECHO "the band structure of four simple systems in the non-collinear case:"
$ECHO "Fe, Cu, Ni, O."

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

# required executables and pseudopotentials
BIN_LIST="pw.x"
PSEUDO_LIST="Cu.pz-d-rrkjus.UPF Ni.pbe-nd-rrkjus.UPF Fe.pz-nd-rrkjus.UPF \
             O.pbe-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
$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 > fe.scf.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

<!-- Fe
Iron -->

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

	<cell type="qecell">
		<qecell ibrav="3" alat="5.217">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Fe">
			<property name="mass">
				<real>55.847</real>
			</property>
			<property name="pseudofile">
				<string>Fe.pz-nd-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5</real>
			</property>			
			<property name="angle1">
				<real>90.0</real>
			</property>
			<property name="angle2">
				<real>0.0</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Fe">
			<position>
				<real rank="1" n1="3">
					0.0 0.0 0.0
				</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="report">
			<integer>
				1
			</integer>
		</parameter>
					
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				25.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				200.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.2
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-8
			</real>
		</parameter>
	
	</field>
	
	<field name="Options">

		<parameter name="occupations">
			<string>
				smearing
			</string>
		</parameter>
		
		<parameter name="smearing">
			<string>
				marzari-vanderbilt
			</string>
		</parameter>
		
		<parameter name="degauss">
			<real>
				0.05
			</real>
		</parameter>
		
		<parameter name="noncolin">
			<logical>
				true
			</logical>
		</parameter>
		
	</field>	
	
	<k_points type="tpiba">
		<mesh npoints="11">
			<real rank="2" n1="4" n2="11">
				       0.0625000  0.0625000  0.0625000   1.00
					   0.0625000  0.0625000  0.1875000   3.00
					   0.0625000  0.0625000  0.3125000   3.00
					   0.0625000  0.0625000  0.4375000   3.00
					   0.0625000  0.0625000  0.5625000   3.00
					   0.0625000  0.0625000  0.6875000   3.00
					   0.0625000  0.0625000  0.8125000   3.00
					   0.0625000  0.0625000  0.9375000   3.00
					   0.0625000  0.1875000  0.1875000   3.00
					   0.0625000  0.1875000  0.3125000   6.00
					   0.0625000  0.1875000  0.4375000   6.00
			</real>
		</mesh> 
	</k_points>
	
</input>
EOF
$ECHO "  running the scf calculation for Fe...\c"
$PW_COMMAND < fe.scf.xml > fe.scf.out
check_failure $?
$ECHO " done"

# band structure calculation
cat > fe.band.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

<!-- Fe
Iron -->

<input calculation="bands" prefix="fe">

	<cell type="qecell">
		<qecell ibrav="3" alat="5.217">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Fe">
			<property name="mass">
				<real>55.847</real>
			</property>
			<property name="pseudofile">
				<string>Fe.pz-nd-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5</real>
			</property>			
			<property name="angle1">
				<real>90.0</real>
			</property>
			<property name="angle2">
				<real>0.0</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Fe">
			<position>
				<real rank="1" n1="3">
					0.0 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>
		
		<parameter name="report">
			<integer>
				1
			</integer>
		</parameter>		
					
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				25.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				200.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.2
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-8
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">
		
		<parameter name="nbnd">
			<integer>
				16
			</integer>
		</parameter>
		
		<parameter name="noncolin">
			<logical>
				true
			</logical>
		</parameter>
		
	</field>	
	
	<k_points type="tpiba">
		<mesh npoints="28">
			<real rank="2" n1="4" n2="28">
				   0.0 0.0 0.0 1.0
				   0.0 0.0 0.1 1.0
				   0.0 0.0 0.2 1.0
				   0.0 0.0 0.3 1.0
				   0.0 0.0 0.4 1.0
				   0.0 0.0 0.5 1.0
				   0.0 0.0 0.6 1.0
				   0.0 0.0 0.7 1.0
				   0.0 0.0 0.8 1.0
				   0.0 0.0 0.9 1.0
				   0.0 0.0 1.0 1.0
				   0.0 0.0 0.0 1.0
				   0.0 0.1 0.1 1.0
				   0.0 0.2 0.2 1.0
				   0.0 0.3 0.3 1.0
				   0.0 0.4 0.4 1.0
				   0.0 0.5 0.5 1.0
				   0.0 0.6 0.6 1.0
				   0.0 0.7 0.7 1.0
				   0.0 0.8 0.8 1.0
				   0.0 0.9 0.9 1.0
				   0.0 1.0 1.0 1.0
				   0.0 0.0 0.0 1.0
				   0.1 0.1 0.1 1.0
				   0.2 0.2 0.2 1.0
				   0.3 0.3 0.3 1.0
				   0.4 0.4 0.4 1.0
				   0.5 0.5 0.5 1.0
			</real>
		</mesh> 
	</k_points>
</input>
EOF
$ECHO "  running band structure calculation for Fe...\c"
$PW_COMMAND < fe.band.xml > fe.band.out
check_failure $?
$ECHO " done"

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

# self-consistent calculation with penalty functional
cat > fe.pen.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>


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

	<cell type="qecell">
		<qecell ibrav="3" alat="5.217">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Fe">
			<property name="mass">
				<real>55.847</real>
			</property>
			<property name="pseudofile">
				<string>Fe.pz-nd-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5</real>
			</property>			
			<property name="angle1">
				<real>85.0</real>
			</property>
			<property name="angle2">
				<real>0.0</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Fe">
			<position>
				<real rank="1" n1="3">
					0.0 0.0 0.0
				</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="report">
			<integer>
				1
			</integer>
		</parameter>		
					
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				25.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				200.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.2
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-8
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">
		
		<parameter name="occupations">
			<string>
				smearing
			</string>
		</parameter>
		
		<parameter name="smearing">
			<string>
				marzari-vanderbilt
			</string>
		</parameter>
		
		<parameter name="degauss">
			<real>
				0.05
			</real>
		</parameter>
		
		<parameter name="noncolin">
			<logical>
				true
			</logical>
		</parameter>
		
		<parameter name="lambda">
			<real>
				1.0
			</real>
		</parameter>			
	
	</field>		
	
	<field name="Fields">
		
		<parameter name="constrained_magnetization">
			<string>
				atomic
			</string>
		</parameter>
		
	</field>
	
	<k_points type="tpiba">
		<mesh npoints="11">
			<real rank="2" n1="4" n2="11">
				   0.0625000  0.0625000  0.0625000   1.00
				   0.0625000  0.0625000  0.1875000   3.00
				   0.0625000  0.0625000  0.3125000   3.00
				   0.0625000  0.0625000  0.4375000   3.00
				   0.0625000  0.0625000  0.5625000   3.00
				   0.0625000  0.0625000  0.6875000   3.00
				   0.0625000  0.0625000  0.8125000   3.00
				   0.0625000  0.0625000  0.9375000   3.00
				   0.0625000  0.1875000  0.1875000   3.00
				   0.0625000  0.1875000  0.3125000   6.00
				   0.0625000  0.1875000  0.4375000   6.00
			</real>
		</mesh> 
	</k_points>
</input>
EOF
$ECHO "  running scf calculation for Fe with penalty functional...\c"
$PW_COMMAND < fe.pen.xml > fe.pen.out
check_failure $?
$ECHO " done"

# scf calculation with penalty functional (angle with z-axis constrained)
cat > fe.angl.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

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

	<cell type="qecell">
		<qecell ibrav="3" alat="5.217">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Fe">
			<property name="mass">
				<real>55.847</real>
			</property>
			<property name="pseudofile">
				<string>Fe.pz-nd-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5</real>
			</property>			
			<property name="angle1">
				<real>90.0</real>
			</property>
			<property name="angle2">
				<real>0.0</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Fe">
			<position>
				<real rank="1" n1="3">
					0.0 0.0 0.0
				</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="report">
			<integer>
				1
			</integer>
		</parameter>		
					
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				25.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				200.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.2
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-8
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">

		<parameter name="occupations">
			<string>
				smearing
			</string>
		</parameter>
		
		<parameter name="smearing">
			<string>
				marzari-vanderbilt
			</string>
		</parameter>
		
		<parameter name="degauss">
			<real>
				0.05
			</real>
		</parameter>
		
		<parameter name="noncolin">
			<logical>
				true
			</logical>
		</parameter>
		
		<parameter name="lambda">
			<real>
				1.0
			</real>
		</parameter>				
	
	</field>	
	
	<field name="Fields">

		<parameter name="constrained_magnetization">
			<string>
				atomic direction
			</string>
		</parameter>
	
	</field>
	
	<k_points type="tpiba">
		<mesh npoints="11">
			<real rank="2" n1="4" n2="11">
				   0.0625000  0.0625000  0.0625000   1.00
				   0.0625000  0.0625000  0.1875000   3.00
				   0.0625000  0.0625000  0.3125000   3.00
				   0.0625000  0.0625000  0.4375000   3.00
				   0.0625000  0.0625000  0.5625000   3.00
				   0.0625000  0.0625000  0.6875000   3.00
				   0.0625000  0.0625000  0.8125000   3.00
				   0.0625000  0.0625000  0.9375000   3.00
				   0.0625000  0.1875000  0.1875000   3.00
				   0.0625000  0.1875000  0.3125000   6.00
				   0.0625000  0.1875000  0.4375000   6.00
			</real>
		</mesh> 
	</k_points>
</input>
EOF
$ECHO "  running the scf calculation for Fe with constrained angle...\c"
$PW_COMMAND < fe.angl.xml > fe.angl.out
check_failure $?
$ECHO " done"

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

# scf calculation with penalty functional (total magnetization constrained)
cat > fe.total.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

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

	<cell type="qecell">
		<qecell ibrav="3" alat="5.217">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Fe">
			<property name="mass">
				<real>55.847</real>
			</property>
			<property name="pseudofile">
				<string>Fe.pz-nd-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5</real>
			</property>			
			<property name="angle1">
				<real>45.0</real>
			</property>
			<property name="angle2">
				<real>53.0</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Fe">
			<position>
				<real rank="1" n1="3">
					0.0 0.0 0.0
				</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="report">
			<integer>
				1
			</integer>
		</parameter>		
					
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				25.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				200.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.3
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-9
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">

		<parameter name="occupations">
			<string>
				smearing
			</string>
		</parameter>
		
		<parameter name="smearing">
			<string>
				marzari-vanderbilt
			</string>
		</parameter>
		
		<parameter name="degauss">
			<real>
				0.05
			</real>
		</parameter>
		
		<parameter name="noncolin">
			<logical>
				true
			</logical>
		</parameter>
				
		<parameter name="fixed_magnetization">
			<real rank="1" n1="3">
				0.3 0.4 0.5
			</real>
		</parameter>
		
		<parameter name="lambda">
			<real>
				0.5
			</real>
		</parameter>				
	
	</field>	
	
	<field name="Fields">

		<parameter name="constrained_magnetization">
			<string>
				total
			</string>
		</parameter>
	
	</field>
	
	<k_points type="automatic">
		<mesh>
			<integer rank="1" n1="6">
				 4 4 4 1 1 1
			</integer>
		</mesh>
	</k_points>
</input>
EOF
$ECHO "  running the scf calculation for Fe with constrained magnetization...\c"
$PW_COMMAND < fe.total.xml > fe.total.out
check_failure $?
$ECHO " done"

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

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


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

	<cell type="qecell">
		<qecell ibrav="2" alat="6.73">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Cu">
			<property name="mass">
				<real>63.55</real>
			</property>
			<property name="pseudofile">
				<string>Cu.pz-d-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5</real>
			</property>			
			<property name="angle1">
				<real>90.0</real>
			</property>
			<property name="angle2">
				<real>0.0</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Cu">
			<position>
				<real rank="1" n1="3">
					0.0 0.0 0.0
				</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>
				25.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				300.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.7
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-8
			</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.02
			</real>
		</parameter>
		
		<parameter name="noncolin">
			<logical>
				true
			</logical>
		</parameter>	
	
	</field>	
	
	<k_points type="automatic">
		<mesh>
			<integer rank="1" n1="6">
				 8 8 8 0 0 0
			</integer>
		</mesh>
	</k_points>
</input>
EOF
$ECHO "  running the scf calculation for Cu...\c"
$PW_COMMAND < cu.scf.xml > cu.scf.out
check_failure $?
$ECHO " done"

# band structure calculation along delta, sigma and lambda lines
cat > cu.band.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>


<input calculation="bands" prefix="cu">

	<cell type="qecell">
		<qecell ibrav="2" alat="6.73">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Cu">
			<property name="mass">
				<real>63.55</real>
			</property>
			<property name="pseudofile">
				<string>Cu.pz-d-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5</real>
			</property>			
			<property name="angle1">
				<real>90.0</real>
			</property>
			<property name="angle2">
				<real>0.0</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Cu">
			<position>
				<real rank="1" n1="3">
					0.0 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>
				25.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				300.0
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">

		<parameter name="nbnd">
			<integer>
				8
			</integer>
		</parameter>
		
		<parameter name="noncolin">
			<logical>
				true
			</logical>
		</parameter>
		
	</field>
	
	<k_points type="tpiba">
		<mesh npoints="28">
			<real rank="2" n1="4" n2="28">
				   0.0 0.0 0.0 1.0
				   0.0 0.0 0.1 1.0
				   0.0 0.0 0.2 1.0
				   0.0 0.0 0.3 1.0
				   0.0 0.0 0.4 1.0
				   0.0 0.0 0.5 1.0
				   0.0 0.0 0.6 1.0
				   0.0 0.0 0.7 1.0
				   0.0 0.0 0.8 1.0
				   0.0 0.0 0.9 1.0
				   0.0 0.0 1.0 1.0
				   0.0 0.0 0.0 1.0
				   0.0 0.1 0.1 1.0
				   0.0 0.2 0.2 1.0
				   0.0 0.3 0.3 1.0
				   0.0 0.4 0.4 1.0
				   0.0 0.5 0.5 1.0
				   0.0 0.6 0.6 1.0
				   0.0 0.7 0.7 1.0
				   0.0 0.8 0.8 1.0
				   0.0 0.9 0.9 1.0
				   0.0 1.0 1.0 1.0
				   0.0 0.0 0.0 1.0
				   0.1 0.1 0.1 1.0
				   0.2 0.2 0.2 1.0
				   0.3 0.3 0.3 1.0
				   0.4 0.4 0.4 1.0
				   0.5 0.5 0.5 1.0				   
			</real>
		</mesh>
	</k_points>
</input>
EOF
$ECHO "  running the band-structure calculation for Cu...\c"
$PW_COMMAND < cu.band.xml > cu.band.out
check_failure $?
$ECHO " done"

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

# self-consistent calculation with cg diagonalization
cat > cu.cg.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>


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

	<cell type="qecell">
		<qecell ibrav="2" alat="6.73">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Cu">
			<property name="mass">
				<real>63.55</real>
			</property>
			<property name="pseudofile">
				<string>Cu.pz-d-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5</real>
			</property>			
			<property name="angle1">
				<real>90.0</real>
			</property>
			<property name="angle2">
				<real>0.0</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Cu">
			<position>
				<real rank="1" n1="3">
					0.0 0.0 0.0
				</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>
				25.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				300.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.7
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-8
			</real>
		</parameter>
		
		<parameter name="diagonalization">
			<string>
				cg
			</string>
		</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.02
			</real>
		</parameter>
		
		<parameter name="noncolin">
			<logical>
				true
			</logical>
		</parameter>
		
	</field>	
	
	<k_points type="automatic">
		<mesh>
			<integer rank="1" n1="6">
				8 8 8 0 0 0
			</integer>
		</mesh> 
	</k_points>
</input>
EOF
$ECHO "  running the scf calculation for Cu with cg diagonalization...\c"
$PW_COMMAND < cu.cg.xml > cu.cg.out
check_failure $?
$ECHO " done"

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

# self-consistent calculation with diis diagonalization
cat > cu.diis.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>


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

	<cell type="qecell">
		<qecell ibrav="2" alat="6.73">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Cu">
			<property name="mass">
				<real>63.55</real>
			</property>
			<property name="pseudofile">
				<string>Cu.pz-d-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5</real>
			</property>			
			<property name="angle1">
				<real>90.0</real>
			</property>
			<property name="angle2">
				<real>0.0</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Cu">
			<position>
				<real rank="1" n1="3">
					0.0 0.0 0.0
				</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>
				25.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				300.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.7
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-8
			</real>
		</parameter>
		
		<parameter name="diagonalization">
			<string>
				diis
			</string>
		</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.02
			</real>
		</parameter>
		
		<parameter name="noncolin">
			<logical>
				true
			</logical>
		</parameter>
		
	</field>	
	
	<k_points type="automatic">
		<mesh>
			<integer rank="1" n1="6">
				8 8 8 0 0 0
			</integer>
		</mesh> 
	</k_points>
</input>
EOF
#$ECHO "  running the scf calculation for Cu with diis diagonalization...\c"
#$PW_COMMAND < cu.diis.xml > cu.diis.out
#check_failure $?
#$ECHO " done"

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

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

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

	<cell type="qecell">
		<qecell ibrav="2" alat="6.48">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Ni">
			<property name="mass">
				<real>58.69</real>
			</property>
			<property name="pseudofile">
				<string>Ni.pbe-nd-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.1</real>
			</property>			
			<property name="angle1">
				<real>90.0</real>
			</property>
			<property name="angle2">
				<real>0.0</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Ni">
			<position>
				<real rank="1" n1="3">
					0.0 0.0 0.0
				</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>
				24.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				288.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.7
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-8
			</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.02
			</real>
		</parameter>
		
		<parameter name="noncolin">
			<logical>
				true
			</logical>
		</parameter>	
		
	</field>	
	
	<k_points type="tpiba">
		<mesh npoints="60">
			<real rank="2" n1="4" n2="60">
				   0.0625000  0.0625000  0.0625000   1.00
				   0.0625000  0.0625000  0.1875000   3.00
				   0.0625000  0.0625000  0.3125000   3.00
				   0.0625000  0.0625000  0.4375000   3.00
				   0.0625000  0.0625000  0.5625000   3.00
				   0.0625000  0.0625000  0.6875000   3.00
				   0.0625000  0.0625000  0.8125000   3.00
				   0.0625000  0.0625000  0.9375000   3.00
				   0.0625000  0.1875000  0.1875000   3.00
				   0.0625000  0.1875000  0.3125000   6.00
				   0.0625000  0.1875000  0.4375000   6.00
				   0.0625000  0.1875000  0.5625000   6.00
				   0.0625000  0.1875000  0.6875000   6.00
				   0.0625000  0.1875000  0.8125000   6.00
				   0.0625000  0.1875000  0.9375000   6.00
				   0.0625000  0.3125000  0.3125000   3.00
				   0.0625000  0.3125000  0.4375000   6.00
				   0.0625000  0.3125000  0.5625000   6.00
				   0.0625000  0.3125000  0.6875000   6.00
				   0.0625000  0.3125000  0.8125000   6.00
				   0.0625000  0.3125000  0.9375000   6.00
				   0.0625000  0.4375000  0.4375000   3.00
				   0.0625000  0.4375000  0.5625000   6.00
				   0.0625000  0.4375000  0.6875000   6.00
				   0.0625000  0.4375000  0.8125000   6.00
				   0.0625000  0.4375000  0.9375000   6.00
				   0.0625000  0.5625000  0.5625000   3.00
				   0.0625000  0.5625000  0.6875000   6.00
				   0.0625000  0.5625000  0.8125000   6.00
				   0.0625000  0.6875000  0.6875000   3.00
				   0.0625000  0.6875000  0.8125000   6.00
				   0.0625000  0.8125000  0.8125000   3.00
				   0.1875000  0.1875000  0.1875000   1.00
				   0.1875000  0.1875000  0.3125000   3.00
				   0.1875000  0.1875000  0.4375000   3.00
				   0.1875000  0.1875000  0.5625000   3.00
				   0.1875000  0.1875000  0.6875000   3.00
				   0.1875000  0.1875000  0.8125000   3.00
				   0.1875000  0.3125000  0.3125000   3.00
				   0.1875000  0.3125000  0.4375000   6.00
				   0.1875000  0.3125000  0.5625000   6.00
				   0.1875000  0.3125000  0.6875000   6.00
				   0.1875000  0.3125000  0.8125000   6.00
				   0.1875000  0.4375000  0.4375000   3.00
				   0.1875000  0.4375000  0.5625000   6.00
				   0.1875000  0.4375000  0.6875000   6.00
				   0.1875000  0.4375000  0.8125000   6.00
				   0.1875000  0.5625000  0.5625000   3.00
				   0.1875000  0.5625000  0.6875000   6.00
				   0.1875000  0.6875000  0.6875000   3.00
				   0.3125000  0.3125000  0.3125000   1.00
				   0.3125000  0.3125000  0.4375000   3.00
				   0.3125000  0.3125000  0.5625000   3.00
				   0.3125000  0.3125000  0.6875000   3.00
				   0.3125000  0.4375000  0.4375000   3.00
				   0.3125000  0.4375000  0.5625000   6.00
				   0.3125000  0.4375000  0.6875000   6.00
				   0.3125000  0.5625000  0.5625000   3.00
				   0.4375000  0.4375000  0.4375000   1.00
				   0.4375000  0.4375000  0.5625000   3.00
			</real>
		</mesh> 
	</k_points>
</input>
EOF
$ECHO "  running the scf calculation for Ni...\c"
$PW_COMMAND < ni.scf.xml > ni.scf.out
check_failure $?
$ECHO " done"

# band structure calculation along delta, sigma and lambda lines
cat > ni.band.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

<input calculation="bands" prefix="ni">

	<cell type="qecell">
		<qecell ibrav="2" alat="6.48">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Ni">
			<property name="mass">
				<real>58.69</real>
			</property>
			<property name="pseudofile">
				<string>Ni.pbe-nd-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5</real>
			</property>			
			<property name="angle1">
				<real>90.0</real>
			</property>
			<property name="angle2">
				<real>0.0</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Ni">
			<position>
				<real rank="1" n1="3">
					0.0 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.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				288.0
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">

		<parameter name="nbnd">
			<integer>
				 8
			</integer>
		</parameter>
		
		<parameter name="noncolin">
			<logical>
				true
			</logical>
		</parameter>
		
	</field>	

	<k_points type="tpiba">
		<mesh npoints="28">
			<real rank="2" n1="4" n2="28">
				   0.0 0.0 0.0 1.0
				   0.0 0.0 0.1 1.0
				   0.0 0.0 0.2 1.0
				   0.0 0.0 0.3 1.0
				   0.0 0.0 0.4 1.0
				   0.0 0.0 0.5 1.0
				   0.0 0.0 0.6 1.0
				   0.0 0.0 0.7 1.0
				   0.0 0.0 0.8 1.0
				   0.0 0.0 0.9 1.0
				   0.0 0.0 1.0 1.0
				   0.0 0.0 0.0 1.0
				   0.0 0.1 0.1 1.0
				   0.0 0.2 0.2 1.0
				   0.0 0.3 0.3 1.0
				   0.0 0.4 0.4 1.0
				   0.0 0.5 0.5 1.0
				   0.0 0.6 0.6 1.0
				   0.0 0.7 0.7 1.0
				   0.0 0.8 0.8 1.0
				   0.0 0.9 0.9 1.0
				   0.0 1.0 1.0 1.0
				   0.0 0.0 0.0 1.0
				   0.1 0.1 0.1 1.0
				   0.2 0.2 0.2 1.0
				   0.3 0.3 0.3 1.0
				   0.4 0.4 0.4 1.0
				   0.5 0.5 0.5 1.0
			</real>
		</mesh> 
	</k_points>
</input>
EOF
$ECHO "  running the band-structure calculation for Ni...\c"
$PW_COMMAND < ni.band.xml > ni.band.out
check_failure $?
$ECHO " done"

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

# self-consistent calculation + relaxation of atoms
cat > o2.relax.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>


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

	<cell type="qecell">
		<qecell ibrav="1" alat="7.50">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="2">
		<specie name="O1">
			<property name="mass">
				<real>16.0</real>
			</property>
			<property name="pseudofile">
				<string>O.pbe-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5</real>
			</property>			
			<property name="angle1">
				<real>90.0</real>
			</property>
			<property name="angle2">
				<real>0.0</real>
			</property>
		</specie>
		<specie name="O2">
			<property name="mass">
				<real>16.0</real>
			</property>
			<property name="pseudofile">
				<string>O.pbe-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5</real>
			</property>			
			<property name="angle1">
				<real>90.0</real>
			</property>
			<property name="angle2">
				<real>0.0</real>
			</property>			
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="2" >
		<atom name="O1">
			<position>
				<real rank="1" n1="3">
					0.0 0.0 0.0
				</real>
			</position>
		</atom>		
		<atom name="O2">
			<position>
				<real rank="1" n1="3">
					0.20 0.20 0.20
				</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="report">
			<integer>
				1
			</integer>
		</parameter>	
		
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				25.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				200.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.2
			</real>
		</parameter>
	
	</field>
	
	<field name="Options">

		<parameter name="occupations">
			<string>
				smearing
			</string>
		</parameter>
		
		<parameter name="smearing">
			<string>
				gaussian
			</string>
		</parameter>
		
		<parameter name="degauss">
			<real>
				0.05
			</real>
		</parameter>
		
		<parameter name="noncolin">
			<logical>
				true
			</logical>
		</parameter>
		
	</field>
	
	<k_points type="tpiba">
		<mesh npoints="1">
			<real rank="2" n1="4" n2="1">
				   0.0 0.0 0.0 1.00
			</real>
		</mesh> 
	</k_points>
</input>
EOF
$ECHO "  running scf calculation with relax for oxygen molecule...\c"
$PW_COMMAND < o2.relax.xml > o2.relax.out
check_failure $?
$ECHO " done"

$ECHO
$ECHO "$EXAMPLE_DIR: done"
