#!/bin/bash

if ! command -v > /dev/null 2>&1
then
    echo 'gnuplot not found - skipping graph creation' >&2
    exit 1
else
    mkdir -p ../postProcessing/report
fi

gnuplot<<EOF

#\*-----------------------------Case parameters------------------------------*/

OMEGA = system("foamDictionary ../constant/MRFProperties -entry MRF1/omega -value | cut -d' ' -f1")
RHOL = system("foamDictionary ../constant/thermophysicalProperties.water -entry mixture/equationOfState/rho -value")
CASE = system("foamDictionary ../caseParameterDict -entry case -value")

#\*----------------------------General settings------------------------------*/

TIME = system("foamListTimes -latestTime -case ..")
TIME_AVERAGE = system("ls ../postProcessing/probes/ | tail -n1")

set terminal pngcairo size 800,600 enhanced font 'Arial,20'

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"

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

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

set key spacing 1
set key top right
set key font ',15'


plot '../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 e}',\
     '< gzip -dc reference/'.CASE.'/residuals.dat.gz' u 1:2 w l ls 4 t 'Ref. p-rgh',\
     '< gzip -dc reference/'.CASE.'/residuals.dat.gz' u 1:3 w l ls 5 t 'Ref. k',\
     '< gzip -dc reference/'.CASE.'/residuals.dat.gz' u 1:4 w l ls 6 t 'Ref. {/Symbol e}'

#\*--------------------------------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

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

unset logscale
unset format
set title 'Co number'
set output '../postProcessing/report/CoNumber.png'
set xlabel 't (s)'
set ylabel 'max Co (-)'

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

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

set title 'Gas holdup'
set output '../postProcessing/report/gasHoldup.png'
set xlabel 't (s)'
set ylabel '<{/Symbol a}_G> (-)
set key right bottom
gH = '../postProcessing/volumeAverage/0/volFieldValue.dat'
correlationValue = 0.019

plot correlationValue w l ls 2 t 'Correlation',\
     gH u 1:2 w l ls 3 t 'Instant',\
     gH u 1:3 w l ls 4 t 'Average',\
     '< gzip -dc reference/'.CASE.'/volumeAverage/0/volFieldValue.dat.gz' u 1:3 w l ls 5 t 'Ref. Average'

#\*---------------------Integral of energy dissipation-----------------------*/

set title ' Volume integral of energy dissipation rate'
set output '../postProcessing/report/VolIntEnergyDissipation.png'
set xlabel 't (s)'
set ylabel '<{/Symbol \a}_L{/Symbol \e}_L>V_L (m^5/s^3)'

plot '../postProcessing/volIntegrateEpsWater/0/volFieldValue.dat' u 1:2 w l ls 2 t 'Instant',\
     '../postProcessing/volIntegrateEpsWater/0/volFieldValue.dat' u 1:3 w l ls 3 t 'Average',\
     '< gzip -dc reference/'.CASE.'/volIntegrateEpsWater/0/volFieldValue.dat.gz' u 1:3 w l ls 5 t 'Ref. Average'

#\*-----------------------------Average of kLa-------------------------------*/

set title ' Volumetric mass transfer coeffcient'
set output '../postProcessing/report/kLa.png'
set xlabel 't (s)'
set ylabel 'kLa (1/s)'
set key center right
correlationValue = 0.02

plot correlationValue w l ls 2 t 'Correlation',\
     gH u 1:4 w l ls 3 t 'Instant',\
     gH u 1:5 w l ls 4 t 'Average'

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

set title 'Flow rate'
set output '../postProcessing/report/flowRates.png'
set xlabel 't (s)'
set ylabel 'mass flow rate (kg/s)'

plot '../postProcessing/inletFlowRate/0/surfaceFieldValue.dat' u 1:(-\$2) w l ls 2 t 'inlet',\
     '../postProcessing/outletFlowRate/0/surfaceFieldValue.dat' u 1:(\$2) w l ls 3 t 'outlet - inst',\
     '../postProcessing/outletFlowRate/0/surfaceFieldValue.dat' u 1:(\$3) w l ls 4 t 'outlet - Avg'

#\*-----------------------------Velocity probes------------------------------*/

set title 'Probes'
set output '../postProcessing/report/velocityProbes.png'
set xlabel 't (s)'
set ylabel '|u_L| (m/s)'
set key right top
vel = '../postProcessing/probes/0/mag(U.water)'
stats vel u 2 name 'statVEL' nooutput
# set yrange [0:1.5*statVEL_max]

plot vel u 1:2 w l ls 2 t 'Near stirrer (Inst)',\
     '../postProcessing/probes/'.TIME_AVERAGE.'/mag(UMean.water)' u 1:2 w l ls 4 t 'Near stirrer (Avg)',\
     vel u 1:3 w l ls 3 t 'Above stirrer (Inst)',\
     vel u 1:4 w l ls 5 t 'Below stirrer (Inst)'

