Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tests for CLI command #2

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading