#!/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 size 550,550
set grid
set key box

set style line 1 lt 1 lw 3 pt 2 lc "black"
set style line 2 lt 1 lw 3 pt 2 lc "red"
set style line 3 lt 1 lw 3 pt 2 lc "blue"
set style line 4 lt 1 lw 3 pt 2 lc "green"
set style line 5 lt 1 lw 3 pt 2 lc "magenta"
set style line 6 lt 1 lw 3 pt 2 lc "cyan"
set style line 7 dt 2 lw 3 pt 2 lc "forest-green"
set style line 8 lt 1 lw 3 pt 2 lc "violet"

#\*----------------------Convergence points Liquid-----------------------------*/
#
#set output '../postProcessing/report/Monitor_points_liquid.png'
#set xlabel "Time"
#set ylabel "u_{L} (m/s)"
#set key width -3
#set yrange [0:1.8]
#
#plot '../postProcessing/probesUWaterTP/0/U.water' u 1:((\$2**2)+(\$3**2)+(\$4**2))**(0.5) "%lf (%lf %lf %lf) (%*lf %*lf %*lf) (%*lf %*lf %*lf) " title 'Near Stirrer (ins)' w l ls 1, \
#     '../postProcessing/probesUWaterTP/'.TIME.'/UMean.water' u 1:((\$2**2)+(\$3**2)+(\$4**2))**(0.5) "%lf (%lf %lf %lf) (%*lf %*lf %*lf) (%*lf %*lf %*lf) " title 'Near Stirrer (avg)' w l ls 2, \
#     '../postProcessing/probesUWaterTP/0/U.water' u 1:((\$2**2)+(\$3**2)+(\$4**2))**(0.5) "%lf (%*lf %*lf %*lf) (%lf %lf %lf) (%*lf %*lf %*lf) " title 'Away from Stirrer (ins)' w l ls 3, \
#     '../postProcessing/probesUWaterTP/'.TIME.'/UMean.water' u 1:((\$2**2)+(\$3**2)+(\$4**2))**(0.5) "%lf (%*lf %*lf %*lf) (%lf %lf %lf) (%*lf %*lf %*lf) " title 'Away from Stirrer (avg)' w l ls 4, \
#     '../postProcessing/probesUWaterTP/0/U.water' u 1:((\$2**2)+(\$3**2)+(\$4**2))**(0.5) "%lf (%*lf %*lf %*lf) (%*lf %*lf %*lf) (%lf %lf %lf) " title 'Near Sparger (ins)' w l ls 5, \
#     '../postProcessing/probesUWaterTP/'.TIME.'/UMean.water' u 1:((\$2**2)+(\$3**2)+(\$4**2))**(0.5) "%lf (%*lf %*lf %*lf) (%*lf %*lf %*lf) (%lf %lf %lf) " title 'Near Sparger (avg)' w l ls 6
#
##\*-----------------------Convergence points Gas-------------------------------*/
#
#set output '../postProcessing/report/Monitor_points_gas.png'
#set xlabel "Time"
#set ylabel "u_{G} (m/s)"
#set key width -3
#
#plot '../postProcessing/probesUWaterTP/0/U.air' u 1:((\$2**2)+(\$3**2)+(\$4**2))**(0.5) "%lf (%lf %lf %lf) (%*lf %*lf %*lf) (%*lf %*lf %*lf) " title 'Near Stirrer (ins)' w l ls 1, \
#     '../postProcessing/probesUWaterTP/'.TIME.'/UMean.air' u 1:((\$2**2)+(\$3**2)+(\$4**2))**(0.5) "%lf (%lf %lf %lf) (%*lf %*lf %*lf) (%*lf %*lf %*lf) " title 'Near Stirrer (avg)' w l ls 2, \
#     '../postProcessing/probesUWaterTP/0/U.air' u 1:((\$2**2)+(\$3**2)+(\$4**2))**(0.5) "%lf (%*lf %*lf %*lf) (%lf %lf %lf) (%*lf %*lf %*lf) " title 'Away from Stirrer (ins)' w l ls 3, \
#     '../postProcessing/probesUWaterTP/'.TIME.'/UMean.air' u 1:((\$2**2)+(\$3**2)+(\$4**2))**(0.5) "%lf (%*lf %*lf %*lf) (%lf %lf %lf) (%*lf %*lf %*lf) " title 'Away from Stirrer (avg)' w l ls 4, \
#     '../postProcessing/probesUWaterTP/0/U.air' u 1:((\$2**2)+(\$3**2)+(\$4**2))**(0.5) "%lf (%*lf %*lf %*lf) (%*lf %*lf %*lf) (%lf %lf %lf) " title 'Near Sparger (ins)' w l ls 5, \
#     '../postProcessing/probesUWaterTP/'.TIME.'/UMean.air' u 1:((\$2**2)+(\$3**2)+(\$4**2))**(0.5) "%lf (%*lf %*lf %*lf) (%*lf %*lf %*lf) (%lf %lf %lf) " title 'Near Sparger (avg)' w l ls 6
#
#EOF
#
#gnuplot<<EOF

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

