#!/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 calculate the phonon dispersion on a GRID"
$ECHO "for AlAs in zincblende structure. Both q-points and irreps are split."

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

# required executables and pseudopotentials
BIN_LIST="pw.x ph.x q2r.x matdyn.x plotband.x"
PSEUDO_LIST="Al.pz-vbc.UPF As.pz-bhs.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 gnuplot
GP_COMMAND=`which gnuplot 2>/dev/null`
if [ "$GP_COMMAND" = "" ]; then
        $ECHO
        $ECHO "gnuplot not in PATH"
        $ECHO "Results will not be plotted"
fi

# 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_3" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/results_3

# 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 $NETWORK_PSEUDO/$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"
PH_COMMAND="$PARA_PREFIX $BIN_DIR/ph.x $PARA_POSTFIX"
PW1_COMMAND="mpirun -n 4 $BIN_DIR/pw.x $PARA_POSTFIX"
PH1_COMMAND="mpirun -n 4 $BIN_DIR/ph.x $PARA_POSTFIX"
Q2R_COMMAND="$PARA_PREFIX $BIN_DIR/q2r.x $PARA_POSTFIX"
MATDYN_COMMAND="$PARA_PREFIX $BIN_DIR/matdyn.x $PARA_POSTFIX"
PLOTBAND_COMMAND="$BIN_DIR/plotband.x"
$ECHO
$ECHO "  running pw.x as:     $PW_COMMAND"
$ECHO "  running ph.x as:     $PH_COMMAND"
$ECHO "  running q2r.x as:    $Q2R_COMMAND"
$ECHO "  running matdyn.x as: $MATDYN_COMMAND"
$ECHO "  running plotband.x as: $PLOTBAND_COMMAND"
$ECHO "  running gnuplot as: $GP_COMMAND"
$ECHO

# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/alas*
rm -rf $TMP_DIR/_ph0/alas*
for q in `seq 1 8 ` ; do
for irr in `seq 1 6` ; do
rm -rf $TMP_DIR/$q.$irr
done 
done
$ECHO " done"

PREFIX='alas'

# self-consistent calculation
cat > alas.scf.in << EOF
 &control
    calculation='scf'
    restart_mode='from_scratch',
    tstress = .true.
    tprnfor = .true.
    prefix='$PREFIX',
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
    wf_collect=.true.
 /
 &system
    ibrav=  2, celldm(1) =10.50, nat=  2, ntyp= 2,
    ecutwfc =16.0
 /
 &electrons
    conv_thr =  1.0d-8
    mixing_beta = 0.7
 /
ATOMIC_SPECIES
 Al  26.98  Al.pz-vbc.UPF
 As  74.92  As.pz-bhs.UPF
ATOMIC_POSITIONS (alat)
 Al 0.00 0.00 0.00
 As 0.25 0.25 0.25
K_POINTS
 2
 0.25 0.25 0.25 1.0
 0.25 0.25 0.75 3.0
EOF
$ECHO "  running the scf calculation...\c"
$PW1_COMMAND < alas.scf.in > alas.scf.out
check_failure $?
$ECHO " done"
#
# Initial calculation, only band structure. Note that here and in pw.x
# you can use a different number of processors with respect to the
# phonon calculation using the wf_collect=.true. flag.
# 
cat > alas.ph.wfc.in << EOF
phonons of AlAs
 &inputph
  tr2_ph=1.0d-12,
  prefix='$PREFIX',
  ldisp=.true.,
  nq1=4, nq2=4, nq3=4
  only_wfc=.true.,
  lqdir=.true.,
  outdir='$TMP_DIR/',
  fildyn='$PREFIX.dyn.xml',
 /
