From 347f4d7dd5ce36285206f3717d70ed26e01c5ddd Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Thu, 18 Jul 2019 11:30:32 -0500 Subject: [PATCH] Separating dagmc cell temperature checks into unit tests. --- src/dagmc.cpp | 19 +++-- .../dagmc/legacy/inputs_true.dat | 3 +- tests/regression_tests/dagmc/legacy/test.py | 35 ++------- tests/unit_tests/dagmc/__init__.py | 0 tests/unit_tests/dagmc/conftest.py | 12 +++ tests/unit_tests/dagmc/dagmc.h5m | 1 + tests/unit_tests/dagmc/test.py | 75 +++++++++++++++++++ 7 files changed, 106 insertions(+), 39 deletions(-) create mode 100644 tests/unit_tests/dagmc/__init__.py create mode 100644 tests/unit_tests/dagmc/conftest.py create mode 120000 tests/unit_tests/dagmc/dagmc.h5m create mode 100644 tests/unit_tests/dagmc/test.py diff --git a/src/dagmc.cpp b/src/dagmc.cpp index 1e6f3d02a6..a4ca81fbe3 100644 --- a/src/dagmc.cpp +++ b/src/dagmc.cpp @@ -35,10 +35,9 @@ const bool dagmc_enabled = false; #ifdef DAGMC -const std::string DAGMC_FILENAME = "dagmc.h5m"; - namespace openmc { +const std::string DAGMC_FILENAME = settings::path_input + "dagmc.h5m"; namespace simulation { @@ -54,8 +53,16 @@ moab::DagMC* DAG; } // namespace model +void check_dagmc_file() { + if (!file_exists(DAGMC_FILENAME)) { + fatal_error("Geometry DAGMC file '" + DAGMC_FILENAME + "' does not exist!"); + } +} + bool get_uwuw_materials_xml(std::string& s) { - UWUW uwuw(DAGMC_FILENAME.c_str()); + check_dagmc_file(); + + UWUW uwuw((settings::path_input + DAGMC_FILENAME).c_str()); std::stringstream ss; bool uwuw_mats_present = false; @@ -376,11 +383,7 @@ void load_dagmc_geometry() void read_geometry_dagmc() { // Check if dagmc.h5m exists - std::string filename = settings::path_input + "dagmc.h5m"; - if (!file_exists(filename)) { - fatal_error("Geometry DAGMC file '" + filename + "' does not exist!"); - } - + check_dagmc_file(); write_message("Reading DAGMC geometry...", 5); load_dagmc_geometry(); diff --git a/tests/regression_tests/dagmc/legacy/inputs_true.dat b/tests/regression_tests/dagmc/legacy/inputs_true.dat index b2b092cff0..8ca49c3246 100644 --- a/tests/regression_tests/dagmc/legacy/inputs_true.dat +++ b/tests/regression_tests/dagmc/legacy/inputs_true.dat @@ -1,6 +1,6 @@ - + @@ -22,7 +22,6 @@ -4 -4 -4 4 4 4 - 50.0 true diff --git a/tests/regression_tests/dagmc/legacy/test.py b/tests/regression_tests/dagmc/legacy/test.py index 1e4107cfbc..73a3babe6d 100644 --- a/tests/regression_tests/dagmc/legacy/test.py +++ b/tests/regression_tests/dagmc/legacy/test.py @@ -1,28 +1,25 @@ import openmc import openmc.capi -from openmc.stats import Box import pytest from tests.testing_harness import PyAPITestHarness -import numpy as np - pytestmark = pytest.mark.skipif( not openmc.capi._dagmc_enabled(), reason="DAGMC CAD geometry is not enabled.") -@pytest.fixture(scope="module") -def dagmc_model(): +def test_dagmc(): model = openmc.model.Model() # settings model.settings.batches = 5 model.settings.inactive = 0 model.settings.particles = 100 - model.settings.temperature = {'tolerance': 50.0} - source = openmc.Source(space=Box([-4, -4, -4], - [ 4, 4, 4])) + source_box = openmc.stats.Box([-4, -4, -4], + [ 4, 4, 4]) + source = openmc.Source(space=source_box) + model.settings.source = source model.settings.dagmc = True @@ -38,7 +35,6 @@ def dagmc_model(): u235.add_nuclide('U235', 1.0, 'ao') u235.set_density('g/cc', 11) u235.id = 40 - u235.temperature = 320 water = openmc.Material(name="water") water.add_nuclide('H1', 2.0, 'ao') @@ -50,24 +46,5 @@ def dagmc_model(): mats = openmc.Materials([u235, water]) model.materials = mats - yield model - -def test_dagmc_temps(dagmc_model): - dagmc_model.export_to_xml() - - # check cell temps as well here - openmc.capi.init([]) - - expected_temps = { 1 : 320.0, # assigned by material - 2 : 300.0, # assigned in dagmc file - 3 : 293.6 } # assigned by default - - for cell_id, temp in expected_temps.items(): - capi_cell = openmc.capi.cells[cell_id] - assert np.isclose(capi_cell.get_temperature(), temp) - - openmc.capi.finalize() - -def test_dagmc(dagmc_model): - harness = PyAPITestHarness('statepoint.5.h5', model=dagmc_model) + harness = PyAPITestHarness('statepoint.5.h5', model=model) harness.main() diff --git a/tests/unit_tests/dagmc/__init__.py b/tests/unit_tests/dagmc/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unit_tests/dagmc/conftest.py b/tests/unit_tests/dagmc/conftest.py new file mode 100644 index 0000000000..6215322314 --- /dev/null +++ b/tests/unit_tests/dagmc/conftest.py @@ -0,0 +1,12 @@ + +import pytest + +@pytest.fixture(scope='module', autouse=True) +def setup_dagmc_unit_test(request): + + # Change to test directory + olddir = request.fspath.dirpath().chdir() + try: + yield + finally: + olddir.chdir() diff --git a/tests/unit_tests/dagmc/dagmc.h5m b/tests/unit_tests/dagmc/dagmc.h5m new file mode 120000 index 0000000000..0c5ab5da82 --- /dev/null +++ b/tests/unit_tests/dagmc/dagmc.h5m @@ -0,0 +1 @@ +../../regression_tests/dagmc/legacy/dagmc.h5m \ No newline at end of file diff --git a/tests/unit_tests/dagmc/test.py b/tests/unit_tests/dagmc/test.py new file mode 100644 index 0000000000..2302ab7e45 --- /dev/null +++ b/tests/unit_tests/dagmc/test.py @@ -0,0 +1,75 @@ +import glob +import os + +import numpy as np +import pytest + +import openmc +import openmc.capi + +from tests import cdtemp + +pytestmark = pytest.mark.skipif( + not openmc.capi._dagmc_enabled(), + reason="DAGMC CAD geometry is not enabled.") + +def test_dagmc_temperatures(): + model = openmc.model.Model() + + # settings + model.settings.batches = 5 + model.settings.inactive = 0 + model.settings.particles = 100 + model.settings.temperature = {'tolerance': 50.0} + model.settings.verbosity = 1 + source_box = openmc.stats.Box([-4, -4, -4], + [ 4, 4, 4]) + source = openmc.Source(space=source_box) + model.settings.source = source + + model.settings.dagmc = True + + # tally + tally = openmc.Tally() + tally.scores = ['total'] + tally.filters = [openmc.CellFilter(1)] + model.tallies = [tally] + + # materials + u235 = openmc.Material(name="fuel") + u235.add_nuclide('U235', 1.0, 'ao') + u235.set_density('g/cc', 11) + u235.id = 40 + u235.temperature = 320 + + water = openmc.Material(name="water") + water.add_nuclide('H1', 2.0, 'ao') + water.add_nuclide('O16', 1.0, 'ao') + water.set_density('g/cc', 1.0) + water.add_s_alpha_beta('c_H_in_H2O') + water.id = 41 + + mats = openmc.Materials([u235, water]) + model.materials = mats + + model.export_to_xml() + + # check cell temps as well here + openmc.capi.init() + + expected_temps = { 1 : 320.0, # assigned by material + 2 : 300.0, # assigned in dagmc file + 3 : 293.6 } # assigned by default + + for cell_id, temp in expected_temps.items(): + capi_cell = openmc.capi.cells[cell_id] + assert np.isclose(capi_cell.get_temperature(), temp) + + openmc.capi.finalize() + + # cleanup + input_files = glob.glob("*.xml") + input_files += glob.glob("*.h5") + for f in input_files: + if os.path.exists(f): + os.remove(f)