File: externalSolver

package info (click to toggle)
openfoam 1812%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 220,284 kB
  • sloc: cpp: 1,038,902; sh: 14,536; ansic: 8,240; lex: 657; xml: 387; python: 300; awk: 212; makefile: 94; sed: 88; csh: 3
file content (176 lines) | stat: -rwxr-xr-x 3,996 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/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
#
# -----------------------------------------------------------------------------

# Check for unassigned variables
set -u

echo "Executing dummy external solver"

commsDir="comms"
patchDir="heater_topAir/coupleGroup"
fieldName="T"

lockFile="${commsDir}/OpenFOAM.lock"
dataFile="${commsDir}/${patchDir}/${fieldName}"
waitSec=5
timeOut=100
nSteps=1000 # maximum number of time steps. Note: should be more than
            # number of iterations on the OpenFOAM side

stopAt=600  # external solver signals OpenFOAM to stop

refGrad=0
valueFraction=1

# Remove any old junk
\rm -f $lockFile 2>/dev/null

log()
{
    echo "External: $@"
}


# Create lock file to pass control to OpenFOAM
useMaster()
{
    log "creating lock file '${lockFile}'"
    echo "status=openfoam" >| ${lockFile}
}

# Lock file with special content to stop OpenFOAM master
stopMasterNow()
{
    log "writeNow terminate via lock file '${lockFile}'"
    echo "action=writeNow" >| ${lockFile}
}


# 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"

    # Local face counter, Local refValue
    local nFaces refValue

    # Patch (heater/minY)
    nFaces="$nFaces1"
    refValue=500

    log "init - adding $nFaces data elements with refValue $refValue"

    while [ "$nFaces" -gt 0 ]
    do
        nFaces=$((nFaces - 1))
        echo "$refValue $refGrad $valueFraction"
    done >> "${dataFile}.in"


    # Patch (topAir/minX)
    nFaces="$nFaces2"
    refValue=300

    log "init - adding $nFaces data elements with refValue $refValue"

    while [ "$nFaces" -gt 0 ]
    do
        nFaces=$((nFaces - 1))
        echo "$refValue $refGrad $valueFraction"
    done >> "${dataFile}.in"

    # Verify line count?
    # log "init ($(wc -l ${dataFile}.in))"

    # Give time for T.in file to flush
    sleep 1

    useMaster
}


# Create the comms directory
mkdir -p ${commsDir}/${patchDir}

# Tutorial case uses 'initByExternal' option, so we must provide initial values
init


totalWait=0
step=0
while [ $step -lt $nSteps ]
do
    if [ -f $lockFile ]
    then
        if grep -q "status=done" ${lockFile}
        then
             log "found lock file '${lockFile}' with 'status=done' - finished"
             break
        elif [ -s $lockFile ]
        then
             log "found lock file '${lockFile}' containing '$(< $lockFile)' - waiting"
        else
             log "found lock file '${lockFile}' - waiting"
        fi

        totalWait=$(expr $totalWait + $waitSec)
        if [ $totalWait -gt $timeOut ]
        then
            log "timeout"
            break
        else
            sleep $waitSec
        fi
    else
        totalWait=0
        step=$(expr $step + 1)
        log "step $step"
        log "lock not present - taking control"

        log "sleeping for $waitSec secs to simulate external process"
        sleep $waitSec

        log "updating ${dataFile}.in from ${dataFile}.out"

        if [ -f "${dataFile}.out" ]
        then
            awk '{if( $1 != "#" ){print $1+1 " 0 1"}}' \
                ${dataFile}.out >| ${dataFile}.in
        else
            log "Warning: no such file ${dataFile}.out"
        fi

        if [ "${stopAt:-0}" -eq $step ]
        then
            stopMasterNow
        else
            useMaster
        fi
    fi
done

log "done"

# Remove the lock file too
\rm -f $lockFile 2>/dev/null

# For log collector:
echo "End"

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