#!/bin/bash
################### PROLOGUE: USER-SET simulation parameters
nr=5        # NR : number of samples to record per each temperature
ti=ti.in    # default input tiling name
mcs=10000   # MCS sweeps : for each temperature, and for each of the NR samples recorded
e_fafa=0.7 # energy coefficient for adjacent fat-fat
e_sksk=1   # energy coefficient for adjacent skinny-skinny
temper=2.0,1.0,0.8,0.6,0.4,0.2,0.1 # comma-separated list of temperatures: NO SPACES please!!!
pr=1   # 1 = print eps images
##### executables to use (paths)
ptexec=../ptmc_ham 
epsexec=../t2eps
################### END PROLOGUE

tem=`echo $temper |awk -F, '{for(i=1;i<=NF;i++){printf " "$i}; print ""}'`
e_ve=0      # something else, so far not functional

if [ ! -e "$ptexec" ] ; then # do we have right path to the executable?
    echo "ptexec $ptexec does not seem accessible from this folder"
    exit
fi
if [ ! -e "$epsexec" ] ; then # do we have right path to the executable?
    echo "epsexec $epsexec does not seem accessible from this folder"
    exit
fi

while [ $# -gt 0 ]
do
    case "$1" in
        -mcs) # integer MCS (MC cycles per node)
            shift
	    mcs=$1
            ;;
        -fat) # energy coefficient for adjacent fat-fat
            shift
	    e_fafa=$1
            ;;
        -ski) # energy coefficient for adjacent fat-fat
            shift
	    e_sksk=$1
            ;;
	-r) # NR : rerun MCS steps NR times. have NR tiling configurations saved in folder ptf-tilings
	    shift
	    nr=$1
	    ;;
	-p) # print tiling to eps file
	    pr=1
	    ;;
	-tem) # T1,T2,...,Tn     comma-separated list of temperatures
	    shift
	    tem=`echo $1 |awk -F, '{for(i=1;i<=NF;i++){printf " "$i}; print ""}'`
	    ;;
        *) # tiling filename
	    if [ -e $1 ] ; then # this must be tiling
		ti=$1
	    fi
            ;;
    esac
    shift
done

if [ "$ti" == "" ] ; then
    echo "Initial tiling not given"
    exit
fi

for i in $tem ; do
    echo $i 
done > temperatures
cp $ti tini
nt=0
for k in $tem ; do  # cycle over temperatures
    echo -n "temperature $k, cycle # and acc_rate : "
    ((nt=nt+1))
    for((i=1;i<=nr;i++)) ; do
	((j=i-1))
	echo -n " "$i
	(echo $mcs $e_fafa $e_sksk $e_ve $k ; cat tini)|$ptexec > out.$nt.$i
	cp fort.99 tini
	mv fort.99 tiling.$nt.$i
	mv fort.98 energy.$nt.$i
	if [ $pr -eq 1 ] ; then
	    cat tiling.$nt.$i | $epsexec > fig.$nt.$i.eps
	fi
	acc=`grep acc out.$nt.$i|awk '{print $5}'`
	echo -n " $acc ; "
    done
    echo ""
done
rm -f tini
##
