Skip to content

Commit

Permalink
config-generator: validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Horn committed Aug 12, 2024
1 parent 0909cb4 commit 9f1c3da
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
10 changes: 10 additions & 0 deletions examples/config-generator/src/generators/coupling_2way.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
9 changes: 9 additions & 0 deletions examples/config-generator/src/generators/solver_cfd.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
27 changes: 26 additions & 1 deletion examples/config-generator/src/generators/use_checkpoint.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
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)
checkpoint_xml = '<checkpoint-configuration filename="CheckpointSimpleMD" write-every-timestep="0"></checkpoint-configuration>'
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.'
)
8 changes: 7 additions & 1 deletion examples/config-generator/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down

0 comments on commit 9f1c3da

Please sign in to comment.