From 9df6820da1605ee8b1df3313896bd6c1a80d0466 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Thu, 20 Jan 2022 10:33:53 -0600 Subject: [PATCH] Implement from_xml_element methods for cylindrical/spherical mesh. Closes #1951 --- openmc/mesh.py | 64 ++++++++++++++++++++++++++++++++---- openmc/stats/multivariate.py | 1 + openmc/weight_windows.py | 2 +- 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 2a16be1ad2..f12c5a3716 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -89,7 +89,7 @@ class MeshBase(IDManagerMixin, ABC): raise ValueError('Unrecognized mesh type: "' + mesh_type + '"') @classmethod - def from_xml(cls, elem): + def from_xml_element(cls, elem): """Generates a mesh from an XML element Parameters @@ -109,6 +109,10 @@ class MeshBase(IDManagerMixin, ABC): return RegularMesh.from_xml_element(elem) elif mesh_type == 'rectilinear': return RectilinearMesh.from_xml_element(elem) + elif mesh_type == 'cylindrical': + return CylindricalMesh.from_xml_element(elem) + elif mesh_type == 'spherical': + return SphericalMesh.from_xml_element(elem) elif mesh_type == 'unstructured': return UnstructuredMesh.from_xml_element(elem) else: @@ -697,10 +701,10 @@ class CylindricalMesh(MeshBase): n_dimension : int Number of mesh dimensions (always 3 for a CylindricalMesh). r_grid : Iterable of float - Mesh boundary points along the r-axis. + Mesh boundary points along the r-axis. Requirement is r >= 0. phi_grid : Iterable of float - Mesh boundary points along the phi-axis. + Mesh boundary points along the phi-axis. The default value is [0, 360], i.e. the full phi range. z_grid : Iterable of float Mesh boundary points along the z-axis. @@ -822,6 +826,29 @@ class CylindricalMesh(MeshBase): return element + @classmethod + def from_xml_element(cls, elem): + """Generate a cylindrical mesh from an XML element + + Parameters + ---------- + elem : xml.etree.ElementTree.Element + XML element + + Returns + ------- + openmc.CylindricalMesh + Cylindrical mesh object + + """ + + mesh_id = int(get_text(elem, 'id')) + mesh = cls(mesh_id) + mesh.r_grid = [float(x) for x in get_text(elem, "r_grid").split()] + mesh.phi_grid = [float(x) for x in get_text(elem, "phi_grid").split()] + mesh.z_grid = [float(x) for x in get_text(elem, "z_grid").split()] + return mesh + def calc_mesh_volumes(self): """Return Volumes for every mesh cell @@ -986,6 +1013,29 @@ class SphericalMesh(MeshBase): return element + @classmethod + def from_xml_element(cls, elem): + """Generate a spherical mesh from an XML element + + Parameters + ---------- + elem : xml.etree.ElementTree.Element + XML element + + Returns + ------- + openmc.SphericalMesh + Spherical mesh object + + """ + + mesh_id = int(get_text(elem, 'id')) + mesh = cls(mesh_id) + mesh.r_grid = [float(x) for x in get_text(elem, "r_grid").split()] + mesh.theta_grid = [float(x) for x in get_text(elem, "theta_grid").split()] + mesh.phi_grid = [float(x) for x in get_text(elem, "phi_grid").split()] + return mesh + def calc_mesh_volumes(self): """Return Volumes for every mesh cell @@ -1003,8 +1053,6 @@ class SphericalMesh(MeshBase): return np.multiply.outer(np.outer(V_r, V_t), V_p) - - class UnstructuredMesh(MeshBase): """A 3D unstructured mesh @@ -1017,6 +1065,8 @@ class UnstructuredMesh(MeshBase): ---------- filename : str Location of the unstructured mesh file + library : {'moab', 'libmesh'} + Mesh library used for the unstructured mesh tally mesh_id : int Unique identifier for the mesh name : str @@ -1034,7 +1084,7 @@ class UnstructuredMesh(MeshBase): Name of the file containing the unstructured mesh length_multiplier: float Multiplicative factor to apply to mesh coordinates - library : str + library : {'moab', 'libmesh'} Mesh library used for the unstructured mesh tally output : bool Indicates whether or not automatic tally output should @@ -1291,4 +1341,4 @@ class UnstructuredMesh(MeshBase): library = get_text(elem, 'library') length_multiplier = float(get_text(elem, 'length_multiplier', 1.0)) - return cls(filename, library, mesh_id, '', length_multiplier) \ No newline at end of file + return cls(filename, library, mesh_id, '', length_multiplier) diff --git a/openmc/stats/multivariate.py b/openmc/stats/multivariate.py index f33427c34d..8635bcd71c 100644 --- a/openmc/stats/multivariate.py +++ b/openmc/stats/multivariate.py @@ -492,6 +492,7 @@ class SphericalIndependent(Spatial): origin = [float(x) for x in elem.get('origin').split()] return cls(r, theta, phi, origin=origin) + class CylindricalIndependent(Spatial): r"""Spatial distribution represented in cylindrical coordinates. diff --git a/openmc/weight_windows.py b/openmc/weight_windows.py index ee8a4a2bf8..cb9fc51b81 100644 --- a/openmc/weight_windows.py +++ b/openmc/weight_windows.py @@ -270,7 +270,7 @@ class WeightWindows(IDManagerMixin): path = f"./mesh[@id='{mesh_id}']" mesh_elem = root.find(path) if mesh_elem is not None: - mesh = MeshBase.from_xml(mesh_elem) + mesh = MeshBase.from_xml_element(mesh_elem) # Read all other parameters lower_ww_bounds = [float(l) for l in get_text(elem, 'lower_ww_bounds').split()]