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

# Source PATO run functions
. $PATO_DIR/src/applications/utilities/runFunctions/RunFunctions

# Initialize the script
pato_init

# source OpenFOAM and PATO if darwin
if [ "$(uname)" = "Darwin" ]; then
    source $FOAM_ETC/bashrc
    source $PATO_DIR/bashrc
fi

# Check number of processors
if [ -z $1 ];
then
    echo "error: correct usage = ./Allrun_parallel <number_processors>"
    exit 1
fi
re='^[0-9]+$'
if ! [[ $1 =~ $re ]] ; then
   echo "error: First argument is not a number" >&2
   exit 1
fi

NPROCESSOR=$1

# change the decomposeParDict files
sed -i 's/numberOfSubdomains \+[0-9]*;/numberOfSubdomains '"$NPROCESSOR"';/g' system/decomposeParDict
sed -i 's/numberOfSubdomains \+[0-9]*;/numberOfSubdomains '"$NPROCESSOR"';/g' system/porousMat/decomposeParDict


# create volume fields (first time step : 50 s)
scp -r origin.0 0
scp -r constant/porousMat/copy.polyMesh constant/porousMat/polyMesh

count=`ls -1 processor* 2>/dev/null | wc -l`
if [ $count != 0 ];
then
    rm -rf processor*
fi

# decompose the mesh                                                                                                          
decomposePar -region porousMat

#run case
mpiexec -np $NPROCESSOR PATOx -parallel

# 3D post-processing
# reconstructPar -region porousMat
# reconstructPar -region subMat1 
# paraFoam -builtin
