#!/bin/bash

device="host"
sizes="16"
initFunctions="sin-bumps"
snapshotPeriod=0.001
finalTime=0.15
solverName="mean-curvature-flow"
boundaryCondition="neumann"
boundaryValue=0
minZ=-1
maxZ=1
contourHeight=0.3

spaceStep=$(expr 1/($sizes-2) | bc | sed 's/^\./0./')

setupInitFunction()
{
   initFunction=$1
      origin=0
      proportions=1
      amplitude=1.0
      waveLength=1.0
      waveLengthX=1.0
      waveLengthY=1.0
      waveLengthZ=1.0
      wavesNumber=0.0
      wavesNumberX=0.0
      wavesNumberY=0.0
      wavesNumberZ=0.0
      phase=0.0
      phaseX=0.0
      phaseY=0.0
      phaseZ=0.0
      sigma=1
}

setupGrid()
{
   gridSize=$1
   tnl-grid-setup --dimensions 2 \
                  --origin-x ${origin} \
                  --origin-y ${origin} \
                  --origin-z ${origin} \
                  --proportions-x ${proportions} \
                  --proportions-y ${proportions} \
                  --proportions-z ${proportions} \
                  --size-x ${gridSize} \
                  --size-y ${gridSize} \
                  --size-z ${gridSize} 
}

setInitialCondition()
{
   initFunction=$1
   tnl-init --test-function ${initFunction} \
	    --output-file initial.tnl \
            --amplitude ${amplitude} \
            --wave-length ${waveLength} \
            --wave-length-x ${waveLengthX} \
            --wave-length-y ${waveLengthY} \
            --wave-length-z ${waveLengthZ} \
            --waves-number ${wavesNumber} \
            --waves-number-x ${wavesNumberX} \
            --waves-number-y ${wavesNumberY} \
            --waves-number-z ${wavesNumberZ} \
            --phase ${phase} \
            --phase-x ${phaseX} \
            --phase-y ${phaseY} \
            --phase-z ${phaseZ} \
            --sigma ${sigma} \
            --time-dependence none
}

solve()
{
   timeDiscretisation=$1
   discreteSolver=$2
   ${solverName} --device ${device} \
                 --mesh mesh.tnl \
                 --initial-condition initial.tnl \
		--snapshot-period ${snapshotPeriod} \
                 --time-discretisation ${timeDiscretisation} \
                 --time-step 1 \
                 --time-step-order 2 \
                 --discrete-solver ${discreteSolver} \
                 --merson-adaptivity 1.0e-7 \
                 --sor-omega 1.95 \
                 --gmres-restarting 20 \
                 --min-iterations 20 \
                 --convergence-residue 1.0e-12 \
		 --boundary-conditions-type ${boundaryCondition} \
		 --boundary-conditions-constant ${boundaryValue} \
		 --final-time ${finalTime}
}

view()
{
   tnl-view --input-files u-*.tnl
   tnl-view --input-files initial.tnl
}

generate3DVid()
{
seznam=`ls u-*.gplt`
step=0
for fname in $seznam ; do
step=$((${step}+1))
time=$(expr $step*$snapshotPeriod | bc | sed 's/^\./0./')
echo "Drawing contour $fname"
gnuplot << EOF
set output '${fname}.png'
set xrange [${1}:${proportions}]
set yrange [${1}:${proportions}]
set zrange [${minZ}:${maxZ}]    
unset surface
set terminal png size 1200,600
set view map
set title 'Numerical solution - contour in height ${contourHeight} - T: ${time}'
set pm3d interpolate 100,100
set size square
set contour base
unset colorbox
set cntrparam levels discrete ${contourHeight}
splot '$fname' with pm3d notitle
EOF
done

mencoder "mf://u-*.png" -mf fps=4 -o mean_curvature_contour_${contourHeight}_$size.avi -ovc lavc -lavcopts vcodec=mpeg4

rm *.png
rm *.tnl         
rm *.gplt 
rm mesh.asy
rm computation-done
     
}

runTest()
{
   for initFunction in ${initFunctions};
   do
      cd ${initFunction}-videos
      setupInitFunction ${initFunction}
         
      for size in $sizes;
      do
          cd $size
          echo ""
          echo ""
          echo ""
          if test ! -f computation-done;
          then
             touch computation-in-progress
             echo "========================================================================="
             echo "===                   SETTING UP THE GRID                             ==="
             echo "========================================================================="
             setupGrid $size 
             echo "========================================================================="
             echo "===                WRITING THE EXACT SOLUTION                         ==="
             echo "========================================================================="
             setInitialCondition $initFunction
             echo "========================================================================="
             echo "===                   STARTING THE SOLVER                             ==="
             echo "========================================================================="
             solve explicit merson
             #solve semi-implicit gmres
             mv computation-in-progress computation-done
             echo "========================================================================="
             echo "===                     COMPUTATION DONE                              ==="            
             echo "========================================================================="
	     view
	     generate3DVid $origin $proportions
             cd ..
             lastSize=$size
          fi
         cd ..
      done
      cd ..
   done
}

runTest
 
