|
| 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) |
0 commit comments