Running parameter sweeps of OpenFOAM cases with Slurm job arrays
Published
A parameter study (inlet velocities, viscosities, angles of attack, mesh variants) often starts as
a loop that copies a case directory a hundred times, edits each copy by hand or with sed, and
submits a hundred separate jobs. That works, but it is slow to set up, error-prone, and hard on
the scheduler: a hundred jobs are a hundred entries to schedule, account and clean up.
A Slurm job array is one submission with many tasks. Each task gets an index, and the job script
uses that index to pick its parameters. Together with foamDictionary, which edits OpenFOAM
dictionaries from the command line, the whole study is one template case, one parameter file and
one script.
The layout
study/
├── template/ a complete, tested OpenFOAM case
│ ├── 0/
│ ├── constant/
│ └── system/
├── params.csv one line per case
├── sweep.sh the array job script
└── runs/ generated cases, one per array task
Test the template first, with a short run of one parameter set. A mistake in the template is repeated in every case of the study.
The parameter file
One header line, then one line per case. The array index selects the line:
name,Uin,nu
u05_nu1e-5,5,1e-05
u10_nu1e-5,10,1e-05
u15_nu1e-5,15,1e-05
u05_nu1e-6,5,1e-06
u10_nu1e-6,10,1e-06
u15_nu1e-6,15,1e-06
A readable name column makes it easy to find a case later without decoding an index. Generate
large files with a short script rather than typing them:
echo "name,Uin,nu" > params.csv
for nu in 1e-05 1e-06; do
for u in 5 10 15; do
printf 'u%02d_nu%s,%s,%s\n' "$u" "$nu" "$u" "$nu" >> params.csv
done
done
The array job script
#!/bin/bash
#SBATCH --job-name=sweep
#SBATCH --array=1-6%4
#SBATCH --ntasks=8
#SBATCH --time=04:00:00
#SBATCH --output=logs/%x-%A_%a.out
set -euo pipefail
module load openfoam-com/v2406
cd "${SLURM_SUBMIT_DIR}"
# Line 1 is the header, so array index 1 is line 2.
IFS=, read -r name Uin nu < <(sed -n "$(( SLURM_ARRAY_TASK_ID + 1 ))p" params.csv)
case_dir="runs/${name}"
echo "Task ${SLURM_ARRAY_TASK_ID}: ${name} (Uin=${Uin}, nu=${nu})"
# Generate the case from the template, unless a previous attempt already did.
if [[ ! -d "${case_dir}" ]]; then
mkdir -p runs
cp -r template "${case_dir}"
fi
cd "${case_dir}"
foamDictionary -entry boundaryField/inlet/value -set "uniform (${Uin} 0 0)" 0/U
foamDictionary -entry nu -set "${nu}" constant/transportProperties
foamDictionary -entry numberOfSubdomains -set "${SLURM_NTASKS}" system/decomposeParDict
decomposePar -force > log.decomposePar 2>&1
mpirun -np "${SLURM_NTASKS}" simpleFoam -parallel > log.simpleFoam 2>&1
reconstructPar -latestTime > log.reconstructPar 2>&1
rm -rf processor[0-9]*
Create the logs/ directory before submitting (mkdir -p logs), because Slurm does not create it.
Then submit once:
sbatch sweep.sh
The details that matter
--array=1-6%4creates tasks 1 to 6 and runs at most four at a time. The%throttle is how you share the cluster politely: the study does not take over a partition, and the remaining tasks start as earlier ones finish. Set the range to match the number of data lines inparams.csv.- Each task is a full job with its own resources: here 8 tasks for MPI. Size them like any other OpenFOAM job (see sizing an OpenFOAM job). For a sweep, many small runs are usually more efficient than a few large ones, because small runs scale best.
%A_%ain the output name is the array’s job id and the task index, so each task has its own Slurm log.foamDictionary -entry … -set …changes one entry and leaves the rest of the file as it was, including comments. Entries inside sub-dictionaries are addressed with/, as inboundaryField/inlet/value. Values with spaces or parentheses need quotes. The file names depend on the OpenFOAM version: viscosity sits inconstant/transportPropertiesin older and openfoam.com versions, and inconstant/physicalPropertiesin recent openfoam.org versions. Check withfoamDictionary -entry nu constant/<file>on the template first. The note on OpenFOAM versions explains why they differ.- The case is created inside the task, not up front. The study directory only fills as tasks run, and a requeued task reuses its case directory instead of copying the template again.
Watching and checking the study
squeue -u "$USER" -r # one line per pending or running task
sacct -j <arrayjobid> --format=JobID,JobName,State,Elapsed,MaxRSS -X
-r expands the array in squeue, which otherwise shows pending tasks on one line. In sacct,
each task appears as <arrayjobid>_<index>. Tasks that failed are easy to rerun on their own:
sbatch --array=3,5 sweep.sh
The same script, the same parameter file, and only the failed indices.
Keeping the study small on disk
A hundred copies of a case is a hundred copies of the mesh. If every case uses the same mesh,
share it instead of copying it: create the mesh once in the template, then link it into each case.
In the script, replace the cp -r template "${case_dir}" line with:
mkdir -p "${case_dir}/constant"
cp -r template/0 template/system "${case_dir}/"
cp template/constant/*Properties "${case_dir}/constant/"
ln -s "$(realpath template/constant/polyMesh)" "${case_dir}/constant/polyMesh"
Do this only for meshes nothing writes to. decomposePar only reads the mesh and writes into the
processor directories, so a shared mesh is safe for ordinary runs. Mesh motion and refinement are
not.
The other big saving is not keeping processor directories (the script above removes them after
reconstructing) and writing only the times you need. For post-processing sweeps, function objects
that write forces, probes or averages to postProcessing/ give you the numbers to compare across
cases without keeping full fields at all. Collecting a result from every case is then one loop
(the name of the forces file differs between versions, so check one case first):
for d in runs/*/; do
printf '%s,' "$(basename "$d")"
tail -n 1 "$d"/postProcessing/forces/0/force.dat
done > results.csv
When the study is done, cleaning up OpenFOAM cases covers what to keep and what to delete.
Limits to know about
MaxArraySizein the Slurm configuration caps the index range (scontrol show config | grep MaxArraySize). Some sites also limit how many array tasks one user may have queued. For very large studies, split the parameter file and submit several arrays.- Very short tasks (seconds each) waste scheduler capacity. If each case runs for less than a few minutes, process several parameter lines per task, in a loop inside the script.
- Every task re-reads the template. Changing the template while the array runs changes the cases that have not started yet. Leave it alone until the study is finished, or copy it per study.
For the single-case job script this array is built from, see the Slurm job script for OpenFOAM.