Merge pull request #1898 from paulromano/two-bug-fixes

Bug fixes for specifying Materials.cross_sections
This commit is contained in:
Patrick Shriwise 2021-10-28 10:03:20 -05:00 committed by GitHub
commit 65fff786ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 18 deletions

View file

@ -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('<materials>\n')
# Write the <cross_sections> 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 <material> 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(' ')

View file

@ -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")) {

View file

@ -8,7 +8,7 @@
</geometry>
<?xml version='1.0' encoding='utf-8'?>
<materials>
<cross_sections>./mgxs.h5</cross_sections>
<cross_sections>mgxs.h5</cross_sections>
<material id="1" name="UO2 fuel">
<density units="macro" value="1.1" />
<macroscopic name="UO2" />