From 7bcf2ca9e8919f5ba51e06d06a9e5e9f3c30ec04 Mon Sep 17 00:00:00 2001 From: agnelson Date: Tue, 27 Jul 2021 19:03:38 -0500 Subject: [PATCH] Adding a check to ensure we dont translate a cell not filled with a universe and then incorporating this in the unit test. --- src/cell.cpp | 6 ++++++ tests/unit_tests/test_lib.py | 40 ++++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/cell.cpp b/src/cell.cpp index 6011081d76..a586677730 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -1318,6 +1318,12 @@ extern "C" int openmc_cell_get_translation(int32_t index, double xyz[]) extern "C" int openmc_cell_set_translation(int32_t index, const double xyz[]) { if (index >= 0 && index < model::cells.size()) { + if (model::cells[index]->fill_ == C_NONE) { + set_errmsg(fmt::format("Cannot apply a translation to cell {}" + " because it is not filled with another universe", + index)); + return OPENMC_E_GEOMETRY; + } model::cells[index]->translation_ = Position(xyz); return 0; } else { diff --git a/tests/unit_tests/test_lib.py b/tests/unit_tests/test_lib.py index 8ec12efdd3..0375f1437c 100644 --- a/tests/unit_tests/test_lib.py +++ b/tests/unit_tests/test_lib.py @@ -99,6 +99,21 @@ def lib_run(lib_simulation_init): openmc.lib.run() +@pytest.fixture(scope='module') +def pincell_model_w_univ(): + """Set up a model to test with and delete files when done""" + openmc.reset_auto_ids() + pincell = openmc.examples.pwr_pin_cell() + clad_univ = openmc.Universe(cells=[openmc.Cell(fill=pincell.materials[1])]) + pincell.geometry.root_universe.cells[2].fill = clad_univ + pincell.settings.verbosity = 1 + + # Write XML files in tmpdir + with cdtemp(): + pincell.export_to_xml() + yield + + def test_cell_mapping(lib_init): cells = openmc.lib.cells assert isinstance(cells, Mapping) @@ -141,12 +156,6 @@ def test_properties_temperature(lib_init): openmc.lib.import_properties('properties.h5') assert cell.get_temperature() == pytest.approx(200.0) -def test_cell_translation(lib_init): - cell = openmc.lib.cells[1] - assert cell.get_translation() == pytest.approx([0., 0., 0.]) - cell.set_translation(np.array([1., 0., -1.])) - assert cell.get_translation() == pytest.approx([1., 0., -1.]) - def test_new_cell(lib_init): with pytest.raises(exc.AllocationError): @@ -699,3 +708,22 @@ def test_trigger_set_n_batches(uo2_trigger_model, mpi_intracomm): # Ensure statepoint was created only at batch 20 when calling set_batches assert not os.path.exists('statepoint.12.h5') assert os.path.exists('statepoint.20.h5') + + +def test_cell_translation(pincell_model_w_univ, mpi_intracomm): + openmc.lib.finalize() + openmc.lib.init(intracomm=mpi_intracomm) + openmc.lib.simulation_init() + # Cell 1 is filled with a material so it has a translation, but we can't + # set it. + cell = openmc.lib.cells[1] + assert cell.get_translation() == pytest.approx([0., 0., 0.]) + with pytest.raises(exc.GeometryError, match='not filled with'): + cell.set_translation(np.array([1., 0., -1.])) + + # Cell 2 was given a universe, so we can assign it a translation vector + cell = openmc.lib.cells[2] + assert cell.get_translation() == pytest.approx([0., 0., 0.]) + # This time we *can* set it + cell.set_translation(np.array([1., 0., -1.])) + assert cell.get_translation() == pytest.approx([1., 0., -1.])