#!/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 .. | tail -1")
CASE = system("foamDictionary ../caseParameterDict -entry case -value")
AVE_TIME = system("ls ../postProcessing/interfaceHeight | sort -g | tail -1")

FILE(x) = "../postProcessing/verticalLine_".x."_Mean/".TIME."/line.csv"
RFILE(x) = "reference/".CASE."/verticalLine_".x."_Mean/line.csv"
EFILE(phi, x) = "experiment/".CASE."/".phi."_".x."mm.dat"

YW_FILE = "../postProcessing/interfaceHeight/".AVE_TIME."/height.dat"
YW_RFILE = "reference/".CASE."/interfaceHeight/height_avg.csv"
YW_EFILE(x) = "experiment/".CASE."/liqLev_".x."mm.dat"

PRES_FILE = "../postProcessing/probes/0/p"
PRES_RFILE = "reference/".CASE."/probes/p_avg.csv"
PRES_EFILE = "experiment/".CASE."/DeltaP.dat"

DR_FILE = "<( paste ../postProcessing/waterIn/".AVE_TIME."/surfaceFieldValue.dat ../postProcessing/waterOutRight/".AVE_TIME."/surfaceFieldValue.dat ../postProcessing/waterOutLeft/".AVE_TIME."/surfaceFieldValue.dat )"
DR_RFILE = "<( paste reference/".CASE."/waterIn/surfaceFieldValue_avg.csv reference/".CASE."/waterOutRight/surfaceFieldValue_avg.csv reference/".CASE."/waterOutLeft/surfaceFieldValue_avg.csv )"
DR_EFILE = "experiment/".CASE."/DeliveryRate.dat"

RESI_FILE = "../postProcessing/residuals/0/residuals.dat"

set terminal pngcairo size 800,600
set grid
set key outside below

set style line 1 dt 2 lw 2        lc "black"
set style line 2      lw 1   pt 7 lc "red"
set style line 3      lw 1.5      lc "green"
set style fill transparent solid 0.1 noborder

#----------------------------Void fraction profile-----------------------------

set xlabel '{/Symbol a}_G (-)'
set xrange [-0.05:1.05]
set ylabel 'y (m)'
set yrange [0:0.03]

set output '../postProcessing/report/alpha_235.png'
set title "x = 235 mm"
plot EFILE("alpha", 235) u 2                        :1             w p ls 2 t 'Experiment'     , \
     RFILE(235)          u (column('alphaMean.air')):(column('y')) w l ls 3 t 'Reference'      , \
      FILE(235)          u (column('alphaMean.air')):(column('y')) w l ls 1 t 'Simulation'

set output '../postProcessing/report/alpha_380.png'
set title "x = 380 mm"
plot EFILE("alpha", 380) u 2                        :1             w p ls 2 t 'Experiment'     , \
     RFILE(380)          u (column('alphaMean.air')):(column('y')) w l ls 3 t 'Reference'      , \
      FILE(380)          u (column('alphaMean.air')):(column('y')) w l ls 1 t 'Simulation'

set autoscale x

#-------------------------Streamwise velocity profile--------------------------

set xlabel 'U_x (m/s)'
set yrange [0:0.09]

set output '../postProcessing/report/U_x_235.png'
set title "x = 235 mm"
plot EFILE("gasVel", 235) u 2                        :1             w p ls 2 pt 6 t 'Experiment: air     '    , \
     EFILE("liqVel", 235) u 2                        :1             w p ls 2      t 'water'                   , \
     RFILE(235)           u (column('UMean.air_x'))  :(column('y')) w l ls 3 lw 1 t 'Reference: air     '     , \
     RFILE(235)           u (column('UMean.water_x')):(column('y')) w l ls 3      t 'water'                   , \
      FILE(235)           u (column('UMean.air_x'))  :(column('y')) w l ls 1 lw 1 t 'Simulation: air     '    , \
      FILE(235)           u (column('UMean.water_x')):(column('y')) w l ls 1      t 'water'

set output '../postProcessing/report/U_x_380.png'
set title "x = 380 mm"
plot EFILE("gasVel", 380) u 2                        :1             w p ls 2 pt 6 t 'Experiment: air     '    , \
     EFILE("liqVel", 380) u 2                        :1             w p ls 2      t 'water'                   , \
     RFILE(380)           u (column('UMean.air_x'))  :(column('y')) w l ls 3 lw 1 t 'Reference: air     '     , \
     RFILE(380)           u (column('UMean.water_x')):(column('y')) w l ls 3      t 'water'                   , \
      FILE(380)           u (column('UMean.air_x'))  :(column('y')) w l ls 1 lw 1 t 'Simulation: air     '    , \
      FILE(380)           u (column('UMean.water_x')):(column('y')) w l ls 1      t 'water'

#--------------------------Vertical velocity profile---------------------------

set xlabel 'U_y (m/s)'

set output '../postProcessing/report/U_y_235.png'
set title "x = 235 mm"
plot EFILE("gasVel", 235) u 3:1                                     w p ls 2 pt 6 t 'Experiment: air     '    , \
     EFILE("liqVel", 235) u 3:1                                     w p ls 2      t 'water'                   , \
     RFILE(235)           u (column('UMean.air_y'))  :(column('y')) w l ls 3 lw 1 t 'Reference: air     '     , \
     RFILE(235)           u (column('UMean.water_y')):(column('y')) w l ls 3      t 'water'                   , \
      FILE(235)           u (column('UMean.air_y'))  :(column('y')) w l ls 1 lw 1 t 'Simulation: air     '    , \
      FILE(235)           u (column('UMean.water_y')):(column('y')) w l ls 1      t 'water'

