#!/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 -latestTime -case ..")
TIME_PROBE = system("ls ../postProcessing/probes/ | tail -n1")
TIME_RESIDUAL = system("ls ../postProcessing/residuals/ | tail -n1")
TIME_REFERENCE = "13"

u_in  = 0.62
Dh    = 0.22
Dv    = 0.305

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 2 lc "green"
set style line 4 dt 2 lt 4 lw 2 pt 2 lc "violet"
set style line 5      lt 5 lw 2 pt 2 lc "cyan"


#---PNG---
set terminal pngcairo enhanced size 800,600


#---residuals---
FILE =  "../postProcessing/residuals/".TIME_RESIDUAL."/residuals.dat"

set output "../postProcessing/report/residuals.png"
set xlabel "t"
set ylabel "initial residuals"
set logscale y
set format y "10^{%.1T}"
plot FILE u 1:2 w l lw 2 t "p_{rgh}"

unset logscale y
set format y "%g"


#---probes---
PATH = "../postProcessing/probes/".TIME_PROBE."/"

set output "../postProcessing/report/probes_U.png"
set xlabel  "t"
set ylabel  "U/u_{in}"
set yrange [0:]
plot PATH."U.gas1" u 1:(\$3/u_in) '%lf (%lf %lf %lf) (%lf %lf %lf)' w l t "probe 1, U_y", \
     PATH."U.gas1" u 1:(\$5/u_in) '%lf (%lf %lf %lf) (%lf %lf %lf)' w l t "probt 2, U_x"
set autoscale y

set output "../postProcessing/report/probes_p_rgh.png"
set ylabel  "p_{rgh}"
plot [1.2e-4:] PATH."p_rgh" u 1:2 w l t "probe 1", \
     [1.2e-4:] PATH."p_rgh" u 1:3 w l t "probe 2"


#---singleGraph---
FILE = "../postProcessing/singleGraph/".TIME."/line.csv"
RFILE = "reference/singleGraph/".TIME_REFERENCE."/line.csv"
BFILE = "benchmark/gasVel.dat"

set output "../postProcessing/report/singleGraph.png"
set xlabel  "U_x/u_{in}"
set ylabel  "y/D_h"
plot RFILE u (column('U.gas1_x')/u_in):(column('y')/Dh) w l ls 3 t "Reference", \
     FILE  u (column('U.gas1_x')/u_in):(column('y')/Dh) w l ls 1 t "Simulation", \
     BFILE u 2:1                                        w p ls 2 t "Benchmark"


#---singleGraphMean---
FILE = "../postProcessing/singleGraphMean/".TIME."/line.csv"
RFILE = "reference/singleGraphMean/".TIME_REFERENCE."/line.csv"

set output "../postProcessing/report/singleGraphMean.png"
set xlabel  "U_x/u_{in}"
set ylabel  "y/D_h"
plot RFILE u (column('UMean.gas1_x')/u_in):(column('y')/Dh) w l ls 3 t "Reference", \
     FILE  u (column('UMean.gas1_x')/u_in):(column('y')/Dh) w l ls 1 t "Simulation"

EOF

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