#!/bin/sh
cd "${0%/*}" || exit 1 # Run from this directory
# Source tutorial run functions
. "$WM_PROJECT_DIR"/bin/tools/RunFunctions
# Set application name
nProcs=16
# 1.a Build the mesh, save the result in log.*
# according to the dictionary system/blockMeshDict
runApplication blockMesh
# 2. Check the mesh, save the result in log.*
# the important values are:
# - max aspect ratio < 10
# - non-ortogonality
runApplication checkMesh
# 3. Overwrite the patch - rename in case of snappyHexMesh
# runApplication createPatch -overwrite
# 4. Toposet
runApplication topoSet
# setFields

PROC_MPI=2

if [ "$nProcs" -ge "$PROC_MPI" ]; then
	 
	# 6. Split the mesh in order to run the solver in parallel
	# according to the dictionary system/decomposeParDict
	runApplication decomposePar
	# 7. msplitMeshRegions
	mpirun -np $nProcs msplitMeshRegions -cellZonesOnly -overwrite -parallel >>log.splitMeshRegions
else
	msplitMeshRegions -cellZonesOnly -overwrite >>log.msplitMeshRegions
fi

cd 0.orig || exit
# copy to the initial time
cp T ../processors$nProcs/0/
cp p_rgh U ../processors$nProcs/0/negativeElectrode
cp p_rgh rhoA U ../processors$nProcs/0/positiveElectrode
cp p_rgh U ../processors$nProcs/0/electrolyte

# 10. Check the mesh, save the result in log.*
# the important values are:
# - max aspect ratio < 10
# - non-ortogonality
cd ..
mv log.checkMesh log.0.checkMesh
runApplication checkMesh
