#!/bin/bash

# Usage
# Run in serial:
# $> ./Allrun
# Run in parallel:
# $> ./Allrun parallel

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

# Compatibility changes for foam extend
if [[ $WM_PROJECT = "foam" ]]
then
    # Copy blockMeshDict to constant/polyMesh
    mkdir constant/polyMesh
    cp system/blockMeshDict constant/polyMesh/
fi

# Create mesh
runApplication blockMesh

if [[ "$1" == "parallel" ]]; then
    # Decompose the case
    runApplication decomposePar

    # Run solver in parallel
    mpirun -np 9 pythonIcoFoam -parallel > log.pythonIcoFoam

    # Reconstruct the case
    runApplication reconstructPar
        
else
    # Run solver in serial
    runApplication pythonIcoFoam
fi

if [[ $WM_PROJECT = "foam" ]]
then
    echo "Plots not created for foam extend"; echo
else
    # Extract components of U
    runApplication -s componentsU postProcess -func "components(U)" -latestTime

    if [[ $WM_PROJECT_VERSION = "v2012" ]]
    then
        runApplication -s singleGraph postProcess -func singleGraph -latestTime
        runApplication gnuplot plotSingleGraph.gnuplot
    elif [[ $WM_PROJECT_VERSION = "9" ]]
    then
        runApplication -s graphUniform postProcess -func graphUniform -latestTime
        runApplication gnuplot plotGraphUniform.gnuplot
    fi

    echo "See plot.pdf"; echo
fi

