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

GRPH_FILE =  "../postProcessing/graphMean/".TIME."/line.csv"
GRPH_RFILE = "reference/".CASE."/line.csv"
GRPH_EFILE(phi) = "experiment/".CASE."/".phi.".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 output '../postProcessing/report/alpha.png'
plot GRPH_RFILE u (column("alphaMean.air")):(column("y")) w l ls 3 t "Reference"    , \
     GRPH_FILE  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 output '../postProcessing/report/U_x.png'
plot GRPH_EFILE("gasVel")   u 2                        :1             w p ls 2 pt 6 t "Experiment: air" , \
     GRPH_EFILE("liqVel_x") u 2                        :1             w p ls 2      t "water"           , \
     GRPH_RFILE             u (column("UMean.air_x")  ):(column("y")) w l ls 3 lw 1 t "Reference: air"  , \
     GRPH_RFILE             u (column("UMean.water_x")):(column("y")) w l ls 3      t "water"           , \
     GRPH_FILE              u (column("UMean.air_x")  ):(column("y")) w l ls 1 lw 1 t "Simulation: air" , \
     GRPH_FILE              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.png'
plot GRPH_EFILE("gasVel")   u 3                        :1             w p ls 2 pt 6 t "Experiment: air" , \
     GRPH_EFILE("liqVel_y") u 2                        :1             w p ls 2      t "water"           , \
     GRPH_RFILE             u (column("UMean.air_y")  ):(column("y")) w l ls 3 lw 1 t "Reference: air"  , \
     GRPH_RFILE             u (column("UMean.water_y")):(column("y")) w l ls 3      t "water"           , \
     GRPH_FILE              u (column("UMean.air_y")  ):(column("y")) w l ls 1 lw 1 t "Simulation: air" , \
     GRPH_FILE              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.png'
plot GRPH_EFILE("gasKe") u 4                      :1             w p ls 2 pt 6 t "Experiment: air"  , \
     GRPH_EFILE("liqKe") u 3                      :1             w p ls 2      t "water"            , \
     GRPH_RFILE          u (column("kMean.air")  ):(column("y")) w l ls 3 lw 1 t "Reference: air"   , \
     GRPH_RFILE          u (column("kMean.water")):(column("y")) w l ls 3      t "water"            , \
     GRPH_FILE           u (column("kMean.air")  ):(column("y")) w l ls 1 lw 1 t "Simulation: air"  , \
     GRPH_FILE           u (column("kMean.water")):(column("y")) w l ls 1      t "water"

unset logscale x
set format x

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

set xlabel 't (s)'
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

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