#!/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
    set key box vertical width 1 height 1 maxcols 1 spacing 1

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

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

    set xrange [0:1]
    set xlabel 'y/H [-]'
    set yrange [0:0.25]
    set ylabel '{/Symbol a}_G [-]'

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

    plot alphaGFOAM u (\$1/4.4):2 w l lw 3 lc rgb 'black' t 'Simulation',\
    alphaGCFX u 1:2 w l dt 1 lw 3 lc rgb 'blue' t 'Rzehak et al. (2016)',\
    alphaGExp u 1:2 pt 5 ps 1.5 lc rgb 'red' t 'Expreiment'

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

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

    set xrange [0:1]
    set xlabel 'y/H [-]'
    set yrange [0:1.5]
    set ylabel '{Y_G}^{CO_2} [-]'

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

    plot YGFOAM u (\$1/4.4):3 w l lw 3 lc rgb 'black' t 'Simulation',\
    YGCFX u 1:2 w l dt 1 lw 3 lc rgb 'blue' t 'Rzehak et al. (2016)',\
    YGExp u 1:2 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 xrange [0:1]
    set xlabel 'y/H [-]'
    set yrange [0:3e-3]
    set ylabel '{Y_L}^{CO_2} [-]'
    set format y "%2.1t{/Symbol \264}10^{%L}";set ytics 0, 5e-4

    YLFOAM = system("ls ../postProcessing/average/$time/layerAverage.csv")
    YLCFX = system("ls ../validation/experimentalData/YLCFX16.dat")

    plot YLFOAM u (\$1/4.4):4 w l lw 3 lc rgb 'black' t 'Simulation',\
    YLCFX u 1:2 w l dt 1 lw 3 lc rgb 'blue' t 'Rzehak et al. (2016)'

EOF

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