set output '../postProcessing/report/U_y_380.png'
set title "x = 380 mm"
plot EFILE("gasVel", 380) u 3:1                                     w p ls 2 pt 6 t 'Experiment: air     '    , \
     EFILE("liqVel", 380) u 3:1                                     w p ls 2      t 'water'                   , \
     RFILE(380)           u (column('UMean.air_y'))  :(column('y')) w l ls 3 lw 1 t 'Reference: air     '     , \
     RFILE(380)           u (column('UMean.water_y')):(column('y')) w l ls 3      t 'water'                   , \
      FILE(380)           u (column('UMean.air_y'))  :(column('y')) w l ls 1 lw 1 t 'Simulation: air     '    , \
      FILE(380)           u (column('UMean.water_y')):(column('y')) w l ls 1      t 'water'

#----------------------Turbulence kinetic energy profile-----------------------

set xlabel 'k (m^2/s^2)'

set logscale x
set format x '10^{%T}'

set output '../postProcessing/report/k_235.png'
set title "x = 235 mm"
plot EFILE("gasKe", 235) u 4:1                                   w p ls 2 pt 6 t 'Experiment: air     '    , \
     EFILE("liqKe", 235) u 4:1                                   w p ls 2      t 'water'                   , \
     RFILE(235)          u (column('kMean.air'))  :(column('y')) w l ls 3 lw 1 t 'Reference: air     '     , \
     RFILE(235)          u (column('kMean.water')):(column('y')) w l ls 3      t 'water'                   , \
      FILE(235)          u (column('kMean.air'))  :(column('y')) w l ls 1 lw 1 t 'Simulation: air     '    , \
      FILE(235)          u (column('kMean.water')):(column('y')) w l ls 1      t 'water'

set output '../postProcessing/report/k_380.png'
set title "x = 380 mm"
plot EFILE("gasKe", 380) u 4:1                                   w p ls 2 pt 6 t 'Experiment: air     '    , \
     EFILE("liqKe", 380) u 4:1                                   w p ls 2      t 'water'                   , \
     RFILE(380)          u (column('kMean.air'))  :(column('y')) w l ls 3 lw 1 t 'Reference: air     '     , \
     RFILE(380)          u (column('kMean.water')):(column('y')) w l ls 3      t 'water'                   , \
      FILE(380)          u (column('kMean.air'))  :(column('y')) w l ls 1 lw 1 t 'Simulation: air     '    , \
      FILE(380)          u (column('kMean.water')):(column('y')) w l ls 1      t 'water'

unset logscale x
set format x
set autoscale y

#---------------------------------Water level----------------------------------

set xlabel 't (s)'
set ylabel 'y_{water} (m)'
set xrange [AVE_TIME+0:]
set yrange [0:]

set output '../postProcessing/report/yWater_235.png'
set title "x = 235 mm"
plot YW_EFILE(235) u 1:2                           w l               ls 2 t 'Experiment {/Symbol \261} wave amplitude'  , \
     YW_EFILE(235) u 1:(\$2-0.5*\$3):(\$2+0.5*\$3) w filledcurves fs ls 2 notitle                                       , \
     YW_RFILE      u (10):(\$1):(10):(0)           w vectors nohead  ls 3 t 'Reference avg'                             ,\
     YW_FILE       u 1:2                           w l               ls 1 t 'Simulation'

set output '../postProcessing/report/yWater_380.png'
set title "x = 380 mm"
plot YW_EFILE(380) u 1:2                           w l               ls 2 t 'Experiment {/Symbol \261} wave amplitude'  , \
     YW_EFILE(380) u 1:(\$2-0.5*\$3):(\$2+0.5*\$3) w filledcurves fs ls 2 notitle                                       , \
     YW_RFILE      u (10):(\$3):(10):(0)           w vectors nohead  ls 3 t 'Reference avg'                             ,\
     YW_FILE       u 1:4                           w l               ls 1 t 'Simulation'

unset title

#-----------------------------Flow reversal rate-------------------------------

set ylabel 'Flow Reversal Rate (-)'
set yrange [-0.05:1.05]

set output '../postProcessing/report/FRR.png'
plot DR_EFILE u 1:(1-\$2)                w l              ls 2 t 'Experiment'       , \
     DR_RFILE u (10):(-\$3/\$1):(10):(0) w vectors nohead ls 3 t 'Reference avg'    , \
     DR_FILE  u 1:(-\$6/\$2)             w l              ls 1 t 'Simulation'

set autoscale y

#--------------------------------Pressure drop---------------------------------

set xrange [2:]
set ylabel '{/Symbol D}_p (Pa)'
set yrange [0:]

set output '../postProcessing/report/DeltaP.png'
plot PRES_EFILE u 1:2                     w l               ls 2 t 'Experiment {/Symbol \261} err.' , \
     PRES_EFILE u 1:(\$2-\$3):(\$2+\$3)   w filledcurves fs ls 2 notitle                            , \
     PRES_RFILE u (10):(\$2-\$1):(10):(0) w vectors nohead  ls 3 t 'Reference avg'                  , \
     PRES_FILE  u 1:(\$3-\$2)             w l               ls 1 t 'Simulation'

set autoscale xy

#----------------------------------Residuals-----------------------------------

set ylabel 'residuals (-)'
set logscale y
set format y '10^{%T}'

set output '../postProcessing/report/residuals.png'
plot RESI_FILE u 1:2 w l t "Simulation p\_{rgh}"    , \
     RESI_FILE u 1:3 w l t "Simulation k.water"     , \
     RESI_FILE u 1:4 w l t "Simulation omega.water" , \
     RESI_FILE u 1:5 w l t "Simulation k.air"       , \
     RESI_FILE u 1:6 w l t "Simulation omega.air"

EOF

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