-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunParameterSweep.sh
More file actions
executable file
·361 lines (302 loc) · 9.92 KB
/
runParameterSweep.sh
File metadata and controls
executable file
·361 lines (302 loc) · 9.92 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
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
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
#!/bin/bash
# # runParameterSweep.sh
#
# Generate a parameter sweep from `sweep.params` (or a custom sweep file) and
# run each case sequentially in `simulationCases/<CaseNo>/`.
#
# ## Usage
# `./runParameterSweep.sh [options] [sweep_file]`
#
# ## Inputs
# - `sweep_file`: sweep configuration (default: `sweep.params`)
# - `BASE_CONFIG` inside the sweep file (starting parameter set)
#
# ## Outputs
# - Case directories under `simulationCases/`
#
# ## Related
# - `runSimulation.sh`
# - `src-local/parse_params.sh`
set -euo pipefail # Exit on error, unset variables, pipeline failures
# ============================================================
# Configuration
# ============================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Source parameter parsing library
if [ -f "${SCRIPT_DIR}/src-local/parse_params.sh" ]; then
source "${SCRIPT_DIR}/src-local/parse_params.sh"
else
echo "ERROR: src-local/parse_params.sh not found" >&2
exit 1
fi
# ============================================================
# Usage Information
# ============================================================
usage() {
cat <<EOF
Usage: $0 [OPTIONS] [sweep_file]
Run parameter sweep with auto-incrementing CaseNo.
Creates case folders in simulationCases/<CaseNo>/ for each parameter combination.
Cases run sequentially (one at a time).
Options:
-n, --dry-run Show parameter combinations without running
-v, --verbose Verbose output
-c, --compile-only Compile only, don't run simulations
-m, --mpi Enable MPI parallel execution for all cases
--cores N Number of MPI cores (default: 4, requires --mpi)
-h, --help Show this help message
Parameter sweep file (default):
$0 sweep.params
If no sweep file specified, uses sweep.params from current directory.
Sweep file format:
BASE_CONFIG=default.params
CASE_START=1000
CASE_END=1011
SWEEP_We=1.0,10.0,100.0
SWEEP_Ohd=1e-4,1e-3,1e-2
CaseNo auto-increments from CASE_START for each parameter combination.
Examples:
# Run sweep with default file (serial)
$0
# Dry run to see parameter combinations
$0 --dry-run
# Run sweep with MPI parallel execution (4 cores per case)
$0 --mpi
# Run sweep with 8 cores per case
$0 --mpi --cores 8
# Run custom sweep file
$0 custom_sweep.params
For more information, see README.md
EOF
}
# ============================================================
# Parse Command Line Options
# ============================================================
DRY_RUN=0
VERBOSE=0
COMPILE_ONLY=0
MPI_ENABLED=0
MPI_CORES=4
while [[ $# -gt 0 ]]; do
case $1 in
-n|--dry-run)
DRY_RUN=1
shift
;;
-v|--verbose)
VERBOSE=1
shift
;;
-c|--compile-only)
COMPILE_ONLY=1
shift
;;
-m|--mpi)
MPI_ENABLED=1
shift
;;
--cores)
MPI_CORES="$2"
if ! [[ "$MPI_CORES" =~ ^[0-9]+$ ]] || [ "$MPI_CORES" -lt 1 ]; then
echo "ERROR: --cores requires a positive integer, got: $MPI_CORES" >&2
exit 1
fi
shift 2
;;
-h|--help)
usage
exit 0
;;
-*)
echo "ERROR: Unknown option: $1" >&2
usage
exit 1
;;
*)
break
;;
esac
done
# ============================================================
# Determine Sweep File
# ============================================================
SWEEP_FILE="${1:-sweep.params}"
if [ ! -f "$SWEEP_FILE" ]; then
echo "ERROR: Sweep file not found: $SWEEP_FILE" >&2
exit 1
fi
echo "========================================="
echo "Drop Impact - Parameter Sweep"
echo "========================================="
echo "Sweep file: $SWEEP_FILE"
[ $DRY_RUN -eq 1 ] && echo "Mode: Dry run (no execution)"
echo ""
# ============================================================
# Parse Sweep Configuration
# ============================================================
# Source the sweep file to get variables
source "$SWEEP_FILE"
# Validate required variables
if [ -z "$BASE_CONFIG" ]; then
echo "ERROR: BASE_CONFIG not defined in sweep file" >&2
exit 1
fi
if [ -z "$CASE_START" ] || [ -z "$CASE_END" ]; then
echo "ERROR: CASE_START and CASE_END must be defined in sweep file" >&2
exit 1
fi
# Validate CaseNo range
if [ "$CASE_START" -lt 1000 ] || [ "$CASE_START" -gt 9999 ]; then
echo "ERROR: CASE_START must be 4-digit (1000-9999), got: $CASE_START" >&2
exit 1
fi
if [ "$CASE_END" -lt "$CASE_START" ] || [ "$CASE_END" -gt 9999 ]; then
echo "ERROR: CASE_END must be >= CASE_START and <= 9999, got: $CASE_END" >&2
exit 1
fi
if [ ! -f "$BASE_CONFIG" ]; then
echo "ERROR: Base configuration file not found: $BASE_CONFIG" >&2
exit 1
fi
echo "Base configuration: $BASE_CONFIG"
echo "Case number range: $CASE_START to $CASE_END"
echo ""
# ============================================================
# Extract Sweep Variables
# ============================================================
SWEEP_VARS=()
SWEEP_VALUES=()
# Read sweep file and extract SWEEP_* variables
while IFS='=' read -r key value; do
# Skip comments and empty lines
[[ "$key" =~ ^[[:space:]]*# ]] && continue
[[ -z "$key" ]] && continue
# Match SWEEP_* variables
if [[ "$key" =~ ^[[:space:]]*SWEEP_([^=]+) ]]; then
var_name="${BASH_REMATCH[1]}"
# Remove inline comments and whitespace
value=$(echo "$value" | sed 's/#.*//' | xargs)
SWEEP_VARS+=("$var_name")
SWEEP_VALUES+=("$value")
fi
done < "$SWEEP_FILE"
if [ ${#SWEEP_VARS[@]} -eq 0 ]; then
echo "ERROR: No SWEEP_* variables found in $SWEEP_FILE" >&2
exit 1
fi
echo "Sweep variables:"
for i in "${!SWEEP_VARS[@]}"; do
echo " ${SWEEP_VARS[$i]} = ${SWEEP_VALUES[$i]}"
done
echo ""
# ============================================================
# Generate Parameter Combinations
# ============================================================
# Create temporary directory for generated parameter files
TEMP_DIR=$(mktemp -d "${TMPDIR:-/tmp}/sweep.XXXXXX")
trap "rm -rf $TEMP_DIR" EXIT
CASE_NUM=$CASE_START
COMBINATION_COUNT=0
# Recursive function to generate all combinations
generate_combinations() {
local depth=$1
shift
local current_values=("$@")
if [ $depth -eq ${#SWEEP_VARS[@]} ]; then
# Base case: all variables assigned, create parameter file
local case_file="${TEMP_DIR}/case_$(printf "%04d" $CASE_NUM).params"
# Copy base config
cp "$BASE_CONFIG" "$case_file"
# Override CaseNo
if grep -q "^CaseNo=" "$case_file"; then
sed -i.bak "s|^CaseNo=.*|CaseNo=${CASE_NUM}|" "$case_file"
else
echo "CaseNo=${CASE_NUM}" >> "$case_file"
fi
rm -f "${case_file}.bak"
# Override with sweep values
for i in "${!SWEEP_VARS[@]}"; do
local var="${SWEEP_VARS[$i]}"
local val="${current_values[$i]}"
if grep -q "^${var}=" "$case_file"; then
sed -i.bak "s|^${var}=.*|${var}=${val}|" "$case_file"
else
echo "${var}=${val}" >> "$case_file"
fi
rm -f "${case_file}.bak"
done
((COMBINATION_COUNT++))
# Print summary
if [ $DRY_RUN -eq 1 ] || [ $VERBOSE -eq 1 ]; then
echo "Case $CASE_NUM:"
for i in "${!SWEEP_VARS[@]}"; do
echo " ${SWEEP_VARS[$i]} = ${current_values[$i]}"
done
echo ""
fi
((CASE_NUM++))
return
fi
# Recursive case: iterate through values for current variable
local values="${SWEEP_VALUES[$depth]}"
IFS=',' read -ra value_array <<< "$values"
for val in "${value_array[@]}"; do
val=$(echo "$val" | xargs) # Trim whitespace
generate_combinations $((depth + 1)) "${current_values[@]}" "$val"
done
}
# Start recursion
generate_combinations 0
echo "Generated $COMBINATION_COUNT parameter combinations"
# Check if number of combinations matches the range
EXPECTED_COUNT=$((CASE_END - CASE_START + 1))
if [ $COMBINATION_COUNT -ne $EXPECTED_COUNT ]; then
echo "WARNING: Generated $COMBINATION_COUNT combinations, but CASE_END suggests $EXPECTED_COUNT" >&2
echo " Consider adjusting CASE_END in sweep file" >&2
fi
if [ $COMBINATION_COUNT -gt $EXPECTED_COUNT ]; then
echo "ERROR: Too many combinations ($COMBINATION_COUNT) for range $CASE_START-$CASE_END" >&2
exit 1
fi
echo ""
# Exit if dry run
if [ $DRY_RUN -eq 1 ]; then
echo "Dry run complete. No simulations executed."
exit 0
fi
# ============================================================
# Run Simulations
# ============================================================
echo "========================================="
echo "Running Simulations"
echo "========================================="
# Build list of parameter files
PARAM_FILES=()
for case_file in "$TEMP_DIR"/case_*.params; do
PARAM_FILES+=("$case_file")
done
# Build flags to pass to runSimulation.sh
RUN_FLAGS=""
if [ $COMPILE_ONLY -eq 1 ]; then
RUN_FLAGS="$RUN_FLAGS --compile-only"
fi
if [ $MPI_ENABLED -eq 1 ]; then
RUN_FLAGS="$RUN_FLAGS --mpi --cores $MPI_CORES"
fi
# Run simulations sequentially (one at a time)
echo "Running $COMBINATION_COUNT simulations sequentially"
if [ $MPI_ENABLED -eq 1 ]; then
echo "Each case will use MPI with $MPI_CORES cores"
fi
echo ""
for param_file in "${PARAM_FILES[@]}"; do
./runSimulation.sh $RUN_FLAGS "$param_file"
done
echo ""
echo "========================================="
echo "Parameter Sweep Complete"
echo "========================================="
echo "Total cases: $COMBINATION_COUNT"
echo "Case range: $CASE_START to $((CASE_START + COMBINATION_COUNT - 1))"
echo "Output location: simulationCases/"
echo ""