#!/bin/bash

device="host"
dimensions="1D 2D 3D"
#dimensions="1D"
sizes1D="16 32 64 128 256 512 1024 2048 4096"
#sizes1D="256"
sizes2D="16 32 64 128 256 512 1024"
#sizes2D="8"
sizes3D="16 32 64 128 256"
testFunctions="paraboloid"
snapshotPeriod=0.1
finalTime=1.5
solverName="tnl-hamilton-jacobi"
#solverName="gdb --args tnl-hamilton-jacobi-dbg"
#

setupTestFunction()
{
   testFunction=$1
#   if test x${testFunction} = "xexp-bump";
#   then
      origin=-1.0
      proportions=2.0
      amplitude=1.0
      waveLength=0.2
      waveLengthX=0.2
      waveLengthY=0.2
      waveLengthZ=0.2
      wavesNumber=3.0
      wavesNumberX=0.5
      wavesNumberY=2.0
      wavesNumberZ=3.0
      phase=0.1
      phaseX=0.0
      phaseY=0.0
      phaseZ=0.0
      sigma=0.5
#   fi
}

setupGrid()
{
   dimensions=$1
   gridSize=$2
   tnl-grid-setup --dimensions ${dimensions} \
                  --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()
{
   testFunction=$1
   tnl-init --test-function ${testFunction} \
            --output-file initial-u.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} 
   
    tnl-init --test-function ${testFunction}-sdf \
            --output-file final-u.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}

}

solve()
{
   timeDiscretisation=$1
   discreteSolver=$2
   ${solverName} --device ${device} \
                 --mesh mesh.tnl \
                 --initial-condition initial-u.tnl \
                 --time-discretisation ${timeDiscretisation} \
                 --time-step 10 \
                 --time-step-order 2 \
                 --discrete-solver ${discreteSolver} \
                 --merson-adaptivity 1.0e-5 \
                 --min-iterations 20 \
                 --convergence-residue 1.0e-12 \
                 --snapshot-period ${snapshotPeriod} \
                 --final-time ${finalTime}
}
               
computeError()
{
   tnl-diff --mesh mesh.tnl \
            --input-files final-u.tnl u-*.tnl \
            --mode sequence \
            --snapshot-period ${snapshotPeriod} \
            --output-file errors.txt \
            --write-difference yes
}

runTest()
{
   for testFunction in ${testFunctions};
   do
      mkdir -p ${testFunction}
      cd ${testFunction}
      setupTestFunction ${testFunction}
      
      for dim in ${dimensions};
      do
         mkdir -p $dim
         cd ${dim}
         if test $dim = 1D;
         then 
            sizes=$sizes1D
         fi
         if test $dim = 2D;
         then 
            sizes=$sizes2D
         fi
         if test $dim = 3D;
         then 
            sizes=$sizes3D
         fi
         
         lastSize=""
         for size in $sizes;
         do
            mkdir -p $size
            cd $size
            echo ""
            echo ""
            echo ""
            if test ! -f computation-done;
            then
               touch computation-in-progress
               echo "========================================================================="
               echo "===                   SETTING UP THE GRID                             ==="
               echo "========================================================================="
               setupGrid $dim $size 
               echo "========================================================================="
               echo "===                WRITING THE EXACT SOLUTION                         ==="
               echo "========================================================================="
               setInitialCondition $testFunction
               echo "========================================================================="
               echo "===                   STARTING THE SOLVER                             ==="
               echo "========================================================================="
               solve explicit merson
               #solve semi-implicit gmres
               mv computation-in-progress computation-done
            fi            
            echo "========================================================================="
            echo "===                   COMPUTING THE ERROR                             ==="
            echo "========================================================================="
            computeError
            echo "========================================================================="
            echo "===                     COMPUTING THE EOC                             ==="            
            echo "========================================================================="
            if test ! x$lastSize = x;
            then
               tnl-err2eoc ../$lastSize/errors.txt errors.txt
            fi
            echo "========================================================================="
            echo "===                     COMPUTATION DONE                              ==="            
            echo "========================================================================="
            cd ..
            lastSize=$size
         done

         cd ..
      done
      cd ..
   done
}

runTest
 
