diff --git a/examples/jupyter/cad-based-geometry.ipynb b/examples/jupyter/cad-based-geometry.ipynb index 6ff79d27c..b13c010cb 100644 --- a/examples/jupyter/cad-based-geometry.ipynb +++ b/examples/jupyter/cad-based-geometry.ipynb @@ -236,6 +236,13 @@ "tallies.export_to_xml()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Note:** Applying tally filters in DagMC models requires prior knowledge of the model. Here, we know that the fuel cell's volume ID in the CAD sofware is 1. To identify cells without use of CAD software, load them into the [OpenMC plotter](https://github.com/openmc/plotter) where cell, material, and volume IDs can be identified for native both OpenMC and DagMC geometries." + ] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/src/dagmc.cpp b/src/dagmc.cpp index 78d8bc4fb..3f7c170ce 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 = "dagmc.h5m"; namespace simulation { @@ -54,8 +53,16 @@ moab::DagMC* DAG; } // namespace model +void check_dagmc_file() { + std::string filename = settings::path_input + DAGMC_FILENAME; + if (!file_exists(filename)) { + fatal_error("Geometry DAGMC file '" + 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; @@ -133,7 +140,7 @@ void legacy_assign_material(const std::string& mat_string, DAGCell* c) } if (settings::verbosity >= 10) { - Material* m = model::materials[model::material_map[c->material_[0]]].get(); + const auto& m = model::materials[model::material_map.at(c->material_[0])]; std::stringstream msg; msg << "DAGMC material " << mat_string << " was assigned"; if (mat_found_by_name) { @@ -147,14 +154,18 @@ void legacy_assign_material(const std::string& mat_string, DAGCell* c) void load_dagmc_geometry() { + check_dagmc_file(); + if (!model::DAG) { model::DAG = new moab::DagMC(); } + + std::string filename = settings::path_input + DAGMC_FILENAME; // --- Materials --- // create uwuw instance - UWUW uwuw(DAGMC_FILENAME.c_str()); + UWUW uwuw(filename.c_str()); // check for uwuw material definitions bool using_uwuw = !uwuw.material_library.empty(); @@ -167,7 +178,7 @@ void load_dagmc_geometry() int32_t dagmc_univ_id = 0; // universe is always 0 for DAGMC runs // load the DAGMC geometry - moab::ErrorCode rval = model::DAG->load_file(DAGMC_FILENAME.c_str()); + moab::ErrorCode rval = model::DAG->load_file(filename.c_str()); MB_CHK_ERR_CONT(rval); // initialize acceleration data structures @@ -215,17 +226,6 @@ void load_dagmc_geometry() model::universes[it->second]->cells_.push_back(i); } - // check for temperature assignment - std::string temp_value; - if (model::DAG->has_prop(vol_handle, "temp")) { - rval = model::DAG->prop_value(vol_handle, "temp", temp_value); - MB_CHK_ERR_CONT(rval); - double temp = std::stod(temp_value); - c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * temp)); - } else { - c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * settings::temperature_default)); - } - // MATERIALS if (model::DAG->is_implicit_complement(vol_handle)) { @@ -295,6 +295,26 @@ void load_dagmc_geometry() legacy_assign_material(mat_value, c); } } + + // check for temperature assignment + std::string temp_value; + + // no temperature if void + if (c->material_[0] == MATERIAL_VOID) continue; + + // assign cell temperature + const auto& mat = model::materials[model::material_map.at(c->material_[0])]; + if (model::DAG->has_prop(vol_handle, "temp")) { + rval = model::DAG->prop_value(vol_handle, "temp", temp_value); + MB_CHK_ERR_CONT(rval); + double temp = std::stod(temp_value); + c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * temp)); + } else if (mat->temperature_ > 0.0) { + c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * mat->temperature_)); + } else { + c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * settings::temperature_default)); + } + } // allocate the cell overlap count if necessary @@ -369,11 +389,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/src/summary.cpp b/src/summary.cpp index 500a5c15c..d2721f97e 100644 --- a/src/summary.cpp +++ b/src/summary.cpp @@ -92,6 +92,7 @@ void write_geometry(hid_t file) #ifdef DAGMC if (settings::dagmc) { write_attribute(geom_group, "dagmc", 1); + close_group(geom_group); return; } #endif diff --git a/tests/regression_tests/dagmc/legacy/dagmc.h5m b/tests/regression_tests/dagmc/legacy/dagmc.h5m index c90b6d674..fbbe9a34a 100644 Binary files a/tests/regression_tests/dagmc/legacy/dagmc.h5m and b/tests/regression_tests/dagmc/legacy/dagmc.h5m differ diff --git a/tests/regression_tests/dagmc/legacy/test.py b/tests/regression_tests/dagmc/legacy/test.py index d48ae9871..db062d36e 100644 --- a/tests/regression_tests/dagmc/legacy/test.py +++ b/tests/regression_tests/dagmc/legacy/test.py @@ -1,6 +1,5 @@ import openmc import openmc.capi -from openmc.stats import Box import pytest from tests.testing_harness import PyAPITestHarness @@ -17,8 +16,10 @@ def test_dagmc(): model.settings.inactive = 0 model.settings.particles = 100 - 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 diff --git a/tests/unit_tests/dagmc/__init__.py b/tests/unit_tests/dagmc/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/unit_tests/dagmc/dagmc.h5m b/tests/unit_tests/dagmc/dagmc.h5m new file mode 120000 index 000000000..0c5ab5da8 --- /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 000000000..bfe054dd8 --- /dev/null +++ b/tests/unit_tests/dagmc/test.py @@ -0,0 +1,74 @@ +import shutil + +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.") + + +@pytest.fixture(scope="module", autouse=True) +def dagmc_model(request): + + 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 + + # location of dagmc file in test directory + dagmc_file = request.fspath.dirpath() + "/dagmc.h5m" + # move to a temporary directory + with cdtemp(): + shutil.copyfile(dagmc_file, "./dagmc.h5m") + model.export_to_xml() + openmc.capi.init() + yield + + openmc.capi.finalize() + + +@pytest.mark.parametrize("cell_id,exp_temp", ((1, 320.0), # assigned by material + (2, 300.0), # assigned in dagmc file + (3, 293.6))) # assigned by default +def test_dagmc_temperatures(cell_id, exp_temp): + cell = openmc.capi.cells[cell_id] + assert np.isclose(cell.get_temperature(), exp_temp) diff --git a/tools/ci/travis-install-dagmc.sh b/tools/ci/travis-install-dagmc.sh index ca3e8b3c7..1797e9dba 100755 --- a/tools/ci/travis-install-dagmc.sh +++ b/tools/ci/travis-install-dagmc.sh @@ -20,7 +20,7 @@ mkdir MOAB && cd MOAB git clone -b $MOAB_BRANCH $MOAB_REPO mkdir build && cd build cmake ../moab -DENABLE_HDF5=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=$MOAB_INSTALL_DIR -make -j && make -j test install +make -j && make -j install cmake ../moab -DBUILD_SHARED_LIBS=OFF make -j install rm -rf $HOME/MOAB/moab