#!/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 -latestTime -case ..")
TIME_AVERAGE = system("ls ../postProcessing/volIntegrateEpsWater/ | tail -n1")
CELLS = system("grep 'cells: ' ../log.checkMesh | awk '{print \$2}'")
CORES = system("foamDictionary -value -entry numberOfSubdomains ../system/decomposeParDict")
RHOL = system("foamDictionary -expand ../constant/physicalProperties.water -entry mixture/equationOfState/rho -value")
N = system("foamDictionary ../caseParameterDict -entry N -value")
CASE = system("foamDictionary ../caseParameterDict -entry case -value")

set term pngcairo
set grid
set key right top box

set style line 1 dt 2 lt 1 lw 2 pt 2 lc "black"
set style line 2 lt 2 lw 2 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 "blue"
set style line 6 lt 2 lw 2 pt 2 lc "cyan"
set style line 7 lt 3 lw 2 pt 2 lc "magenta"
set style line 8 lt 4 lw 2 pt 2 lc "gold"
set style line 9 lt 5 lw 2 pt 2 lc "black"

#\*--------------------------------Timestep----------------------------------*/

set title 'Timestep'
set output '../postProcessing/report/timestep.png'
set xlabel 't (s)'
set ylabel '{/Symbol D}t (s)'
set logscale y
set format y '%.0e'

plot '../postProcessing/timeStep/0/timeStep.dat' u 1:2 w l ls 2 notitle

#\*----------------------------Computation time------------------------------*/

unset logscale
unset format
set title 'Using '.CORES.' CPU cores for '.CELLS.' cells'
set output '../postProcessing/report/computationTime.png'
set xlabel 'Computation time (h)'
set ylabel 't (s)'

plot '../postProcessing/time/0/time.dat' u (\$3/3600):1 w l ls 2 notitle

#\*------------------------------Max Co number-------------------------------*/

set title 'Max Co number'
set output '../postProcessing/report/MaxCoNumber.png'
set xlabel 't (s)'
set ylabel 'Co (-)'

plot '../postProcessing/cellMax(Co.air)/0/volFieldValue.dat' u 1:2 w l ls 2 t 'Air (max)',\
     '../postProcessing/cellMax(Co.water)/0/volFieldValue.dat' u 1:2 w l ls 3 t 'Water (max)'

#\*----------------------------Average Co number-----------------------------*/

set title 'Average Co number'
set output '../postProcessing/report/AvgCoNumber.png'
set xlabel 't (s)'
set ylabel 'Co (-)'

plot '../postProcessing/cellAvg(Co.air)/0/volFieldValue.dat' u 1:2 w l ls 2 t 'Air (avg)',\
     '../postProcessing/cellAvg(Co.water)/0/volFieldValue.dat' u 1:2 w l ls 3 t 'Water (avg)'

#\*-------------------------Co number distribution---------------------------*/

set title 'Co number distribution'
set output '../postProcessing/report/CoNumberDistribution.png'
set xlabel 'Co (-)'
set ylabel 'v/v_T (-)'
set logscale y
set format y '%g'
set style fill empty border

air     = "../postProcessing/histogramAir/".TIME."/histogram.csv"
water  = "../postProcessing/histogramWater/".TIME."/histogram.csv"

plot air     u 1:2 w boxes lw 2 lc 'red' t 'Air',\
     water  u 1:2 w boxes lw 2 lc 'green' t 'Water'

#\*--------------------------------Residuals---------------------------------*/

set title 'Residuals'
set output '../postProcessing/report/residuals.png'
set xlabel 't (s)'
set ylabel 'residuals (-)'
set logscale y
set format y '%.0e'

plot '../postProcessing/residuals/0/residuals.dat' u 1:2 w l ls 2 t 'p',\
     '../postProcessing/residuals/0/residuals.dat' u 1:3 w l ls 3 t 'k',\
     '../postProcessing/residuals/0/residuals.dat' u 1:4 w l ls 5 t '{/Symbol w}'

#\*----------------------------Continuity error------------------------------*/

unset logscale
unset format
set title 'Continuity error'
set output '../postProcessing/report/continuityError.png'
set xlabel 't (s)'
set ylabel 'Relative continuity error (1/s)'

