#!/bin/sh
cd ${0%/*} || exit 1    # Run from this directory

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

# Create dynamic mesh with only one topology change at first write time
cp $FOAM_TUTORIALS/shockFluid/movingCone/constant/dynamicMeshDict constant/dynamicMeshDict
foamDictionary -entry mover -remove constant/dynamicMeshDict
foamDictionary -set '
    topoChanger/timeDelta=1e-10,
    topoChanger/times=( ${${FOAM_CASE}/system/controlDict!writeInterval} ),
' constant/dynamicMeshDict

# Set topology change settings
foamDictionary -set '
    solvers/pcorr={ 
        solver          PCG;
        preconditioner  DIC;
        tolerance       1e-6;
        relTol          0;
    },
    PIMPLE/correctPhi=true
' system/fvSolution

# Generate meshToMesh region
writeInterval=$(foamDictionary -entry writeInterval -value system/controlDict)
mkdir constant/meshToMesh_${writeInterval}
ln -sf ../polyMesh constant/meshToMesh_${writeInterval}/polyMesh 

# Decompose meshToMesh after the main mesh
sed -i "/decomposePar/a runApplication -s ${writeInterval} decomposePar -noFields -region meshToMesh_${writeInterval}" Allrun*
