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

#\*-----------Radial component of liquid velocity along centerline-------------*/

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

plot  'reference/'.CASE.'/centerline/line.csv' u (column("x")):(column("U.water_x")) t 'Reference' w line ls 3, \
      '../postProcessing/centerline/'.TIME.'/line.csv' u (column("x")):(column("U.water_x")) t 'Simulation' w lines ls 1, \
      'experiment/'.CASE.'/centerline/centerline_velocity_L.dat' u 1:2 t 'Experiment' w points ls 2

#\*----------------Axial component of liquid velocity along radial direction at x=5D-----------------*/

set xrange[0:0.1]
set title 'x = 5D'
set output '../postProcessing/report/velocity_L@x=5D.png'
set xlabel 'y (m)'
set ylabel 'U_L_{x} (m/s)'

plot  'reference/'.CASE.'/D5/line.csv' u (column("y")):(column("U.water_x")) t 'Reference' w line ls 3, \
      '../postProcessing/D5/'.TIME.'/line.csv' u (column("y")):(column("U.water_x")) t 'Simulation' w lines ls 1, \
      'experiment/'.CASE.'/D5/U_L.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------Axial component of liquid velocity along radial direction at x=10D-----------------*/

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

plot  'reference/'.CASE.'/D10/line.csv' u (column("y")):(column("U.water_x")) t 'Reference' w line ls 3, \
      '../postProcessing/D10/'.TIME.'/line.csv' u (column("y")):(column("U.water_x")) t 'Simulation' w lines ls 1, \
      'experiment/'.CASE.'/D10/U_L.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------Axial component of liquid velocity along radial direction at x=15D-----------------*/

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

plot  'reference/'.CASE.'/D15/line.csv' u (column("y")):(column("U.water_x")) t 'Reference' w line ls 3, \
      '../postProcessing/D15/'.TIME.'/line.csv' u (column("y")):(column("U.water_x")) t 'Simulation' w lines ls 1, \
      'experiment/'.CASE.'/D15/U_L.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------Axial component of liquid velocity along radial direction at x=20D-----------------*/

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

plot  'reference/'.CASE.'/D20/line.csv' u (column("y")):(column("U.water_x")) t 'Reference' w line ls 3, \
      '../postProcessing/D20/'.TIME.'/line.csv' u (column("y")):(column("U.water_x")) t 'Simulation' w lines ls 1, \
      'experiment/'.CASE.'/D20/U_L.dat' u 1:2 t 'Experiment' w points ls 2

#\*---------------Relative frequency of gas fraction at x=5D------------------*/

EXP_D5     = 'experiment/'.CASE.'/D5/alpha.dat'
FILE_D5    = '../postProcessing/D5/'.TIME.'/line.csv'
REF_D5     = 'reference/'.CASE.'/D5/line.csv'

set datafile columnheaders

stats FILE_D5 u 'alpha.air' nooutput
sum_sim = STATS_sum
len_sim = STATS_records

stats REF_D5 u 'alpha.air' nooutput
sum_ref = STATS_sum
len_ref = STATS_records

stats EXP_D5 using 2 nooutput
len_exp = STATS_records

scale_sim = len_exp*1.0/len_sim
scale_ref = len_exp*1.0/len_ref

set title 'x = 5D'
set output '../postProcessing/report/alpha=5D.png'
set ylabel 'Relative frequency of gas fraction (-)'

plot  REF_D5   u (column("y")):((column("alpha.air")/sum_ref)*scale_ref) t 'Reference' w lines ls 3, \
      FILE_D5  u (column("y")):((column("alpha.air")/sum_sim)*scale_sim) t 'Simulation' w lines ls 1, \
      EXP_D5   u 1:2 t 'Experiment' w points ls 2

#\*---------------Relative frequency of gas fraction at x=10D-----------------*/

EXP_D10     = 'experiment/'.CASE.'/D10/alpha.dat'
FILE_D10    = '../postProcessing/D10/'.TIME.'/line.csv'
REF_D10     = 'reference/'.CASE.'/D10/line.csv'

stats FILE_D10 u 'alpha.air' nooutput
sum_sim = STATS_sum
len_sim = STATS_records

stats REF_D10 u 'alpha.air' nooutput
sum_ref = STATS_sum
len_ref = STATS_records

stats EXP_D10 using 2 nooutput
len_exp = STATS_records

scale_sim = len_exp*1.0/len_sim
scale_ref = len_exp*1.0/len_ref

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

plot  REF_D10   u (column("y")):((column("alpha.air")/sum_ref)*scale_ref) t 'Reference' w lines ls 3, \
      FILE_D10  u (column("y")):((column("alpha.air")/sum_sim)*scale_sim) t 'Simulation' w lines ls 1, \
      EXP_D10   u 1:2 t 'Experiment' w points ls 2

#\*---------------Relative frequency of gas fraction at x=15D-----------------*/

EXP_D15     = 'experiment/'.CASE.'/D15/alpha.dat'
FILE_D15    = '../postProcessing/D15/'.TIME.'/line.csv'
REF_D15     = 'reference/'.CASE.'/D15/line.csv'

stats FILE_D15 u 'alpha.air' nooutput
sum_sim = STATS_sum
len_sim = STATS_records

stats REF_D15 u 'alpha.air' nooutput
sum_ref = STATS_sum
len_ref = STATS_records

stats EXP_D15 using 2 nooutput
len_exp = STATS_records

scale_sim = len_exp*1.0/len_sim
scale_ref = len_exp*1.0/len_ref

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

plot  REF_D15   u (column("y")):((column("alpha.air")/sum_ref)*scale_ref) t 'Reference' w lines ls 3, \
      FILE_D15  u (column("y")):((column("alpha.air")/sum_sim)*scale_sim) t 'Simulation' w lines ls 1, \
      EXP_D15   u 1:2 t 'Experiment' w points ls 2

#\*---------------Relative frequency of gas fraction at x=20D-----------------*/

EXP_D20     = 'experiment/'.CASE.'/D20/alpha.dat'
FILE_D20    = '../postProcessing/D20/'.TIME.'/line.csv'
REF_D20     = 'reference/'.CASE.'/D20/line.csv'

stats FILE_D20 u 'alpha.air' nooutput
sum_sim = STATS_sum
len_sim = STATS_records

stats REF_D20 u 'alpha.air' nooutput
sum_ref = STATS_sum
len_ref = STATS_records

stats EXP_D20 using 2 nooutput
len_exp = STATS_records

scale_sim = len_exp*1.0/len_sim
scale_ref = len_exp*1.0/len_ref

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

plot  REF_D20   u (column("y")):((column("alpha.air")/sum_ref)*scale_ref) t 'Reference' w lines ls 3, \
      FILE_D20  u (column("y")):((column("alpha.air")/sum_sim)*scale_sim) t 'Simulation' w lines ls 1, \
      EXP_D20   u 1: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/0/residuals.dat' u 1:2 w l ls 1 t 'p',\
     '../postProcessing/residuals/0/residuals.dat' u 1:3 w l ls 2 t 'k',\
     '../postProcessing/residuals/0/residuals.dat' u 1:4 w l ls 3 t '{/Symbol w}'
EOF

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