#!/bin/bash

runParallel()
{
    nproc="$1"
    shift
    exe="$(which $1)"
    sol=$(basename -- "$1")
    sol="${sol%.*}"
    shift
    if [ -f log."$sol" ]; then rm log."$sol"; fi
    export OMPI_MCA_btl_vader_single_copy_mechanism=none  # Workaround for open-mpi/docker bug
    mpiexec  -np $nproc "$exe" -parallel "$@" 1> >(tee -a log."$sol") 2> >(tee -a log."$sol" >&2)
    err=$?
    if [ ! $err -eq 0 ]; then exit $err; fi
}

runCommand()
{
    sol=$(basename -- "$1")
    sol="${sol%.*}"
    if [ -f log."$sol" ]; then rm log."$sol"; fi
    "$@" 1> >(tee -a log."$sol") 2> >(tee -a log."$sol" >&2)
    err=$?
    if [ ! $err -eq 0 ]; then exit $err; fi
}

# Unset and source bashrc
if [ ! -z "$FOAMDIR" ]
then
    source "$FOAMDIR/etc/config.sh/unset" 2> /dev/null
    source "$FOAMDIR/etc/bashrc"
fi

# Copy mesh from mesh case dir if available
MESHDIR="../meshCaseblock"
if [ -f "$MESHDIR"/constant/polyMesh/faces ]
then
    rm -rf constant/polyMesh
    cp -r "$MESHDIR"/constant/polyMesh constant/polyMesh
elif [ ! -f constant/polyMesh/faces ]
then
    echo "Fatal error: Unable to find mesh in directory $MESHDIR" 1>&2
    exit 1
fi

# Detect available turbulence lib
if [ -f "$FOAM_LIBBIN/libmomentumTransportModels.so" ] || [ -f "$FOAM_LIBBIN/libmomentumTransportModels.dll" ]
then
    echo \"libmomentumTransportModels.so\" > system/turbulenceLib
else
    echo \"libturbulenceModels.so\" > system/turbulenceLib
fi

# Change specification of interface compression in OF 13+
if [ -z ${FOAM_API+x} ] && [ "$WM_PROJECT_VERSION" -ge 13 ]
then
    echo "div(phi,alpha) Gauss interfaceCompression vanLeer 1;" > system/alphaDivScheme
    echo "" > system/cAlpha
else
    echo "div(phi,alpha) Gauss vanLeer;" > system/alphaDivScheme
    echo "cAlpha 1;" > system/cAlpha
fi

# Update patch name and type
runCommand createPatch -overwrite

PNAME=p

# Mesh renumbering
runCommand renumberMesh -overwrite

# Initialise flow
runCommand potentialFoam -initialiseUBCs -pName $PNAME -writep

# Run application
# Detect new foamRun in Foundation versions >= 11 and translate solver
which foamRun > /dev/null 2>&1
if [ $? == 0 ]
then
    runCommand foamRun -solver incompressibleFluid
else
    runCommand simpleFoam
fi

