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

TIME = system("foamListTimes -functionEntries -latestTime -case ..")
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 BEN_FILE("Position") every 8 u 1:2 w p ls 2 t "Benchmark"   , \
     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") every 8 u 1:2 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 (-)"

plot BEN_FILE("Sphericity") every 8 u 1:2  w p ls 2 t "Benchmark"     , \
     REF_PROP_FILE                  u 1:10 w l ls 3 t "Reference"     , \
     PROP_FILE                      u 1:10 w l ls 1 t "Simulation"

#---------------------------------Bubble shape---------------------------------

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

set size ratio -1

set title   "t = ".TIME." s"
set xlabel  "x (m)"
set ylabel  "y (m)"

ZCENTRE = 0.5
EPSILON = 0.01

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

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

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