#!/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 ..")
TIME_PROBE = system("ls ../postProcessing/probes/ | tail -n1")
TIME_RESIDUAL = system("ls ../postProcessing/residuals/ | tail -n1")
CASE = system("foamDictionary ../caseParameterDict -entry case -value")

FILE = "../postProcessing/layerAverage/".TIME."/layerAverage.csv"
RFILE = "reference/".CASE."/layerAverage.csv"
BFILE = "benchmark/".CASE."/reference.dat"

h = 0.0125      # channel half width
nu = 1e-6       # kinematic viscosity
Re_tau = system("foamDictionary -entry ReTau -value ../caseParameterDict")

u_tau = Re_tau * nu / h
y_tau = h / Re_tau

set terminal pngcairo enhanced size 800,600
set grid
set key outside below
set title CASE

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"

#\*-----------------------Stream-wise velocity profile------------------------*/

set output "../postProcessing/report/U_wallLaw.png"
set xlabel "y^+ (-)"
set logscale x
set format x "10^{%T}"
set xrange [1e-1:3e2]
set ylabel "U^+ (-)"
set yrange [0:20]
plot RFILE u (column("y") + h)/y_tau:(column("U_x"))/u_tau w l ls 3 t "Reference"       , \
     FILE  u (column("y") + h)/y_tau:(column("U_x"))/u_tau w l ls 1 t "Simulation"      , \
     BFILE u (\$1 + h)/y_tau:(\$8/u_tau)                   w p ls 2 t "Benchmark"       , \
             x                                                      t "viscous sublayer", \
             1/0.41*log(x) + 5                                      t "log-law"
unset logscale x
set format x
set autoscale xy

set output "../postProcessing/report/U.png"
set xlabel "U_x (m/s)"
set ylabel "y/h (-)"
set yrange [-1:1]
plot RFILE u (column("U_x")):(column("y")/h) w l ls 3 t "Reference"     , \
     FILE  u (column("U_x")):(column("y")/h) w l ls 1 t "Simulation"    , \
     BFILE u 8:(\$1/h)                       w p ls 2 t "Benchmark"


#\*--------------------------Reynolds-stress profile--------------------------*/

set output "../postProcessing/report/R.png"
set xlabel "R_{xy} (m^2/s^2)"
plot RFILE u (column("R_xy")):(column("y")/h) w l ls 3 t "Reference"    , \
     FILE  u (column("R_xy")):(column("y")/h) w l ls 1 t "Simulation"   , \
     BFILE u 7:(\$1/h)                        w p ls 2 t "Benchmark"

set autoscale y

#\*----------------------------------Probes-----------------------------------*/

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

FILE = "../postProcessing/probes/".TIME_PROBE."/U"

set xlabel "t (s)"
set ylabel "U_x (m/s)"
plot FILE u 1:2 "%lf (%lf %lf %lf) (%lf %lf %lf)" w l t "y=0.0075"  , \
     FILE u 1:5 "%lf (%lf %lf %lf) (%lf %lf %lf)" w l t "y=0.011"

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

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

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

set xlabel 't (s)'
set ylabel 'residual (-)'
set logscale y
set format y '%.0e'

plot FILE u 1:2  w l lw 1 dt 1 t 'p_{rgh}'      , \
     FILE u 1:3  w l lw 1 dt 1 t 'U_x'          , \
     FILE u 1:4  w l lw 1 dt 1 t 'U_y'          , \
     FILE u 1:5  w l lw 1 dt 1 t 'R_{xx}'       , \
     FILE u 1:6  w l lw 1 dt 1 t 'R_{xy}'       , \
     FILE u 1:7  w l lw 1 dt 1 t 'R_{yy}'       , \
     FILE u 1:8  w l lw 1 dt 1 t 'R_{zz}'       , \
     FILE u 1:9  w l lw 1 dt 1 t '{/Symbol e}'  , \
     FILE u 1:10 w l lw 1 dt 1 t 'alphaRelax'

EOF

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