#!/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
#     Extract number of pimple loops from log file into dat file
#
#------------------------------------------------------------------------------

PROGNAME=$(basename "$0")
FORCE=false

usage() {
   while [ "$#" -ge 1 ]; do echo "$1"; shift; done
   cat<<USAGE

usage: $PROGNAME [OPTION] LOGFILE

options:
  -f | --force  remove existing data file
  -h | --help   this usage

* Extract number of pimple loops over runTime from log file into dat file:
  ./postProcessing/nPimpleLoops/nPimpleLoops.dat

USAGE
   exit 1
}

# parse options
while [ "$#" -gt 0 ]
do
   case "$1" in
   -h | --help)
      usage
      ;;
   -f | --force)
      FORCE=true
      shift
      ;;
   -*)
      usage "Error: Invalid option '$1'"
      ;;
   *)
      break
      ;;
   esac
done

# Create some default variables
[ -z "$1" ] && usage "Error: No log file given" || LOGFILE=$1
PWD=$(pwd)
DATPATH="postProcessing/nPimpleLoops"
DATFILE=$DATPATH"/nPimpleLoops.dat"

# Check log file
if [ ! -f "$PWD/$LOGFILE" ]; then
    usage "Error: Log file does not exist:\n  $LOGFILE"
fi

# Remove old files
if [ "$FORCE" == "true" ] && [ -f "$PWD/$DATFILE" ]; then
    echo "  Removing old dat file"
    rm -f "$PWD/$DATFILE"
fi

# Create directories
if [ ! -d $DATPATH ]; then
    echo "  Creating directory "$DATPATH
    mkdir -p $DATPATH
fi

# Reading log file
# Attention: grep require revision to avoid shellcheck warning
#            https://www.shellcheck.net/wiki/SC2207
# shellcheck disable=SC2207
TIMES=($(grep "^Time = " "$LOGFILE" | sed 's/Time = //'))
NUMT=${#TIMES[@]}

# shellcheck disable=SC2207
# cspell: disable-next-line
NLOOPS=($(grep -E "[Cc]onverged (with)?in" "$LOGFILE" | sed 's/.*in //' | \
    sed 's/ iterations//'))
NUMN=${#NLOOPS[@]}

if [ "$NUMT" -ne "$NUMN" ]; then
    if [ "$NUMT" -lt "$NUMN" ]; then
        NUM=$NUMT
    else
        NUM=$NUMN
    fi
    echo "  Numbers of time and nLoops information differ."
    echo "  Using only first $NUM entries."
else
    NUM=$NUMT
fi

# Create dat file
if [ -f "$PWD/$DATFILE" ]; then
    usage "Error: Dat file exists but must not be overwritten either"
else
    echo "  Creating "$DATFILE

    echo "# Time nPimpleLoops" >> "$PWD/$DATFILE"

    if [ "$NUM" -gt 0 ]; then
        for i in $(seq 1 "$NUM"); do
            echo "${TIMES[i-1]} ${NLOOPS[i-1]}" >> "$PWD/$DATFILE"
        done
    else
        echo "  No PIMPLE loops found: nPimpleLoops set to 1."

        echo "0 1" >> "$PWD/$DATFILE"
        echo "${TIMES[$NUMT-1]} 1" >> "$PWD/$DATFILE"
    fi
fi

# Exit
echo "  Done"
exit 0

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