Skip to content

Commit

Permalink
Fix locating h5m files references in DAGMC universes (#2842)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulromano committed Jan 16, 2024
1 parent 5549b58 commit d7d2230
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 15 deletions.
5 changes: 5 additions & 0 deletions include/openmc/file_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions src/dagmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -123,7 +123,6 @@ void DAGUniverse::init_dagmc()
dagmc_instance_ = std::make_shared<moab::DagMC>();

// load the DAGMC geometry
filename_ = settings::path_input + filename_;
if (!file_exists(filename_)) {
fatal_error("Geometry DAGMC file '" + filename_ + "' does not exist!");
}
Expand Down
6 changes: 6 additions & 0 deletions src/file_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down
11 changes: 11 additions & 0 deletions src/finalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
18 changes: 6 additions & 12 deletions src/initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 += "/";
}
}
Expand All @@ -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))
Expand Down
40 changes: 40 additions & 0 deletions tests/unit_tests/dagmc/test_h5m_subdir.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit d7d2230

Please sign in to comment.