From c5beee63f37e01827c387db3c18227e7f45db6bc Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Fri, 30 Nov 2018 19:29:49 +0100
Subject: [PATCH] ENH: add isTrue function to RunFunctions

- check if the first argument corresponds to an OpenFOAM value for
  'true' (as per Switch).
  True == 't', 'y', 'true', 'yes', 'on'. Everything else is not true.

- when the first argument is '-dict', it initializes the value
  with a query via foamDictionary.
  Eg,
       isTrue -dict mydict -entry parallel

   ==> value=$(foamDictionary mydict -entry parallel -value)
       isTrue $value

   a missing entry is silently treated as false.

ENH: add getNumberOfPatchFaces function in RunFunctions

- simple extraction of nFaces from boundary file for given patch/region
---
 bin/tools/RunFunctions                        | 70 +++++++++++++++++++
 .../externalCoupledHeater/externalSolver      | 46 ++++++++----
 .../externalCoupledHeater/externalSolver      | 46 ++++++++----
 3 files changed, 134 insertions(+), 28 deletions(-)

diff --git a/bin/tools/RunFunctions b/bin/tools/RunFunctions
index ed1e5357039..6efa5b74744 100644
--- a/bin/tools/RunFunctions
+++ b/bin/tools/RunFunctions
@@ -52,6 +52,76 @@ notTest()
 }
 
 
+#
+# Check if '$1' corresponds to an OpenFOAM value for 'true' (see Switch.H)
+# - does not handle integers very much, although Switch does
+#
+# Handles -dict as first argument to relay the balance to foamDictionary
+# Eg,
+#     isTrue -dict controls -entry coupling
+# ->
+#     value=$(foamDictionary controls -entry coupling -value)
+#     if value ...
+#
+isTrue()
+{
+    local value="$1"
+
+    if [ "$value" = "-dict" ]
+    then
+        shift
+        value="$(foamDictionary -value $@ 2>/dev/null)" || return 2
+    fi
+
+    case "$value" in
+        (t | y | true | yes | on)  return 0 ;;
+        (f | n | false | no | off) return 1 ;;
+    esac
+    return 2
+}
+
+
+#
+# Extract 'nFaces' for given patchName from constant/polyMesh/boundary
+# or constant/{region}/polyMesh/boundary
+#
+# On failure:
+#    return '1'
+#    exit status 1
+#
+getNumberOfPatchFaces()
+{
+    local patch="${1:-}"
+    local file="${2:-}"
+
+    file="constant/$file${file:+/}polyMesh/boundary"
+
+    [ -n "$patch" ] || {
+        echo "No patch name given" 1>&2
+        return 1
+    }
+
+    [ -f "$file" ] || {
+        echo "No such file: $file" 1>&2
+        return 2
+    }
+
+    local nFaces
+    nFaces=$(sed -ne \
+        '/^ *'"$patch"' *$/,/}/{s/^ *nFaces  *\([0-9][0-9]*\) *;.*$/\1/p}' \
+        "$file")
+
+    if [ -n "nFaces" ]
+    then
+        echo "$nFaces"
+    else
+        echo "No patch entry found for '$patch' in $file" 1>&2
+        echo 0      # Report as 0
+        return 2
+    fi
+}
+
+
 #
 # Extract 'numberOfSubdomains' from system/decomposeParDict
 # (or alternative location).
diff --git a/tutorials/heatTransfer/chtMultiRegionFoam/externalCoupledHeater/externalSolver b/tutorials/heatTransfer/chtMultiRegionFoam/externalCoupledHeater/externalSolver
index 3b7080915d6..dc8b42fe155 100755
--- a/tutorials/heatTransfer/chtMultiRegionFoam/externalCoupledHeater/externalSolver
+++ b/tutorials/heatTransfer/chtMultiRegionFoam/externalCoupledHeater/externalSolver
@@ -1,12 +1,14 @@
 #!/bin/sh
