#!/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=20000   # 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 (real space tilings)
nsap=10  # collect perp-density images
npix=300 # half-number of pixels (density array is -npix to npix)
perpsize=10.0  # side of the square perp-window
bwtresh=0.2  # bw threshold for perpdens.eps
satur=0.7     # saturation threshold (fraction of max density)
##### executables to use (paths)
ptexec=../ptmc_ham 
epsexec=../t2eps
pdexec=../pdens2eps
################### 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?
    ptexec=./ptmc_ham # maybe the execs are in the current dir?
    if [ ! -e "$ptexec" ] ; then     
	echo "ptexec $ptexec does not seem accessible from this folder"
	exit
    fi
fi
if [ ! -e "$epsexec" ] ; then # do we have right path to the executable?
    epsexec=./t2eps # maybe the execs are in the current dir?
    if [ ! -e "$epsexec" ] ; then     
	echo "epsexec $epsexec does not seem accessible from this folder"
	exit
    fi
fi
if [ ! -e "$pdexec" ] ; then # do we have right path to the executable?
    pdexec=./pdens2eps # maybe the execs are in the current dir?
    if [ ! -e "$pdexec" ] ; then     
	echo "perpdens $pdexec does not seem accessible from this folder"
	exit
    fi
fi

while [ $# -gt 0 ]
do
    case "$1" in
        -mcs) # integer MCS (MC cycles per node)
            shift
	    mcs=$1
            ;;
        -nsap) # add perp data every nsap trial
            shift
	    nsap=$1
            ;;
        -npix) # half-number of pixels for perp-density array
            shift
	    npix=$1
            ;;
        -pesize) # side of the square-perp window
            shift
	    perpsize=$1
            ;;
        -bw) # bw-treshold for perpdens.eps
            shift
	    bwtresh=$1
            ;;
        -sat) # saturation treshold for perpdens.eps
            shift
	    satur=$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 final tilings to eps files
	    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
rm -f fort.*
nt=0
for k in $tem ; do  # cycle over temperatures
    ((nt=nt+1))
    echo -n "$nt T=$k : "
    for((i=1;i<=nr;i++)) ; do
	((j=i-1))
	echo -n " "$i
	(echo $mcs $e_fafa $e_sksk $e_ve $k $nsap $npix $perpsize; cat tini)|$ptexec > out.$nt.$i
	cp fort.99 tini
	mv fort.99 tiling.$nt.$i
	if [ -e fort.98 ] ; then mv fort.98 energy.$nt.$i ; fi
	if [ -e fort.96 ] ; then
	    cp fort.96 pdens2eps.dat
	    head -2 perp.info > pdens2eps.inp
	    echo "$bwtresh  # bw-threshold for perp-dens image" >> pdens2eps.inp
	    echo "$satur    # saturtion threshold for perp-dens image" >> pdens2eps.inp
	    $pdexec
	    mv perpdens.eps pdens.$nt.$i.eps
	    mv fort.96 pdens.$nt.$i
	    mv perp.info perpinfo.$nt.$i
	fi
	mv fort.97 wperp.$nt.$i
	if [ $pr -eq 1 ] ; then
	    cat tiling.$nt.$i | $epsexec > fig.$nt.$i.eps
	fi
    done
    acc=`grep acc out.$nt.*|awk '{acc+=$5;na++}END{print acc/na}'`
    wp=`cat wperp.$nt.*|awk '{x+=$1;n++}END{print x/n}'`
    echo " ; accrate = $acc ; W_perp = $wp"
done
rm -f tini
##
