#!/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 box
set xrange [0:0.4]
set yrange [0:1.9]

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"

#\*-------------------------Centreline velocity for liquid plot--------------------------*/

set title 'r = 0'
set output '../postProcessing/report/Centerline_velocity_L.png'
set xlabel 'x (m)'
set ylabel 'U_L (m/s)'

plot  'reference/'.CASE.'/centerline/line.csv' u 1:2 t 'Reference' w line ls 3, \
      '../postProcessing/centerline/'.TIME.'/line.csv' u 1:2 t 'Simulation' w lines ls 1,\
      'experiment/'.CASE.'/centerline/centerline_velocity_L.dat' u 1:2 t 'Experiment' w points ls 2

#\*-------------------------Centreline velocity for gas plot--------------------------*/

set title 'r = 0'
set output '../postProcessing/report/Centerline_velocity_G.png'
set xlabel 'x (m)'
set ylabel 'U_G (m/s)'

plot  'reference/'.CASE.'/centerline/line.csv' u 1:5 t 'Reference' w line ls 3, \
      '../postProcessing/centerline/'.TIME.'/line.csv' u 1:5 t 'Simulation' w lines ls 1,\
      'experiment/'.CASE.'/centerline/centerline_velocity_G.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------------Perpendicular profiles: velocity----------------------*/

set xlabel 'r (m)'
set xrange [0:0.05]
set yrange [0:1.8]

#\*---------------------------------At x = 8D, liquid velocity profiles---------------------------------*/

set output '../postProcessing/report/velocity_L@x=8D.png'
set title 'x = 8D'
set ylabel 'U_L (m/s)'

plot 'reference/'.CASE.'/D8/line.csv' u 1:2 t 'Reference' w lines ls 3, \
     '../postProcessing/D8/'.TIME.'/line.csv' u 1:2 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D8/U_L.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 8D, gas velocity profiles---------------------------------*/

set output '../postProcessing/report/velocity_G@x=8D.png'
set title 'x = 8D'
set ylabel 'U_G (m/s)'

plot 'reference/'.CASE.'/D8/line.csv' u 1:5 t 'Reference' w lines ls 3, \
     '../postProcessing/D8/'.TIME.'/line.csv' u 1:5 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D8/U_G.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 24D, liquid velocity profiles---------------------------------*/

set output '../postProcessing/report/velocity_L@x=24D.png'
set title 'x = 24D'
set ylabel 'U_L (m/s)'

plot 'reference/'.CASE.'/D24/line.csv' u 1:2 t 'Reference' w lines ls 3, \
     '../postProcessing/D24/'.TIME.'/line.csv' u 1:2 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D24/U_L.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 24D, gas velocity profiles---------------------------------*/

set output '../postProcessing/report/velocity_G@x=24D.png'
set title 'x = 24D'
set ylabel 'U_G (m/s)'

plot 'reference/'.CASE.'/D24/line.csv' u 1:5 t 'Reference' w lines ls 3, \
     '../postProcessing/D24/'.TIME.'/line.csv' u 1:5 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D24/U_G.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 40D, liquid velocity profiles---------------------------------*/

set output '../postProcessing/report/velocity_L@x=40D.png'
set title 'x = 40D'
set ylabel 'U_L (m/s)'

plot 'reference/'.CASE.'/D40/line.csv' u 1:2 t 'Reference' w lines ls 3, \
     '../postProcessing/D40/'.TIME.'/line.csv' u 1:2 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D40/U_L.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 40D, gas velocity profiles---------------------------------*/

set output '../postProcessing/report/velocity_G@x=40D.png'
set title 'x = 40D'
set ylabel 'U_G (m/s)'

plot 'reference/'.CASE.'/D40/line.csv' u 1:5 t 'Reference' w lines ls 3, \
     '../postProcessing/D40/'.TIME.'/line.csv' u 1:5 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D40/U_G.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 60D, liquid velocity profiles---------------------------------*/

set output '../postProcessing/report/velocity_L@x=60D.png'
set title 'x = 60D'
set ylabel 'U_L (m/s)'