EOF
$ECHO "  running the band structure calculation ...\c"
$PH1_COMMAND < alas.ph.wfc.in > alas.ph.wfc.out
check_failure $?
$ECHO " done"
#
# Initial calculation, initial part of the dynamical matrix
# 
cat > alas.ph.in0 << EOF
phonons of AlAs
 &inputph
  tr2_ph=1.0d-12,
  prefix='$PREFIX',
  ldisp=.true.,
  nq1=4, nq2=4, nq3=4
  only_init=.true.,
  recover=.true.
  lqdir=.true.,
  outdir='$TMP_DIR/',
  fildyn='$PREFIX.dyn.xml',
 /
EOF
$ECHO "  running the initialization phonon calculation ...\c"
$PH_COMMAND < alas.ph.in0 > alas.ph.out0
check_failure $?
$ECHO " done"


for q in `seq 1 8 ` ; do

for irr in `seq 1 6` ; do

cat > input.$q.$irr << EOF
phonons of AlAs
 &inputph
  tr2_ph=1.0d-12,
  prefix='$PREFIX',
  ldisp=.true.,
  lqdir=.true.,
  nq1=4, nq2=4, nq3=4
  start_q=$q
  last_q=$q
  start_irr=$irr
  last_irr=$irr
  recover=.true.,
  low_directory_check=.true.
  outdir="$TMP_DIR/$q.$irr",
  fildyn='$PREFIX.dyn.xml',
 /
EOF

