Skip to content

Commit

Permalink
Adding some simple cell by cell tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pshriwise committed Sep 19, 2024
1 parent 0519844 commit a5a86e8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/openmc_cad_adapter/to_cubit_journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,41 @@ def vector_to_euler_xyz(v):
return phi * oe, theta * oe, psi * oe


def to_cubit_journal(geometry : openmc.Geometry, world : Iterable[Real] = None, cells: Iterable[int] = None, filename: str = "openmc.jou", to_cubit: bool = False, seen: set = set()):
def to_cubit_journal(geometry : openmc.Geometry, world : Iterable[Real] = None,
cells: Iterable[int, openmc.Cell] = None,
filename: str = "openmc.jou",
to_cubit: bool = False,
seen: set = set()):
"""Convert an OpenMC geometry to a Cubit journal.
Parameters
----------
geometry : openmc.Geometry
The geometry to convert to a Cubit journal.
world : Iterable[Real], optional
Extents of the model in X, Y, and Z. Defaults to None.
cells : Iterable[int, openmc.Cell], optional
List of cells or cell IDs to write to individual journal files. If None,
all cells will be written to the same journal file. Defaults to None.
filename : str, optional
Output filename. Defaults to "openmc.jou".
to_cubit : bool, optional
Uses the cubit Python module to write the model as a .cub5 file.
Defaults to False.
seen : set, optional
Internal parameter.
"""

if not filename.endswith('.jou'):
filename += '.jou'

if isinstance(geometry, openmc.Model):
geometry = geometry.geometry

if cells is not None:
cells = [c if not isinstance(c, openmc.Cell) else c.id for c in cells]

if to_cubit:
try:
import cubit
Expand Down Expand Up @@ -920,14 +947,17 @@ def do_cell(cell, cell_ids: Iterable[int] = None):
after = len( cmds )

if cell_ids is not None and cell.id in cell_ids:
with open( filename + f"_cell{cell.id}", "w" ) as f:
if filename.endswith(".jou"):
cell_filename = filename[:-4] + f"_cell{cell.id}.jou"
else:
cell_filename = filename + f"_cell{cell.id}"
with open(cell_filename, "w" ) as f:
for x in cmds[before:after]:
f.write( x + "\n" )

for cell in geom.root_universe._cells.values():
if cells:
if cell.id in cells:
do_cell( cell, cell_ids=cells)
if cells is not None and cell.id in cells:
do_cell( cell, cell_ids=cells)
else:
do_cell( cell )

Expand Down
18 changes: 18 additions & 0 deletions test/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ def test_examples(example, request):
diff_files(output, gold_file)


def test_cell_by_cell_conversion(request):
openmc.reset_auto_ids()
exec(open(OPENMC_EXAMPLES_DIR / "pincell/build_xml.py").read())

openmc.reset_auto_ids()
model = openmc.Model.from_xml()

cell_ids = list(model.geometry.get_all_cells().keys())

world = [500, 500, 500]
output = 'pincell'
to_cubit_journal(model.geometry, world=world, cells=cell_ids, filename=output)

for cell_id in cell_ids:
output = f'pincell_cell{cell_id}.jou'
gold_file = request.path.parent / Path('gold') / Path(output)
diff_files(output, gold_file)

@pytest.mark.parametrize("example", examples, ids=example_name)
def test_examples_cli(example, request):

Expand Down

0 comments on commit a5a86e8

Please sign in to comment.