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

time=$(foamListTimes -case .. -latestTime)

gnuplot<<EOF

    set term pngcairo size 500,500

    set grid
    set key box

#\*---------------------Average cross-sectional gas volume fraction-------------------------------*/

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

    set yrange [0:5]
    set ylabel 'y [m]'
    set xrange [0:0.2]
    set xlabel '{/Symbol a}_G [-]'

    alphaGFOAM = system("ls ../postProcessing/average/$time/layerAverage.csv")
    alphaGExp = system("ls ../validation/experimentalData/alphaGExp16.dat")

    plot alphaGFOAM u 2:1 w l lw 3 lc rgb 'black' t ' Simulation',\
    alphaGExp u 2:(\$1*4.4) pt 5 ps 1.5 lc rgb 'red' t 'Experiment'

#\*---------------------Average cross-sectional CO2 mass fraction in gas-------------------------------*/

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

    set yrange [0:5]
    set ylabel 'y [m]'
    set xrange [0.2:1]
    set xlabel '{Y_G}^{CO_2} [-]'

    YGFOAM= system("ls ../postProcessing/average/$time/layerAverage.csv")
    YGExp = system("ls ../validation/experimentalData/YGExp16.dat")

    plot YGFOAM u 3:1 w l lw 3 lc rgb 'black' t 'Simulation',\
    YGExp u 2:(\$1*4.4) pt 5 ps 1.5 lc rgb 'red' t 'Experiment'

#\*---------------------Average cross-sectional CO2 mass fraction in liquid-------------------------------*/

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

    set yrange [0:5]
    set ylabel 'y [m]'
    set xrange [0:1.5e-3]
    set xlabel '{Y_L}^{CO_2} [-]'
    set format x "%2.0t{/Symbol \327}10^{%L}";set xtics 0, 3e-4

    YLFOAM= system("ls ../postProcessing/average/$time/layerAverage.csv")

    plot YLFOAM u 4:1 w l lw 3 lc rgb 'blue' t 'Simulation'

EOF

gnuplot<<EOF

    set term pngcairo size 500,500

    set grid
    set key box

#\*---------------------interfacial area-------------------------------*/

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

    set yrange [0:5]
    set ylabel 'y [m]'
    set xrange [0:550]
    set xlabel 'a_i [m^{-1}]'

    ai= system("ls ../postProcessing/average/$time/layerAverage.csv")

    plot ai u ((\$2/\$5)*6):1 w l lw 3 lc rgb 'blue' t 'Simulation'

#\*---------------------Average bubble diameter-------------------------------*/

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

    set yrange [0:5]
    set ylabel 'y [m]'
    set xrange [1.5:3]
    set xlabel 'd_b [mm]'
    set xtics 0, 0.3

    d= system("ls ../postProcessing/average/$time/layerAverage.csv")

    plot d u (\$5/1e-3):1 w l lw 3 lc rgb 'blue' t 'Simulation'

EOF

#\*---------------------Bubble distribution at different height-------------------------------*/

gnuplot<<EOF

    set term pngcairo size 500,500
    set grid

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

    set xlabel 'd_b [mm]'
    set yrange [0:10]
    set ylabel '({/Symbol a}/{/Symbol a}_{tot})/{/Symbol D}d [mm^{-1}]'
    set xtics 1

    x1 = system("ls ../postProcessing/probabilityDensity.x1/$time/probabilityDensity.x1.csv")
    x2 = system("ls ../postProcessing/probabilityDensity.x2/$time/probabilityDensity.x2.csv")
    x3 = system("ls ../postProcessing/probabilityDensity.x3/$time/probabilityDensity.x3.csv")
    x4 = system("ls ../postProcessing/probabilityDensity.x4/$time/probabilityDensity.x4.csv")
    x5 = system("ls ../postProcessing/probabilityDensity.x5/$time/probabilityDensity.x5.csv")

    plot x1 u (\$1/1e-3):(\$2/1e3) w histeps lw 3 lc rgb 'purple' t 'x = 0.01 [m]',\
    x2 u (\$1/1e-3):(\$2/1e3) w histeps lw 3 lc rgb 'black' t 'x = 0.1 [m]',\
    x3 u (\$1/1e-3):(\$2/1e3) w histeps lw 3 lc rgb 'blue' t 'x = 0.25 [m]',\
    x4 u (\$1/1e-3):(\$2/1e3) w histeps lw 3 lc rgb 'red' t 'x = 0.5 [m]',\
    x5 u (\$1/1e-3):(\$2/1e3) w histeps lw 3 lc rgb 'green' t 'x = 4 [m]'

EOF

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