#!/bin/bash

set -euo pipefail

case_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
cd "$case_dir"

"$case_dir/clean.sh" --step1-only > /dev/null

cp -r 0_org 0

mkdir -p log

nProcs=$(foamDictionary -entry numberOfSubdomains -value system/decomposeParDict)

decomposePar -force 2>&1 | tee log/decomposePar
mpirun -np "$nProcs" renumberMesh -parallel -overwrite 2>&1 | tee log/renumberMesh
mpirun -np "$nProcs" simpleFoam -parallel 2>&1 | tee log/simpleFoam

reconstructPar -latestTime 2>&1 | tee log/reconstructPar

find "$case_dir" -mindepth 1 -maxdepth 1 -type d \
    -regextype posix-extended -regex '.*/processor[0-9]+' \
    -exec rm -rf -- {} +

simpleFoam -postProcess -func wallShearStress -noZero -noFunctionObjects 2>&1 | tee log/simpleFoam_postProcess

postProcess -func sampleDict -time 2000 2>&1 | tee log/postProcess

foamToVTK -latestTime \
    -fields '(UMean pMean nutMean UPrime2Mean pPrime2Mean nutPrime2Mean)' \
    -name VTK_Mean 2>&1 | tee log/exportVTK