set term pngcairo size 550,550
set grid
set key box
set key width 1

set style line 1 lt 2 lw 2 pt 2 lc "blue"
set style line 2 lt 2 lw 2 pt 2 lc "red"
set style line 3 lt 3 lw 2 pt 2 lc "forest-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"
set style line 6 lt 5 lw 2 pt 2 lc "orange"
set style line 7 lt 5 lw 2 pt 2 lc "grey"

set xlabel 'time'
set logscale y
set format y '%.0e'
set yrange [1e-6:10]

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

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

 plot '../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}',\
     'reference/'.CASE.'/residuals.dat' u 1:2 w l ls 4 t 'Ref. p',\
     '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}'

EOF

gnuplot<<EOF

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

set term pngcairo size 550,550
set grid
set key box

set style line 1 lt 2 lw 3 pt 2 lc "blue"
set style line 2 dt 2 lw 3 pt 2 lc "red"
set style line 3 lt 3 lw 3 pt 2 lc "forest-green"
set style line 4 dt 3 lw 3 pt 2 lc "black"
set style line 5 lt 5 lw 3 pt 2 lc "cyan"
set style line 6 lt 5 lw 3 pt 2 lc "orange"
set style line 7 lt 5 lw 3 pt 2 lc "grey"

set xlabel 'time'
set ylabel 'torque [Nm]'
set yrange[-0.6:0.8]
set key width -4

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

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

plot '../postProcessing/forcesRotatingPatchesWater/0/forces.dat' u 1:(\$4+\$7) "%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)*(-1) "%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 output '../torqueTotal.png'
set key width -3
set yrange[0:0.3]

plot '< paste ../postProcessing/forcesRotatingPatchesWater/0/forces.dat ../postProcessing/forcesRotatingPatchesAir/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 1 t 'torque rotating' ,\
     '< paste ../postProcessing/forcesStationaryPatchesWater/0/forces.dat ../postProcessing/forcesStationaryPatchesAir/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 2 t 'torque stationary'

EOF

gnuplot<<EOF

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

volume = 1.0323e-2 #domain volume

set term pngcairo size 550,550
set grid
set key box

set style line 1 lt 2 lw 3 pt 2 lc "blue"
set style line 2 lt 2 lw 3 pt 2 lc "red"
set style line 3 lt 3 lw 3 pt 2 lc "forest-green"
set style line 4 lt 4 lw 3 pt 2 lc "violet"
set style line 5 lt 5 lw 3 pt 2 lc "cyan"
set style line 6 lt 5 lw 3 pt 2 lc "orange"
set style line 7 lt 5 lw 3 pt 2 lc "grey"

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

set output '../postProcessing/report/volIntegrateAlphaAir.png'
set xlabel 'time'
set ylabel 'gas holdup [-]' offset 1,0
set yrange[0:0.01]
set lmargin 10

plot '../postProcessing/volIntegrateAlphaAir/0/volFieldValue.dat' u 1:(\$2)/volume w l ls 2 t 'holdup'

EOF

gnuplot<<EOF

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

set term pngcairo size 550,550
set grid
set key box

set style line 1 lt 2 lw 2 pt 2 lc "blue"
set style line 2 lt 2 lw 2 pt 2 lc "red"
set style line 3 lt 3 lw 2 pt 2 lc "forest-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"
set style line 6 lt 5 lw 2 pt 2 lc "orange"
set style line 7 lt 5 lw 2 pt 2 lc "grey"

