-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_model.py
More file actions
executable file
·62 lines (49 loc) · 1.74 KB
/
Copy pathgenerate_model.py
File metadata and controls
executable file
·62 lines (49 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
"""Generate a runnable OpenMC XML model from a benchmark model.
For example::
python generate_model.py mesh_tally_regular --output-dir /tmp
The model module is imported from ``benchmarks.models``. The output directory
contains a model XML file named after the model module.
"""
import argparse
import importlib
from pathlib import Path
def main() -> int:
parser = argparse.ArgumentParser(
description="Generate a model XML file from an OpenMC benchmark."
)
parser.add_argument(
"model_name",
help="benchmark model module name, e.g. mesh_tally_regular",
)
parser.add_argument(
"--output-dir",
type=Path,
default=Path("."),
help="directory for the generated XML (default: current directory)",
)
args = parser.parse_args()
# Import the model module
try:
module = importlib.import_module(f"benchmarks.models.{args.model_name}")
except ImportError as exc:
parser.error(
f"could not import benchmarks.models.{args.model_name}: {exc}"
)
# Check that build_model function exists
if not hasattr(module, "build_model"):
parser.error(
f"benchmarks.models.{args.model_name} does not have a build_model() function"
)
# Build the model
print(f"Building model from benchmarks.models.{args.model_name}...")
model = module.build_model()
output_dir = args.output_dir
output_dir.mkdir(parents=True, exist_ok=True)
output_file = output_dir / f"{args.model_name}.xml"
print(f"Exporting to {output_file}...")
model.export_to_model_xml(output_file)
print(f"Successfully generated {output_file}")
return 0
if __name__ == "__main__":
raise SystemExit(main())