#!/bin/sh

###############################################################################
#      A Generalized PWscf SCF Script for NM YBCO or RE-BCO Structures        #
#                             P. M. Grant                                     #
#                            August, 2010                                     #
###############################################################################

###############################################################################
#    Element and Structure-Specific Constants & Data (Meant To Be Edited)     #
#                              (!con cuidado!)                                # 
#                                                                             #
#           Change Log:                                                       #
#              12/05/10 - Two SCF Steps as Prelude to Lambda e-p Calc         #
#              12/17/10 - Four MP                                             #
#                                                                             #
#              THIS SCRIPT IS ONLY FOR NON-MAGNETIC Re-BCO                    #
###############################################################################
#
# Conversion Factors (If Needed)
Ryd_eV=13.6057
Bohr_Ang=0.529

# Element Values & Notation
RE='Y'; RE_M='88.90585'
AE='Ba'; AE_M='137.327'
TM='Cu'; TM_M='63.546'
A='O'; A_M='15.994'

AE1="Ba1"	AE2='Ba2'
TM1='Cu1'	TM2='Cu2'	TM3='Cu3'
A1='O1'   A2='O2'   A3='O3'   A4='O4'   A4='O4'   A5='O5'   A6='O6'   A7='O7'
mp_size_1='4'
ep_la2F_1='.true.'
mp_size_2='4'
ep_la2F_2='.false.'
prefix=nm-$RE$AE$TM$A

# PseudoPots List
# Rare Earths
RE_pseudopot='Y.pbe-nsp-van.UPF'
#RE_pseudopot='Y.pw91-nsp-van.UPF"
# Alkaline Earths
# Barium
AE_pseudopot='Ba.pbe-nsp-van.UPF'
#AE_pseudopot='Ba.pw91-nsp-van.UPF'
# Transition Metal
# Copper
TM_pseudopot='Cu.pbe-d-rrkjus.UPF'
#TM_pseudopot='Cu.pz-3d9_4s2-rrkjus.UPF';	
#TM_pseudopot='029-Cu-gpw-n-campos.uspp.UPF';	
#TM_pseudopot='CuLDA.RRKJ3.PAW'		
#TM_pseudopot='Cu.pz-d-rrkjus.UPF';
#TM_pseudopot='Cu.pz-n-van_ak.UPF';
#TM_pseudopot='Cu.blyp-n-van_ak.UPF';
#TM_pseudopot='Cu.pw91-n-van_ak.UPF';
# Anions
# Oxygen
#A_pseudopot='008-O-ca--vgrp.uspp.UPF'
#A_pseudopot='OLDA.RRKJ3.PAW'
#A_pseudopot='O.pz-rrkjus.UPF'
#A_pseudopot='O.LDA.US.RRKJ3.UPF'
#A_pseudopot='O.vdb.UPF'
A_pseudopot='O.pbe-rrkjus.UPF'
#A_pseudopot='O.pz-van_ak.UPF'
#A_pseudopot='O.pw91-van_ak.UPF'

# Run Control Paramenters
electron_maxstep='100'

###############################################################################
# Housekeeping and Various Setup Stuff                                        #
###############################################################################
sysinfo=`uname '-a'`
kubuntu=`cat '/etc/issue'` 
now=`date`
cd `echo $0 | sed 's/\(.*\)\/.*/\1/'`
home_dir=`pwd`
cd ..
home_dir_up1=`pwd`
cd $home_dir

#pwscf_distro='espresso-4.0'
pwscf_distro='espresso-4.2.1'
#pwscf_distro='espresso-3.2.3'
pwscf_exec='/home/pmpgrant/'$pwscf_distro'/PW/pw.x'
home_pot=$home_dir_up1'/PSEUDOPOTS'
home_bz_kvects=$home_dir_up1'/BZ_&_K-VECTS'
#LAT_BZ_dir=$home_bz_kvects/'LAT_BZ_3905_1'

# cat to $prefix.scf.data file
printf "$0 \n" > $prefix.scf.data
cat >> $prefix.scf.data << EOF
  A PWscf script to calculate the electronic properties of $prefix
  Starting at $now
*******************************************************************************
System & OS Info
---------------------------------------------------------------------
  $sysinfo
  $kubuntu
