#!/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
#     Plot phase-specific Courant numbers and residuals at run time.
#
#------------------------------------------------------------------------------

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

gnuplot -p<<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

    TIMES = system('ls -1 postProcessing/residuals')
    TIME(n) = word(TIMES, n)
    NUM = words(TIMES)

    COFILE(s,t) = "postProcessing/".s."/".TIME(t)."/volFieldValue.dat"
    DTFILE(t)   = "postProcessing/timeStep/".TIME(t)."/timeStep.dat"
    REFILE(t)   = "postProcessing/residuals/".TIME(t)."/residuals.dat"

    refreshTime = 2

    finish = 0

    while (1){

        if (finish){
            set term pngcairo size 800,600
            set output '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"
            set y2tics
            set format y2 "10^{%T}"
            set logscale y2

            plot for [i=1:NUM] COFILE("Co"        ,i) w l ls 1    \
                                t (i==1 ? "Co.mixture" : "")    , \
                 for [i=1:NUM] COFILE("Co.gasC"   ,i) w l ls 2    \
                                t (i==1 ? "Co.gasC"    : "")    , \
                 for [i=1:NUM] COFILE("Co.liquidC",i) w l ls 3    \
                                t (i==1 ? "Co.liquidC" : "")    , \
                 for [i=1:NUM] COFILE("Co.liquidD",i) w l ls 4    \
                                t (i==1 ? "Co.liquidD" : "")    , \
                 for [i=1:NUM] DTFILE(             i) w l axis x1y2 ls 5 \
                                t (i==1 ? "dt" : "")

            set ytics mirror
            unset y2label
            unset y2tics
            set autoscale y

            #===

            set xlabel "t"
            set format x "%g"

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

            plot for [i=1:NUM] REFILE(i) u 1:2 w l ls 1           \
                                t (i==1 ? "p\\\_rgh"      : "") , \
                 for [i=1:NUM] REFILE(i) u 1:3 w l ls 2           \
                                t (i==1 ? "k.gasC"        : "") , \
                 for [i=1:NUM] REFILE(i) u 1:4 w l ls 3           \
                                t (i==1 ? "omega.gasC"    : "") , \
                 for [i=1:NUM] REFILE(i) u 1:5 w l ls 4           \
                                t (i==1 ? "k.liquidC"     : "") , \
                 for [i=1:NUM] REFILE(i) u 1:6 w l ls 5           \
                                t (i==1 ? "omega.liquidC" : "")

            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

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