From 9f1c3dad51a42af094d7a351d06ab654c40e2c3d Mon Sep 17 00:00:00 2001 From: Ruben Horn <> Date: Mon, 12 Aug 2024 20:18:21 +0200 Subject: [PATCH] config-generator: validation --- .../src/generators/coupling_2way.py | 10 +++++++ .../src/generators/solver_cfd.py | 9 +++++++ .../src/generators/use_checkpoint.py | 27 ++++++++++++++++++- examples/config-generator/src/main.py | 8 +++++- 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/examples/config-generator/src/generators/coupling_2way.py b/examples/config-generator/src/generators/coupling_2way.py index 7736fca1e..e8ea40d5d 100644 --- a/examples/config-generator/src/generators/coupling_2way.py +++ b/examples/config-generator/src/generators/coupling_2way.py @@ -1,3 +1,13 @@ +def validate(get_config_value) -> str: + key = __name__.split(".")[-1] + use_2way_coupling = get_config_value(key) + simulation_type = get_config_value("simulation") + num_md_sims = get_config_value("multi_md") + if use_2way_coupling and simulation_type != "test" and num_md_sims < 200: + return f"Too many coupling cycles or too few MD instances for 2-way coupling." + return None + + def apply(partial_xml, get_config_value) -> None: key = __name__.split(".")[-1] value = "yes" if get_config_value(key) else "no" diff --git a/examples/config-generator/src/generators/solver_cfd.py b/examples/config-generator/src/generators/solver_cfd.py index 07bd931e5..12df5f4cf 100644 --- a/examples/config-generator/src/generators/solver_cfd.py +++ b/examples/config-generator/src/generators/solver_cfd.py @@ -1,3 +1,12 @@ +def validate(get_config_value) -> str: + key = __name__.split(".")[-1] + solver = get_config_value(key) + use_2way_coupling = get_config_value("coupling_2way") + if solver == "analytical" and use_2way_coupling: + return f"Cannot use 2-way coupling with analytical CFD solver." + return None + + def apply(partial_xml, get_config_value) -> None: key = __name__.split(".")[-1] solver = get_config_value(key) diff --git a/examples/config-generator/src/generators/use_checkpoint.py b/examples/config-generator/src/generators/use_checkpoint.py index 789bef3c8..f80fe3413 100644 --- a/examples/config-generator/src/generators/use_checkpoint.py +++ b/examples/config-generator/src/generators/use_checkpoint.py @@ -1,3 +1,17 @@ +import shutil +from pathlib import Path + + +def validate(get_config_value) -> str: + key = __name__.split(".")[-1] + use_checkpoint = get_config_value(key) + domain_size = get_config_value("domain") + solver_md = get_config_value("solver_md") + if use_checkpoint and (domain_size > 1 or solver_md != "md"): + return f"Example checkpoint file only provided for small Simple MD simulation." + return None + + def apply(partial_xml, get_config_value) -> None: key = __name__.split(".")[-1] use_checkpoint = get_config_value(key) @@ -5,6 +19,17 @@ def apply(partial_xml, get_config_value) -> None: partial_xml.substitute("checkpoint", checkpoint_xml if use_checkpoint else "") print("Substituted loading checkpoint") if use_checkpoint: + boundary_condition = get_config_value("boundary") + checkpoint_src_path = ( + Path(__file__).parent.parent.parent + / "assets" + / f"CheckpointSimpleMD_10000_{boundary_condition}_0.checkpoint" + ) + checkpoint_dst_path = ( + Path(get_config_value("output_filename")).parent + / "CheckpointSimpleMD.checkpoint" + ) + shutil.copyfile(checkpoint_src_path, checkpoint_dst_path) print( - 'TODO: Please make sure to provide the file "CheckpointSimpleMD.checkpoint" in the same directory as couette.xml!' + 'Also created file "CheckpointSimpleMD.checkpoint" in the same directory as couette.xml.' ) diff --git a/examples/config-generator/src/main.py b/examples/config-generator/src/main.py index 151c32f94..3b283a6ca 100644 --- a/examples/config-generator/src/main.py +++ b/examples/config-generator/src/main.py @@ -93,7 +93,13 @@ def generate(configs: list, filename: str) -> None: print("Loaded configuration template") for config in configs: generator = load_generator(config["key"]) - generator.apply(xml, lambda k: get_config_value(configs, k)) + # Make output path accessible to generators + config_output_filename = [ + dict(key="output_filename", options=[dict(selected=True, value=filename)]) + ] + generator.apply( + xml, lambda k: get_config_value(configs + config_output_filename, k) + ) with open(filename, "w") as file: file.write(xml.get()) print(f"\nWrote configuration to {filename}")