*******************************************************************************
Element, Structure and Run Parameter Editable Options (!con cuidado!)
---------------------------------------------------------------------
  RE = $RE	AE = $AE	TM = $TM	A = $O
  RE_mass = $RE_M	AE_mass = $AE_M	TM_mass = $TM_M A_mass = $A_M
  LAT_BZ_dir = $LAT_BZ_dir	MP_mesh = $mp_size
  RE_PP = $RE_pseudopot
  AE_PP = $AE_pseudopot
  TM_PP = $TM_pseudopot
  A_PP = $A_pseudopot
  la2F = $ep_la2F_1

   Maximum Number of SCF Iterations = $electron_maxstep	

*******************************************************************************
Housekeeping
------------
  PWscf Distro = $pwscf_distro
  My Home      = $home_dir
  PP Home      = $home_pot
  PW Exec      = $pwscf_exec
EOF
#exit
###############################################################################
# Generate SCF1 Run Stuff                                                     #
###############################################################################
cat > $prefix.scf1.in << EOF
 &control
  title = '$prefix.scf1',		calculation = 'scf',
  restart_mode = 'from_scratch',	prefix = '$prefix',
  pseudo_dir = '$home_pot',
  outdir = '$home_dir',
  tprnfor = .false.,			tstress = .false.,	
  verbosity = 'minimal',
 /
 &system
  ibrav = 0,			celldm(1)=0,
  nat = 13,			ntyp = 4,
  ecutwfc = 40.0		ecutrho = 320.0,
  smearing = 'methfessel-paxton',	degauss = 0.02,	occupations = 'smearing'
  la2F = $ep_la2F_1
 /
 &electrons
  electron_maxstep      = $electron_maxstep
  mixing_mode		= 'plain',
  mixing_fixed_ns	= 0,
  diagonalization	= 'david',
  conv_thr		= 1.0d-6,
  mixing_beta		= 0.3,
 /
CELL_PARAMETERS
7.2255	0	0
0	7.3464	0
0	0	22.0752
ATOMIC_SPECIES
    $RE	     $RE_M	  $RE_pseudopot
    $AE      $AE_M	  $AE_pseudopot
    $TM      $TM_M        $TM_pseudopot
    $A       $A_M         $A_pseudopot
 
ATOMIC_POSITIONS {angstrom}
$RE	1.9115	1.9435	5.84
$AE	1.9115	1.9435	2.146901
$AE	1.9115	1.9435	9.533099
$TM	0	0	0
$TM	0	0	4.145115
$TM	0	0	7.534885
$A	0	1.9435	0
$A	1.9115	0	4.421464
$A	0	1.9435	4.415858
$A	0	0	1.856653
$A	1.9115	0	7.258536
$A	0	1.9435	7.264142
$A	0	0	9.823347
K_POINTS {automatic}
    $mp_size_1    $mp_size_1    $mp_size_1    0    0    0
EOF

###############################################################################
# Do SCF1 Run                                                                 #
###############################################################################
cat >> $prefix.scf.data << EOF
  Beginning $prefix SCF1...
  Starting at `date`
*******************************************************************************
$prefix.scf1.in follows...
----------------------------------
EOF

cat >> $prefix.scf.data < $prefix.scf1.in
cat >> $prefix.scf.data << EOF

*******************************************************************************
  Running $prefix SCF1...
EOF

# Exit Point for Testing
#exit

$pwscf_exec < $prefix.scf1.in | tee $prefix.scf1.out
cat >> $prefix.scf.data << EOF
  Finished...at `date`
  Collecting key data
  `grep 'kinetic-energy cutoff     ' $prefix.scf1.out`
  `grep 'charge density cutoff     ' $prefix.scf1.out`
  `grep 'convergence threshold     ' $prefix.scf1.out`
  `grep 'mixing beta               ' $prefix.scf1.out`
  `grep 'number of electrons       ' $prefix.scf1.out`
  `grep 'number of Kohn-Sham states' $prefix.scf1.out`
  `grep 'number of iterations used ' $prefix.scf1.out`
  `grep 'Exchange-correlation      ' $prefix.scf1.out`

  # k-points = `grep 'number of k points=' $prefix.scf1.out | cut -c 25-30`
  Fermi Energy = `grep 'Fermi' $prefix.scf1.out | cut -c 28-36`
  Total Energy = `grep '!' $prefix.scf1.out | cut -c 35-48`

  Total CPU Time = `grep 'CPU time' $prefix.scf1.out | cut -c 20-30`
  Total Wall Time = `grep 'wall time' $prefix.scf1.out | cut -c 40-53`

  Iteration Summary
  -----------------