plot '../postProcessing/continuityError/0/volFieldValue.dat' u 1:(\$2/\$4) w l ls 2 t 'Air',\
     '../postProcessing/continuityError/0/volFieldValue.dat' u 1:(\$3/\$5) w l ls 3 t 'Water'

#\*-------------------------------Gas holdup---------------------------------*/

set title 'Gas holdup'
set output '../postProcessing/report/gasHoldupDispersed.png'
set xlabel 't (s)'
set ylabel '<~{/Symbol \a }{.6—} ~_G{.6—}  > (-)'
gH = '../postProcessing/gasHoldup/0/gasHoldup.dat'

plot gH u 1:5 w l ls 2 notitle

#\*----------------------------Total gas holdup------------------------------*/

set title 'Total gas holdup'
set output '../postProcessing/report/gasHoldupTotal.png'
set xlabel 't (s)'
set ylabel '<~{/Symbol \a }{.6—} ~_G{.6—}  > (-)'
gH = '../postProcessing/gasHoldup/0/gasHoldup.dat'
stats 'experiment/'.CASE.'/gasHoldup.dat' using 1 nooutput
gHE = STATS_mean

plot gH u 1:4 w l ls 2 t 'Simulation', \
     gHE w l dt 2 lw 2 t 'Experiment'

#\*------------------------------Water volume--------------------------------*/

set title 'Water volume'
set output '../postProcessing/report/waterVolume.png'
set xlabel 't (s)'
set ylabel 'V_L (m^3)'

plot '../postProcessing/waterVolume/0/volFieldValue.dat' u 1:2 w l ls 2 notitle

#\*-------------------------------Flow rates---------------------------------*/

set title 'Flow rate'
set output '../postProcessing/report/flowRates.png'
set xlabel 't (s)'
set ylabel 'Flow rate (m^3/s)'

plot '../postProcessing/inletAirFlow/0/surfaceFieldValue.dat' u 1:2 w l ls 2 t 'Air inlet',\
     '../postProcessing/outletAirFlow/0/surfaceFieldValue.dat' u 1:2 w l ls 3 t 'Air outlet',\
     '../postProcessing/inletWaterFlow/0/surfaceFieldValue.dat' u 1:2 w l ls 6 t 'Water inlet',\
     '../postProcessing/outletWaterFlow/0/surfaceFieldValue.dat' u 1:2 w l ls 7 t 'Water outlet'

#\*----------------------------------Power-----------------------------------*/

unset logscale
unset format
set title 'Power'
set output '../postProcessing/report/power.png'
set xlabel 'time'
set ylabel 'Power [W]'
set key width -4
stats 'experiment/'.CASE.'/powerInput.dat' using 1 nooutput
pE = STATS_mean

plot '< paste ../postProcessing/forcesRotatingPatchesWater/0/forces.dat ../postProcessing/forcesRotatingPatchesAir/0/forces.dat' u 1:((\$2+\$3+\$4+\$5)*(-2*pi*N/60.0)) "%lf ((%*lf %*lf %*lf) (%*lf %*lf %*lf)) ((%*lf %*lf %lf) (%*lf %*lf %lf)) %*lf ((%*lf %*lf %*lf) (%*lf %*lf %*lf)) ((%*lf %*lf %lf) (%*lf %*lf %lf))" w l ls 2 t 'Rotating' ,\
     '< paste ../postProcessing/forcesStationaryPatchesWater/0/forces.dat ../postProcessing/forcesStationaryPatchesAir/0/forces.dat' u 1:((\$2+\$3+\$4+\$5)*(2*pi*N/60.0)) "%lf ((%*lf %*lf %*lf) (%*lf %*lf %*lf)) ((%*lf %*lf %lf) (%*lf %*lf %lf)) %*lf ((%*lf %*lf %*lf) (%*lf %*lf %*lf)) ((%*lf %*lf %lf) (%*lf %*lf %lf))" w l ls 3 t 'Stationary' ,\
     '../postProcessing/volIntegrateEpsWater/'.TIME_AVERAGE.'/volFieldValue.dat' u 1:(\$2)*RHOL w l ls 1 t 'Dissipated', \
     pE w l dt 2 lw 2 t 'Experiment'

EOF
