#!/bin/bash
cd ${0%/*} || exit 1    # Run from this directory

# Source tutorial run functions
. $WM_PROJECT_DIR/bin/tools/RunFunctions

# Get the serial/parallel mode
! isTest "$@" && [ -n "$1" ] && mode=$1 || mode=serial

case $mode in
    serial)
        ./Allmesh serial
        runApplication foamMultiRun
        ;;
    parallel | stitchSerial)
        ./Allmesh serial

        # Decompose the meshes and fields
        runApplication decomposePar -allRegions
        for meshDir in constant/meshes/*
        do
            runApplication -a decomposePar -mesh "$(basename "$meshDir")" \
                           -region fluid
        done

        runParallel foamMultiRun

        runApplication reconstructPar -allRegions
        ;;
    stitchParallel)
        ./Allmesh parallel

        # Reconstruct the mesh and then decompose the fields
        #
        # !!! Note: We could do decomposePar -copyZero here instead if the
        # field files were set up with boundary conditions for the processor
        # and nonConformalProcessorCyclic patch groups
        #
        runApplication reconstructPar -allRegions -noFields -constant
        runApplication -a decomposePar -allRegions -fields

        runParallel foamMultiRun

        runApplication -a reconstructPar -allRegions
        ;;
    *)
        echo "Error: mode $mode not recognised"
        exit 1
        ;;
esac

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