forked from openfoamdotcom/openfoam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foamMonitor
executable file
·298 lines (250 loc) · 7.09 KB
/
foamMonitor
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/bin/sh
#------------------------------------------------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | www.openfoam.com
# \\/ M anipulation |
#-------------------------------------------------------------------------------
# Copyright (C) 2015 OpenFOAM Foundation
# Copyright (C) 2019-2023 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
# This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# Script
# foamMonitor
#
# Description
# Monitor data with Gnuplot from time-value(s) graphs written by OpenFOAM
# e.g. by functionObjects
# - requires gnuplot, gnuplot_x11, sed, awk
#
#------------------------------------------------------------------------------
printHelp() {
cat<<USAGE
Usage: ${0##*/} [OPTIONS] <file>
Options:
-g | -grid Draw grid lines
-i | -idle <time> Stop if <file> unchanging for <time> sec (default = 60)
-l | -logscale Plot y-axis data on log scale
-r | -refresh <time> Refresh display every <time> sec (default = 10)
-x | -xrange <range> Set <range> of x-axis data, format "[0:1]"
-y | -yrange <range> Set <range> of y-axis data, format "[0:1]"
-h | -help Display short help and exit
Monitor data with Gnuplot from time-value(s) graphs written by OpenFOAM
e.g. by functionObjects. For example,
foamMonitor -l postProcessing/residuals/0/residuals.dat
USAGE
exit 0 # A clean exit
}
# Report error and exit
die()
{
exec 1>&2
echo
echo "Error encountered:"
while [ "$#" -ge 1 ]; do echo " $1"; shift; done
echo
echo "See '${0##*/} -help' for usage"
echo
exit 1
}
# Count the number of scalars between ( and ) to deduce the field dimensions
count_components() {
_text="$1"
# Extract text between the first ( and )
_matches="$(echo "$_text" | sed -n 's/^[^(]*(\([^)]*\)).*$/\1/p')"
# Use grep to find numbers (doubles and 0) between ( and ), then count them
_count="$(echo "$_matches" | grep -Eo '\b[0-9]+\.?[0-9]*[eE]?[-+]?[0-9]+\b|\b0\b' | wc -l)"
echo "$_count"
}
# Set Gnuplot header
plotFileHeader() {
TITLE="$1"
NCOLOURS="$2"
SIZE="1200,627"
cat<<EOF
set term x11 1 font "helvetica,17" linewidth 1.5 persist noraise size $SIZE
$LOGSCALE
$XRANGE
$YRANGE
$GRID
set title "$TITLE"
set xlabel "$XLABEL"
set key outside
set linetype cycle "$NCOLOURS"
plot \\
EOF
}
# Set Gnuplot footer
plotFileFooter() {
cat<<EOF
pause $REFRESH
reread
EOF
}
# Count number of tokens in a variable
howMany() {
( set -f; set -- $1; echo $# )
}
#-------------------------------------------------------------------------------
IDLE=60
REFRESH=10
LOGSCALE=""
XRANGE=""
YRANGE=""
GRID=""
GNUPLOT=$(which gnuplot)
[ ! "$GNUPLOT" = "" ] || die "foamMonitor requires Gnuplot installed"
command -v sed >/dev/null || die "foamMonitor requires sed installed"
command -v awk >/dev/null || die "foamMonitor requires awk installed"
#-------------------------------------------------------------------------------
# Parse options
while [ "$#" -gt 0 ]
do
case "$1" in
-h | -help*)
printHelp
;;
-i | -idle)
[ "$#" -ge 2 ] || die "'$1' option requires an argument"
if [ -n "${2##*[!0-9]*}" ]
then
IDLE=$2
else
die "Argument of '$1' is not an integer: '$2'"
fi
shift 2
;;
-l | -logscale)
LOGSCALE="set logscale y"
shift 1
;;
-r | -refresh)
[ "$#" -ge 2 ] || die "'$1' option requires an argument"
if [ -n "${2##*[!0-9]*}" ]
then
REFRESH=$2
else
die "Argument of '$1' is not an integer: '$2'"
fi
shift 2
;;
-x | -xrange)
[ "$#" -ge 2 ] || die "'$1' option requires an argument"
XRANGE="set xrange $2"
shift 2
;;
-y | -yrange)
[ "$#" -ge 2 ] || die "'$1' option requires an argument"
YRANGE="set yrange $2"
shift 2
;;
-g | -grid)
GRID="set grid"
shift 1
;;
-*)
die "unknown option: '$*'"
;;
*)
break
;;
esac
done
[ "$#" -eq 1 ] || die "Incorrect arguments specified"
[ -f "$1" ] || die "File $1 does not exit"
FILE="$1"
# Get KEYS from header
KEYS=$(grep -E '^#' "$FILE" | tail -1)
[ "$KEYS" = "" ] && KEYS="# Step"
NKEYS=$(howMany "$KEYS")
NCOLS=$(grep -m 1 '^[^#]' "$FILE" | awk '{ print NF }')
NCOMPS=$(count_components "$(grep -m 1 '^[^#]' "$FILE")")
# With full column labels, NKEYS = NCOLS + 1, since it includes "#"
# If NKEYS > NCOLS + 1, REMOVE EXCESS KEYS
NCOLSPONE=$((NCOLS+1))
[ "$NKEYS" -gt "$NCOLSPONE" ] && KEYS=$(echo "$KEYS" | cut -d" " -f1-$NCOLSPONE)
# Remove # and Time keys
XLABEL=$(echo "$KEYS" | cut -d " " -f2)
KEYS=$(echo "$KEYS" | cut -d " " -f3-)
NKEYS=$(howMany "$KEYS")
# Create the legend items
TEMPKEYS=""
for i in $KEYS
do
case "$NCOMPS" in
0) # scalar
TEMPKEYS="$TEMPKEYS $i"
;;
3) # vector
for j in x y z
do
TEMPKEYS="$TEMPKEYS ${i}_${j}"
done
;;
6) # symmetric tensor
for j in xx xy xz yy yz zz
do
TEMPKEYS="$TEMPKEYS ${i}_${j}"
done
;;
9) # tensor
for j in xx xy xz yx yy yz zx zy zz
do
TEMPKEYS="$TEMPKEYS ${i}_${j}"
done
;;
*) # handle other cases if needed
echo "Unsupported number of components: $NCOMPS"
exit 1
;;
esac
done
KEYS=$TEMPKEYS
# Create plots
GPFILE=$(mktemp)
PLOT_TITLE="$(echo "$FILE" | awk -F'/' '{print $(NF-2) "/" $NF}')"
NUM_ELEMENTS="$((NKEYS * NCOMPS))"
plotFileHeader "$PLOT_TITLE" "$NUM_ELEMENTS" > "$GPFILE"
i=1
j=1
for field in $KEYS
do
i=$((i+1))
# Reject any keys consisting of 'iter|converged|solver' words
case "$field" in
*"iter"*|*"converged"*|*"solver"*)
continue
;;
esac
# Reject any parentheses, and configure gnuplot for the underscore character
PLOTLINE="\"< sed 's/[()]//g' $FILE\" u 1:${i} w l dt ${j} t \"$(echo "$field" | sed 's/_/\\\\_/g')\""
if [ "$i" -lt "$NCOLS" ]
then
PLOTLINE="$PLOTLINE, \\"
fi
echo "$PLOTLINE" >> "$GPFILE"
# Change the dash type after every 5 non-rejected lines
[ $(((i-1) % 5)) -eq 0 ] && j=$((j+1))
done
# Scrap ', \' characters from the tail of plot command, if necessary
if tail -n 1 "$GPFILE" | grep -q ', \\$'; then
{ head -n -1 "$GPFILE"; tail -n 1 "$GPFILE" | sed 's/, \\$//'; } > tmp.gp
mv -f -- tmp.gp "$GPFILE"
fi
plotFileFooter >> "$GPFILE"
touch "$FILE"
$GNUPLOT "$GPFILE" &
PID=$!
while true
do
MODTIME="$(stat --format=%Y "$FILE")"
IDLEAGO=$(($(date +%s)-IDLE))
test "$MODTIME" -gt "$IDLEAGO" || break
sleep "$REFRESH"
done
kill -9 $PID
rm -f "$GPFILE"
#------------------------------------------------------------------------------