set xlabel 'time'
set ylabel 'power input/dissipated [W]'
set yrange[0:6]

#\*--------------------Dissipated power vs power input------------------------*/

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

plot '../postProcessing/volIntegrateEpsWater/0/volFieldValue.dat' u 1:(\$2*996) w l ls 1 t 'dissipated',\
     '< paste ../postProcessing/forcesRotatingPatchesWater/0/forces.dat ../postProcessing/forcesRotatingPatchesAir/0/forces.dat' u 1:(\$2+\$3+\$4+\$5)*42 "%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 input'

EOF

gnuplot<<EOF

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

TIME = system("foamListTimes -latestTime -case ..")
set term pngcairo size 550,550
set grid
set key box

set style line 1 lt 2 lw 3 pt 2 lc "blue"
set style line 2 lt 2 lw 2 pt 2 lc "red"
set style line 3 lt 3 lw 2 pt 2 lc "forest-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"
set style line 6 lt 5 lw 2 pt 2 lc "orange"
set style line 7 lt 5 lw 2 pt 2 lc "grey"

set xlabel 'time'
set ylabel 'degassing mass flow rate [kg/s]' offset 1.5,0
set xrange[0:TIME]
set yrange[0:5e-6]
set lmargin 11

#\*--------------------Dissipated power vs power input------------------------*/

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

plot '../postProcessing/volIntegrateDegassingSp/0.0001/volFieldValue.dat' u 1:(-\$2*1.164) w l ls 1 t 'degassing mass flow rate'

EOF

gnuplot<<EOF

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

TIME = system("foamListTimes -latestTime -case ..")
CASE = system("foamDictionary ../caseParameterDict -entry case -value")

set term pngcairo size 650,650
set grid
set key box

set key font "Calibri,16"
set xtics font "Calibri,16"
set ytics font "Calibri,16"

set style line 1 lt 1 lw 3 pt 2 lc "red"
set style line 2 dt 2 lw 3 pt 2 lc "black"
set style line 3 dt 3 lw 3 pt 2 lc "blue"
set style line 4 dt 4 lw 3 pt 2 lc "forest-green"
set style line 5 dt 5 lw 3 pt 2 lc "magenta"
set style line 6 lt 6 lw 3 pt 2 lc "cyan"

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

#-----------------------------------Radial-Z0.52-----------------------------------#

set output '../postProcessing/report/Z0.52_U_Gy.png'
set xlabel 'z/H (-)' font "Calibri,16"
set ylabel 'u_{Gr}/u_{tip} (-)' font "Calibri,16"
set xrange [0:1]
set yrange [-0.2:1]
set key spacing 1.2

plot  '../postProcessing/Z0.52/'.TIME.'/line.csv' u (\$1/0.236):(\$12/1.85) t 'Simulation' w lines ls 1,\
      'CFX/'.CASE.'/Z0.52/U_Gy.dat' u 1:2 t 'CFX' w lines ls 2,\
      'reference/'.CASE.'/Z0.52/line.csv' u (\$1/0.236):(\$12/1.85) t 'Reference' w lines ls 3,\
      'data/'.CASE.'/Z0.52/U_Gy.dat' u 1:2 pt 5 ps 1 lc 8 t "Experiment",\

#-----------------------------------Radial-Z0.50-----------------------------------#

set output '../postProcessing/report/Z0.50_U_Ly.png'
set ylabel 'u_{Lr}/u_{tip} (-)' font "Calibri,16"

plot  '../postProcessing/Z0.50/'.TIME.'/line.csv' u (\$1/0.236):(\$9/1.85) t 'Simulation' w lines ls 1,\
      'CFX/'.CASE.'/Z0.50/U_Ly.dat' u 1:2 t 'CFX' w lines ls 2,\
      'reference/'.CASE.'/Z0.50/line.csv' u (\$1/0.236):(\$9/1.85) t 'Reference' w lines ls 3,\
      'data/'.CASE.'/Z0.50/U_Ly.dat' u 1:2 pt 5 ps 1 lc 8 t "Experiment",\

