Skip to content

Commit

Permalink
Adding tests for CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
pshriwise committed Aug 21, 2024
1 parent bb9d048 commit 165e947
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
37 changes: 23 additions & 14 deletions src/openmc_cad_adapter/to_cubit_journal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from argparse import ArgumentParser
import math
from pathlib import Path
import sys


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
23 changes: 22 additions & 1 deletion test/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import filecmp
from pathlib import Path

import subprocess

import pytest

from openmc_cad_adapter import to_cubit_journal
Expand Down Expand Up @@ -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)
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)

0 comments on commit 165e947

Please sign in to comment.