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

# 'source' case is interFoam laminar damBreak tutorial, run in parallel
# 'target' case is interFoam laminar damBreak tutorial decomposed but not run
# mapFieldsPar from source latestTime to target
# processor boundaries on source and target are compared. 
#  - p_rgh existed on the target before mapping, it's processor0to1 patch is tested
#  - p did not exist on the target before mapping, it's processor0to1 patch is tested
# pass if patches are identical, fail if not.

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

red=$(tput setaf 1)
green=$(tput setaf 2)
reset=$(tput sgr0)

extractPatch() {
    awk '$0 ~ /'"$1"'/,/}/ { print }' $2
}

# MAIN
if [ -d source ]; then 
    printf "Source case already exists, no need to recreate.\n"
else
    printf "Creating and running source case...\n"
    cp -r $FOAM_TUTORIALS/multiphase/interFoam/laminar/damBreak source
    (
	cd source
	runApplication blockMesh > /dev/null
	cp 0/alpha.water.org 0/alpha.water
	runApplication setFields  > /dev/null
	runApplication decomposePar  > /dev/null
	runParallel interFoam 4  > /dev/null
    )
fi

printf "Creating target case\n"
rm -rf target
mkdir target
cp -r source/[0cs]* target
(
    cd target
    runApplication decomposePar > /dev/null
    if [ "$WM_PROJECT_VERSION" = "dev" ]; then
	runParallel mapFieldsPar 4 -consistent -sourceTime latestTime ../source
    else
	runParallel mapFields 4 -consistent -sourceTime latestTime ../source
    fi
)

printf "Testing processor patch mapping:\n"
# test processor patch on a field that existed in target before mapping
sourceDir="source/processor0/1"
targetDir="target/processor0/0"

sourceField="${sourceDir}/p_rgh"
targetField="${targetDir}/p_rgh"

sourcePatch=$(mktemp)
targetPatch=$(mktemp)

extractPatch procBoundary0to1 $sourceField > $sourcePatch
extractPatch procBoundary0to1 $targetField > $targetPatch

if ( diff $sourcePatch $targetPatch > /dev/null ); then
    printf "Pre-existing target field (p_rgh): ${green}PASS${reset}\n"
else
    printf "Pre-existing target field (p_rgh): ${red}FAIL${reset} (see log.preExistingField)\n"
    printf "${sourceField}:\n\n" > log.preExistingField
    cat $sourcePatch >> log.preExistingField
    printf "\n\n${targetField}:\n\n" >> log.preExistingField
    cat $targetPatch >> log.preExistingField
fi

# test processor patch on a field that didn't exist in target before mapping
sourceField="${sourceDir}/p"
targetField="${targetDir}/p"
extractPatch procBoundary0to1 $sourceField > $sourcePatch
extractPatch procBoundary0to1 $targetField > $targetPatch

if ( diff $sourcePatch $targetPatch > /dev/null ); then
    printf "NON pre-existing target field (p): ${green}PASS${reset}\n"
else
    printf "NON pre-existing target field (p): ${red}FAIL${reset} (see log.nonPreExistingField)\n"
    printf "${sourceField}:\n\n" > log.nonPreExistingField
    cat $sourcePatch >> log.nonPreExistingField
    printf "\n\n${targetField}:\n\n" >> log.nonPreExistingField
    cat $targetPatch >> log.nonPreExistingField
fi

# clean up temp files
rm $sourcePatch $targetPatch
