#!/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 -functionEntries -case .. | tail -n1")
CASE = system("foamDictionary ../caseParameterDict -entry case -value")

set term pngcairo

set grid
set key box
set xrange [0:25.6]
set xlabel 'r (mm)'

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 2 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"


#\*------------------Void fraction profile------------------------------------*/

set output '../postProcessing/report/alpha.png'
set ylabel '{/Symbol a} (-)'

plot 'alpha/m01_'.CASE.'l_01.epsrad_20_bub.dat' u (\$1*1000):2 t 'Experiment' w points ls 2, \
     'reference/'.CASE.'/line.csv' u (\$1*1000.):3 t 'Reference' w l ls 3, \
     '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*1000.):3 t 'Simulation' w l ls 1

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

plot 'alpha/m01_'.CASE.'l_01.epsrad_20_bub.dat' u (\$1*1000):2 t 'Exp' w points ls 1, \
     '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*1000.):3 t 'Sim' w l ls 1, \
     'alpha/m01_'.CASE.'l_01.epsrad_20_bub.dat' u (\$1*1000):3 t '< 4.4mm' w points ls 1 lc rgb 'red', \
     '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*1000.):4 t ' ' w l ls 1 lc rgb 'red', \
     'alpha/m01_'.CASE.'l_01.epsrad_20_bub.dat' u (\$1*1000):4 t '< 5.8mm' w points ls 1 lc rgb 'green', \
     '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*1000.):5 t ' ' w l ls 1 lc rgb 'green', \
     'alpha/m01_'.CASE.'l_01.epsrad_20_bub.dat' u (\$1*1000):5 t '< 7mm' w points ls 1 lc rgb 'blue', \
     '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*1000.):6 t ' ' w l ls 1 lc rgb 'blue', \
     'alpha/m01_'.CASE.'l_01.epsrad_20_bub.dat' u (\$1*1000):6 t '>= 7mm' w points ls 1 lc rgb 'orange', \
     '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*1000.):7 t ' ' w l ls 1 lc rgb 'orange', \

#\*-------------------Velocity profile----------------------------------------*/

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

plot 'velocity/m01_'.CASE.'l_01.vel.dat' u (\$1*1000):2 t 'Experiment' w points ls 2, \
     'reference/'.CASE.'/line.csv' u (\$1*1000.):10 t 'Reference' w l ls 3, \
     '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*1000.):10 t 'Simulation' w l ls 1

#\*------------------Radial diameter profile----------------------------------*/

set output '../postProcessing/report/diameterRadial.png'
set ylabel 'd (mm)'

plot 'diameter/m01_'.CASE.'l_01.his_lin_r.d32.dat' u (\$1*1000):(\$2*1000.) t 'Experiment' w points ls 2, \
     'reference/'.CASE.'/line.csv' u (\$1*1000.):(\$2*1000.) t 'Reference' w l ls 3, \
     '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*1000.):(\$2*1000.) t 'Simulation' w l ls 1

#\*------------------Non-drag forces------------------------------------------*/

set output '../postProcessing/report/forces.png'
set ylabel 'F_r (N/m^3)'

plot '../postProcessing/forces/'.TIME.'/line.csv' u (\$1*1000.):(-\$2) t 'Lift' w l ls 1 lc rgb 'red', \
     '../postProcessing/forces/'.TIME.'/line.csv' u (\$1*1000.):(-\$5) t 'Wall lubrication' w l ls 1 lc rgb 'green', \
     '../postProcessing/forces/'.TIME.'/line.csv' u (\$1*1000.):(-\$8) t 'Turbulent dispersion' w l ls 1 lc rgb 'blue'

#\*------------------Axial diameter profile-----------------------------------*/

set output '../postProcessing/report/diameterAxial.png'
set xlabel 'd (mm)'
set ylabel 'z (m)'
unset xrange

plot 'diameter/m01_'.CASE.'_01.his_lin_a.d32.dat' u (\$2*1000.):1 t 'Experiment' w points ls 2, \
     'reference/'.CASE.'/layerAverage.csv' u (\$2*1000.):1 t 'Reference' w l ls 3, \
     '../postProcessing/layerAverage/'.TIME.'/layerAverage.csv' u (\$2*1000.):1 t 'Simulation' w l ls 1

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

set output '../postProcessing/report/sizeDistribution.png'
set xlabel 'd (mm)'
set ylabel '({/Symbol a_{i}}/{/Symbol a}_{d})/{/Symbol D} d (mm^{-1})'
unset xrange

set style fill transparent solid 0.5 border

simA = "../postProcessing/sizeDistributionA/".TIME."/sizeDistributionA.csv"
simL = "../postProcessing/sizeDistributionL/".TIME."/sizeDistributionL.csv"

stats 'sizeDistribution/'.CASE.'a' u 3 nooutput name 'Aexp'
stats 'sizeDistribution/'.CASE.'l' u 3 nooutput name 'Lexp'

plot 'sizeDistribution/'.CASE.'l' u 1:(\$3/(Lexp_sum*0.25)) w boxes lc 'red' t 'Experiment L',\
     simL u (\$1/1e-3):(\$2/1e3) w histeps ls 1 t 'Simulation L'

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

plot 'sizeDistribution/'.CASE.'a' u 1:(\$3/(Aexp_sum*0.25)) w boxes lc 'gray' t 'Experiment A',\
     'sizeDistribution/'.CASE.'l' u 1:(\$3/(Lexp_sum*0.25)) w boxes lc 'red' t 'Experiment L',\
     simA u (\$1/1e-3):(\$2/1e3) w histeps ls 1 lw 0.5 t 'Simulation A',\
     simL u (\$1/1e-3):(\$2/1e3) w histeps ls 1 t 'Simulation L'

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(p_rgh,k.water,omega.water)/0/residuals.dat' u 1:2 w l ls 1 t 'p-rgh',\
     '../postProcessing/residuals(p_rgh,k.water,omega.water)/0/residuals.dat' u 1:3 w l ls 2 t 'k',\
     '../postProcessing/residuals(p_rgh,k.water,omega.water)/0/residuals.dat' u 1:4 w l ls 3 t '{/Symbol w}'
EOF

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