From 165e947683781c13ce56a15b986fed2fb646cd27 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 21 Aug 2024 14:18:09 -0500 Subject: [PATCH] Adding tests for CLI command --- src/openmc_cad_adapter/to_cubit_journal.py | 37 ++++++++++++++-------- test/test_examples.py | 23 +++++++++++++- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/openmc_cad_adapter/to_cubit_journal.py b/src/openmc_cad_adapter/to_cubit_journal.py index 574651b..91c45bb 100644 --- a/src/openmc_cad_adapter/to_cubit_journal.py +++ b/src/openmc_cad_adapter/to_cubit_journal.py @@ -1,5 +1,6 @@ from argparse import ArgumentParser import math +from pathlib import Path import sys @@ -173,10 +174,24 @@ def vector_to_euler_xyz(v): return phi * oe, theta * oe, psi * oe -def to_cubit_journal(geom, seen=set(), world=None, cells=None, filename=None, to_cubit=False): +def to_cubit_journal(geometry : openmc.Geometry, seen=set(), world=None, cells=None, filename=None, to_cubit=False): + + if isinstance(geometry, openmc.Model): + geometry = geometry.geometry + + geom = geometry + + if world is None: + bbox = geometry.bounding_box + if not all(np.isfinite(bbox[0])) or not all(np.isfinite(bbox[1])): + raise RuntimeError('Model bounds were not provided and the bounding box determined by OpenMC is not finite.' + 'Please provide a world size argument to proceed') + # to ensure that the box + box_max = np.max(np.abs(bbox[0], bbox[1]).T) + world_size = (2 * box_max, 2 * box_max, 2 * box_max) if world is None: - raise RuntimeError("Model extents must be provided") + raise RuntimeError("Model extents could not be determined automatically and must be provided manually") w = world cid = 1 @@ -925,22 +940,16 @@ def do_cell( cell ): def openmc_to_cad(): """Command-line interface for OpenMC to CAD model conversion""" parser = ArgumentParser() - parser.add_argument('input', help='Path to a OpenMC model.xml file') + parser.add_argument('input', help='Path to a OpenMC model.xml file or path to a directory containing OpenMC XMLs') parser.add_argument('-o', '--output', help='Output filename', default='openmc.jou') parser.add_argument('-w', '--world-size', help='Maximum width of the geometry in X, Y, and Z', nargs=3, type=int) args = parser.parse_args() - model = openmc.Model.from_model_xml(args.input) + model_path = Path(args.input) - if args.world_size is None: - bbox = model.geometry.bounding_box - if not all(np.isfinite(bbox[0])) or not all(np.isfinite(bbox[1])): - raise RuntimeError('Model bounds were not provided and the bounding box determined by OpenMC is not finite.' - 'Please provide a world size argument to proceed') - # to ensure that the box - box_max = np.max(np.abs(bbox[0], bbox[1]).T) - world_size = (2 * box_max, 2 * box_max, 2 * box_max) + if model_path.is_dir(): + model = openmc.Model.from_xml() else: - world_size = args.world_size + model = openmc.Model.from_model_xml() - to_cubit_journal(model.geometry, world=world_size, filename=args.output) + to_cubit_journal(model.geometry, world=args.world_size, filename=args.output) diff --git a/test/test_examples.py b/test/test_examples.py index ced76b9..e9f3a43 100644 --- a/test/test_examples.py +++ b/test/test_examples.py @@ -3,6 +3,8 @@ import filecmp from pathlib import Path +import subprocess + import pytest from openmc_cad_adapter import to_cubit_journal @@ -44,4 +46,23 @@ def test_examples(example, request): to_cubit_journal(model.geometry, world=world, filename=output) gold_file = request.path.parent / Path('gold') / Path(output) - diff_files(output, gold_file) \ No newline at end of file + diff_files(output, gold_file) + + +@pytest.mark.parametrize("example", examples, ids=example_name) +def test_examples_cli(example, request): + + openmc.reset_auto_ids() + example_path = OPENMC_EXAMPLES_DIR / example + exec(open(example_path).read()) + + world = [500, 500, 500] + output = example_name(example) + cmd = ['openmc_to_cad', example_path.parent, '-o', output, '--world'] + [str(w) for w in world] + pipe = subprocess.Popen(cmd) + pipe.wait() + if pipe.returncode != 0: + raise RuntimeError(f'Command {" ".join(cmd)} failed') + + gold_file = request.path.parent / Path('gold') / Path(output) + diff_files(output, gold_file)