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

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_LFRACF_FILE = "experiment/liquidCrossSection_z_fit.dat"

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

PHIS = system("cd ".FT_PATH." && ls -1 filmThickness_z_*pi.dat | sed 's/filmThickness_z_\\\([0-9.]*\\\)pi.dat/\\\1/'")
NUM_PHI = words(PHIS)
PHI(n) = word(PHIS, n)

FT_Z_FILE(phi) = FT_PATH."filmThickness_z_".phi."pi.dat"
REF_FT_Z_FILE(phi) = "reference/".CASE."/filmThickness_z_".phi."pi.dat"
EXP_FT_Z_FILE(phi) = "experiment/filmThickness_z_".phi."pi.dat"
EXP_FTF_Z_FILE(phi) = "experiment/filmThickness_z_".phi."pi_fit.dat"

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-------------------------------*/

set output "../postProcessing/report/deltaP.png"
set xlabel "t (s)"
set ylabel "{/Symbol D} p (kPa/m)"
set yrange [0:40]
plot EXP_DP_FILE u     1:(\$2*1e-3)                   w l ls 2 t "Experiment"   , \
     REF_DP_FILE u (0.5):((\$1-\$2)/L*1e-3):(0.5):(0) w vectors nohead ls 3 t "Reference avg"    , \
     DP_FILE     u     1:((\$2-\$3)/L*1e-3)           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:20]

set output "../postProcessing/report/crossSectionalLiquidTotal.png"
set ylabel "{/symbol e}_{liquid} (%)"
plot EXP_LFRAC_FILE  u       1:(\$2*100) w l ls 2 lw 1 dt 1 lc rgb '#CCFF0000' t "Experiment"   , \
     EXP_LFRACF_FILE u       1:(\$2*100) w l ls 2      dt 1          t "cubic spline interp."   , \
     REF_LFRAC_FILE  u (\$1/D):(\$4*100) w l ls 3 t "Reference"  , \
     LFRAC_FILE      u (\$1/D):(\$4*100) w l ls 1 t "Simulation"

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

set output "../postProcessing/report/crossSectionalLiquidD.png"
set ylabel "{/symbol e}_{liquidD} (%)"
set yrange [0:1]
plot REF_LFRAC_FILE u (\$1/D):(\$3*100) w l ls 3 t "Reference"  , \
     LFRAC_FILE     u (\$1/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:1.2]
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 autoscale x
set format x "%g"

set xlabel "z/D (-)"
set xrange [:L/D]
do for [i=1:NUM_PHI]{
    set output "../postProcessing/report/filmThickness_z_".PHI(i)."pi.png"

    set title "{/symbol j} = ".PHI(i)." {/symbol p}"

    plot EXP_FT_Z_FILE(PHI(i))  u       1:2         w l ls 2 lw 1 dt 1 lc rgb '#CCFF0000' t "Experiment", \
         EXP_FTF_Z_FILE(PHI(i)) u       1:2         w l ls 2 dt 1 t "cubic spline interp."        , \
         REF_FT_Z_FILE(PHI(i))  u (\$1/D):(\$2*1e3) w l ls 3 t "Reference"   , \
         FT_Z_FILE(PHI(i))      u (\$1/D):(\$2*1e3) w l ls 1 t "Simulation"
}
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

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