#!/bin/sh
#cd ${0%/*} || exit 1    # Run from this directory
#cd $1 || exit 1 # Run from the parent script $PWD
# Source tutorial run functions
#. $WM_PROJECT_USER_DIR/XylemFoam/tools/RunFunctions #Prints log to stdout AND the log.* file
#. $WM_PROJECT_DIR/bin/tools/RunFunctions #Prints log to the log.* file only


logHeader "START 'AllmeshCf'"

echo "Copying O.orig/ to 0/..."

mkdir -p 0
cp 0.orig/* 0/

#runApplication preparePar
runApplication cartesianMesh
#runApplication cartesianMesh -parallel


if [ "$runCommand" = "runParallel" ];
then
  runApplication decomposePar -copyZero
  decomposed=true
fi

i=0

  $runCommand -suffix initial -overwrite checkMesh

while [ $i -lt $nImproveCycles ]
do
  $runCommand -suffix $i improveMeshQuality
  if [ $i -lt $((nImproveCycles-1)) ]
  then
    $runCommand -suffix $i checkMesh
  fi
  i=$((i+1))
done

#./checkMesh
$runCommand -suffix "final" -overwrite checkMesh -writeSets vtk

if [ "$runCommand" = "runParallel" ];
then
    runApplication reconstructParMesh -mergeTol 1E-06 -constant

    #./copyProcessorFields

    #runApplication reconstructPar -latestTime -withZero -constant
   # runApplication reconstructPar -latestTime

  if [ "$(isReconstructed)" == "true" ];
  then
    rm -rf processor*/
    rm log.decomposePar
    decomposed=false
  else
    echo "WARNING!: Case is not properly reconstructed."
  fi

    if [ "$topoSet" = true ]
    then
        runApplication -suffix "serial" topoSet
    fi


fi

logHeader "END 'AllMeshCf'"

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