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

FILE  = '../postProcessing/sampleLine/'.TIME.'/line.csv'
APATH = '../postProcessing/python/'.TIME.'/H3-depthAvg_'
RFILE = '../validation/reference/'.CASE.'/line.csv'
RPATH = '../validation/reference/'.CASE.'/H3-depthAvg_'
EPATH = '../validation/experiment/'.CASE.'/'

set term pngcairo
set grid

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 dt 2 lt 5 lw 2 pt 2 lc "cyan"

#\*---------------------Axial liquid velocity profile-------------------------*/

set output '../postProcessing/report/profile-liqVelMean.png'

set xlabel 'x/L'
set xrange [0:1]
set ylabel 'W_L (m/s)'

plot RFILE                 u (column("x")/L):(column("UMean.water_z")) w lines  ls 3 t 'Reference'      , \
     FILE                  u (column("x")/L):(column("UMean.water_z")) w lines  ls 1 t 'Simulation'    , \
     EPATH.'liqVel.dat'    u (\$1/L)        :2                         w points ls 2 t 'Experiment'

#\*---------------------Axial gas velocity profile----------------------------*/

set output '../postProcessing/report/profile-gasVelMean.png'

set ylabel 'W_G (m/s)'

plot FILE               u (column("x")/L):(column("UMean.air1_z")) w lines ls 1 t 'Simulation - air1', \
     FILE               u (column("x")/L):(column("UMean.air2_z")) w lines ls 5 t 'Simulation - air2'

set output '../postProcessing/report/profile-gasVelMean-depthAvg.png'

plot RPATH.'UMean.bubbles.csv' u (column("x")/L):(column("UMean.bubbles_z")) w lines  ls 3 t 'Reference', \
     APATH.'UMean.bubbles.csv' u (column("x")/L):(column("UMean.bubbles_z")) w lines  ls 1 t 'Simulation', \
     EPATH.'gasVel.dat'        u (\$1/L)        :2                           w points ls 2 t 'Experiment'

#\*-----------------Liquid turbulence kinetic energy profile------------------*/

set output '../postProcessing/report/profile-liqKeMean.png'

set ylabel 'k_L (m^2/s^2)'

plot RFILE             u (column("x")/L):(column("kMean.water")) w lines  ls 3 t 'Reference'     , \
     FILE              u (column("x")/L):(column("kMean.water")) w lines  ls 1 t 'Simulation'

#\*---------------------------Gas fraction profile----------------------------*/

set output '../postProcessing/report/profile-gasFracMean.png'

set ylabel 'r_G (-)'

plot FILE u (column("x")/L):(column("alphaMean.air1")+column("alphaMean.air2")) w lines  ls 1      t 'Simulation - air' , \
     FILE u (column("x")/L):(column("alphaMean.air1"))                          w lines  ls 1 dt 2 t 'Simulation - air1', \
     FILE u (column("x")/L):(column("alphaMean.air2"))                          w lines  ls 1 dt 3 t 'Simulation - air2', \

set output '../postProcessing/report/profile-gasFracMean-depthAvg.png'

plot RPATH.'alphaMean.bubbles.csv' u (column("x")/L):(column("alphaMean.bubbles")) w lines  ls 3 t 'Reference', \
     APATH.'alphaMean.bubbles.csv' u (column("x")/L):(column("alphaMean.bubbles")) w lines  ls 1 t 'Simulation', \
     EPATH.'alpha.dat'             u (\$1/L)        :2                             w points ls 2 t 'Experiment'

set autoscale x

#\*----------------------------Gas fraction probe-----------------------------*/

set output '../postProcessing/report/probes-gasFrac.png'

FILE = '../postProcessing/probes/0/alpha.air'

set xlabel 't (s)'
set ylabel 'r_G (-)'

plot FILE.'1' u 1:2 w lines t 'air1 - x=0.0375' , \
     FILE.'1' u 1:3 w lines t 'air1 - x=0.0750'  , \
     FILE.'2' u 1:2 w lines t 'air2 - x=0.0375' , \
     FILE.'2' u 1:3 w lines t 'air2 - x=0.0750'


set output '../postProcessing/report/probes-gasFracMean.png'

FILE = '../postProcessing/probes/'.TIME_PROBE.'/alphaMean.air'

set logscale x
set ylabel '<r_G> (-)'

plot FILE.'1' u 1:2 w lines t 'air1 - x=0.0375', \
     FILE.'1' u 1:3 w lines t 'air1 - x=0.0750', \
     FILE.'2' u 1:2 w lines t 'air2 - x=0.0375', \
     FILE.'2' u 1:3 w lines t 'air2 - x=0.0750'

unset logscale x

#\*--------------------------Liquid velocity probe----------------------------*/

set output '../postProcessing/report/probes-liqVel.png'

FILE = '../postProcessing/probes/0/U.water'

set ylabel 'W_L (m/s)'

plot FILE u 1:4 '%lf (%lf %lf %lf) (%lf %lf %lf)' w lines t 'x=0.0375', \
     FILE u 1:7 '%lf (%lf %lf %lf) (%lf %lf %lf)' w lines t 'x=0.0750'

set output '../postProcessing/report/probes-liqVelMean.png'

FILE = '../postProcessing/probes/'.TIME_PROBE.'/UMean.water'

set logscale x
set ylabel '<W_L> (m/s)'

plot FILE u 1:4 '%lf (%lf %lf %lf) (%lf %lf %lf)' w lines t 'x=0.0375'  , \
     FILE u 1:7 '%lf (%lf %lf %lf) (%lf %lf %lf)' w lines t 'x=0.0750'

unset logscale x

#\*-------------------------------Vector field--------------------------------*/

set term pngcairo size 400,3500

FILE = '../postProcessing/cutPlaneSurface/'.TIME.'/cutPlane.xy'

set size ratio -1
set xlabel 'x (m)'
set xrange [0:L]
set ylabel 'z (m)'
set yrange [0:H]
set xtics  0.1
set ytics  0.1

scale_factor = 0.08

set output '../postProcessing/report/vecField_liqVelocity.png'
plot FILE u 1:3:(scale_factor*\$4):(scale_factor*\$6) every 3 with vectors head filled ls 1 lw 1 notitle

set output '../postProcessing/report/vecField_liqVelocityMean.png'
plot FILE u 1:3:(scale_factor*\$7):(scale_factor*\$9) every 3 with vectors head filled ls 1 lw 1 notitle

set autoscale xy
set xtics auto
set ytics auto
set size ratio 0

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

set term pngcairo

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

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

plot '../validation/reference/'.CASE.'/residuals.dat'    u 1:2 w l ls 4 t 'Reference   p\_rgh'         , \
     '../validation/reference/'.CASE.'/residuals.dat'    u 1:3 w l ls 5 t 'Reference   k'              , \
     '../validation/reference/'.CASE.'/residuals.dat'    u 1:4 w l ls 6 t 'Reference   {/Symbol w}'    , \
     '../postProcessing/residuals/0/residuals.dat' u 1:2 w l ls 1 t 'Simulation p\_rgh'         , \
     '../postProcessing/residuals/0/residuals.dat' u 1:3 w l ls 2 t 'Simulation k'              , \
     '../postProcessing/residuals/0/residuals.dat' u 1:4 w l ls 3 t 'Simulation {/Symbol w}'
EOF

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