From 4f5b8e8034c7ff85f07503b985d0a0ba147c4b34 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Thu, 28 Nov 2019 07:56:51 -0600 Subject: [PATCH] Adding unstructured mesh class. --- openmc/filter.py | 5 +++- openmc/mesh.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/openmc/filter.py b/openmc/filter.py index 1988aee81e..0b4929a8be 100644 --- a/openmc/filter.py +++ b/openmc/filter.py @@ -749,7 +749,10 @@ class MeshFilter(Filter): def mesh(self, mesh): cv.check_type('filter mesh', mesh, openmc.MeshBase) self._mesh = mesh - self.bins = list(mesh.indices) + if isinstance(mesh, openmc.UnstructuredMesh): + self.bins = [] + else: + self.bins = list(mesh.indices) def can_merge(self, other): # Mesh filters cannot have more than one bin diff --git a/openmc/mesh.py b/openmc/mesh.py index 6ef8776fe8..ef46695f5f 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -80,6 +80,8 @@ class MeshBase(IDManagerMixin, metaclass=ABCMeta): return RegularMesh.from_hdf5(group) elif mesh_type == 'rectilinear': return RectilinearMesh.from_hdf5(group) + elif mesh_type == 'unstructured': + return UnstructuredMesh.from_hdf5(group) else: raise ValueError('Unrecognized mesh type: "' + mesh_type + '"') @@ -586,3 +588,71 @@ class RectilinearMesh(MeshBase): subelement.text = ' '.join(map(str, self.z_grid)) return element + +class UnstructuredMesh(MeshBase): + """An unstructured mesh, assumed to be three dimensionality + + Parameters + ---------- + mesh_id : int + Unique identifier for the mesh + name : str + Name of the mesh + + Attributes + ---------- + id : int + Unique identifier for the mesh + name : str + Name of the mesh + filename : str + Name of the file containing the unstructured mesh + """ + + def __init__(self, mesh_id=None, name='', filename=''): + super().__init__(mesh_id, name) + self._filename = filename + + @property + def filename(self): + return self._filename + + @filename.setter + def filename(self, filename): + if filename is not None: + cv.check_type('Unstructured Mesh filename: {}'.format(filename), + filename, str) + self._filename = filename + else: + self.filename = '' + + def __repr__(self): + string = super().__repr__() + string += '{0: <16}{1}{2}\n'.format('\tFilename', '=\t', self.filename) + return string + + @classmethod + def from_hdf5(cls, group): + mesh_id = int(group.name.split('/')[-1].lstrip('mesh ')) + + mesh = cls(mesh_id) + mesh = group['filename'] + + def to_xml_element(self): + """Return XML representation of the mesh + + Returns + ------- + element : xml.etree.ElementTree.Element + XML element containing the mesh data + + """ + + element = ET.Element("mesh") + element.set("id", str(self._id)) + element.set("type", "unstructured") + + subelement = ET.SubElement(element, "mesh_file") + subelement.text = self.filename + + return element