#!/bin/sh

cd ${0%/*} || exit 1

. $WM_PROJECT_DIR/bin/tools/RunFunctions

# gYs="air.gas H2O.gas C3H8O.gas none.gas"
# lYs="H2O.liquid C3H8O.liquid none.liquid"
gYs="air.gas H2O.gas C3H8O.gas"
lYs="H2O.liquid C3H8O.liquid"

runApplication blockMesh

setInertY()
{
    y=${1%.*}
    phase=${1#*.}
    [ $y != "none" ] && arg="-set $y" || arg="-remove"
    runApplication -a foamDictionary -entry defaultSpecie $arg \
        constant/thermophysicalProperties.$phase
}

for gY in $gYs
do
    setInertY $gY
    for lY in $lYs
    do
        setInertY $lY
        runApplication -s ${gY}_${lY} $(getApplication)
        mv postProcessing postProcessing_${gY}_$lY
    done
done

gnuplot << EOF

set terminal eps enhanced size 5.83,8.27
set output 'postProcessing.eps'

set lmargin at screen 0.15
set rmargin at screen 0.84

gYs = '$gYs'
lYs = '$lYs'

gY0 = word(gYs, 1)
lY0 = word(lYs, 1)

set multiplot layout 6,1

set xlabel "Time (s)"

set ytics nomirror
set y2tics
set ylabel 'Gas volume fraction'
set y2label 'Liquid volume fraction'

plot \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/alpha.gas' w l lc 1 t 'Gas', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/alpha.gas' w l lc 1 notitle, \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/alpha.liquid' w l axes x1y2 lc 2 t 'Liquid', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/alpha.liquid' w l axes x1y2 lc 2 notitle

set ytics mirror
unset y2tics
set ylabel 'Temperature (K)'
unset y2label

plot \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/T.gas' w l lc 1 t 'Gas', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/T.gas' w l lc 1 notitle, \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/T.liquid' w l lc 2 t 'Liquid', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/T.liquid' w l lc 2 notitle, \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/interfaceCompositionPhaseChange:Tf.gasAndLiquid' w l lc 3 t 'Interface', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/interfaceCompositionPhaseChange:Tf.gasAndLiquid' w l lc 3 notitle

set ytics nomirror
set y2tics
set ylabel "Vapour mass fraction"
set y2label "Air mass fraction"

plot \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/H2O.gas' w l lc 1 t 'H2O', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/H2O.gas' w l lc 1 notitle, \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/C3H8O.gas' w l lc 2 t 'C3H8O', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/C3H8O.gas' w l lc 2 notitle, \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/air.gas' w l axes x1y2 lc 3 t 'Air', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/air.gas' w l axes x1y2 lc 3 notitle

set ytics mirror
unset y2tics
set ylabel "Liquid mass fraction"
unset y2label

plot \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/H2O.liquid' w l lc 1 t 'H2O', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/H2O.liquid' w l lc 1 notitle, \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/C3H8O.liquid' w l lc 2 t 'C3H8O', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/C3H8O.liquid' w l lc 2 notitle

set ytics nomirror
set y2tics
set ylabel "Mass (kg/m^3)"
set y2label "Energy (J/m^3)"

plot \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/dMass.gas' w l lc 1 t 'Gas Mass Change', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/dMass.gas' w l lc 1 notitle, \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/dMass.liquid' w l lc 2 t 'Liquid Mass Change', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/dMass.liquid' w l lc 2 notitle, \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/dEnergy.gas' w l axes x1y2 lc 3 t 'Gas Energy Change', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/dEnergy.gas' w l axes x1y2 lc 3 notitle, \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/dEnergy.liquid' w l axes x1y2 lc 4 t 'Liquid Energy Change', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/dEnergy.liquid' w l axes x1y2 lc 4 notitle

set ytics nomirror
set y2tics
set ylabel "Mass (kg/m^3)"
set y2label "Energy (J/m^3)"

plot \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/dMass' w l lc 1 t 'Mass Error', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/dMass' w l lc 1 notitle, \
    'postProcessing_'.gY0.'_'.lY0.'/plot/0/dEnergy' w l axes x1y2 lc 2 t 'Energy Error', \
    for [gY in gYs] for [lY in lYs] \
    'postProcessing_'.gY.'_'.lY.'/plot/0/dEnergy' w l axes x1y2 lc 2 notitle

unset multiplot

EOF