`grep 'estimated scf accuracy  ' $prefix.scf1.out`

EOF
exit
###############################################################################
# Generate SCF2 Run Stuff                                                     #
###############################################################################
cat > $prefix.scf2.in << EOF
 &control
  title = '$prefix.scf2',		calculation = 'scf',
  restart_mode = 'from_scratch',	prefix = '$prefix',
  pseudo_dir = '$home_pot',
  outdir = '$home_dir',
  tprnfor = .false.,			tstress = .false.,	
  verbosity = 'minimal',
 /
 &system
  ibrav = 0,			celldm(1)=0,
  nat = 13,			ntyp = 4,
  ecutwfc = 40.0		ecutrho = 320.0,
  smearing = 'methfessel-paxton',	degauss = 0.02,	occupations = 'smearing'
  la2F = $ep_la2F_2
 /
 &electrons
  electron_maxstep      = $electron_maxstep
  mixing_mode		= 'plain',
  mixing_fixed_ns	= 0,
  diagonalization	= 'david',
  conv_thr		= 1.0d-6,
  mixing_beta		= 0.3,
 /
CELL_PARAMETERS
7.2255	0	0
0	7.3464	0
0	0	22.0752
ATOMIC_SPECIES
    $RE	     $RE_M	  $RE_pseudopot
    $AE      $AE_M	  $AE_pseudopot
    $TM      $TM_M        $TM_pseudopot
    $A       $A_M         $A_pseudopot
 
ATOMIC_POSITIONS {angstrom}
$RE	1.9115	1.9435	5.84
$AE	1.9115	1.9435	2.146901
$AE	1.9115	1.9435	9.533099
$TM	0	0	0
$TM	0	0	4.145115
$TM	0	0	7.534885
$A	0	1.9435	0
$A	1.9115	0	4.421464
$A	0	1.9435	4.415858
$A	0	0	1.856653
$A	1.9115	0	7.258536
$A	0	1.9435	7.264142
$A	0	0	9.823347
K_POINTS {automatic}
    $mp_size_2    $mp_size_2    $mp_size_2    0    0    0
EOF

###############################################################################
# Do SCF2 Run                                                                 #
###############################################################################
cat >> $prefix.scf.data << EOF
  Beginning $prefix SCF2...
  Starting at `date`
*******************************************************************************
$prefix.scf2.in follows...
----------------------------------
EOF

cat >> $prefix.scf.data < $prefix.scf2.in
cat >> $prefix.scf.data << EOF

*******************************************************************************
  Running $prefix SCF2...
EOF

# Exit Point for Testing
#exit

$pwscf_exec < $prefix.scf2.in | tee $prefix.scf2.out
cat >> $prefix.scf.data << EOF
  Finished...at `date`
  Collecting key data
  `grep 'kinetic-energy cutoff     ' $prefix.scf2.out`
  `grep 'charge density cutoff     ' $prefix.scf2.out`
  `grep 'convergence threshold     ' $prefix.scf2.out`
  `grep 'mixing beta               ' $prefix.scf2.out`
  `grep 'number of electrons       ' $prefix.scf2.out`
  `grep 'number of Kohn-Sham states' $prefix.scf2.out`
  `grep 'number of iterations used ' $prefix.scf2.out`
  `grep 'Exchange-correlation      ' $prefix.scf2.out`

  # k-points = `grep 'number of k points=' $prefix.scf2.out | cut -c 25-30`
  Fermi Energy = `grep 'Fermi' $prefix.scf2.out | cut -c 28-36`
  Total Energy = `grep '!' $prefix.scf2.out | cut -c 35-48`

  Total CPU Time = `grep 'CPU time' $prefix.scf2.out | cut -c 20-30`
  Total Wall Time = `grep 'wall time' $prefix.scf2.out | cut -c 40-53`

  Iteration Summary
  -----------------
`grep 'estimated scf accuracy  ' $prefix.scf2.out`
EOF

exit