From d7d2230e5f550473bb9121f12b04267c71c636ed Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Tue, 16 Jan 2024 11:12:44 -0600 Subject: [PATCH] Fix locating h5m files references in DAGMC universes (#2842) --- include/openmc/file_utils.h | 5 +++ src/dagmc.cpp | 5 ++- src/file_utils.cpp | 6 ++++ src/finalize.cpp | 11 +++++++ src/initialize.cpp | 18 ++++------ tests/unit_tests/dagmc/test_h5m_subdir.py | 40 +++++++++++++++++++++++ 6 files changed, 70 insertions(+), 15 deletions(-) create mode 100644 tests/unit_tests/dagmc/test_h5m_subdir.py diff --git a/include/openmc/file_utils.h b/include/openmc/file_utils.h index 11b20b4aabc..6e9812d5e83 100644 --- a/include/openmc/file_utils.h +++ b/include/openmc/file_utils.h @@ -16,6 +16,11 @@ bool dir_exists(const std::string& path); //! \return Whether file exists bool file_exists(const std::string& filename); +//! Determine directory containing given file +//! \param[in] filename Path to file +//! \return Name of directory containing file +std::string dir_name(const std::string& filename); + // Gets the file extension of whatever string is passed in. This is defined as // a sequence of strictly alphanumeric characters which follow the last period, // i.e. at least one alphabet character is present, and zero or more numbers. diff --git a/src/dagmc.cpp b/src/dagmc.cpp index 561fdca81ba..56722fde664 100644 --- a/src/dagmc.cpp +++ b/src/dagmc.cpp @@ -49,8 +49,8 @@ DAGUniverse::DAGUniverse(pugi::xml_node node) if (check_for_node(node, "filename")) { filename_ = get_node_value(node, "filename"); - if (!file_exists(filename_)) { - fatal_error(fmt::format("DAGMC file '{}' could not be found", filename_)); + if (!starts_with(filename_, "/")) { + filename_ = dir_name(settings::path_input) + filename_; } } else { fatal_error("Must specify a file for the DAGMC universe"); @@ -123,7 +123,6 @@ void DAGUniverse::init_dagmc() dagmc_instance_ = std::make_shared(); // load the DAGMC geometry - filename_ = settings::path_input + filename_; if (!file_exists(filename_)) { fatal_error("Geometry DAGMC file '" + filename_ + "' does not exist!"); } diff --git a/src/file_utils.cpp b/src/file_utils.cpp index 8a078fe18b3..197c9767cb0 100644 --- a/src/file_utils.cpp +++ b/src/file_utils.cpp @@ -26,6 +26,12 @@ bool file_exists(const std::string& filename) return s.good(); } +std::string dir_name(const std::string& filename) +{ + size_t pos = filename.find_last_of("\\/"); + return (std::string::npos == pos) ? "" : filename.substr(0, pos + 1); +} + std::string get_file_extension(const std::string& filename) { // try our best to work on windows... diff --git a/src/finalize.cpp b/src/finalize.cpp index ea0470f9b2c..a41c2f783a5 100644 --- a/src/finalize.cpp +++ b/src/finalize.cpp @@ -88,17 +88,27 @@ int openmc_finalize() settings::legendre_to_tabular = true; settings::legendre_to_tabular_points = -1; settings::material_cell_offsets = true; + settings::max_lost_particles = 10; + settings::max_order = 0; settings::max_particles_in_flight = 100000; settings::max_splits = 1000; settings::max_tracks = 1000; settings::max_write_lost_particles = -1; + settings::n_log_bins = 8000; settings::n_inactive = 0; settings::n_particles = -1; settings::output_summary = true; settings::output_tallies = true; settings::particle_restart_run = false; + settings::path_cross_sections.clear(); + settings::path_input.clear(); + settings::path_output.clear(); + settings::path_particle_restart.clear(); + settings::path_sourcepoint.clear(); + settings::path_statepoint.clear(); settings::photon_transport = false; settings::reduce_tallies = true; + settings::rel_max_lost_particles = 1.0e-6; settings::res_scat_on = false; settings::res_scat_method = ResScatMethod::rvs; settings::res_scat_energy_min = 0.01; @@ -123,6 +133,7 @@ int openmc_finalize() settings::verbosity = 7; settings::weight_cutoff = 0.25; settings::weight_survive = 1.0; + settings::weight_windows_file.clear(); settings::weight_windows_on = false; settings::write_all_tracks = false; settings::write_initial_source = false; diff --git a/src/initialize.cpp b/src/initialize.cpp index 9e3c886039c..cc1eac9cf35 100644 --- a/src/initialize.cpp +++ b/src/initialize.cpp @@ -304,8 +304,9 @@ int parse_command_line(int argc, char* argv[]) settings::path_input)); } - // Add slash at end of directory if it isn't the - if (!ends_with(settings::path_input, "/")) { + // Add slash at end of directory if it isn't there + if (!ends_with(settings::path_input, "/") && + dir_exists(settings::path_input)) { settings::path_input += "/"; } } @@ -315,18 +316,11 @@ int parse_command_line(int argc, char* argv[]) bool read_model_xml() { - std::string model_filename = - settings::path_input.empty() ? "." : settings::path_input; - - // some string cleanup - // a trailing "/" is applied to path_input if it's specified, - // remove it for the first attempt at reading the input file - if (ends_with(model_filename, "/")) - model_filename.pop_back(); + std::string model_filename = settings::path_input; // if the current filename is a directory, append the default model filename - if (dir_exists(model_filename)) - model_filename += "/model.xml"; + if (model_filename.empty() || dir_exists(model_filename)) + model_filename += "model.xml"; // if this file doesn't exist, stop here if (!file_exists(model_filename)) diff --git a/tests/unit_tests/dagmc/test_h5m_subdir.py b/tests/unit_tests/dagmc/test_h5m_subdir.py new file mode 100644 index 00000000000..dd9c6b043b6 --- /dev/null +++ b/tests/unit_tests/dagmc/test_h5m_subdir.py @@ -0,0 +1,40 @@ +import shutil +from pathlib import Path + +import openmc +import openmc.lib +import pytest + +pytestmark = pytest.mark.skipif( + not openmc.lib._dagmc_enabled(), reason="DAGMC CAD geometry is not enabled." +) + + +@pytest.mark.parametrize("absolute", [True, False]) +def test_model_h5m_in_subdirectory(run_in_tmpdir, request, absolute): + # Create new subdirectory and copy h5m file there + h5m = Path(request.fspath).parent / "dagmc.h5m" + subdir = Path("h5m") + subdir.mkdir() + shutil.copy(h5m, subdir) + + # Create simple model with h5m file in subdirectory + if absolute: + dag_univ = openmc.DAGMCUniverse((subdir / "dagmc.h5m").absolute()) + else: + dag_univ = openmc.DAGMCUniverse(subdir / "dagmc.h5m") + model = openmc.Model() + model.geometry = openmc.Geometry(dag_univ.bounded_universe()) + mat1 = openmc.Material(name="41") + mat1.add_nuclide("H1", 1.0) + mat2 = openmc.Material(name="no-void fuel") + mat2.add_nuclide("U235", 1.0) + model.materials = [mat1, mat2] + model.settings.batches = 10 + model.settings.inactive = 5 + model.settings.particles = 1000 + + # Make sure model can load + model.export_to_model_xml() + openmc.lib.init(["model.xml"]) + openmc.lib.finalize()