#-----------------------------------Radial-Z0.85-----------------------------------#

set output '../postProcessing/report/Z0.85_U_Gy.png'
set ylabel 'u_{Gr}/u_{tip} (-)' font "Calibri,16"
set yrange [-0.2:0.4]

plot  '../postProcessing/Z0.85/'.TIME.'/line.csv' u (\$1/0.236):(\$12/1.85) t 'Simulation' w lines ls 1,\
      'CFX/'.CASE.'/Z0.85/U_Gy.dat' u 1:2 t 'CFX' w lines ls 2,\
      'reference/'.CASE.'/Z0.85/line.csv' u (\$1/0.236):(\$12/1.85) t 'Reference' w lines ls 3,\
      'data/'.CASE.'/Z0.85/U_Gy.dat' u 1:2 pt 5 ps 1 lc 8 t "Experiment",\

#-----------------------------------Radial-Z0.85-----------------------------------#

set output '../postProcessing/report/Z0.85_U_Ly.png'
set ylabel 'u_{Lr}/u_{tip} (-)' font "Calibri,16"

plot  '../postProcessing/Z0.85/'.TIME.'/line.csv' u (\$1/0.236):(\$9/1.85) t 'Simulation' w lines ls 1,\
      'CFX/'.CASE.'/Z0.85/U_Ly.dat' u 1:2 t 'CFX' w lines ls 2,\
      'reference/'.CASE.'/Z0.85/line.csv' u (\$1/0.236):(\$9/1.85) t 'Reference' w lines ls 3,\
      'data/'.CASE.'/Z0.85/U_Ly.dat' u 1:2 pt 5 ps 1 lc 8 t "Experiment",\

#-----------------------------------Radial-Z0.92-----------------------------------#

set output '../postProcessing/report/Z0.92_U_Gy.png'
set ylabel 'u_{Gr}/u_{tip} (-)' font "Calibri,16"
set yrange [-0.1:0.25]

plot  '../postProcessing/Z0.92/'.TIME.'/line.csv' u (\$1/0.236):(\$12/1.85) t 'Simulation' w lines ls 1,\
      'CFX/'.CASE.'/Z0.92/U_Gy.dat' u 1:2 t 'CFX' w lines ls 2,\
      'reference/'.CASE.'/Z0.92/line.csv' u (\$1/0.236):(\$12/1.85) t 'Reference' w lines ls 3,\
      'data/'.CASE.'/Z0.92/U_Gy.dat' u 1:2 pt 5 ps 1 lc 8 t "Experiment",\

#-----------------------------------Radial-Z0.92-----------------------------------#

set output '../postProcessing/report/Z0.92_U_Ly.png'
set ylabel 'u_{Lr}/u_{tip} (-)' font "Calibri,16"

plot  '../postProcessing/Z0.92/'.TIME.'/line.csv' u (\$1/0.236):(\$9/1.85) t 'Simulation' w lines ls 1,\
      'CFX/'.CASE.'/Z0.92/U_Ly.dat' u 1:2 t 'CFX' w lines ls 2,\
      'reference/'.CASE.'/Z0.92/line.csv' u (\$1/0.236):(\$9/1.85) t 'Reference' w lines ls 3,\
      'data/'.CASE.'/Z0.92/U_Ly.dat' u 1:2 pt 5 ps 1 lc 8 t "Experiment",\

#-----------------------------------Axial-Z0.52-----------------------------------#

set output '../postProcessing/report/Z0.52_U_Gz.png'
set ylabel 'u_{Gz}/u_{tip} (-)' font "Calibri,16"
set xrange [0:1]
set yrange [-0.2:0.45]
set key spacing 1.2

plot  '../postProcessing/Z0.52/'.TIME.'/line.csv' u (\$1/0.236):(\$13/1.85) t 'Simulation' w lines ls 1,\
      'CFX/'.CASE.'/Z0.52/U_Gz.dat' u 1:2 t 'CFX' w lines ls 2,\
      'reference/'.CASE.'/Z0.52/line.csv' u (\$1/0.236):(\$13/1.85) t 'Reference' w lines ls 3,\
      'data/'.CASE.'/Z0.52/U_Gz.dat' u 1:2 pt 5 ps 1 lc 8 t "Experiment",\

