#!/bin/bash    
#
################################################################################
#
# This bash script executes SEQUENTIALLY exciting for each of the input files 
# (for given atomic displacement) created by "SETUP-diamond-gamma-phonon". 
# Results for each calculation are stored inside the directory "workdir"
# Each calculation corresponds to directories named
# "rundir-"+(the label of the single calculation: r:unning index 01, 02, etc).
# The energy versus displacement data are stored in the file 
# "energy-vs-displacement".
# The force versus displacement data are stored in the file 
# "force-vs-displacement".
#
# VERY IMPORTANT: Before running the script the user must define the following
#                 global variables:
#
# $EXCITINGROOT   => path to the exciting root directory
# $EXCITINGRUNDIR => path to the EXSISTING directory where the user wants to run
#                    exciting
#
# @Pasquale Pavone  (2010, October, 8)
#_______________________________________________________________________________
#
EXECUTABLE=$EXCITINGROOT/bin/excitingser
RUNDIR=$EXCITINGRUNDIR
#
CURRENT=$PWD
OUTE=$CURRENT/workdir/energy-vs-displacement
OUTF=$CURRENT/workdir/force-vs-displacement
#
if [ -f $OUTE ] ; then
    mv $OUTE $OUTE.save
fi
#
if [ -f $OUTF ] ; then
    mv $OUTF $OUTF.save
fi
#
cd workdir 
input_list=`ls -d input-*`
cd $RUNDIR
#
for input in $input_list ; do
    echo
    echo "Running exciting for file" $input "----------------------------------"
    echo
#
    rm -Rf xc-rundir
    mkdir  xc-rundir
    cd     xc-rundir
    cp $CURRENT/workdir/$input input.xml
#
    time $EXECUTABLE | tee output.screen
#
    suffix=$(echo $input | cut -c7-8)
#
    uuu=$(cat $CURRENT/workdir/displ-$suffix)
    tot=TOTENERGY.OUT
#
    awk -v eta="$uuu" \
        '/ / {printf "%11.8f   %20.10f\n",eta,$1}' $tot | tail -n1
    awk -v eta="$uuu" \
        '/ / {printf "%11.8f   %20.10f\n",eta,$1}' $tot | tail -n1>>$OUTE
#
    grep -A12 "Forces :" INFO.OUT | tail -n1 >> dumforce
    awk -v eta="$uuu" \
        '/ / {printf "%11.8f   %20.10f\n",eta,$4}' dumforce | tail -n1>>$OUTF   
    rm -f dumforce
#
    cd ../
#
    rm -Rf $CURRENT/workdir/rundir-$suffix
    mv xc-rundir $CURRENT/workdir/rundir-$suffix
    cd $CURRENT/workdir
    GNU-energy
    cd $RUNDIR
    echo
    echo "Run completed for file" $input "-------------------------------------"
#
done
echo

