#!/bin/bash

if ! which gnuplot > /dev/null 2>&1
then
    echo 'gnuplot not found - skipping graph creation' >&2
    exit 1
fi

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

graphFile=../postProcessing/graph/$time/line.csv
referFile=reference/line.csv
fluidFile=fluid/line.csv
analyFile=analytical/line.csv

gnuplot<<EOF
    set terminal pngcairo
    set key outside below
    set grid

    set style line 1 dt 2 lt 1 lw 2 pt 2        lc "black"
    set style line 2 dt 1 lt 2 lw 1 pt 2        lc "red"
    set style line 3      lt 3 lw 2 pt 4 ps 1.2 lc "green"
    set style line 4 dt 4 lt 4 lw 2 pt 2        lc "violet"
    set style line 5 dt 2 lt 5 lw 2 pt 2        lc "cyan"

    set xlabel 'x [m]'
    set xrange [-5:5]
    set xtics 1

    set output 'T.png'
    set ylabel 'T [K]'
    plot "$analyFile" u (column('x')):(column('T'))     w l ls 2 t 'Analytical'     , \
         "$fluidFile" u (column('x')):(column('T'))     w l ls 4 t 'Fluid'          , \
         "$referFile" u (column('x')):(column('T.gas')) w l ls 3 t 'Reference'      , \
         "$graphFile" u (column('x')):(column('T.gas')) w l ls 1 t 'Simulation'

    set output 'U.png'
    set ylabel 'U [m/s]'
    set yrange [-10:]
    plot "$analyFile" u (column('x')):(column('U_x'))     w l ls 2 t 'Analytical'   , \
         "$fluidFile" u (column('x')):(column('U_x'))     w l ls 4 t 'Fluid'        , \
         "$referFile" u (column('x')):(column('U.gas_x')) w l ls 3 t 'Reference'    , \
         "$graphFile" u (column('x')):(column('U.gas_x')) w l ls 1 t 'Simulation'

    set output 'p.png'
    set ylabel 'p [Pa]'
    set yrange [0:1.05e5]
    plot "$analyFile" u (column('x')):(column('p')) w l ls 2 t 'Analytical'         , \
         "$fluidFile" u (column('x')):(column('p')) w l ls 4 t 'Fluid'              , \
         "$referFile" u (column('x')):(column('p')) w l ls 3 t 'Reference'          , \
         "$graphFile" u (column('x')):(column('p')) w l ls 1 t 'Simulation'

    set output 'rho.png'
    set ylabel '{/Symbol r} [kg/m^3]'
    set yrange [0:1.05]
    plot "$analyFile" u (column('x')):(column('rho'))     w l ls 2 t 'Analytical'   , \
         "$fluidFile" u (column('x')):(column('rho'))     w l ls 4 t 'Fluid'        , \
         "$referFile" u (column('x')):(column('rho.gas')) w l ls 3 t 'Reference'    , \
         "$graphFile" u (column('x')):(column('rho.gas')) w l ls 1 t 'Simulation'

EOF

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