#!/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

# create volume fields                                                                                                                             
  cp -rf origin.0 0

# generate mesh blocks
blockMesh -region tube

# change the decomposeParDict files 
cmd_sed=sed
if [ "$(uname)" = "Darwin" ]; then
    cmd_sed=gsed
    source $FOAM_ETC/bashrc
    source $PATO_DIR/bashrc
fi

# run PATOx
if [ -z $1 ];
then
    PATOx
else
    re='^[0-9]+$'
    if ! [[ $1 =~ $re ]] ; then
        echo "error: First argument is not a number" >&2
        echo "Correct usage = ./Allrun <number_processors>"
        exit 1
    fi
    $cmd_sed -i 's/numberOfSubdomains \+[0-9]*;/numberOfSubdomains '"$1"';/g' system/tube/decomposeParDict
    decomposePar -allRegions -force
    mpirun -np $1 PATOx -parallel
    reconstructPar -allRegions
fi

# Create data
postProcess -region  tube  -func sampleDict
postProcess -region  tube  -func sampleDict1


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