#-----------------------------------Axial-Z0.50-----------------------------------#

set output '../postProcessing/report/Z0.50_U_Lz.png'
set ylabel 'u_{Lz}/u_{tip} (-)' font "Calibri,16"

plot  '../postProcessing/Z0.50/'.TIME.'/line.csv' u (\$1/0.236):(\$10/1.85) t 'Simulation' w lines ls 1,\
      'CFX/'.CASE.'/Z0.50/U_Lz.dat' u 1:2 t 'CFX' w lines ls 2,\
      'reference/'.CASE.'/Z0.50/line.csv' u (\$1/0.236):(\$10/1.85) t 'Reference' w lines ls 3,\
      'data/'.CASE.'/Z0.50/U_Lz.dat' u 1:2 pt 5 ps 1 lc 8 t "Experiment",\

#-----------------------------------Axial-Z0.85-----------------------------------#

set output '../postProcessing/report/Z0.85_U_Gz.png'
set ylabel 'u_{Gz}/u_{tip} (-)' font "Calibri,16"
set yrange [-0.2:0.4]

plot  '../postProcessing/Z0.85/'.TIME.'/line.csv' u (\$1/0.236):(\$13/1.85) t 'Simulation' w lines ls 1,\
      'CFX/'.CASE.'/Z0.85/U_Gz.dat' u 1:2 t 'CFX' w lines ls 2,\
      'reference/'.CASE.'/Z0.85/line.csv' u (\$1/0.236):(\$13/1.85) t 'Reference' w lines ls 3,\
      'data/'.CASE.'/Z0.85/U_Gz.dat' u 1:2 pt 5 ps 1 lc 8 t "Experiment",\

#-----------------------------------Axial-Z0.85-----------------------------------#

set output '../postProcessing/report/Z0.85_U_Lz.png'
set ylabel 'u_{Lz}/u_{tip} (-)' font "Calibri,16"

plot  '../postProcessing/Z0.85/'.TIME.'/line.csv' u (\$1/0.236):(\$10/1.85) t 'Simulation' w lines ls 1,\
      'CFX/'.CASE.'/Z0.85/U_Lz.dat' u 1:2 t 'CFX' w lines ls 2,\
      'reference/'.CASE.'/Z0.85/line.csv' u (\$1/0.236):(\$10/1.85) t 'Reference' w lines ls 3,\
      'data/'.CASE.'/Z0.85/U_Lz.dat' u 1:2 pt 5 ps 1 lc 8 t "Experiment",\

#-----------------------------------Axial-Z0.92-----------------------------------#

set output '../postProcessing/report/Z0.92_U_Gz.png'
set ylabel 'u_{Gz}/u_{tip} (-)' font "Calibri,16"
set yrange [-0.3:0.6]

plot  '../postProcessing/Z0.92/'.TIME.'/line.csv' u (\$1/0.236):(\$13/1.85) t 'Simulation' w lines ls 1,\
      'CFX/'.CASE.'/Z0.92/U_Gz.dat' u 1:2 t 'CFX' w lines ls 2,\
      'reference/'.CASE.'/Z0.92/line.csv' u (\$1/0.236):(\$13/1.85) t 'Reference' w lines ls 3,\
      'data/'.CASE.'/Z0.92/U_Gz.dat' u 1:2 pt 5 ps 1 lc 8 t "Experiment",\

#-----------------------------------Axial-Z0.92-----------------------------------#

set output '../postProcessing/report/Z0.92_U_Lz.png'
set ylabel 'u_{Lz}/u_{tip} (-)' font "Calibri,16"

plot  '../postProcessing/Z0.92/'.TIME.'/line.csv' u (\$1/0.236):(\$10/1.85) t 'Simulation' w lines ls 1,\
      'CFX/'.CASE.'/Z0.92/U_Lz.dat' u 1:2 t 'CFX' w lines ls 2,\
      'reference/'.CASE.'/Z0.92/line.csv' u (\$1/0.236):(\$10/1.85) t 'Reference' w lines ls 3,\
      'data/'.CASE.'/Z0.92/U_Lz.dat' u 1:2 pt 5 ps 1 lc 8 t "Experiment",\

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

EOF