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

# Source run functions
. "$WM_PROJECT_SITE/$WM_PROJECT_VERSION/bin/tools/RunFunctions"

MODELS="KleinKetterlExtendedGradient \
        KobayashiKetterlExtendedGradient-LS \
        KobayashiKetterlExtendedGradient-KKK1 \
        MixedscaleSimilaritySmagorinsky \
        NicoudSigma \
        pureClark \
        pureKetterlExtendedGradient \
        purescaleSimilarity"

for FULLMODEL in ${MODELS}; do

    # Prepare case
    if [ "${FULLMODEL}" = "KobayashiKetterlExtendedGradient-LS" ]; then

        MODEL="KobayashiKetterlExtendedGradient"
        mkdir ${MODEL}
        DIR="${MODEL}/LS"

    elif [ "${FULLMODEL}" = "KobayashiKetterlExtendedGradient-KKK1" ]; then

        MODEL="KobayashiKetterlExtendedGradient"
        mkdir ${MODEL}
        DIR="${MODEL}/KKK1"
        KFACT="1"

    else

        DIR=${FULLMODEL}
        MODEL=${FULLMODEL}

    fi

    foamCloneCase baseCase "${DIR}"
    foamDictionary -entry LES/LESModel -set "${MODEL}" \
        "${DIR}/constant/momentumTransport.air"
    foamDictionary -entry LES/LESModel -set "${MODEL}" \
        "${DIR}/constant/momentumTransport.water"

    if [ -n "${KFACT}" ]; then
        foamDictionary -entry LES/kFactor -set "${KFACT}" \
            "${DIR}/constant/momentumTransport.air"
        foamDictionary -entry LES/kFactor -set "${KFACT}" \
            "${DIR}/constant/momentumTransport.water"
        unset KFACT
    fi

    # Run case
    "${DIR}/Allrun"

    # Compare solutions
    cp "validation/${DIR}/"*".ref.gz" "${DIR}/1/"

    if [[ ${MODEL} != "NicoudSigma" ]]; then
        runApplication -s "${FULLMODEL}.tauSgs.air" foamPostProcess -case \
            "${DIR}" -time 1 \
            -func "deviation(field=tauSgs.air, reference=tauSgs.air.ref)"
        runApplication -s "${FULLMODEL}.tauEff.air" foamPostProcess \
            -case "${DIR}" -time 1 \
            -func "deviation(field=tauEff.air, reference=tauEff.air.ref)"
    fi

    if [[ ${MODEL} = "NicoudSigma" ||
          ${MODEL} = "MixedscaleSimilaritySmagorinsky" ]]; then
        runApplication -s "${FULLMODEL}.nut.air" foamPostProcess \
            -case "${DIR}" -time 1 \
            -func "deviation(field=nut.air, reference=nut.air.ref)"
    fi

done

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