#!/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
set grid
set key left top box
set xlabel 'r/R'
set xrange [0:1]

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 2 pt 2 lc "violet"
set style line 5 lt 5 lw 2 pt 2 lc "cyan"

#\*----------------------Radial void fraction profile-------------------------*/

set output '../postProcessing/report/alpha.png'
#templates can not operate with set yrange [0:0.7]
set ylabel '{/Symbol a}_G'

plot 'reference/'.CASE.'/line.csv' u (\$1*39.37):2 t 'Reference' w lines ls 3, \
    '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*39.37):2 t 'Simulation' w lines ls 1, \
    'experiment/'.CASE.'/alpha.dat' u (\$1*39.37):(\$2) t 'Experiment' w points ls 2

#\*---------------------Radial liquid velocity profile------------------------*/

set output '../postProcessing/report/liqVel.png'
#templates can not operate with yrange [1.0:4.0]
set ylabel 'W_L (m/s)'
set key right top box

plot 'reference/'.CASE.'/line.csv' u (\$1*39.37):8 t 'Reference' w lines ls 3, \
    '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*39.37):8 t 'Simulation' w lines ls 1, \
    'experiment/'.CASE.'/liqVel.dat' u (\$1*39.37):2 t 'Experiment' w points ls 2

#\*-----------------------Radial gas velocity profile-------------------------*/

set output '../postProcessing/report/gasVel.png'
#templates can not operate with set yrange [1.5:4.0]
set ylabel 'W_G (m/s)'

plot 'reference/'.CASE.'/line.csv' u (\$1*39.37):5 t 'Reference' w lines ls 3, \
    '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*39.37):5 t 'Simulation' w lines ls 1, \
    'experiment/'.CASE.'/gasVel.dat' u (\$1*39.37):2 t 'Experiment' w points ls 2

#\*--------------------liquid turbulence kinetic energy-----------------------*/

set output '../postProcessing/report/liqKe.png'
#templates can not operate with set yrange [0:0.4]
set ylabel 'k_L (m^2/s^2)'

plot 'reference/'.CASE.'/line.csv' u (\$1*39.37):9 t 'Reference' w lines ls 3, \
    '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*39.37):9 t 'Simulation' w lines ls 1, \
    'experiment/'.CASE.'/liqKe.dat' u (\$1*39.37):2 t 'Experiment' w points ls 2

EOF

#------------------------------------------------------------------------------

gnuplot<<EOF

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

set term pngcairo

set style line 1 lw 2 lc "black"
set style line 2 lw 2 lc "red"
set style line 3 lw 2 lc "forest-green"
set style line 4 lw 2 lc "gray"
set style line 5 lw 2 lc "orange"
set style line 6 lw 2 lc "green"

set xlabel 't (s)'
set logscale y
set format y '%.0e'

set output '../postProcessing/report/residuals.png'

plot 'reference/'.CASE.'/residuals.dat' u 1:2 w l ls 4 t 'Ref. p-rgh',\
     '../postProcessing/residuals(p_rgh)/0/residuals.dat' u 1:2 w l ls 1 t 'p-rgh'
EOF

#------------------------------------------------------------------------------
