#!/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.72) t 'Experiment' w points ls 2

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

set title 'r = 0'
set output '../postProcessing/report/Centerline_velocity_S.png'
set xlabel 'x (m)'
set ylabel 'U_S (m/s)'
set xrange [0:0.4]
set yrange [0:1.8]

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

#\*-------------------------Centreline velocity for solid fraction--------------------------*/

set title 'r = 0'
set output '../postProcessing/report/Centerline_alpha_S.png'
set xrange [0:0.4]
set yrange [0:0.05]
set xlabel 'x'
set ylabel '{/Symbol a}_C (-)'

plot  'reference/centerline/line.csv' u 1:8 t 'Reference' w line ls 3, \
      '../postProcessing/centerline/'.TIME.'/line.csv' u 1:8 t 'Simulation' w lines ls 1,\
      'data/centerline/alpha.dat' u (\$1*0.00508):(\$2*0.048) 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.2264) t 'Experiment' w points ls 2

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

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

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

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

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

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

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

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

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

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

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

#\*---------------------------------At x = 16D, solid velocity profiles---------------------------------*/

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

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

#\*---------------------------------At x = 40D, solid velocity profiles---------------------------------*/

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

plot 'reference/D40/line.csv' u 1:5 t 'Reference' w line ls 3, \
     '../postProcessing/D40/'.TIME.'/line.csv' u 1:5 t 'Simulation' w lines ls 1, \
     'data/D40/U_S.dat' u (\$1*0.2032):(\$2*0.2637) 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.1]

#\*---------------------------------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:8 t 'Simulation' w lines ls 1, \
     'data/D8/k.dat' u (\$1*0.04064):(\$2*1.5041) 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:8 t 'Simulation' w lines ls 1, \
     'data/D16/k.dat' u (\$1*0.08128):(\$2*0.5552) 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:8 t 'Simulation' w lines ls 1, \
     'data/D40/k.dat' u (\$1*0.2032):(\$2*0.0756) t 'Experiment' w points ls 2


#\*-------------Perpendicular profiles: solid fraction--------------*/

set ylabel '{/Symbol a} (-)'
set xrange [0:0.03]
set yrange [0:0.05]

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

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

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

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

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

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

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

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

plot 'reference/D40/line.csv' u 1:9 t 'Reference' w line ls 3, \
     '../postProcessing/D40/'.TIME.'/line.csv' u 1:9 t 'Simulation' w lines ls 1, \
     'data/D40/alpha.dat' u (\$1*0.2032):(\$2*0.01031) 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      'reference/residuals.dat' u 1:2 w l ls 4 t 'Ref. p',\
          'reference/residuals.dat' u 1:3 w l ls 5 t 'Ref. k',\
          'reference/residuals.dat' u 1:4 w l ls 6 t 'Ref. {/Symbol w}',\
          '../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

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