#!/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 xlabel 'r/R'
set xrange [-1: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 profiles------------------------*/

set yrange [0:0.2]
set ylabel '{/Symbol a}_G'

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

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


#\*-------------------- Axial gas void fraction profile -------------------------*/

set term pngcairo size 480,640

set output "../postProcessing/report/axialAlpha.png"
set ylabel 'z (m)'
unset xrange
set yrange[-0.3:0.5]
set xlabel '{/Symbol a}_{G} [%]'

plot '../postProcessing/layerAverageFromInlet/'.TIME.'/layerAverage.csv' u (\$2*100):(\$1 < 0 ? \$1 : NaN) t 'Simulation' w lines ls 1, \
     '../postProcessing/layerAverageFromOutlet/'.TIME.'/layerAverage.csv' u (\$2*100):(\$1 > 0 ? \$1 : NaN) notitle w lines ls 1, \
    'experiment/'.CASE.'/axial_csAvg.val' u (\$2):(\$1/1000) w linespoints ls 2 lw 2 t 'Experiment'


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

set output "../postProcessing/report/axialGasVel.png"
set ylabel 'z (m)'
set yrange[-0.3:0.5]
set xlabel 'u_{z,G} (m/s)'

plot '../postProcessing/layerAverageFromInlet/'.TIME.'/layerAverage.csv' u 5:(\$1 < 0 ? \$1 : NaN) t 'Simulation' w lines ls 1, \
     '../postProcessing/layerAverageFromOutlet/'.TIME.'/layerAverage.csv' u 5:(\$1 > 0 ? \$1 : NaN) notitle w lines ls 1, \
    'experiment/'.CASE.'/axial_csAvg.val' u 7:(\$1/1000) w linespoints ls 2 lw 2 t 'Experiment', \


#\*-------------------Size distribution---------------------------------------*/

set xlabel 'd (mm)'
set ylabel '({/Symbol a_{i}}/{/Symbol a}_{d})/{/Symbol D} d (mm^{-1})'
unset yrange

set style fill transparent solid 0.5 border

simF2 = system("ls ../postProcessing/sizeDistributionF2/".TIME."/sizeDistributionF2.csv")
stats 'experiment/'.CASE.'/sizeDistribution.val' u 13 nooutput name 'F2exp'

set yrange[0:0.35]

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

plot 'experiment/'.CASE.'/sizeDistribution.val' u 1:(\$13/(F2exp_sum*0.5)) w boxes lc 'red' t 'Experiment',\
     simF2 u (\$1/1e-3):(\$2/1e3) w histeps ls 1 t 'Simulation'

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-rgh',\
     '../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

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