Skip to content

Commit

Permalink
Fix creation of meshes when from loading settings from XML (#2805)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulromano committed Dec 13, 2023
1 parent 85c963e commit 3efd242
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 29 deletions.
2 changes: 1 addition & 1 deletion openmc/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2316,7 +2316,7 @@ def _read_meshes(elem):
A dictionary with mesh IDs as keys and openmc.MeshBase
instanaces as values
"""
out = dict()
out = {}
for mesh_elem in elem.findall('mesh'):
mesh = MeshBase.from_xml_element(mesh_elem)
out[mesh.id] = mesh
Expand Down
35 changes: 15 additions & 20 deletions openmc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1603,15 +1603,14 @@ def _cutoff_from_xml_element(self, root):
if value is not None:
self.cutoff[key] = float(value)

def _entropy_mesh_from_xml_element(self, root, meshes=None):
def _entropy_mesh_from_xml_element(self, root, meshes):
text = get_text(root, 'entropy_mesh')
if text is not None:
path = f"./mesh[@id='{int(text)}']"
elem = root.find(path)
if elem is not None:
self.entropy_mesh = RegularMesh.from_xml_element(elem)
if meshes is not None and self.entropy_mesh is not None:
meshes[self.entropy_mesh.id] = self.entropy_mesh
if text is None:
return
mesh_id = int(text)
if mesh_id not in meshes:
raise ValueError(f'Could not locate mesh with ID "{mesh_id}"')
self.entropy_mesh = meshes[mesh_id]

def _trigger_from_xml_element(self, root):
elem = root.find('trigger')
Expand Down Expand Up @@ -1671,15 +1670,14 @@ def _track_from_xml_element(self, root):
values = [int(x) for x in text.split()]
self.track = list(zip(values[::3], values[1::3], values[2::3]))

def _ufs_mesh_from_xml_element(self, root, meshes=None):
def _ufs_mesh_from_xml_element(self, root, meshes):
text = get_text(root, 'ufs_mesh')
if text is not None:
path = f"./mesh[@id='{int(text)}']"
elem = root.find(path)
if elem is not None:
self.ufs_mesh = RegularMesh.from_xml_element(elem)
if meshes is not None and self.ufs_mesh is not None:
meshes[self.ufs_mesh.id] = self.ufs_mesh
if text is None:
return
mesh_id = int(text)
if mesh_id not in meshes:
raise ValueError(f'Could not locate mesh with ID "{mesh_id}"')
self.ufs_mesh = meshes[mesh_id]

def _resonance_scattering_from_xml_element(self, root):
elem = root.find('resonance_scattering')
Expand Down Expand Up @@ -1743,16 +1741,13 @@ def _weight_window_generators_from_xml_element(self, root, meshes=None):

def _weight_windows_from_xml_element(self, root, meshes=None):
for elem in root.findall('weight_windows'):
ww = WeightWindows.from_xml_element(elem, root)
ww = WeightWindows.from_xml_element(elem, meshes)
self.weight_windows.append(ww)

text = get_text(root, 'weight_windows_on')
if text is not None:
self.weight_windows_on = text in ('true', '1')

if meshes is not None and self.weight_windows:
meshes.update({ww.mesh.id: ww.mesh for ww in self.weight_windows})

def _weight_window_checkpoints_from_xml_element(self, root):
elem = root.find('weight_window_checkpoints')
if elem is None:
Expand Down
2 changes: 1 addition & 1 deletion openmc/stats/multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ def from_xml_element(cls, elem, meshes):

# check if this mesh has been read in from another location already
if mesh_id not in meshes:
raise RuntimeError(f'Could not locate mesh with ID "{mesh_id}"')
raise ValueError(f'Could not locate mesh with ID "{mesh_id}"')

volume_normalized = elem.get("volume_normalized")
volume_normalized = get_text(elem, 'volume_normalized').lower() == 'true'
Expand Down
13 changes: 6 additions & 7 deletions openmc/weight_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,15 @@ def to_xml_element(self) -> ET.Element:
return element

@classmethod
def from_xml_element(cls, elem: ET.Element, root: ET.Element) -> WeightWindows:
def from_xml_element(cls, elem: ET.Element, meshes: Dict[int, MeshBase]) -> WeightWindows:
"""Generate weight window settings from an XML element
Parameters
----------
elem : lxml.etree._Element
XML element
root : lxml.etree._Element
Root element for the file where meshes can be found
meshes : dict
Dictionary mapping IDs to mesh objects
Returns
-------
Expand All @@ -370,10 +370,9 @@ def from_xml_element(cls, elem: ET.Element, root: ET.Element) -> WeightWindows:
"""
# Get mesh for weight windows
mesh_id = int(get_text(elem, 'mesh'))
path = f"./mesh[@id='{mesh_id}']"
mesh_elem = root.find(path)
if mesh_elem is not None:
mesh = MeshBase.from_xml_element(mesh_elem)
if mesh_id not in meshes:
raise ValueError(f'Could not locate mesh with ID "{mesh_id}"')
mesh = meshes[mesh_id]

# Read all other parameters
lower_ww_bounds = [float(l) for l in get_text(elem, 'lower_ww_bounds').split()]
Expand Down

0 comments on commit 3efd242

Please sign in to comment.