mkdir -p $TMP_DIR/$q.$irr/_ph0/$PREFIX.q_$q
mkdir -p $TMP_DIR/$q.$irr/_ph0/$PREFIX.phsave
cp -r $TMP_DIR/$PREFIX.* $TMP_DIR/$q.$irr
cp $TMP_DIR/_ph0/$PREFIX.phsave/* $TMP_DIR/$q.$irr/_ph0/$PREFIX.phsave
if [ "$q" != "1" ]; then
cp -r $TMP_DIR/_ph0/$PREFIX.q_$q/* $TMP_DIR/$q.$irr/_ph0/$PREFIX.q_$q
fi

$ECHO "  running the phonon calculation for q= " $q " irr=" $irr "...\c"
$PH_COMMAND < input.$q.$irr > output.$q.$irr
$ECHO " done"

done
done 
#
#  Collecting all results in a single directory:
#
for q in `seq 1 8 ` ; do

for irr in `seq 1 6` ; do

\cp -f $TMP_DIR/$q.$irr/_ph0/$PREFIX.phsave/dynmat.$q.$irr.xml $TMP_DIR/_ph0/$PREFIX.phsave 2> /dev/null

done 
#
#  collect also the representation 0 (contribution to the dynamical 
#  matrix independent from the induced charge).
#
\cp -f $TMP_DIR/$q.1/_ph0/$PREFIX.phsave/dynmat.$q.0.xml $TMP_DIR/_ph0/$PREFIX.phsave 2> /dev/null

done
#
# cp electric field part
#
\cp -f $TMP_DIR/1.1/_ph0/$PREFIX.phsave/tensors.xml $TMP_DIR/_ph0/$PREFIX.phsave
#
cat > alas.ph.in << EOF
phonons of AlAs
 &inputph
  tr2_ph=1.0d-12,
  prefix='$PREFIX',
  ldisp=.true.,
  nq1=4, nq2=4, nq3=4
  recover=.true.,
  outdir='$TMP_DIR/',
  fildyn='$PREFIX.dyn.xml',
 /
EOF
$ECHO "  running the phonon calculation to collect the results...\c"
$PH_COMMAND < alas.ph.in > alas.ph.out
check_failure $?
$ECHO " done"

cat > q2r.in <<EOF
 &input
   fildyn='$PREFIX.dyn.xml', zasr='simple', flfrc='$PREFIX.444.fc'
 /
EOF

$ECHO "  transforming C(q) => C(R)...\c"
$Q2R_COMMAND < q2r.in > q2r.out
check_failure $?
$ECHO " done"

cat > matdyn.in <<EOF
 &input
    asr='simple',  amass(1)=26.98, amass(2)=74.922,
    flfrc='$PREFIX.444.fc.xml', flfrq='$PREFIX.freq', q_in_band_form=.true.,
 /
 6
  0.000 0.0 0.0   40
  1.0   0.0 0.0   20
  1.0   0.5 0.0   20
  1.0   1.0 0.0   40
  0.00  0.0 0.0   40
  0.5   0.5 0.5    1
EOF

$ECHO "  recalculating omega(q) from C(R)...\c"
$MATDYN_COMMAND < matdyn.in > matdyn.out
check_failure $?
$ECHO " done"

cat > plotband.in <<EOF
alas.freq
0 600
freq.plot
freq.ps
0.0
50.0 0.1
EOF

$ECHO "  writing the phonon dispersions in freq.plot...\c"
$PLOTBAND_COMMAND < plotband.in > /dev/null
check_failure $?
$ECHO " done"

if [ "$GP_COMMAND" = "" ]; then
    break
else
cat > gnuplot.tmp <<EOF
set encoding iso_8859_15
set terminal postscript enhanced solid color "Helvetica" 20
set output "alas.dispersions.ps"
#
set key off

set xrange [0:4.280239]
dim=450
set border front
set yrange [0:dim]
set arrow from 1,0. to 1,dim nohead  lw 2  front
set arrow from 2,0. to 2,dim nohead  lw 2  front
set arrow from 1.5,0. to 1.5,dim nohead  lw 2 front
set arrow from 3.4142,0. to 3.4142,dim nohead  lw 2 front
set ylabel "frequency (cm^{-1})"
unset xtics
lpos=-15
set label "{/Symbol G}" at -0.05,lpos
set label "X" at 0.95,lpos
set label "W" at 1.45,lpos
set label "X" at 1.95,lpos
set label "{/Symbol G}" at 3.37,lpos
set label "L" at 4.1897,lpos

plot "freq.plot.1.1" u 1:2 w l lw 3 lt 1, \
     "freq.plot.1.3" u 1:2 w l lw 3 lt 2, \
     "freq.plot.2.1" u 1:2 w l lw 3 lt 1, \
     "freq.plot.2.2" u 1:2 w l lw 3 lt 2, \
     "freq.plot.3.1" u 1:2 w l lw 3 lt 1, \
     "freq.plot.3.2" u 1:2 w l lw 3 lt 2, \
     "freq.plot.4.1" u 1:2 w l lw 3 lt 1, \
     "freq.plot.4.2" u 1:2 w l lw 3 lt 2, \
     "freq.plot.5.1" u 1:2 w l lw 3 lt 1, \
     "freq.plot.5.3" u 1:2 w l lw 3 lt 2
EOF
$ECHO "  creating the postscript file alas.dispersion.ps...\c"
$GP_COMMAND gnuplot.tmp
check_failure $?
$ECHO " done"
fi

cat > phdos.in <<EOF
 &input
    asr='simple',  dos=.true. amass(1)=26.98, amass(2)=74.922,
    flfrc='$PREFIX.444.fc.xml', fldos='$PREFIX.phdos', nk1=6,nk2=6,nk3=6
 /
EOF

$ECHO "  calculating phonon DOS ...\c"
$MATDYN_COMMAND < phdos.in > phdos.out
check_failure $?
$ECHO " done"

if [ "$GP_COMMAND" = "" ]; then
    break
else
cat > gnuplot1.tmp <<EOF
set encoding iso_8859_15
set terminal postscript enhanced solid color "Helvetica" 20
set output "alas.phdos.ps"
#
set key off
set xrange [0:450]
set xlabel "frequency (cm^{-1})"
set ylabel "DOS"
plot 'alas.phdos' u 1:2 w l lw 3 
EOF
$ECHO "  generating plot of phonon_dos in the file alas.phdos.ps...\c"
$GP_COMMAND  gnuplot1.tmp
check_failure $?
$ECHO " done"
fi

$ECHO
$ECHO "$EXAMPLE_DIR: done"

