#!/bin/bash

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

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


setCavityFine()
{
    caseName=cavityFine
    blockMeshDict="$caseName/system/blockMeshDict"
    controlDict="$caseName/system/controlDict"
    sed s/"20 20 1"/"41 41 1"/g $blockMeshDict > temp.$$
    mv temp.$$ $blockMeshDict
    #sed \
    #-e s/"\(startTime[ \t]*\) 0;"/"\1 0.5;"/g \
    #-e s/"\(endTime[ \t]*\) 0.5;"/"\1 0.7;"/g \
    #-e s/"\(deltaT[ \t]*\) 0.005;"/"\1 0.0025;"/g \
    #-e s/"\(writeControl[ \t]*\) timeStep;"/"\1 runTime;"/g \
    #-e s/"\(writeInterval[ \t]*\) 20;"/"\1 0.1;"/g \
    #$controlDict > temp.$$
    #mv temp.$$ $controlDict
}

rm -rf cavity*
cp -r $FOAM_TUTORIALS/incompressible/icoFoam/cavity/cavity .
cloneCase cavity cavityFine
setCavityFine

pushd cavityFine > /dev/null
    runApplication blockMesh
    #set parallel version
    cp $FOAM_ETC/caseDicts/preProcessing/decomposeParDict system/decomposeParDict

    #MUST BE ADDED
    foamDictionary system/controlDict -entry startFrom -set latestTime

    runApplication decomposePar
    runParallel icoFoam
popd > /dev/null

pushd cavity > /dev/null
    runApplication blockMesh
    #FAILS by hanging the execution
    cp $FOAM_ETC/caseDicts/preProcessing/decomposeParDict system/decomposeParDict
    runApplication decomposePar
    
    # MUST BE ADDED
    foamDictionary system/controlDict -entry startFrom -set latestTime

    runParallel mapFieldsPar ../cavityFine -sourceTime latestTime -consistent
    #WORKS
    #mapFieldsPar ../cavityFine -sourceTime 0.5 -consistent
    #WORKS
    #mapFields ../cavityFine -sourceTime 0.5 -consistent
popd > /dev/null



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