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

set term pngcairo size 600,600
set grid
set key box
set xrange [0:0.4]
set yrange [0:1.8]

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 for liquid plot--------------------------*/

set title 'r = 0'
set output '../postProcessing/report/Centerline_velocity_L.png'
set xlabel 'x (m)'
set ylabel 'U_L (m/s)'

plot  'reference/centerline/line.csv' u 1:2 t 'Reference' w line ls 3, \
      '../postProcessing/centerline/'.TIME.'/line.csv' u 1:2 t 'Simulation' w lines ls 1,\
      'data/centerline/centerline_velocity_L.dat' u (\$1*0.00508):(\$2*1.61) t 'Experiment' w points ls 2

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

set xlabel 'r (m)'
set ylabel 'U_L (m/s)'
set xrange [0:0.03]
set yrange [0:1.3]

#\*---------------------------------At x = 8D, liquid velocity profiles---------------------------------*/

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

plot  'reference/D8/line.csv' u 1:2 t 'Reference' w line ls 3, \
      '../postProcessing/D8/'.TIME.'/line.csv' u 1:2 t 'Simulation' w lines ls 1, \
      'data/D8/U_L.dat' u (\$1*0.04064):(\$2*1.1197) t 'Experiment' w points ls 2

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

set xlabel 'r (m)'
set ylabel 'k (m^{2}/s^{2})'
set xrange [0:0.03]
set yrange [0:0.08]

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

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

plot  'reference/D8/line.csv' u 1:8 t 'Reference' w line ls 3, \
      '../postProcessing/D8/'.TIME.'/line.csv' u 1:5 t 'Simulation' w lines ls 1, \
      'data/D8/k.dat' u (\$1*0.04064):(\$2*1.2537) t 'Experiment' w points ls 2

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

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

plot  'reference/D16/line.csv' u 1:8 t 'Reference' w line ls 3, \
      '../postProcessing/D16/'.TIME.'/line.csv' u 1:2 t 'Simulation' w lines ls 1, \
      'data/D16/k.dat' u (\$1*0.08128):(\$2*0.2933) t 'Experiment' w points ls 2

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

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

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

EOF

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

gnuplot<<EOF

set term pngcairo size 500,500

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 '../postProcessing/residuals(p_rgh,k.water,omega.water)/0/residuals.dat' u 1:2 w l ls 1 t 'p',\
     '../postProcessing/residuals(p_rgh,k.water,omega.water)/0/residuals.dat' u 1:3 w l ls 2 t 'k',\
     '../postProcessing/residuals(p_rgh,k.water,omega.water)/0/residuals.dat' u 1:4 w l ls 3 t '{/Symbol w}'
EOF

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