From 1c76958c3544f28bfb0f9f10e45253166ecbb273 Mon Sep 17 00:00:00 2001 From: Elwardi Date: Tue, 9 Jul 2024 12:33:17 +0200 Subject: [PATCH 1/2] fix: ensemble name not found as a data source --- .../smartRedisClient/smartRedisClient.C | 6 ++ .../smartRedisClient/smartRedisClient.H | 6 ++ tutorials/functionObject/openfoam-svd.py | 90 +++++++++++-------- .../pitzDaily/system/controlDict | 6 ++ 4 files changed, 69 insertions(+), 39 deletions(-) diff --git a/src/smartRedis/smartRedisClient/smartRedisClient.C b/src/smartRedis/smartRedisClient/smartRedisClient.C index 7e7dd0a..3e86a09 100644 --- a/src/smartRedis/smartRedisClient/smartRedisClient.C +++ b/src/smartRedis/smartRedisClient/smartRedisClient.C @@ -54,6 +54,7 @@ Foam::smartRedisClient::smartRedisClient region_(dict.getOrDefault("region", polyMesh::defaultRegion)), mesh_(runTime.lookupObject(region_)), clientName_(dict.getOrDefault("clientName", "default")), + ensemble_(dict.found("ensemble") ? new word(dict.get("ensemble")) : nullptr), redisDB_( runTime.foundObject(clientName_) ? &runTime.lookupObjectRef(clientName_) @@ -144,6 +145,11 @@ Foam::smartRedisClient::postMetadata() DataSet Foam::smartRedisClient::getMetadata() { + if (ensemble_) + { + Info << "***** --------- ****** " << ensemble() << endl; + redisDB_->client().set_data_source(ensemble()); + } word dsName = name()+"_metadata"; if (!redisDB_->client().dataset_exists(dsName)) { diff --git a/src/smartRedis/smartRedisClient/smartRedisClient.H b/src/smartRedis/smartRedisClient/smartRedisClient.H index 0fce389..b8af0f9 100644 --- a/src/smartRedis/smartRedisClient/smartRedisClient.H +++ b/src/smartRedis/smartRedisClient/smartRedisClient.H @@ -73,6 +73,9 @@ protected: //- Client name (for debug output) word clientName_; + //- ensemble name if any + autoPtr ensemble_; + //- The RedisAI database client tmp redisDB_; @@ -216,6 +219,9 @@ public: const word& value ); + //- return ensemble name if set + const word ensemble() const { return ensemble_ ? ensemble_() : ""; } + //- Send fields of type T to SmartRedis Dataset template void packFields diff --git a/tutorials/functionObject/openfoam-svd.py b/tutorials/functionObject/openfoam-svd.py index 2fbbd83..66acaf9 100755 --- a/tutorials/functionObject/openfoam-svd.py +++ b/tutorials/functionObject/openfoam-svd.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 # This script sets up a smartsim experiment that runs the simpleFoam solver -# on the pitzDaily case. +# on the pitzDaily case with an ensemble of parameters. # The experiment involves the use of the fieldToSmartRedis function objects, # which writes a set of OpenFOAM fields to the SmartRedis database. The SmartRedis client # then reads these fields, initiating a process of Singular Value Decomposition. @@ -48,55 +48,67 @@ def calc_svd(input_tensor): of_case_name = "pitzDaily" fn_name = "pUPhiTest" +control_dict = ParsedParameterFile(os.path.join(of_case_name, "system/controlDict")) +ens_name = control_dict["functions"][fn_name]["ensemble"] +print(ens_name) # Set up the OpenFOAM parameter variation as a SmartSim Experiment exp = Experiment("smartsim-openfoam-function-object", launcher="local") # Assumes SSDB="localhost:8000" db = exp.create_database(port=8000, interface="lo") +params = { + "dummy": [1, 2] +} exp.start(db) # blockMesh settings & model -blockMesh_settings = exp.create_run_settings(exe="blockMesh", exe_args=f"-case {of_case_name}") +blockMesh_settings = exp.create_run_settings(exe="./pitzDaily/Allrun") blockMesh_model = exp.create_model(name="blockMesh", run_settings=blockMesh_settings) +ens = exp.create_ensemble(ens_name, params, None, blockMesh_settings) +ens.attach_generator_files( + to_copy=[f"./{of_case_name}"], + to_configure=[]) # Mesh with blockMesh, and wait for meshing to finish before running the next model -exp.start(blockMesh_model, summary=True, block=True) +#exp.start(blockMesh_model, summary=True, block=True) +exp.generate(ens, overwrite=True) +exp.start(ens) -# simpleFoam settings & model -simpleFoam_settings = exp.create_run_settings(exe="simpleFoam", exe_args=f"-case {of_case_name}") -simpleFoam_model = exp.create_model(name="simpleFoam", run_settings=simpleFoam_settings) -# Run simpleFoam solver -exp.start(simpleFoam_model, summary=True, block=True) - -# Get the names of OpenFOAM fiels from controlDict.functionObject -control_dict = ParsedParameterFile(os.path.join(of_case_name, "system/controlDict")) -field_names = list(control_dict["functions"][fn_name]["fields"]) - -# Open a connection to the SmartRedis database -client = Client(address=db.get_address()[0], cluster=False) -client.set_function("svd", calc_svd) - -# Get last timestep index from the metadata dataset -end_ts = int(client.get_dataset(fn_name+"_metadata").get_meta_strings("EndTimeIndex")[0]) - -# Apply SVD to fields -print(f"SVD will be performed on OpenFOAM fields: {field_names}") -for field_name in field_names: - print (f"SVD decomposition of field: {field_name}...") - db_field_name = get_field_name(fn_name, field_name, processor=0, timestep=end_ts) - print(f"Using {db_field_name} from the database") - client.run_script("svd", "calc_svd", [db_field_name], ["U", "S", "V"]) - U = client.get_tensor("U") - S = client.get_tensor("S") - V = client.get_tensor("V") - - # Compute the Singular Value Decomposition of the field - field_svd = np.dot(U, np.dot(S, V)) - field_svd = field_svd[:, np.newaxis] - - # Compute the mean error of the SVD - field = client.get_tensor(db_field_name) - svd_rmse = np.sqrt(np.mean((field - field_svd) ** 2)) - print (f"RMSE({field_name}, SVD({field_name})): {svd_rmse}") +## simpleFoam settings & model +#simpleFoam_settings = exp.create_run_settings(exe="simpleFoam", exe_args=f"-case {of_case_name}") +#simpleFoam_model = exp.create_model(name="simpleFoam", run_settings=simpleFoam_settings) +## Run simpleFoam solver +#exp.start(simpleFoam_model, summary=True, block=True) +# +## Get the names of OpenFOAM fiels from controlDict.functionObject +#control_dict = ParsedParameterFile(os.path.join(of_case_name, "system/controlDict")) +#field_names = list(control_dict["functions"][fn_name]["fields"]) +# +## Open a connection to the SmartRedis database +#client = Client(address=db.get_address()[0], cluster=False) +#client.set_function("svd", calc_svd) +# +## Get last timestep index from the metadata dataset +#end_ts = int(client.get_dataset(fn_name+"_metadata").get_meta_strings("EndTimeIndex")[0]) +# +## Apply SVD to fields +#print(f"SVD will be performed on OpenFOAM fields: {field_names}") +#for field_name in field_names: +# print (f"SVD decomposition of field: {field_name}...") +# db_field_name = get_field_name(fn_name, field_name, processor=0, timestep=end_ts) +# print(f"Using {db_field_name} from the database") +# client.run_script("svd", "calc_svd", [db_field_name], ["U", "S", "V"]) +# U = client.get_tensor("U") +# S = client.get_tensor("S") +# V = client.get_tensor("V") +# +# # Compute the Singular Value Decomposition of the field +# field_svd = np.dot(U, np.dot(S, V)) +# field_svd = field_svd[:, np.newaxis] +# +# # Compute the mean error of the SVD +# field = client.get_tensor(db_field_name) +# svd_rmse = np.sqrt(np.mean((field - field_svd) ** 2)) +# print (f"RMSE({field_name}, SVD({field_name})): {svd_rmse}") exp.stop(db) diff --git a/tutorials/functionObject/pitzDaily/system/controlDict b/tutorials/functionObject/pitzDaily/system/controlDict index eb2dc7e..72015da 100644 --- a/tutorials/functionObject/pitzDaily/system/controlDict +++ b/tutorials/functionObject/pitzDaily/system/controlDict @@ -51,9 +51,15 @@ functions type fieldsToSmartRedis; libs ("libsmartredisFunctionObjects.so"); clusterMode off; + ensemble pitzDaily; fields (p U phi); patches (internal); } } +DebugSwitches +{ + smartRedisClient 1; +} + // ************************************************************************* // From 1d6f88877721c81e3e0df07043dfb8f4b90f8729 Mon Sep 17 00:00:00 2001 From: Elwardi Date: Tue, 9 Jul 2024 12:38:43 +0200 Subject: [PATCH 2/2] fix: add Allrun script --- tutorials/functionObject/pitzDaily/Allrun | 3 +++ 1 file changed, 3 insertions(+) create mode 100755 tutorials/functionObject/pitzDaily/Allrun diff --git a/tutorials/functionObject/pitzDaily/Allrun b/tutorials/functionObject/pitzDaily/Allrun new file mode 100755 index 0000000..115d3a2 --- /dev/null +++ b/tutorials/functionObject/pitzDaily/Allrun @@ -0,0 +1,3 @@ +#!/usr/bin/bash +blockMesh +simpleFoam