#!/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")
L = 0.24
H = 0.7

FILE  = '../postProcessing/sampleLine/'.TIME.'/line.csv'
RFILE = '../validation/reference/line.csv'
EPATH = '../validation/'

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'    , \
     EPATH.'liqVelCFX.dat' u (\$1/L)        :2                         w lines  ls 4 t 'CFX'

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

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

set ylabel 'W_G (m/s)'

plot RFILE              u (column("x")/L):(column("UMean.bubbles_z")) w lines  ls 3 t 'Reference'         , \
     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', \
     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'   , \
     EPATH.'liqKe.dat' u (\$1/L)        :2                       w points ls 2 t 'Experiment'

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

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

set ylabel 'r_G (-)'

plot RFILE                u (column("x")/L):(column("alphaMean.bubbles"))                       w lines  ls 3      t 'Reference'         , \
     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', \
     EPATH.'alpha.dat'    u (\$1/L)        :2                                                   w points ls 2      t 'Experiment'       , \
     EPATH.'alphaCFX.dat' u (\$1/L)        :2                                                   w lines  ls 4      t 'CFX'

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.07', \
     FILE.'1' u 1:3 w lines t 'air1 - x=0.17', \
     FILE.'2' u 1:2 w lines t 'air2 - x=0.07', \
     FILE.'2' u 1:3 w lines t 'air2 - x=0.17'


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.07', \
     FILE.'1' u 1:3 w lines t 'air1 - x=0.17', \
     FILE.'2' u 1:2 w lines t 'air2 - x=0.07', \
     FILE.'2' u 1:3 w lines t 'air2 - x=0.17'

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.07', \
     FILE u 1:7 '%lf (%lf %lf %lf) (%lf %lf %lf)' w lines t 'x=0.17'

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.07', \
     FILE u 1:7 '%lf (%lf %lf %lf) (%lf %lf %lf)' w lines t 'x=0.17'

unset logscale x

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

set term pngcairo size 400,800

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.05
set ytics  0.05

scale_factor = 0.04

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/residuals.dat'    u 1:2 w l ls 4 t 'Reference   p\_rgh'         , \
     '../validation/reference/residuals.dat'    u 1:3 w l ls 5 t 'Reference   k'              , \
     '../validation/reference/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

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