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

GRPH_FILE =  "../postProcessing/graph/".TIME."/line.csv"
GRPH_RFILE = "reference/line.csv"
GRPH_AFILE = "analytical/line.csv"

PROB_U_FILE =   "../postProcessing/probes/0/U"
PROB_U_RFILE =  "reference/U"
PROB_U_AFILE =  "analytical/U"

PROB_P_FILE =  "../postProcessing/probes/0/p"
PROB_P_RFILE = "reference/p"
PROB_P_AFILE = "analytical/p"

RESI_FILE = "../postProcessing/residuals/0/residuals.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 pi 10 lc "red"
set style line 3      lw 1.5            lc "green"

#-----------------------------------Graph---------------------------------------

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

set output "../postProcessing/report/graph_U.png"
plot GRPH_AFILE u (column("y")):(column("U_x")) w lp ls 2 t "Analytical"    , \
     GRPH_RFILE u (column("y")):(column("U_x")) w l  ls 3 t "Reference"     , \
     GRPH_FILE  u (column("y")):(column("U_x")) w l  ls 1 t "Simulation"

set ylabel  "p (Pa)"
set output "../postProcessing/report/graph_p.png"
plot GRPH_AFILE u (column("y")):(column("p")) w lp ls 2 t "Analytical"      , \
     GRPH_RFILE u (column("y")):(column("p")) w l  ls 3 t "Reference"       , \
     GRPH_FILE  u (column("y")):(column("p")) w l  ls 1 t "Simulation"

#-----------------------------------Probes--------------------------------------

set title   "Top boundary"
set xlabel  "t (s)"
set ylabel  "U_x (m/s)"

set output "../postProcessing/report/probe_U.png"
plot PROB_U_AFILE u 1:2 '%lf (%lf %lf %lf) (%lf %lf %lf)' w lp ls 2 t "Analytical"  , \
     PROB_U_RFILE u 1:2 '%lf (%lf %lf %lf) (%lf %lf %lf)' w l  ls 3 t "Reference"   , \
     PROB_U_FILE  u 1:2 '%lf (%lf %lf %lf) (%lf %lf %lf)' w l  ls 1 t "Simulation"

set title   "Centre"
set ylabel  "p (Pa)"

set output "../postProcessing/report/probe_p.png"
plot PROB_P_AFILE u 1:3 w lp ls 2 t "Analytical"    , \
     PROB_P_RFILE u 1:3 w l  ls 3 t "Reference"     , \
     PROB_P_FILE  u 1:3 w l  ls 1 t "Simulation"

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

set ylabel "residuals (-)"
set logscale y
set format y "10^{%.0T}"

set output "../postProcessing/report/residuals.png"
plot RESI_FILE u 1:2 w l lc 1 t 'p\_{rgh}'  , \
     RESI_FILE u 1:3 w l lc 2 t 'U.gas1'    , \
     RESI_FILE u 1:4 w l lc 3 t 'U.gas2'

EOF

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