#!/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 ..")
CASE = system("foamDictionary ../caseParameterDict -entry case -value")

set term pngcairo
set grid
set key box
set xrange [0:1]

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

#\*-------------------------Centreline velocity plot--------------------------*/

set title 'r = 0'
set output '../postProcessing/report/Centreline_velocity.png'
set xlabel 'x/L (-)'
set ylabel 'U (m/s)'

plot  'reference/'.CASE.'/centerLine/line.csv' u (\$1*2.5):2 t 'Reference' w line ls 3, \
      '../postProcessing/centerLine/'.TIME.'/line.csv' u (\$1*2.5):2 t 'Simulation' w lines ls 1,\
      'experiment/'.CASE.'/centreline_velocity.dat' u (\$1*2.5):2 t 'Experiment' w points ls 2

#\*---------------------Perpendicular profiles: velocity----------------------*/

set xlabel 'r/R (-)'
set yrange [-0.2:1.6]

#\*---------------------------------At x = 8D---------------------------------*/

set output '../postProcessing/report/velocity@x=8D.png'
set title 'x = 8D'

plot 'reference/'.CASE.'/D8/line.csv' u (\$1*20):2 t 'Reference' w lines ls 3, \
     '../postProcessing/D8/'.TIME.'/line.csv' u (\$1*20):2 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D8/U.dat' u (\$1*20):2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 24D---------------------------------*/

set output '../postProcessing/report/velocity@x=24D.png'
set title 'x = 24D'

plot 'reference/'.CASE.'/D24/line.csv' u (\$1*20):2 t 'Reference' w lines ls 3, \
     '../postProcessing/D24/'.TIME.'/line.csv' u (\$1*20):2 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D24/U.dat' u (\$1*20):2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 40D---------------------------------*/

set output '../postProcessing/report/velocity@x=40D.png'
set title 'x = 40D'

plot 'reference/'.CASE.'/D40/line.csv' u (\$1*20):2 t 'Reference' w lines ls 3, \
     '../postProcessing/D40/'.TIME.'/line.csv' u (\$1*20):2 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D40/U.dat' u (\$1*20):2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 60D---------------------------------*/

set output '../postProcessing/report/velocity@x=60D.png'
set title 'x = 60D'

plot 'reference/'.CASE.'/D60/line.csv' u (\$1*20):2 t 'Reference' w lines ls 3, \
     '../postProcessing/D60/'.TIME.'/line.csv' u (\$1*20):2 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D60/U.dat' u (\$1*20):2 t 'Experiment' w points ls 2

#\*-------------Perpendicular profiles: turbulent kinetic energy--------------*/

set ylabel 'k (m^2/s^2)'
set yrange [0:0.08]

#\*---------------------------------At x = 8D---------------------------------*/

set output '../postProcessing/report/k@x=8D.png'
set title 'x = 8D'

plot 'reference/'.CASE.'/D8/line.csv' u (\$1*20):2 t 'Reference' w lines ls 3, \
     '../postProcessing/D8/'.TIME.'/line.csv' u (\$1*20):2 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D8/k.dat' u (\$1*20):2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 24D---------------------------------*/

set output '../postProcessing/report/k@x=24D.png'
set title 'x = 24D'

plot 'reference/'.CASE.'/D24/line.csv' u (\$1*20):2 t 'Reference' w lines ls 3, \
     '../postProcessing/D24/'.TIME.'/line.csv' u (\$1*20):2 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D24/k.dat' u (\$1*20):2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 40D---------------------------------*/

set output '../postProcessing/report/k@x=40D.png'
set title 'x = 40D'

plot 'reference/'.CASE.'/D40/line.csv' u (\$1*20):2 t 'Reference' w lines ls 3, \
     '../postProcessing/D40/'.TIME.'/line.csv' u (\$1*20):2 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D40/k.dat' u (\$1*20):2 t 'Experiment' w points ls 2

#\*---------------------------------At x = 60D---------------------------------*/

set output '../postProcessing/report/k@x=60D.png'
set title 'x = 60D'

plot 'reference/'.CASE.'/D60/line.csv' u (\$1*20):2 t 'Reference' w lines ls 3, \
     '../postProcessing/D60/'.TIME.'/line.csv' u (\$1*20):2 t 'Simulation' w lines ls 1, \
     'experiment/'.CASE.'/D60/k.dat' u (\$1*20):2 t 'Experiment' w points ls 2

EOF

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

gnuplot<<EOF

CASE = system("foamDictionary ../caseParameterDict -entry case -value")

set term pngcairo

set style line 1 lw 2 lc "black"
set style line 2 lw 2 lc "red"
set style line 3 lw 2 lc "forest-green"
set style line 4 lw 2 lc "gray"
set style line 5 lw 2 lc "orange"
set style line 6 lw 2 lc "green"

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

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

# plotting data
plot 'reference/'.CASE.'/residuals.dat' u 1:2 w l ls 4 t 'Ref. p',\
     'reference/'.CASE.'/residuals.dat' u 1:3 w l ls 5 t 'Ref. k',\
     'reference/'.CASE.'/residuals.dat' u 1:4 w l ls 6 t 'Ref. {/Symbol w}',\
     '../postProcessing/residuals(p,U)/0/residuals.dat' u 1:2 w l ls 1 t 'p',\
     '../postProcessing/residuals(p,U)/0/residuals.dat' u 1:3 w l ls 2 t 'k',\
     '../postProcessing/residuals(p,U)/0/residuals.dat' u 1:4 w l ls 3 t '{/Symbol w}'
EOF

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