#!/bin/bash

##runs a single TAPSIM simulation given an existent TPASIM input meshgen results single-threaded
##Markus Kuehbach, 2017/12/18, m.kuehbach at mpie.de

##user required input
##which pillar queue
simid=$1
pillarid=$2

#echo $simid
#echo $pillarid

##make folder to isolate results
output_prefix="BatchMillSimID$simidPID$pillarid"
#echo $output_prefix

#sitting in top folder
##generate therein folder for pillar simulation, each pillar one folder
workingdir="$output_prefix"
eval "mkdir -p $workingdir"
eval "cd $workingdir"


##make copy of cfg file, as the pillars are different only in their orientation we can take the same cfg
# if (( $simid == (1) ))
    # then
	# eval "cp ../Inputdependencies/AlMeshCorrected.cfg ../$workingdir"
# fi
# if (( $simid == (2) ))
    # then
	# eval "cp ../Inputdependencies/MgMeshCorrected.cfg ../$workingdir"
# fi
# if (( $simid == (3) ))
    # then
	# eval "cp ../Inputdependencies/ZrMeshCorrected.cfg ../$workingdir"
# fi
# if (( ($simid == (4) ))
	# then
	# eval "cp ../Inputdependencies/WMeshCorrected.cfg ../$workingdir"
# fi
eval "cp ../Inputdependencies/ZrMeshCorrected.cfg ../$workingdir"

##make copy of ini file, for all pillars the same meshgen.ini file
eval "cp ../Inputdependencies/meshgen.ini ../$workingdir"

##start meshing of the pillar grid, redirect stdout to file
tapsim_i="TAPSIMInputMesh$pillarid"

eval "cp ../Inputmeshfiles/$tapsim_i ../$workingdir"

##start TAPSIM simulation
tapsim_o="TAPSIMSimID$simidPID$pillarid"
# if (( $simid == (1) ))
	# then
	# eval "./../tapsim --threads=1 evaporation AlMeshCorrected.cfg $tapsim_i $tapsim_o 1>TAPSIMSimID$simidPID$pillarid.STDOUT.txt 2>TAPSIMSimID$simidPID$pillarid.STDERR.txt"
# fi
# if (( $simid == (2) ))
	# then
	# eval "./../tapsim --threads=1 evaporation MgMeshCorrected.cfg $tapsim_i $tapsim_o 1>TAPSIMSimID$simidPID$pillarid.STDOUT.txt 2>TAPSIMSimID$simidPID$pillarid.STDERR.txt"
# fi
# if (( $simid == (3) ))
	# then
	# eval "./../tapsim --threads=1 evaporation ZrMeshCorrected.cfg $tapsim_i $tapsim_o 1>TAPSIMSimID$simidPID$pillarid.STDOUT.txt 2>TAPSIMSimID$simidPID$pillarid.STDERR.txt"
# fi
# if (( $simid == (4) ))
	# then
	# eval "./../tapsim --threads=1 evaporation WMeshCorrected.cfg $tapsim_i $tapsim_o 1>TAPSIMSimID$simidPID$pillarid.STDOUT.txt 2>TAPSIMSimID$simidPID$pillarid.STDERR.txt"
# fi

eval "./../tapsim_O3 --threads=1 evaporation ZrMeshCorrected.cfg $tapsim_i $tapsim_o 1>TAPSIMSimID$simidPID$pillarid.STDOUT.txt 2>TAPSIMSimID$simidPID$pillarid.STDERR.txt"


##compress everything in $workingdir via tar.gz 
#eval "tar -zcvf Dump.tar.gz dump*"
#eval "tar --exclude=*.tar.gz --exclude=dump* --exclude=TAPSIMOutput* --exclude=trajectory_data* -zcvf SimMeta.tar.gz *"
#eval "tar -zcvf Trajectories.tar.gz trajectory_data*"
#eval "tar -zcvf DetectorHits.tar.gz TAPSIMOutput*"

##leave folder by changing directory in order to proceed with next pillar in the queue
#eval "cd .."
#echo "$workingdir done processing!"
