#!/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--------------------------------

g       = 9.81
d       = 1.
nuL     = 0.098549

EPSILON = 0.001

REF     = "Reference"
REFA    = "Reference avg."
SIM     = "Simulation"

TIME    = system("foamListTimes -latestTime -processor -case ..")
TIME_RESIDUALS = system("ls ../postProcessing/residuals/ | tail -n1")

RFILE(s)= "reference/surface".s."/10/isoSurface.xy"
FILE(s) = "../postProcessing/surface".s."/".TIME."/isoSurface.xy"

HOLD_RFILE(s) = "reference/holdup".s."/0/volFieldValue_avg.csv"
HOLD_FILE(s) = "../postProcessing/holdup".s."/0/volFieldValue.dat"

LAYER_RFILE = "reference/layerAverage/10/layerAverage.csv"
LAYER_FILE = "../postProcessing/layerAverage/".TIME."/layerAverage.csv"

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

set terminal pngcairo
set grid
set key outside below

set style line 1 dt 2 lw 2   pt 6 ps 1   lc "black"
set style line 2 dt 2 lw 2   pt 7 ps 0.5 lc "black"
set style line 3      lw 1.5 pt 4 ps 1.2 lc "green"
set style line 4      lw 1.5 pt 5 ps 0.7 lc "green"

set style arrow 1 nohead lw 1.5 lc "green"

#-----------------------------------Holdup--------------------------------------

set xlabel "t (s)"
set yrange [0:]

set output "../postProcessing/report/holdupGasD.png"
set ylabel "{/symbol e}_{gasD} (%)"
plot HOLD_RFILE("GasD")    u (5):(\$1*100):(5):(0) w vectors as 1 t REFA    , \
     HOLD_FILE("GasD")     u   1:(\$2*100)         w l       ls 1 t SIM

set output "../postProcessing/report/holdupLiquidC.png"
set ylabel "{/symbol e}_{liquidC} (%)"
plot HOLD_RFILE("LiquidC") u (5):(\$1*100):(5):(0) w vectors as 1 t REFA    , \
     HOLD_FILE("LiquidC")  u   1:(\$2*100)         w l       ls 1 t SIM

set arrow arrowstyle 1
set output "../postProcessing/report/holdupLiquidD.png"
set ylabel "{/symbol e}_{liquidD} (%)"
plot HOLD_RFILE("LiquidD") u (5):(\$1*100):(5):(0) w vectors as 1 t REFA    , \
     HOLD_FILE("LiquidD")  u   1:(\$2*100)         w l       ls 1 t SIM

set autoscale xy

#---------------------------------Interface-YZ----------------------------------
set output '../postProcessing/report/interface_YZ.png'

set size ratio -1

X0 = -0.36
XPLANE = "(((\$1-X0)>=-EPSILON) && ((\$1-X0)<=EPSILON) ? \$2 : 1/0)"


set title   sprintf('t = %s s, x = %g m', TIME, X0)
set xlabel  'z (m)'
set xrange  [-0.15:0.15]
set ylabel  'y (m)'
set yrange  [0:]

plot RFILE('GasC')    u 3:@XPLANE w p ls 3 t REF.' - gasC'      , \
     RFILE('LiquidC') u 3:@XPLANE w p ls 4 t REF.' - liquidC'   , \
      FILE('GasC')    u 3:@XPLANE w p ls 1 t SIM.' - gasC'      , \
      FILE('LiquidC') u 3:@XPLANE w p ls 2 t SIM.' - liquidC'

#---------------------------------Interface-XY----------------------------------
set output '../postProcessing/report/interface_XY.png'

Z0 = 0
ZPLANE = "(((\$3-Z0)>=-EPSILON) && ((\$3-Z0)<=EPSILON) ? \$2 : 1/0)"

set title   sprintf('t = %s s, z = %g m', TIME, Z0)
set xlabel  'x (m)'
set xrange  [-0.5:-0.2]
plot RFILE('GasC')    u 1:@ZPLANE w p ls 3 t REF.' - gasC'      , \
     RFILE('LiquidC') u 1:@ZPLANE w p ls 4 t REF.' - liquidC'   , \
      FILE('GasC')    u 1:@ZPLANE w p ls 1 t SIM.' - gasC'      , \
      FILE('LiquidC') u 1:@ZPLANE w p ls 2 t SIM.' - liquidC'

set autoscale xy
unset title

set size ratio 0

#--------------------------------Layer-Average----------------------------------

set title   sprintf('t = %s s', TIME)
set xrange [0:]
set ylabel "y (m)"

set output "../postProcessing/report/layerAverageGasD.png"
set xlabel "<~r{.7-}_{ gasD}>_{xz} (%)"
plot LAYER_RFILE u (\$3*100):1 w l ls 3 t REF , \
     LAYER_FILE  u (\$3*100):1 w l ls 1 t SIM

set output "../postProcessing/report/layerAverageLiquidC.png"
set xlabel "<~r{.7-}_{ liquidC}>_{xz} (%)"
plot LAYER_RFILE u (\$4*100):1 w l ls 3 t REF , \
     LAYER_FILE  u (\$4*100):1 w l ls 1 t SIM

set output "../postProcessing/report/layerAverageLiquidD.png"
set xlabel "<~r{.7-}_{ liquidD}>_{xz} (%)"
plot LAYER_RFILE u (\$5*100):1 w l ls 3 t REF , \
     LAYER_FILE  u (\$5*100):1 w l ls 1 t SIM

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

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

set xlabel "t (s)"
set ylabel "residuals (-)"
set logscale y
set format y "10^{%.1T}"

plot RESI_FILE u 1:2 w l lc 1 t "p\_{rgh}"              , \
     RESI_FILE u 1:3 w l lc 2 t "k_{gasC}"              , \
     RESI_FILE u 1:4 w l lc 3 t "{/Symbol w}_{gasC}"    , \
     RESI_FILE u 1:5 w l lc 4 t "k_{liquidC}"           , \
     RESI_FILE u 1:6 w l lc 5 t "{/Symbol w}_{liquidC}"

EOF

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