From 89fafb4fdff9fd00ebad6161beef36189308054c Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 26 Oct 2022 13:20:53 +0000 Subject: [PATCH 1/3] Improving missing material error for DAGMC material by ID --- src/dagmc.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/dagmc.cpp b/src/dagmc.cpp index 0e04869fdc..b06db792ee 100644 --- a/src/dagmc.cpp +++ b/src/dagmc.cpp @@ -486,13 +486,20 @@ void DAGUniverse::legacy_assign_material( // if no material was set using a name, assign by id if (!mat_found_by_name) { + bool found_by_id = true; try { auto id = std::stoi(mat_string); + if (model::material_map.find(id) == model::material_map.end()) + found_by_id = false; c->material_.emplace_back(id); } catch (const std::invalid_argument&) { + found_by_id = false; + } + + // report failure for failed int conversion or missing material + if (!found_by_id) fatal_error(fmt::format( "No material '{}' found for volume (cell) {}", mat_string, c->id_)); - } } if (settings::verbosity >= 10) { From 075ff0304c8acc6b5907387ba919b44f4737aa2e Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 26 Oct 2022 14:10:16 +0000 Subject: [PATCH 2/3] Adding tests for expected error messages --- tests/regression_tests/dagmc/legacy/test.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/regression_tests/dagmc/legacy/test.py b/tests/regression_tests/dagmc/legacy/test.py index 91734a2195..706b52beaf 100644 --- a/tests/regression_tests/dagmc/legacy/test.py +++ b/tests/regression_tests/dagmc/legacy/test.py @@ -11,6 +11,7 @@ pytestmark = pytest.mark.skipif( @pytest.fixture def model(): + openmc.reset_auto_ids() model = openmc.model.Model() @@ -56,6 +57,24 @@ def model(): return model +def test_missing_material_id(model): + # remove the last material, which is identified by ID in the DAGMC file + model.materials = model.materials[:-1] + with pytest.raises(RuntimeError) as exec_info: + model.run() + exp_error_msg = "Material with ID '41' not found for volume (cell) 3" + assert exp_error_msg in str(exec_info.value) + + +def test_missing_material_name(model): + # remove the first material, which is identified by name in the DAGMC file + model.materials = model.materials[1:] + with pytest.raises(RuntimeError) as exec_info: + model.run() + exp_error_msg = "No material 'no-void fuel' found for volume (cell) 1" + assert exp_error_msg in str(exec_info.value) + + def test_dagmc(model): harness = PyAPITestHarness('statepoint.5.h5', model) harness.main() From 7266fcc5edc11816f2ea010dfb896dc1af0a0e50 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 26 Oct 2022 22:06:28 +0000 Subject: [PATCH 3/3] Making error messages consistent --- src/dagmc.cpp | 5 +++-- tests/regression_tests/dagmc/legacy/test.py | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/dagmc.cpp b/src/dagmc.cpp index b06db792ee..1292918919 100644 --- a/src/dagmc.cpp +++ b/src/dagmc.cpp @@ -498,8 +498,9 @@ void DAGUniverse::legacy_assign_material( // report failure for failed int conversion or missing material if (!found_by_id) - fatal_error(fmt::format( - "No material '{}' found for volume (cell) {}", mat_string, c->id_)); + fatal_error( + fmt::format("Material with name/ID '{}' not found for volume (cell) {}", + mat_string, c->id_)); } if (settings::verbosity >= 10) { diff --git a/tests/regression_tests/dagmc/legacy/test.py b/tests/regression_tests/dagmc/legacy/test.py index 706b52beaf..5027751482 100644 --- a/tests/regression_tests/dagmc/legacy/test.py +++ b/tests/regression_tests/dagmc/legacy/test.py @@ -62,7 +62,7 @@ def test_missing_material_id(model): model.materials = model.materials[:-1] with pytest.raises(RuntimeError) as exec_info: model.run() - exp_error_msg = "Material with ID '41' not found for volume (cell) 3" + exp_error_msg = "Material with name/ID '41' not found for volume (cell) 3" assert exp_error_msg in str(exec_info.value) @@ -71,7 +71,7 @@ def test_missing_material_name(model): model.materials = model.materials[1:] with pytest.raises(RuntimeError) as exec_info: model.run() - exp_error_msg = "No material 'no-void fuel' found for volume (cell) 1" + exp_error_msg = "Material with name/ID 'no-void fuel' not found for volume (cell) 1" assert exp_error_msg in str(exec_info.value)