#!/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 bottom box
set xlabel 'x/W'
set xrange [-0.5:0.5]

set style line 1 dt 2 lt 1 lw 2 pt 2 lc "black"
set style line 2 lt 2 lw 2 pt 7 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 7 lc "red"

#\*---------------------Horizontal gas fraction profile-----------------------*/

set output '../postProcessing/report/alphaG.png'
set ylabel '{/Symbol a}_G'

plot 'reference/'.CASE.'/horizontal/layerAverage.csv' u (\$1*10):2 t 'Reference' w lines ls 3, \
    '../postProcessing/shadowgraphy/'.TIME.'/layerAverage.csv' u (\$1*10):2 t 'Simulation' w lines ls 1, \
    'experiment/'.CASE.'/horizontal/alphaG.dat' u 1:2 t 'Experiment' w points ls 2

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

set output '../postProcessing/report/liqVel.png'
set ylabel 'W_L (m/s)'

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

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

set output '../postProcessing/report/gasVel.png'
set ylabel 'W_G (m/s)'

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

#\*-------------------Horizontal solid velocity profile-----------------------*/

set output '../postProcessing/report/solVel.png'
set ylabel 'W_S (m/s)'

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

#\*-------------------Vertical solid fraction profile-------------------------*/

set output '../postProcessing/report/alphaS.png'
set ylabel '{/Symbol a}_S'
set xlabel 'z/H'
set xrange [0:1]

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

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',\
     '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

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