From ab4ef1d7be99154edfb6567d86cdc2f5b32e403d Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Wed, 20 Oct 2021 10:47:44 -0500 Subject: [PATCH 1/3] Ensure Materials.cross_sections accepts path-like object --- openmc/material.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/openmc/material.py b/openmc/material.py index 4d43664336..9041fee595 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -935,14 +935,9 @@ class Material(IDManagerMixin): xml_elements.append(self._get_nuclide_xml(nuclide)) return xml_elements - def to_xml_element(self, cross_sections=None): + def to_xml_element(self): """Return XML representation of the material - Parameters - ---------- - cross_sections : str - Path to an XML cross sections listing file - Returns ------- element : xml.etree.ElementTree.Element @@ -1092,7 +1087,7 @@ class Material(IDManagerMixin): new_density = np.sum([dens for dens in mass_per_cc.values()]) new_mat.set_density('g/cm3', new_density) - # If any of the involved materials is depletable, the new material is + # If any of the involved materials is depletable, the new material is # depletable new_mat.depletable = any(mat.depletable for mat in materials) @@ -1173,7 +1168,10 @@ class Materials(cv.CheckedList): ---------- materials : Iterable of openmc.Material Materials to add to the collection - cross_sections : str + + Attributes + ---------- + cross_sections : str or path-like Indicates the path to an XML cross section listing file (usually named cross_sections.xml). If it is not set, the :envvar:`OPENMC_CROSS_SECTIONS` environment variable will be used for @@ -1196,8 +1194,8 @@ class Materials(cv.CheckedList): @cross_sections.setter def cross_sections(self, cross_sections): - cv.check_type('cross sections', cross_sections, str) - self._cross_sections = cross_sections + if cross_sections is not None: + self._cross_sections = Path(cross_sections) def append(self, material): """Append material to collection @@ -1252,9 +1250,9 @@ class Materials(cv.CheckedList): fh.write('\n') # Write the element. - if self._cross_sections is not None: + if self.cross_sections is not None: element = ET.Element('cross_sections') - element.text = str(self._cross_sections) + element.text = str(self.cross_sections) clean_indentation(element, level=1) element.tail = element.tail.strip(' ') fh.write(' ') @@ -1263,7 +1261,7 @@ class Materials(cv.CheckedList): # Write the elements. for material in sorted(self, key=lambda x: x.id): - element = material.to_xml_element(self.cross_sections) + element = material.to_xml_element() clean_indentation(element, level=1) element.tail = element.tail.strip(' ') fh.write(' ') From 32d041ec90deb4e2bffd9714cba7661d32a05328 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Wed, 20 Oct 2021 10:48:27 -0500 Subject: [PATCH 2/3] Fix bug for when local cross_sections.xml is specified --- src/cross_sections.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/cross_sections.cpp b/src/cross_sections.cpp index 9156bdc901..afb0a1f36c 100644 --- a/src/cross_sections.cpp +++ b/src/cross_sections.cpp @@ -76,8 +76,10 @@ Library::Library(pugi::xml_node node, const std::string& directory) path_ = path; } else if (ends_with(directory, "/")) { path_ = directory + path; - } else { + } else if (!directory.empty()) { path_ = directory + "/" + path; + } else { + path_ = path; } if (!file_exists(path_)) { @@ -135,6 +137,13 @@ void read_cross_sections_xml() } } else { settings::path_cross_sections = get_node_value(root, "cross_sections"); + + // If no '/' found, the file is probably in the input directory + auto pos = settings::path_cross_sections.rfind("/"); + if (pos == std::string::npos && !settings::path_input.empty()) { + settings::path_cross_sections = + settings::path_input + "/" + settings::path_cross_sections; + } } // Now that the cross_sections.xml or mgxs.h5 has been located, read it in @@ -295,10 +304,12 @@ void read_ce_cross_sections_xml() // TODO: Use std::filesystem functionality when C++17 is adopted auto pos = filename.rfind("/"); if (pos == std::string::npos) { - // no '/' found, probably a Windows directory - pos = filename.rfind("\\"); + // No '\\' found, so the file must be in the same directory as + // materials.xml + directory = settings::path_input; + } else { + directory = filename.substr(0, pos); } - directory = filename.substr(0, pos); } for (const auto& node_library : root.children("library")) { From 96e3b2c9b8bee87797b34c21470c0865a44b87ae Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Wed, 20 Oct 2021 13:27:10 -0500 Subject: [PATCH 3/3] Update inputs for mg_convert test --- tests/regression_tests/mg_convert/inputs_true.dat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/regression_tests/mg_convert/inputs_true.dat b/tests/regression_tests/mg_convert/inputs_true.dat index 9db9b7d7c3..753c0d304c 100644 --- a/tests/regression_tests/mg_convert/inputs_true.dat +++ b/tests/regression_tests/mg_convert/inputs_true.dat @@ -8,7 +8,7 @@ - ./mgxs.h5 + mgxs.h5