#!/bin/bash

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

gnuplot<<EOF

D = 0.014
L = 1.4
zO = 0.4

TIME = system("foamListTimes -functionEntries -latestTime -case .. | tail -1")
TIME_DP = system("ls ../postProcessing/pressureProbes/ | tail -n1")
TIME_HOLD = system("ls ../postProcessing/holdupLiquidC/ | tail -n1")
TIME_LFRAC = system("ls ../postProcessing/liquidCrossSection/ | tail -n1")
TIME_RESIDUALS = system("ls ../postProcessing/residuals/ | tail -n1")
CASE = system("foamDictionary ../caseParameterDict -entry case -value")

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

DP_FILE = "../postProcessing/pressureProbes/".TIME_DP."/p"
REF_DP_FILE = "reference/".CASE."/p_avg.csv"
EXP_DP_FILE = "experiment/pressureDrop.dat"

HOLD_FILE(p) = "../postProcessing/holdup".p."/".TIME_HOLD."/volFieldValue.dat"
REF_HOLD_FILE(p) = "reference/".CASE."/holdup".p."_avg.csv"

LFRAC_FILE = "../postProcessing/liquidCrossSection/".TIME_LFRAC."/layerAverage.xy"
REF_LFRAC_FILE = "reference/".CASE."/layerAverage.xy"
EXP_LFRAC_FILE = "experiment/liquidCrossSection_z.dat"
EXP_LFRACC_FILE = "experiment/liquidCCrossSection_z.dat"
EXP_LFRACD_FILE = "experiment/liquidDCrossSection_z.dat"

FT_PATH = "../postProcessing/interfaceMean/".TIME."/"

ZS = system("cd ".FT_PATH." && ls -1 filmThickness_phi_*D.dat | sed 's/filmThickness_phi_\\\([0-9.-]*\\\)D.dat/\\\1/'")
NUM_Z = words(ZS)
Z(n) = word(ZS, n)

FT_PHI_FILE(z) = FT_PATH."filmThickness_phi_".z."D.dat"
REF_FT_PHI_FILE(z) = "reference/".CASE."/filmThickness_phi_".z."D.dat"
EXP_FT_PHI_FILE(z) = "experiment/filmThickness_phi_".z."D.dat"



set terminal pngcairo
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 pt 4 lc "green"
set style line 4 dt 2 lw 2        lc "violet"
set style line 5 dt 2 lw 2        lc "cyan"
set style fill transparent solid 0.1 noborder

#\*-------------------------------Pressure Drop-------------------------------*/

z1 = 0
z2 = 0.3
z3 = 0.64
z4 = 1.16

A1 = ( 1-(zO-z1)/(z2-z1))*1e-3
A2 = (   (zO-z1)/(z2-z1))*1e-3
A3 = (-1+(zO-z3)/(z4-z3))*1e-3
A4 = (  -(zO-z3)/(z4-z3))*1e-3

set output "../postProcessing/report/deltaP.png"
set xlabel "t (s)"
set ylabel "{/Symbol D} p_O (kPa)"
set yrange [0:20]
plot EXP_DP_FILE u     1:(\$2*1e-3)                                w l               ls 2 t "Experiment"    , \
     EXP_DP_FILE u     1:((\$2-0.5*\$3)*1e-3):((\$2+0.5*\$3)*1e-3) w filledcurves fs ls 2 notitle           , \
     REF_DP_FILE u (0.5):(A1*\$1+A2*\$2+A3*\$3+A4*\$4):(0.5):(0)   w vectors nohead  ls 3 t "Reference avg" , \
     DP_FILE     u     1:(A1*\$2+A2*\$3+A3*\$4+A4*\$5)             w l               ls 1 t "Simulation"

set autoscale y

#\*----------------------------------Holdup-----------------------------------*/

set output "../postProcessing/report/holdupLiquidC.png"
set ylabel "{/symbol e}_{liquidC} (%)"
set yrange [0:14]
plot REF_HOLD_FILE("LiquidC") u (0.5):(\$1*100):(0.5):(0) w vectors nohead ls 3 t "Reference avg"  , \
     HOLD_FILE("LiquidC")     u     1:(\$2*100)           w l              ls 1 t "Simulation"

set output "../postProcessing/report/holdupLiquidD.png"
set ylabel "{/symbol e}_{liquidD} (%)"
set autoscale y
set yrange [0:1]
plot REF_HOLD_FILE("LiquidD") u (0.5):(\$1*100):(0.5):(0) w vectors nohead ls 3 t "Reference avg"  , \
     HOLD_FILE("LiquidD")     u     1:(\$2*100)           w l              ls 1 t "Simulation"
set autoscale xy

#\*------------------------Liquid-Volume-Fraction-----------------------------*/

set xlabel "z/D (-)"
set xrange [:L/D]
set yrange [0:40]

set output "../postProcessing/report/crossSectionalLiquidTotal.png"
set ylabel "{/symbol e}_{liquid} (%)"
plot EXP_LFRAC_FILE  u             1:(\$2*100) w l ls 2 t "Experiment"  , \
     REF_LFRAC_FILE  u ((\$1-zO)/D):(\$4*100)  w l ls 3 t "Reference"   , \
     LFRAC_FILE      u ((\$1-zO)/D):(\$4*100)  w l ls 1 t "Simulation"

set output "../postProcessing/report/crossSectionalLiquidC.png"
set ylabel "{/symbol e}_{liquidC} (%)"
plot EXP_LFRACC_FILE  u            1:(\$2*100) w l ls 2 t "Experiment"  , \
     REF_LFRAC_FILE   u ((\$1-zO)/D):(\$2*100) w l ls 3 t "Reference"   , \
     LFRAC_FILE       u ((\$1-zO)/D):(\$2*100) w l ls 1 t "Simulation"

set output "../postProcessing/report/crossSectionalLiquidD.png"
set ylabel "{/symbol e}_{liquidD} (%)"
set yrange [0:10]
plot EXP_LFRACD_FILE  u            1:(\$2*100) w l ls 2 t "Experiment"  , \
     REF_LFRAC_FILE   u ((\$1-zO)/D):(\$3*100) w l ls 3 t "Reference"   , \
     LFRAC_FILE       u ((\$1-zO)/D):(\$3*100) w l ls 1 t "Simulation"

#\*-----------------------------Film-Thickness--------------------------------*/

set xlabel "{/symbol j} (rad)"
set xrange [0:2]
set format x "%g {/symbol p}"
set ylabel "~h{1.4\\\_}   (mm)"
set yrange [0:2.3]
do for [i=1:NUM_Z]{
    set output "../postProcessing/report/filmThickness_phi_".Z(i)."D.png"

    set title "z = ".Z(i)." D"

    plot EXP_FT_PHI_FILE(Z(i)) u 1:2         w p ls 2 t "Experiment" , \
         REF_FT_PHI_FILE(Z(i)) u 1:(\$2*1e3) w l ls 3 t "Reference"  , \
         FT_PHI_FILE(Z(i))     u 1:(\$2*1e3) w l ls 1 t "Simulation"
}
set format x "%g"
unset title
set autoscale xy

#\*--------------------------------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 t "Simulation p\_{rgh}"       , \
     RESI_FILE u 1:3 w l t "Simulation k.gasC"         , \
     RESI_FILE u 1:4 w l t "Simulation k.liquidC"      , \
     RESI_FILE u 1:5 w l t "Simulation omega.gasC"     , \
     RESI_FILE u 1:6 w l t "Simulation omega.liquidC"

EOF

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