#!/bin/bash

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

gnuplot<<EOF

    set linetype 1 lc rgb "red"     lw 4 dt 1
    set linetype 2 lc rgb "blue"    lw 2 dt 1
    set linetype 3 lc rgb "green"   lw 2 dt 1
    set linetype 4 lc rgb "black"   lw 2 dt 1
    set linetype 5 lc rgb "orange"  lw 2 dt 1
    set linetype 6 lc rgb "gray"    lw 2 dt 1
    set linetype 7 lc rgb "pink"    lw 2 dt 1

    set grid
    set key outside below noenhanced
    set terminal pngcairo enhanced

    #---residuals---
    set     output "residuals-vs-time.png"
    FILE =  "../postProcessing/residuals/0/residuals.dat"
    RFILE = "postProcessing_reference/residuals/0/residuals.dat"
    set     title "Residuals"
    set     xlabel "t"
    set     ylabel "initial residuals"
    set     logscale y
    set     format y "10^{%.0T}"
    headerline = system('head -2 '.FILE.' | tail -1')
    numcols = words(headerline) - 1
    plot for [col=2:numcols] RFILE u 1:col w l lw 2 t 'reference - '.word(headerline,col+1), \
         for [col=2:numcols]  FILE u 1:col w l lw 2 t   'current - '.word(headerline,col+1)

    set     terminal pngcairo enhanced
    unset   logscale y
    set     format y "%g"


    #---probes---
    PATH =      "../postProcessing/probes/0/"
    APATH =     "postProcessing_analytical/probes/0/"
    RPATH =     "postProcessing_reference/probes/0/"

    set output  "probes-UGas1-UGas2.png"
    set title   "probe at top"
    set xlabel  "t"
    set ylabel  "U.x"
    plot        RPATH."U.gas1"                                                          \
                u 1:2 '%lf (%lf %lf %lf) (%lf %lf %lf)' w l lt 4 t "reference - gas 1", \
                RPATH."U.gas2"                                                          \
                u 1:2 '%lf (%lf %lf %lf) (%lf %lf %lf)' w l lt 5 t "reference - gas 2", \
                PATH."U.gas1"                                                           \
                u 1:2 '%lf (%lf %lf %lf) (%lf %lf %lf)' w l lt 1 t "current - gas 1",   \
                PATH."U.gas2"                                                           \
                u 1:2 '%lf (%lf %lf %lf) (%lf %lf %lf)' w l lt 2 t "current - gas 2",   \
                APATH."U.gas1"                                                          \
                u 1:2 '%lf (%lf %lf %lf) (%lf %lf %lf)' w l lt 3 t "analytical"

    set output  "probes-p_rgh.png"
    set title   "probe at centre"
    set xlabel  "t"
    set ylabel  "p_{rgh}"
    plot        RPATH."p_rgh" u 1:3 w l lt 4 lw 5 t "reference",    \
                PATH."p_rgh"  u 1:3 w l lt 1      t "current",      \
                APATH."p_rgh" u 1:3 w l lt 3      t "analytical"


    #---graph---
    TIME =      system("foamListTimes -latestTime -case ..")
    FILE =      "../postProcessing/graph/".TIME."/line.csv"
    RFILE =     "postProcessing_reference/graph/".TIME."/line.csv"
    AFILE =     "postProcessing_analytical/graph/".TIME."/line.csv"

    set output  "graph-UGas1-UGas2.png"
    set title   "graph, t = ".TIME." s"
    set xlabel  "y"
    set ylabel  "U.x"
    plot        RFILE u (column("y")):(column("U.gas1_x")) w l lt 4 t "reference - gas 1", \
                RFILE u (column("y")):(column("U.gas2_x")) w l lt 5 t "reference - gas 2", \
                FILE  u (column("y")):(column("U.gas1_x")) w l lt 1 t "current - gas 1",   \
                FILE  u (column("y")):(column("U.gas2_x")) w l lt 2 t "current - gas 2",   \
                AFILE u (column("y")):(column("U.gas1_x")) w l lt 3 t "analytical"

    set output  "graph-p_rgh.png"
    set title   "graph, t = ".TIME." s"
    set xlabel  "y"
    set ylabel  "p_{rgh}"
    plot        RFILE u (column("y")):(column("p_rgh")) w l lt 4 lw 5 t "reference",    \
                FILE  u (column("y")):(column("p_rgh")) w l lt 1      t "current",      \
                AFILE u (column("y")):(column("p_rgh")) w l lt 3      t "analytical"

EOF

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