#!/bin/bash
#
# Author: Giacomo Miceli
# EPFL, November 2012  

here=${PWD}
EXE=${here}/src

cd ${EXE}
make all
echo "done!"
cd ../

mkdir -p workdir

####------------------------
####  INPUT PARAMETERS
####------------------------
NSTEP=10000
TAU=10
TIMEMAX=$(echo "5*${TAU}" | bc -l)
IT0=$(echo "4*${TAU}" | bc -l)
DATA="DATA.dat"
PDF="PDF.dat"
CORR="CORRFUNC.dat"
BLOCK="SIGMA.dat"
####------------------------

cat > workdir/launcher.sh << EOF
#!/bin/bash
#
# Author: Giacomo Miceli
# EPFL, November 2012 

EXEfolder=${EXE}

########################
# output file names

  DATA=${DATA}
  PDF=${PDF}
  CORR=${CORR}
  BLOCK=${BLOCK}

######################## 
# input data

  NSTEP=${NSTEP}
  TAU=${TAU}
  TIMEMAX=${TIMEMAX}
  IT0=${IT0}
 

cat > input << END
&stochastic
 nstep=\${NSTEP}
 tau=\${TAU}
/
&correlation
 nstep=\${NSTEP}
 timemax=\${TIMEMAX}
 nsamp=1
 it0=\${IT0}
 dt=1
 inputfile="\${DATA}"
/
&blocking
 nstep=\${NSTEP}
 inputfile="\${DATA}"
/
END

echo "-----------------------------------------------------"
echo " Select the case: "
echo "	1   calculates probability distribution function "
echo "	2   calculates correlation function "
echo "	3   performs a blocking analysis "

read task

echo ""
echo " RUNNING "
echo ""

if [ -e DATA.dat ] ; then
  echo "	data are already available! (file ${DATA})"
else
  echo "	* generating trajectory.... "
  ${EXE}/stochastic.x < input > ${DATA}
  echo "		output: ${DATA} "
  echo "		done! "
fi

if [[ "\${task}" == 1 ]] ; then

  echo ""
  echo "	* probability distribution function.... "
  ${EXE}/histogram.x -in ${DATA} -min -5.0 -max 5.0 -nbin 200 > ${PDF}
  echo "		output: ${PDF} "
  echo "		done! "

elif [[ "\${task}" == 2 ]] ; then

  echo ""
  echo "	* correlation function.... "
  ${EXE}/correlation.x < input > ${CORR}
  echo "	  output: ${CORR} "
  echo "	  done! "

elif [[ "\${task}" == 3 ]] ; then

  echo ""
  echo "	* blocking analysis.... "
  ${EXE}/blocking.x < input > ${BLOCK}
  echo "		output: ${BLOCK} "
  echo "		done! "
  
else 

  echo "	Data analysis has not been performed! "

fi
EOF
chmod +x workdir/launcher.sh
