-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoamParm
More file actions
executable file
·90 lines (80 loc) · 3 KB
/
foamParm
File metadata and controls
executable file
·90 lines (80 loc) · 3 KB
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
#!/bin/bash
# Efficient inspection and change of openfoam parameters with tab auto-complete.
# Usage:
#
# foamParm[TAB] lists files in current dir
# foamParm sys[TAB] autocompletes e.g. system if dir exists
# foamParm system/con[TAB] autocompletes e.g. controlDict if file exists
# foamParm system/controlDict [TAB] lists all parameters in controlDict
# foamParm system/controlDict start[TAB] lists all parameters in controlDict starting with "start" (startFrom startTime)
# foamParm system/controlDict startTime [TAB] fills in value of startTime
# foamParm system/controlDict startTime 12 sets startTime to 12
#
# Limitations:
# - Parameters must be on a new line
# - Currently unaware of subdict syntax so
#
# foamParm damBreak/constant/transportProperties rho 10
#
# will set both water.rho and air.rho to 10.
# - Based on grep for finding and sed for replacement.
# - More robust subdict aware version could be based on foamDictionary
#
# Copyright, Johan Roenby, 2025
# ---- foamParm function ----
foamParm() {
local file="$1"
local param="$2"
local value="$3"
if [[ "$file" == "-help" ]]; then
echo "foamParm usage:"
echo " foamParm <dictFile> <parameter> [value]"
echo " - print parameter value if no [value]"
echo " - set parameter value if [value] given"
echo " - show this help if -help"
return
fi
if [[ ! -f "$file" ]]; then
echo "File '$file' not found."
return 1
fi
if [[ -n "$param" && -z "$value" ]]; then
# Print parameter value
grep -E "^\s*${param}\s+" "$file" || echo "Parameter '$param' not found."
elif [[ -n "$param" && -n "$value" ]]; then
# Replace or insert parameter
if grep -q -E "^\s*${param}\s+" "$file"; then
sed -i "s|^\(\s*${param}\s\+\).*|\1${value};|" "$file"
echo "Updated $param = $value in $file"
else
echo "${param} ${value};" >> "$file"
echo "Added $param = $value in $file"
fi
else
echo "Usage: foamParm <dictFile> <parameter> [value]"
fi
}
_foamParm_complete() {
local cur prev words cword
_init_completion || return
if [[ ${COMP_CWORD} -eq 1 ]]; then
compopt -o filenames -o nospace
COMPREPLY=( $(compgen -f -- "$cur") )
return
elif [[ ${COMP_CWORD} -eq 2 ]]; then
local file="${COMP_WORDS[1]}"
[[ -f "$file" ]] || return
local params=($(grep -E '^\s*[^/ \t]+' "$file" | sed 's/^\s*\([^/ \t]*\).*/\1/' | sort -u))
COMPREPLY=( $(compgen -W "${params[*]}" -- "$cur") )
return
elif [[ ${COMP_CWORD} -eq 3 ]]; then
local file="${COMP_WORDS[1]}"
local param="${COMP_WORDS[2]}"
[[ -f "$file" ]] || return
local val=$(grep -E "^\s*${param}\s+" "$file" | sed -E "s/^\s*${param}\s+([^;]+);.*/\1/")
COMPREPLY=( $(compgen -W "$val" -- "$cur") )
return
fi
}
# ---- Register completion ----
complete -F _foamParm_complete foamParm