Skip to content

Commit 2c26ab4

Browse files
authored
Merge pull request #27 from FoamScience/ensembles_with_fo
feat: add tutorial for ensembles using the function object
2 parents 760edc4 + c9463b1 commit 2c26ab4

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

Allwmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ elif [ $WM_COMPILER == "Icx" ]; then
3030
fi
3131
make lib
3232
cd "$_REPO_ROOT" || exit 1
33-
cp -r $FOAM_SMARTREDIS/install/lib/*.* $FOAM_USER_LIBBIN
33+
cp $FOAM_SMARTREDIS/install/lib/libsmartredis.so $FOAM_USER_LIBBIN
34+
cp $FOAM_SMARTREDIS/build/Release/hiredis-prefix/src/hiredis-build/libhiredis.a $FOAM_USER_LIBBIN
35+
cp $FOAM_SMARTREDIS/build/Release/redis++-prefix/src/redis++-build/libredis++.a $FOAM_USER_LIBBIN
3436
export FOAM_CODE_TEMPLATES=$_REPO_ROOT/etc/dynamicCode/
3537

3638
## Compile OpenFOAM libs

tutorials/functionObject/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Computing the SVD of OpenFOAM fields
2+
3+
Running `openfoam-svd.py` will perform an SVD on OpenFOAM fields that results off
4+
of a `pitzDaily` case. The case runs with `simpleFoam` and communicates its fields
5+
to the `smartRedis` database through a `fieldsToSmartRedis` function object.
6+
7+
## Usage of the Function Object with SmartSim ensembles
8+
9+
The same function object is used in an ensemble setting to showcase a dummy parameter
10+
variation. The only requirement is that the solver has access to an environment variable:
11+
```bash
12+
export SSKEYIN=${SSKEYOUT}
13+
```
14+
This ensures each ensemble member reads and writes fields to a prefixed datasets to prevent
15+
race conditions and members reading data from each other.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/python3
2+
3+
# This script sets up a smartsim experiment that runs the simpleFoam solver
4+
# on the pitzDaily case with an ensemble of parameters.
5+
# The experiment involves the use of the fieldToSmartRedis function objects,
6+
# which writes a set of OpenFOAM fields to the SmartRedis database. The SmartRedis client
7+
# then reads these fields
8+
9+
# Adapted from:
10+
# https://github.com/OFDataCommittee/OFMLHackathon/tree/main/2023-01/smartsim/smartsim_function_object
11+
12+
from smartsim import Experiment
13+
import jinja2 as jj
14+
15+
env = jj.Environment()
16+
17+
def get_field_name(fn_name, field_name, processor=0, timestep=None):
18+
"""
19+
Get the name of the field from the database. This function uses
20+
a metadata dataset posted by the function object itself to determine
21+
how things are named through Jinja2 templates
22+
23+
Args:
24+
fn_name (str): The name of the function object
25+
field_name (str): The name of the OpenFOAM field
26+
processor (int): The MPI rank
27+
timestep (int): The target timestep index
28+
"""
29+
client.poll_dataset(fn_name+"_metadata", 10, 1000)
30+
meta = client.get_dataset(fn_name+"_metadata")
31+
ds_naming = env.from_string(str(meta.get_meta_strings("dataset")[0]))
32+
ds_name = ds_naming.render(time_index=timestep, mpi_rank=processor)
33+
f_naming = env.from_string(str(meta.get_meta_strings("field")[0]))
34+
f_name = f_naming.render(name=field_name, patch="internal")
35+
return f"{{{ds_name}}}.{f_name}"
36+
37+
of_case_name = "pitzDaily"
38+
fn_name = "pUPhiTest"
39+
ens_name = "pitzDaily"
40+
41+
# Set up the OpenFOAM parameter variation as a SmartSim Experiment
42+
exp = Experiment("smartsim-openfoam-function-object", launcher="local")
43+
44+
# Assumes SSDB="localhost:8000"
45+
db = exp.create_database(port=8000, interface="lo")
46+
params = {
47+
"dummy": [1, 2]
48+
}
49+
exp.start(db)
50+
51+
blockMesh_settings = exp.create_run_settings(exe="./pitzDaily/Allrun")
52+
blockMesh_model = exp.create_model(name="blockMesh", run_settings=blockMesh_settings)
53+
ens = exp.create_ensemble(ens_name, params, None, blockMesh_settings)
54+
ens.attach_generator_files(
55+
to_copy=[f"./{of_case_name}"],
56+
to_configure=[])
57+
exp.generate(ens, overwrite=True)
58+
exp.start(ens)
59+
exp.stop(db)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/bash
2+
export SSKEYIN=$SSKEYOUT
3+
blockMesh
4+
simpleFoam

0 commit comments

Comments
 (0)