-#
+cd ${0%/*} || exit 1                        # Run from this directory
+. $WM_PROJECT_DIR/bin/tools/RunFunctions    # Tutorial run functions
+
 # Dummy external solver to communicate with OpenFOAM via externalCoupled
 # functionObject
 #
 # Functionality is hard-coded for this particular test case
 # - patch temperatures increased by 1K on each step
 #
-cd ${0%/*} || exit 1    # Run from this directory
+# -----------------------------------------------------------------------------
 
 # Check for unassigned variables
 set -u
@@ -53,28 +55,44 @@ stopMasterNow()
 }
 
 
+# Patch size (heater/minY)
+nFaces1=$(getNumberOfPatchFaces minY heater) || exit $?
+
+# Patch size (topAir/minX)
+nFaces2=$(getNumberOfPatchFaces minX topAir) || exit $?
+
+
 init()
 {
     log "init - creating ${dataFile}.in"
     cat /dev/null >| "${dataFile}.in"
 
-    # Hard-coded for patch of size 8 (heater/minY)
-    local n1=8
-    local refValue1=500
+    # Local face counter, Local refValue
+    local nFaces refValue
 
-    log "init - adding $n1 data elements with refValue $refValue1"
-    for i in $(seq 1 $n1)
+    # Patch (heater/minY)
+    nFaces="$nFaces1"
+    refValue=500
+
+    log "init - adding $nFaces data elements with refValue $refValue"
+
+    while [ "$nFaces" -gt 0 ]
     do
-        echo "$refValue1 $refGrad $valueFraction"
+        nFaces=$((nFaces - 1))
+        echo "$refValue $refGrad $valueFraction"
     done >> "${dataFile}.in"
 
-    # Hard-coded for patch of size 40 (topAir/minX)
-    local n2=40
-    local refValue2=300
-    log "init - adding $n2 data elements with refValue $refValue2"
-    for i in $(seq 1 $n2)
+
+    # Patch (topAir/minX)
+    nFaces="$nFaces2"
+    refValue=300
+
+    log "init - adding $nFaces data elements with refValue $refValue"
+
+    while [ "$nFaces" -gt 0 ]
     do
-        echo "$refValue2 $refGrad $valueFraction"
+        nFaces=$((nFaces - 1))
+        echo "$refValue $refGrad $valueFraction"
     done >> "${dataFile}.in"
 
     # Verify line count?
diff --git a/tutorials/heatTransfer/chtMultiRegionSimpleFoam/externalCoupledHeater/externalSolver b/tutorials/heatTransfer/chtMultiRegionSimpleFoam/externalCoupledHeater/externalSolver
index 5afbca0ff4c..d83dd3109ca 100755
--- a/tutorials/heatTransfer/chtMultiRegionSimpleFoam/externalCoupledHeater/externalSolver
+++ b/tutorials/heatTransfer/chtMultiRegionSimpleFoam/externalCoupledHeater/externalSolver
@@ -1,12 +1,14 @@
 #!/bin/sh
-#
+cd ${0%/*} || exit 1                        # Run from this directory
+. $WM_PROJECT_DIR/bin/tools/RunFunctions    # Tutorial run functions
+
 # Dummy external solver to communicate with OpenFOAM via externalCoupled
 # functionObject
 #
 # Functionality is hard-coded for this particular test case
 # - patch temperatures increased by 1K on each step
 #
-cd ${0%/*} || exit 1    # Run from this directory
+# -----------------------------------------------------------------------------
 
 # Check for unassigned variables
 set -u
@@ -53,28 +55,44 @@ stopMasterNow()
 }
 
 
+# Patch size (heater/minY)
+nFaces1=$(getNumberOfPatchFaces minY heater) || exit $?
+
+# Patch size (topAir/minX)
+nFaces2=$(getNumberOfPatchFaces minX topAir) || exit $?
+
+
 init()
 {
     log "init - creating ${dataFile}.in"
     cat /dev/null >| "${dataFile}.in"
 
-    # Hard-coded for patch of size 8 (heater/minY)
-    local n1=8
-    local refValue1=500
+    # Local face counter, Local refValue
+    local nFaces refValue
 
-    log "init - adding $n1 data elements with refValue $refValue1"
-    for i in $(seq 1 $n1)
+    # Patch (heater/minY)
+    nFaces="$nFaces1"
+    refValue=500
+
+    log "init - adding $nFaces data elements with refValue $refValue"
+
+    while [ "$nFaces" -gt 0 ]
     do
-        echo "$refValue1 $refGrad $valueFraction"
+        nFaces=$((nFaces - 1))
+        echo "$refValue $refGrad $valueFraction"
     done >> "${dataFile}.in"
 
-    # Hard-coded for patch of size 40 (topAir/minX)
-    local n2=40
-    local refValue2=300
-    log "init - adding $n2 data elements with refValue $refValue2"
-    for i in $(seq 1 $n2)
+
+    # Patch (topAir/minX)
+    nFaces="$nFaces2"
+    refValue=300
+
+    log "init - adding $nFaces data elements with refValue $refValue"
+
+    while [ "$nFaces" -gt 0 ]
     do
-        echo "$refValue2 $refGrad $valueFraction"
+        nFaces=$((nFaces - 1))
+        echo "$refValue $refGrad $valueFraction"
     done >> "${dataFile}.in"
 
     # Verify line count?
-- 
GitLab