#!/bin/sh

if [ -z "$NSLOTS" ]; then
    NSLOTS=4
fi

echo "clearing results..."
rm -rf 0.* [1-9]* demMesh.log makeFaMesh.log releaseAreaMapping.log faSavageHutterFoam.log 
rm -rf 0
rm -rf processor*
cp -r org0 0


echo -n "creating FVMesh..."
if slopeMesh > slopeMesh.log; then
    echo "   OK"
else 
	echo "   FAILED"
	exit 1
fi

echo -n "creating FAMesh..."
if makeFaMesh > makeFaMesh.log; then
    echo "   OK"
else 
	echo "   FAILED"
	exit 1
fi

echo -n "setting releaseArea..."
if releaseAreaMapping > releaseAreaMapping.log; then
    echo "   OK"
else 
	echo "   FAILED"
	exit 1
fi

echo -n "running decomposePar"
if decomposePar > log.decomposePar; then
    echo "   OK"
else 
    echo "   FAILED"
    exit 1
fi

echo "running simulation in parallel (N=$NSLOTS)"
if mpirun -np $NSLOTS faSavageHutterFoamExp -parallel | tee log.faSavageHutterFoamExp; then
    echo "   OK"
else 
    echo "   FAILED"
    exit 1
fi
