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

LATEST_TIME = system("foamListTimes -functionEntries -latestTime -case .. | tail -n1")
MAX_TIME = 4
if (LATEST_TIME + 0<MAX_TIME) TIME = LATEST_TIME; else TIME = MAX_TIME
TIME_PROPS = system("ls ../postProcessing/bubbleProperties/ | 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"
PROP_FILE = "../postProcessing/bubbleProperties/".TIME_PROPS."/bubbleProperties.dat"
SURF_FILE = "../postProcessing/bubbleSurface/".TIME."/isoSurface.xy"

REF_PROP_FILE = "reference/".CASE."/bubbleProperties.dat"
REF_SURF_FILE = "reference/".CASE."/isoSurface.xy"

BEN_FILE(s) = "benchmark/".CASE."/bubble".s.".dat"

set terminal pngcairo
set grid
set key outside below

set style line 1 dt 2 lt 1 lw 2 pt 2        lc "black"
set style line 2 dt 2 lt 2 lw 5 pt 2        lc "red"
set style line 3      lt 3 lw 2 pt 4 ps 1.2 lc "green"
set style line 4 dt 2 lt 4 lw 2 pt 2        lc "violet"
set style line 5 dt 2 lt 5 lw 2 pt 2        lc "cyan"

#\*-----------------------------Bubble position------------------------------*/

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

set xlabel  "t (s)"
set ylabel  "COG_{b,y} (m)"

plot REF_PROP_FILE u 1:3 w l ls 3 t "Reference"   , \
     PROP_FILE     u 1:3 w l ls 1 t "Simulation"

#\*------------------------------Bubble velocity------------------------------*/

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

set ylabel  "U_{b,y} (m)"

plot BEN_FILE("Velocity")       w p ls 2 t "Benchmark"      , \
     REF_PROP_FILE        u 1:6 w l ls 3 t "Reference"      , \
     PROP_FILE            u 1:6 w l ls 1 t "Simulation"

#\*-----------------------------Bubble sphericity-----------------------------*/

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

set ylabel  "s_b (-)"
set yrange  [:1.1]

plot REF_PROP_FILE u 1:10 w l ls 3 t "Reference"  , \
     PROP_FILE     u 1:10 w l ls 1 t "Simulation"

set autoscale y

#\*-------------------------------Bubble shape--------------------------------*/

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

set terminal pngcairo size 700,800
set size ratio -1

set title   "t = ".TIME." s"
set xlabel  "x (m)"
set ylabel  "y (m)"
set xrange [-1:1]

YTHRESHOLD = 10.9
EPSILON = 0.05

plot BEN_FILE("Shape")                                                                          w p ls 2 t "Benchmark"  , \
     REF_SURF_FILE     u 1:((\$2<=YTHRESHOLD) && (\$3>=-EPSILON) && (\$3<=EPSILON) ? \$2 : 1/0) w p ls 3 t "Reference"  , \
     SURF_FILE         u 1:((\$2<=YTHRESHOLD) && (\$3>=-EPSILON) && (\$3<=EPSILON) ? \$2 : 1/0) w p ls 1 t "Simulation"

set autoscale x
set size noratio

#\*--------------------------------Residuals----------------------------------*/

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

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

plot RESI_FILE u 1:2 w l ls 1 t "Simulation p\_{rgh}"

EOF

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