#!/bin/bash

if ! command -v gnuplot > /dev/null 2>&1
then
    echo 'gnuplot not found - skipping graph creation' >&2
    exit 1
else
    mkdir -p ../postProcessing/report
fi

gnuplot<<EOF

#\*--------------------------------------General settings---------------------------------------------------*/

TIME = system("foamListTimes -latestTime -case ..")
CASE = system("foamDictionary ../caseParameterDict -entry case -value")

set term pngcairo size 500,500

set grid
set key box
set key font ",11"
set xlabel 'x/D [-]'
set xrange [0:1]
set format x "%.1f"
set format y "%.2f"

set style line 1 dt 2 lt 1 lw 2 pt 2 lc "black"
set style line 2 lt 2 lw 5 pt 2 lc "red"
set style line 3 lt 3 lw 2 pt 2 lc "green"
set style line 4 lt 4 lw 3.5 pt 2 lc "navy"
set style line 5 lt 5 lw 3.5 pt 2 lc "gold"

#\*------------------------Void fraction profile at height = 0.50 [m]--------------------------------------*/

set output '../postProcessing/report/alpha.png'
set ylabel '{/Symbol a}_G [-] % '


plot  'reference/'.CASE.'/DOF.csv' u (\$1/0.1):(\$2*100) w l ls 3 t 'Reference', \
      '../postProcessing/DOF1/'.TIME.'/layerAverage.csv' u (\$1/0.1):(\$2*100) w l ls 1 t 'Simulation', \
      'experiment/'.CASE.'/alphaG.dat' u (\$1/0.1):(\$2*100) pt 5 ps 1 lc rgb 'red' t 'Experiment'

#\*--------------------------Gas velocity profile at height = 0.50 [m]--------------------------------------*/

set output '../postProcessing/report/uG.png'
set ylabel 'u_G [m/s]'

plot  'reference/'.CASE.'/DOF.csv' u (\$1/0.1):5 w l ls 3 t 'Reference', \
      '../postProcessing/DOF1/'.TIME.'/layerAverage.csv'u (\$1/0.1):5 w l ls 1 t 'Simulation', \
      'experiment/'.CASE.'/uG.dat' u (\$1/0.1):2 pt 5 ps 1 lc rgb 'red' t 'Experiment'

#\*------------------------Lateral liquid velocity profile at height = 0.50 [m]------------------------------*/

set output '../postProcessing/report/uLRad.png'
set ylabel 'u_L [m/s]'

plot  'reference/'.CASE.'/VX.csv' u (\$1/0.1):4 w l ls 3 t 'Reference', \
      '../postProcessing/VX1/'.TIME.'/layerAverage.csv' u (\$1/0.1):4 w l ls 1 t 'Simulation', \
      '../validation/experiment/'.CASE.'/uLLateral.dat' u 1:2 pt 5 ps 1 lc rgb 'red' t 'Experiment'

EOF

#\*--------------------Axial liquid velocity profile in the centre of the column----------------------------*/

gnuplot<<EOF

TIME = system("foamListTimes -latestTime -case ..")
CASE = system("foamDictionary ../caseParameterDict -entry case -value")

set term pngcairo size 500,500

set grid
set key box
set key font ",11"

set output '../postProcessing/report/uLAxl.png'
set xlabel 'z/L [-]'
set xrange [0:1]
set ylabel 'u_L [m/s]'

plot  'reference/'.CASE.'/VZ.csv' u (\$1-0.535):4 w l ls 3 t 'Reference', \
      '../postProcessing/VZ1/'.TIME.'/layerAverage.csv' u (\$1-0.535):4 w l ls 1 t 'Simulation', \
      '../validation/experiment/'.CASE.'/uLAxial.dat' u 1:2 pt 5 ps 1 lc rgb 'red' t 'Experiment'

EOF

#\*-------------------------------------Residual------------------------------------------------------------*/

gnuplot<<EOF

    set term pngcairo size 500,500

    set grid
    set key box
    set output '../postProcessing/report/residual.png'

    set xlabel 'Timesteps'
    set xtics 100
    set logscale y
    set format y '%.0e'
    set ylabel 'Residual'

    residualprgh = system("ls ../postProcessing/residuals/0/residuals.dat")

    plot residualprgh u 1:2 w l lw 3 lc rgb 'black' t 'residual, prgh',\
         residualprgh u 1:3 w l lw 3 lc rgb 'green' t 'residual, k'

EOF
#------------------------------------------------------------------------------
