Newer
Older
#!/bin/bash
#------------------------------------------------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\/ M anipulation |
#------------------------------------------------------------------------------
# Copyright (C) 2019-2020 OpenCFD Ltd.
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
#------------------------------------------------------------------------------
# License
# This file is part of OpenFOAM.
#
# OpenFOAM is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
#
# Script
# foamGetDict
#
# Description
# Find an OpenFOAM dictionary file from OpenFOAM/etc/caseDicts/
# or {user,site} locations and copy it into the case directory.
#
# Environment
# FOAM_API
# WM_PROJECT_DIR
# WM_PROJECT_SITE
#
#------------------------------------------------------------------------------
printHelp() {
cat<<USAGE
Usage: ${0##*/} [OPTIONS] <file>
options:
-case <dir> Alternative case directory, default is the cwd
-ext <ext> File extension
-cfg Same as '-ext cfg' for '.cfg' files
-f | -force Force overwrite of existing files
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
-no-ext Files without extension
-target <dir> Target directory (default: system, or auto-detected)
-with-api=NUM Alternative api value for searching
-help Display short help and exit
Find an OpenFOAM dictionary file from etc/caseDicts/ or {user,site} locations
and copy it into the case directory. For example,
foamGetDict decomposeParDict
foamGetDict extrudeMeshDict
foamGetDict createPatchDict
foamGetDict surfaces
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
}
#-------------------------------------------------------------------------------
projectDir="$WM_PROJECT_DIR" # Project dir
userDir="$HOME/.OpenFOAM" # As per foamVersion.H
groupDir="${WM_PROJECT_SITE:-$projectDir/site}" # As per foamVersion.H
projectApi="$FOAM_API"
#-------------------------------------------------------------------------------
searchExt="<any>"
unset targetDir optForce
while [ "$#" -gt 0 ]
do
case "$1" in
-h | -help*)
printHelp
;;
-case)
[ "$#" -ge 2 ] || die "'$1' option requires an argument"
cd "$2" 2>/dev/null || die "directory does not exist: '$2'"
shift
;;
-with-api=*)
projectApi="${1#*=}"
;;
-ext)
[ "$#" -ge 2 ] || die "'$1' option requires an argument"
searchExt="$2"
shift
;;
-no-ext)
unset searchExt
;;
-cfg)
searchExt="cfg"
;;
-f | -force)
optForce=true
;;
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
-target)
[ "$#" -ge 2 ] || die "'$1' option requires an argument"
targetDir="$2"
shift
;;
-target=*)
targetDir="${1#*=}"
;;
--)
shift
break # Stop here
;;
-*)
die "invalid option '$1'"
;;
*)
break
;;
esac
shift
done
[ "$#" -eq 1 ] || die "Incorrect number of arguments"
filePrefix="$1"
# The target (time-dir) directory as one of (. | constant | system | 0)
if [ -z "$targetDir" ]
then
targetDir="system"
case "$filePrefix" in
All*)
targetDir="."
;;
*Properties | *Cloud)
targetDir="constant"
;;
s)
targetDir="0"
;;
esac
fi
# No api specified -with-api= or from environment (FOAM_API)
if [ -z "$projectApi" ]
then
projectApi="$("$projectDir"/bin/foamEtcFile -show-api 2>/dev/null)"
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
fi
# Define the various places to be searched.
# Similar to foamEtcFile, but with etc/caseDicts/ for the projectDir
# Filter out nonexistent directories later
searchDirs="${projectApi:+$userDir/$projectApi} $userDir \
${projectApi:+$groupDir/$projectApi/etc} $groupDir/etc \
$projectDir/etc/caseDicts";
## echo "Using <$searchDirs>" 1>&2
# ------------------------------------------------------------------------------
# Search directories (searchDirs) for file
# $1 = file-name
#
# Globals: searchDirs
findFilesInDirs()
{
local fileName="$1"
local dir
for dir in $searchDirs
do
if [ -d "$dir" ]
then
find "$dir" -name "$fileName" -type f | sort
fi
done
}
# Search directories (searchDirs) for file-prefix with extension (searchExt)
#
# $1 = file-prefix
#
# Globals: searchDirs, searchExt
findFiles()
{
local prefix="$1"
if [ -z "$searchExt" ]
then
# No extension
findFilesInDirs "$prefix"
elif [ "$searchExt" = "<any>" ]
then
# No extension or any extension
findFilesInDirs "$prefix"
findFilesInDirs "${prefix}.*"
else
# With specific extension
findFilesInDirs "${prefix}.$searchExt"
fi
}
# Slightly dressed up version of 'cp -v' that does not clobber existing files
local targetFile="$2"
[ -d "$2" ] && targetFile="$targetFile/${1##*/}"
if [ -f "$targetFile" ]
then
if [ "$optForce" = true ]
then
echo "Overwrite $1 to $2" 1>&2
\cp "$1" "$2"
else
echo "Skip copy $1 to $2" 1>&2
fi
else
echo "Copying $1 to $2" 1>&2
\cp "$1" "$2"
fi
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
echo 1>&2
}
# Priority locations for suggestion
priorityLocations="\
caseDicts/postProcessing/ \
caseDicts/preProcessing/ \
caseDicts/general/ \
caseDicts/mesh/ \
caseDicts/surface/ \
caseDicts/solvers/"
# Create an indexed list of suggestions
listArgs()
{
local arg loc n suggest
local i=0 pri=100
for arg in $1
do
i=$((i + 1))
echo "${i}) $arg" >&2
n=0
for loc in $priorityLocations
do
n=$((n + 1))
if [ "$n" -lt "$pri" ] && echo "$arg" | grep -q "$loc"
then
suggest="$i"
pri="$n"
fi
done
done
echo "$suggest"
}
# Select specified file by index from list
selectFile()
{
local files="$1"
local index="$2"
echo "$files" | tr -s '\n' ' ' | awk -v n="$index" '{print $n}'
}
# ------------------------------------------------------------------------------
[ -s "system/controlDict" ] || \
echo "Warning: no OpenFOAM system/controlDict file" 1>&2
[ -d "$targetDir" ] || \
die "target directory does not exist: '$targetDir'"
# Begin search
filesFound="$(findFiles "$filePrefix")"
nFilesFound=0
if [ -z "$filesFound" ]
then
message="No file $filePrefix found"
if [ "$searchExt" = "<any>" ]
then
message="$message with/without file extensions"
elif [ -n "$searchExt" ]
then
message="$message with file extension '$searchExt'"
fi
die "$message"
else
nFilesFound="$(echo "$filesFound" | xargs -n 1 | wc -l)"
fi
# Exactly one file found - we are done
if [ "$nFilesFound" -eq 1 ]
then
copyFile "$filesFound" "$targetDir"
exit 0
fi
# ------------------------------------------------------------------------------
# Multiple files found - offer simple selection
echo "Multiple files with \"$filePrefix\" prefix found:"
echo
suggest="$(listArgs "$filesFound")"
if echo "$filesFound" | grep -q "annotated/"
then
echo
echo "** Note: it may be easier not to use 'annotated/' files"
fi
echo
echo "Choose file number [1-$nFilesFound]${suggest:+ (suggest $suggest)} :"
read -r nFile
# Nothing specified? Use default suggestion
: "${nFile:=$suggest}"
if [ -z "$nFile" ]
then
echo "Nothing specify - re-run and enter a file number" 1>&2
exit 1
elif [ "$nFile" -lt 1 ] || [ "$nFile" -gt "$nFilesFound" ]
then
echo "\"$nFile\" is not a number between 1 and $nFilesFound" 1>&2
exit 1
fi
# Appears to have been successful. Select the file from the list
file="$(selectFile "$filesFound" "$nFile")"
copyFile "$file" "$targetDir"
#------------------------------------------------------------------------------