#\*---------------------------Gas fraction probes----------------------------*/

set title 'Probes'
set output '../postProcessing/report/gasFractionProbes.png'
set xlabel 't (s)'
set ylabel '{/Symbol a}_G (-)'
gV = '../postProcessing/probes/0/alpha.air'
stats gV u 2 name 'statGV' nooutput
ymax = (2*statGV_max < 1) ? 2*statGV_max : 1
# set yrange [0:ymax]

plot gV u 1:2 w l ls 2 t 'Near stirrer (Inst)',\
     '../postProcessing/probes/'.TIME_AVERAGE.'/alphaMean.air' u 1:2 w l ls 5 t 'Near stirrer (Avg)',\
     gV u 1:3 w l ls 3 t 'Above stirrer (Inst)',\
     gV u 1:4 w l ls 4 t 'Below stirrer (Inst)'

#\*---------------------------------Torque-----------------------------------*/

set title 'Torque'
set output '../postProcessing/report/torque.png'
set xlabel 'time'
set ylabel 'torque [Nm]'
# set yrange[-0.6:2]
set key width -4

plot '../postProcessing/forcesRotatingPatchesWater/0/forces.dat' u 1:(\$4+\$7)*(-1) "%lf ((%*lf %*lf %*lf) (%*lf %*lf %*lf)) ((%lf %lf %lf) (%lf %lf %lf))" w l ls 1 t 'torque rotating (water)' ,\
     '../postProcessing/forcesStationaryPatchesWater/0/forces.dat' u 1:(\$4+\$7) "%lf ((%*lf %*lf %*lf) (%*lf %*lf %*lf)) ((%lf %lf %lf) (%lf %lf %lf))" w l ls 2 t 'torque stationary (water)' ,\
     '../postProcessing/forcesRotatingPatchesAir/0/forces.dat' u 1:(\$4+\$7) "%lf ((%*lf %*lf %*lf) (%*lf %*lf %*lf)) ((%lf %lf %lf) (%lf %lf %lf))" w l ls 3 t 'torque rotating (air)' ,\
     '../postProcessing/forcesStationaryPatchesAir/0/forces.dat' u 1:(\$4+\$7)*(-1) "%lf ((%*lf %*lf %*lf) (%*lf %*lf %*lf)) ((%lf %lf %lf) (%lf %lf %lf))" w l ls 4 t 'torque stationary (air)'

set title 'Total torque'
set output '../postProcessing/report/totalTorque.png'
set key width -3
# set yrange[0:0.6]

system("gzip -dc reference/".CASE."/forcesRotatingPatchesWater/0/forces.dat.gz > forcesRotatingPatchesWater.dat")
system("gzip -dc reference/".CASE."/forcesRotatingPatchesAir/0/forces.dat.gz > forcesRotatingPatchesAir.dat")
system("gzip -dc reference/".CASE."/forcesStationaryPatchesWater/0/forces.dat.gz > forcesStationaryPatchesWater.dat")
system("gzip -dc reference/".CASE."/forcesStationaryPatchesAir/0/forces.dat.gz > forcesStationaryPatchesAir.dat")

plot '< paste ../postProcessing/forcesRotatingPatchesWater/0/forces.dat ../postProcessing/forcesRotatingPatchesAir/0/forces.dat' u 1:(\$2+\$3+\$4+\$5)*(-1) "%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 1 t 'torque rotating' ,\
     '< paste ../postProcessing/forcesStationaryPatchesWater/0/forces.dat ../postProcessing/forcesStationaryPatchesAir/0/forces.dat' u 1:(\$2+\$3+\$4+\$5) "%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 'torque stationary',\
     '< paste forcesRotatingPatchesWater.dat forcesRotatingPatchesAir.dat' u 1:(\$2+\$3+\$4+\$5)*(-1) "%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 'Ref. torque rotating' ,\
     '< paste forcesStationaryPatchesWater.dat forcesStationaryPatchesAir.dat' u 1:(\$2+\$3+\$4+\$5) "%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 4 t 'Ref. torque stationary'

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

set title 'Power'
set output '../postProcessing/report/power.png'
set xlabel 'time'
set ylabel 'Power [W]'
# set yrange[0:7]
set key width -4

plot '< paste ../postProcessing/forcesRotatingPatchesWater/0/forces.dat ../postProcessing/forcesRotatingPatchesAir/0/forces.dat' u 1:((\$2+\$3+\$4+\$5)*(-2*pi*OMEGA/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 1 t 'P input rotating' ,\
     '< paste ../postProcessing/forcesStationaryPatchesWater/0/forces.dat ../postProcessing/forcesStationaryPatchesAir/0/forces.dat' u 1:((\$2+\$3+\$4+\$5)*2*pi*OMEGA/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 'P input stationary' ,\
     '../postProcessing/volIntegrateEpsWater/0/volFieldValue.dat' u 1:(\$2)*RHOL w l ls 3 t 'P dissipated'

EOF

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