plot 'reference/'.CASE.'/D60/line.csv' u 1:2 t 'Reference' w lines ls 3, \
     '../postProcessing/D60/'.TIME.'/line.csv' u 1:2 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D60/U_L.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 60D, gas velocity profiles---------------------------------*/

set output '../postProcessing/report/velocity_G@x=60D.png'
set title 'x = 60D'
set ylabel 'U_G (m/s)'

plot 'reference/'.CASE.'/D60/line.csv' u 1:5 t 'Reference' w lines ls 3, \
     '../postProcessing/D60/'.TIME.'/line.csv' u 1:5 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D60/U_G.dat' u 1:2 t 'Experiment' w points ls 2

#\*-------------Perpendicular profiles: turbulent kinetic energy--------------*/

set ylabel 'k (m^2/s^2)'
unset yrange

#\*---------------------------------At x = 8D---------------------------------*/

set output '../postProcessing/report/k@x=8D.png'
set title 'x = 8D'

plot 'reference/'.CASE.'/D8/line.csv' u 1:8 t 'Reference' w lines ls 3, \
     '../postProcessing/D8/'.TIME.'/line.csv' u 1:8 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D8/k.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 24D---------------------------------*/

set output '../postProcessing/report/k@x=24D.png'
set title 'x = 24D'

plot 'reference/'.CASE.'/D24/line.csv' u 1:8 t 'Reference' w lines ls 3, \
     '../postProcessing/D24/'.TIME.'/line.csv' u 1:8 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D24/k.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 40D---------------------------------*/

set output '../postProcessing/report/k@x=40D.png'
set title 'x = 40D'

plot 'reference/'.CASE.'/D40/line.csv' u 1:8 t 'Reference' w lines ls 3, \
     '../postProcessing/D40/'.TIME.'/line.csv' u 1:8 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D40/k.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 60D---------------------------------*/

set output '../postProcessing/report/k@x=60D.png'
set title 'x = 60D'

plot 'reference/'.CASE.'/D60/line.csv' u 1:8 t 'Reference' w lines ls 3, \
     '../postProcessing/D60/'.TIME.'/line.csv' u 1:8 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D60/k.dat' u 1:2 t 'Experiment' w points ls 2


#\*-------------Perpendicular profiles: gas fraction--------------*/

set ylabel '{/Symbol a} (-)'

#\*---------------------------------At x = 24D---------------------------------*/

set output '../postProcessing/report/alpha@x=24D.png'
set title 'x = 24D'

plot 'reference/'.CASE.'/D24/line.csv' u 1:9 t 'Reference' w lines ls 3, \
     '../postProcessing/D24/'.TIME.'/line.csv' u 1:9 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D24/alpha.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 40D---------------------------------*/

set output '../postProcessing/report/alpha@x=40D.png'
set title 'x = 40D'

plot 'reference/'.CASE.'/D40/line.csv' u 1:9 t 'Reference' w lines ls 3, \
     '../postProcessing/D40/'.TIME.'/line.csv' u 1:9 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D40/alpha.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 60D---------------------------------*/

set output '../postProcessing/report/alpha@x=60D.png'
set title 'x = 60D'

plot 'reference/'.CASE.'/D60/line.csv' u 1:9 t 'Reference' w lines ls 3, \
     '../postProcessing/D60/'.TIME.'/line.csv' u 1:9 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D60/alpha.dat' u 1: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'

# plotting data
plot 'reference/'.CASE.'/residuals.dat' u 1:2 w l ls 4 t 'Ref. p',\
     'reference/'.CASE.'/residuals.dat' u 1:3 w l ls 5 t 'Ref. k',\
     'reference/'.CASE.'/residuals.dat' u 1:4 w l ls 6 t 'Ref. {/Symbol w}',\
     '../postProcessing/residuals/0/residuals.dat' u 1:2 w l ls 1 t 'p',\
     '../postProcessing/residuals/0/residuals.dat' u 1:3 w l ls 2 t 'k',\
     '../postProcessing/residuals/0/residuals.dat' u 1:4 w l ls 3 t '{/Symbol w}'
EOF

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