#!/bin/bash
#---------------------------------*- sh -*-------------------------------------
# ==  == ====== ====   ====    |
#                   \\     ||  | Multiphase Code Repository by HZDR
# ======   //   ||  || ===//   | Website: https://doi.org/10.14278/rodare.767
# ||  ||  //    ||  // || \\   | License: GPL-3.0-or-later
# ==  == ====== ====   ==  ==  |
#------------------------------------------------------------------------------
# License
#     This file is part of the Multiphase Code Repository by HZDR.
#
#     Copyright (C) 2025 by Helmholtz-Zentrum Dresden-Rossendorf e.V. (HZDR),
#     Website: https://hzdr.de
#
#     Multiphase Code Repository by HZDR is based on the free software for
#     computational fluid dynamics (CFD) from the OpenFOAM Foundation.
#     Copyright (C) 2025 by OpenFOAM Foundation, Website: https://openfoam.org
#
#     If you are interested in which files are original OpenFOAM Foundation
#     files, which OpenFOAM Foundation files were modified, and which files were
#     newly created, see FILES.md.
#
#     Multiphase Code Repository by HZDR is free software: you can redistribute
#     it and/or modify it under the terms of the GNU General Public License as
#     published by the Free Software Foundation, either version 3 of the
#     License, or (at your option) any later version.
#
#     Multiphase Code Repository by HZDR is distributed in the hope that it will
#     be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
#     Public License for more details.
#
#     You should have received a copy of the GNU General Public License along
#     with the Multiphase Code Repository by HZDR. If not, see
#     <http://www.gnu.org/licenses/>.
#
# Description
#     This script plots Courant number and, if adjustable, the time step size
#     of a running simulation. It extracts the data from the log file and saves
#     the gnuplot plot as PNG file.
#
#------------------------------------------------------------------------------

FILE=${1:-log.foamRun}
OFILE="postProcessing/${FILE}.CourantDeltaT"
IMGFILE="postProcessing/courantDeltaT.png"

# Ensure existence of output directory and remove old image
mkdir -p postProcessing
rm -f "${OFILE}" "${IMGFILE}"

# Check input file
if [ ! -f "${FILE}" ]; then
    echo "FOAM FATAL ERROR: File does not exist: ${FILE}"
    exit 1
fi

if grep -q "^deltaT = " "${FILE}"; then
    # Adaptive time step: Courant and deltaT

    # Extract data
    echo "# Courant DeltaT Time" > "${OFILE}"
    grep -e "^Time = " -e "^Courant" -e "^deltaT = " "${FILE}" \
    | tail -n +2 \
    | sed 's/Courant\ Number\ mean:.*max:\ //g' \
    | sed -e ':a' -e 'N' -e '$!ba' -e 's/\ndeltaT\ =\ / /g' \
    | sed -e ':a' -e 'N' -e '$!ba' -e 's/\nTime\ =\(\ [0-9.e-]*\)s/\1/g'  \
    >> "${OFILE}"

    # Plot
    gnuplot -e "set terminal png size 600,400; \
                set output \"${IMGFILE}\"; \
                set grid; \
                set xlabel \"t\"; \
                set ylabel \"Co\"; \
                set y2label \"dt\"; \
                set ytics nomirror; \
                set y2tics; \
                set format y2 \"10^{%T}\"; \
                set logscale y2; \
                plot \"${OFILE}\" using 3:1 with l t \"Co\", \
                     \"${OFILE}\" using 3:2 axis x1y2 with l t \"dt\""

else # Constant time step: Courant only
    regexp="Courant Number mean: [0-9.e-]* max: \([0-9.e-]*\)\n"

    # Extract data
    echo "# Courant Time" > "${OFILE}"
    grep -e "^Time = " -e "^Courant" "${FILE}" \
    | tail -n+2 \
    | sed ':a;N;$!ba;s/'"${regexp}"'/\1 /g' \
    | sed 's/Time = \([0-9.e-]*\)s$/\1/' \
    >> "${OFILE}"

    # Plot
    gnuplot -e "set terminal png size 600,400; \
                set output \"${IMGFILE}\"; \
                set grid; \
                set xlabel \"t\"; \
                set ylabel \"Co\"; \
                plot \"${OFILE}\" using 2:1 with l t \"Co\""
fi

# Report
if [ -f "${IMGFILE}" ]; then
    echo "created ${IMGFILE}"
else
    echo "FOAM FATAL ERROR: Not created: ${IMGFILE}"
    exit 1
fi
