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

eval `tutoInDevel.sh` # Tutorial in development. Usage: ./Allrun <continue_flag> <error_msg>

ROOTDIR=`pwd`

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

# Check number of processors
if [ -z $1 ];
then
    echo "error: correct usage = ./Allrun <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_cmd) -i 's/numberOfSubdomains \+[0-9]*;/numberOfSubdomains '"$NPROCESSOR"';/g' Velocity_X_Direction/system/decomposeParDict
$(sed_cmd) -i 's/numberOfSubdomains \+[0-9]*;/numberOfSubdomains '"$NPROCESSOR"';/g' Velocity_Y_Direction/system/decomposeParDict
$(sed_cmd) -i 's/numberOfSubdomains \+[0-9]*;/numberOfSubdomains '"$NPROCESSOR"';/g' Velocity_Z_Direction/system/decomposeParDict 

# run 1_Velocity_X_Direction
cd Velocity_X_Direction
./Allclean
./AllrunBasic

# run 2_Velocity_Y_Direction
cd $ROOTDIR
cd Velocity_Y_Direction
./Allclean
./AllrunBasic

# run 3_Velocity_Z_Direction
cd $ROOTDIR
cd Velocity_Z_Direction
./Allclean
./AllrunBasic

# Calculate K tensor
cd $ROOTDIR
#python3 Permeability_Tensor_Estimation.py # error macOS

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