#!/bin/bash
#---------------------------------*- sh -*-------------------------------------
# ==  == ====== ====   ====    |
#                   \\     ||  | Multiphase Cases Repository by HZDR
# ======   //   ||  || ===//   | Website: https://doi.org/10.14278/rodare.811
# ||  ||  //    ||  // || \\   | License: Creative Commons Attribution 4.0
# ==  == ====== ====   ==  ==  |
#------------------------------------------------------------------------------
# License
#     This file is part of the Multiphase Cases Repository by HZDR.
#
#     Copyright (C) 2025 by Helmholtz-Zentrum Dresden-Rossendorf e.V. (HZDR),
#     Website: https://hzdr.de
#
#     The Multiphase Cases Repository by HZDR is licensed under the
#     Creative Commons Attribution 4.0 International License. To view a copy of
#     this license, visit http://creativecommons.org/licenses/by/4.0/ or send a
#     letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
#
# Description
#     Monitor simulation data on-the-run
#
#------------------------------------------------------------------------------

if ! which gnuplot > /dev/null 2>&1
then
    echo 'gnuplot not found - skipping graph creation' >&2
    exit 1
else
    mkdir -p ../postProcessing/report
fi

gnuplot -p 2> /dev/null <<EOF

    set terminal qt size 800,600 noraise
    set grid
    set key top left

    set lmargin at screen 0.08
    set rmargin at screen 0.93

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

    COFILE(s) = "postProcessing/".s."/0/volFieldValue.dat"
    DTFILE    = "postProcessing/timeStep/0/timeStep.dat"
    REFILE    = "postProcessing/residuals/0/residuals.dat"

    refreshTime = 0.5

    finish = 0

    while (1){

        if (finish){
            set term pngcairo size 800,600
            set output '../postProcessing/report/validation/Monitor.png'
            set lmargin at screen 0.1
            set rmargin at screen 0.9
        }

        set multiplot layout 2,1

            unset xlabel
            set format x ""
            set ylabel "Co [-]"
            set yrange [0:1.2]
            set ytics nomirror
            set y2label "dt [s]"
            set y2tics
            set format y2 "10^{%T}"
            set logscale y2

            plot COFILE("Co"      ) w l           t "Co.mixture"    , \
                 COFILE("Co.airC" ) w l           t "Co.airC"       , \
                 COFILE("Co.airD" ) w l           t "Co.airD"       , \
                 COFILE("Co.water") w l           t "Co.water"      , \
                 DTFILE             w l axis x1y2 t "dt"

            set ytics mirror
            unset y2label
            unset y2tics
            set autoscale y

            #===

            set xlabel "t [s]"
            set format x "%g"

            set ylabel "Resisuals [-]"
            set logscale y
            set format y "10^{%T}"
            set yrange [:1]

            plot REFILE u 1:2 w l t "p\\\_rgh"     , \
                 REFILE u 1:3 w l t "k.airC"       , \
                 REFILE u 1:4 w l t "omega.airC"   , \
                 REFILE u 1:5 w l t "k.water"      , \
                 REFILE u 1:6 w l t "omega.water"

            unset logscale y
            set format y "%g"
            set autoscale y

        unset multiplot

        if (finish){
            print('FINISH')
            break
        }

        pause refreshTime

        now = int(system("date +%s"))
        lastModified = \
            int(system("stat postProcessing/timeStep/0/timeStep.dat -c %Y"))
        if ( (now-lastModified) > (3.0*refreshTime) ){
            finish = 1
        }
    }

EOF

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