From 90126846f44d4d90aee0eb487af4f27e497a5174 Mon Sep 17 00:00:00 2001 From: Ethan Peterson Date: Wed, 16 Mar 2022 13:30:54 -0400 Subject: [PATCH 1/8] adding centroids and meshgrid methods --- openmc/mesh.py | 160 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/openmc/mesh.py b/openmc/mesh.py index 584ea81ce8..76a527357d 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -244,6 +244,70 @@ class RegularMesh(MeshBase): nx, = self.dimension return ((x,) for x in range(1, nx + 1)) + @property + def meshgrid(self): + """Return coordinates of mesh vertices + + Returns + ------- + coordinates : numpy.ndarray or tuple of numpy.ndarray + Returns a numpy.ndarray for each dimension representing the mesh + vertex coordinates. Each array has an identical shape equal to + (nx + 1, ny + 1, nz + 1) where nx, ny, nz = dimension. + + """ + ndim = len(self._dimension) + if ndim == 3: + x0, y0, z0 = self.lower_left + x1, y1, z1 = self.upper_right + nx, ny, nz = self.dimension + xarr = np.linspace(x0, x1, nx + 1) + yarr = np.linspace(y0, y1, ny + 1) + zarr = np.linspace(z0, z1, nz + 1) + return np.meshgrid(xarr, yarr, zarr, indexing='ij') + elif ndim == 2: + x0, y0 = self.lower_left + x1, y1 = self.upper_right + nx, ny = self.dimension + xarr = np.linspace(x0, x1, nx + 1) + yarr = np.linspace(y0, y1, ny + 1) + return np.meshgrid(xarr, yarr, indexing='ij') + else: + nx, = self.dimension + x0, = self.lower_left + x1, = self.upper_right + return np.linspace(x0, x1, nx + 1) + + @property + def centroids(self): + """Return coordinates of mesh element centroids + + Returns + ------- + coordinates : numpy.ndarray or tuple of numpy.ndarray + Returns a numpy.ndarray for each dimension representing the mesh + element centroid coordinates. Each array has an identical shape + equal to (nx, ny, nz) where nx, ny, nz = n_dimension. + + """ + ndim = len(self._dimension) + meshgrid = self.meshgrid + if ndim == 3: + xarr, yarr, zarr = meshgrid + xc = (xarr[:-1, :-1, :-1] + xarr[1:, 1:, 1:]) / 2 + yc = (yarr[:-1, :-1, :-1] + yarr[1:, 1:, 1:]) / 2 + zc = (zarr[:-1, :-1, :-1] + zarr[1:, 1:, 1:]) / 2 + return xc, yc, zc + elif ndim == 2: + xarr, yarr = meshgrid + xc = (xarr[:-1, :-1] + xarr[1:, 1:]) / 2 + yc = (yarr[:-1, :-1] + yarr[1:, 1:]) / 2 + return xc, yc + else: + xarr = meshgrid + xc = (xarr[:-1] + xarr[1:]) / 2 + return xc + @dimension.setter def dimension(self, dimension): cv.check_type('mesh dimension', dimension, Iterable, Integral) @@ -629,6 +693,38 @@ class RectilinearMesh(MeshBase): for y in range(1, ny + 1) for x in range(1, nx + 1)) + @property + def meshgrid(self): + """Return coordinates of mesh vertices + + Returns + ------- + coordinates : 3-tuple of numpy.ndarray + Returns a numpy.ndarray for each dimension representing the mesh + vertex coordinates. Each array has an identical shape equal to + (nx + 1, ny + 1, nz + 1) where nx, ny, nz = dimension. + + """ + return np.meshgrid(self.x_grid, self.y_grid, self.z_grid, indexing='ij') + + @property + def centroids(self): + """Return coordinates of mesh element centroids + + Returns + ------- + coordinates : 3-tuple of numpy.ndarray + Returns a numpy.ndarray for each dimension representing the mesh + element centroid coordinates. Each array has an identical shape + equal to (nx, ny, nz) where nx, ny, nz = n_dimension. + + """ + xx, yy, zz = self.meshgrid + xc = (xx[:-1, :-1, :-1] + xx[1:, 1:, 1:]) / 2 + yc = (yy[:-1, :-1, :-1] + yy[1:, 1:, 1:]) / 2 + zc = (zz[:-1, :-1, :-1] + zz[1:, 1:, 1:]) / 2 + return xc, yc, zc + @x_grid.setter def x_grid(self, grid): cv.check_type('mesh x_grid', grid, Iterable, Real) @@ -799,6 +895,38 @@ class CylindricalMesh(MeshBase): for p in range(1, np + 1) for r in range(1, nr + 1)) + @property + def meshgrid(self): + """Return coordinates of mesh vertices + + Returns + ------- + coordinates : 3-tuple of numpy.ndarray + Returns a numpy.ndarray for each dimension representing the mesh + vertex coordinates. Each array has an identical shape equal to + (nr + 1, nphi + 1, nz + 1) where nr, nphi, nz = dimension. + + """ + return np.meshgrid(self.r_grid, self.phi_grid, self.z_grid, indexing='ij') + + @property + def centroids(self): + """Return coordinates of mesh element centroids + + Returns + ------- + coordinates : 3-tuple of numpy.ndarray + Returns a numpy.ndarray for each dimension representing the mesh + element centroid coordinates. Each array has an identical shape + equal to (nx, nphi, nz) where nx, nphi, nz = n_dimension. + + """ + rr, pp, zz = self.meshgrid + rc = (rr[:-1, :-1, :-1] + rr[1:, 1:, 1:]) / 2 + pc = (pp[:-1, :-1, :-1] + pp[1:, 1:, 1:]) / 2 + zc = (zz[:-1, :-1, :-1] + zz[1:, 1:, 1:]) / 2 + return rc, pc, zc + @r_grid.setter def r_grid(self, grid): cv.check_type('mesh r_grid', grid, Iterable, Real) @@ -987,6 +1115,38 @@ class SphericalMesh(MeshBase): for t in range(1, nt + 1) for r in range(1, nr + 1)) + @property + def meshgrid(self): + """Return coordinates of mesh vertices + + Returns + ------- + coordinates : 3-tuple of numpy.ndarray + Returns a numpy.ndarray for each dimension representing the mesh + vertex coordinates. Each array has an identical shape equal to + (nr + 1, ntheta + 1, nphi + 1) where nr, ntheta, nphi = dimension. + + """ + return np.meshgrid(self.r_grid, self.phi_grid, self.z_grid, indexing='ij') + + @property + def centroids(self): + """Return coordinates of mesh element centroids + + Returns + ------- + coordinates : 3-tuple of numpy.ndarray + Returns a numpy.ndarray for each dimension representing the mesh + element centroid coordinates. Each array has an identical shape + equal to (nx, ntheta, nphi) where nx, ntheta, nphi = n_dimension. + + """ + rr, tt, pp = self.meshgrid + rc = (rr[:-1, :-1, :-1] + rr[1:, 1:, 1:]) / 2 + tc = (tt[:-1, :-1, :-1] + tt[1:, 1:, 1:]) / 2 + pc = (pp[:-1, :-1, :-1] + pp[1:, 1:, 1:]) / 2 + return rc, tc, pc + @r_grid.setter def r_grid(self, grid): cv.check_type('mesh r_grid', grid, Iterable, Real) From a93751ce7bcc5589a1dfa1c187c50f9ce6451c42 Mon Sep 17 00:00:00 2001 From: Ethan Peterson Date: Thu, 17 Mar 2022 10:39:22 -0400 Subject: [PATCH 2/8] changing meshgrid to vertices --- openmc/mesh.py | 85 ++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 41 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 76a527357d..056dd34acb 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -245,15 +245,16 @@ class RegularMesh(MeshBase): return ((x,) for x in range(1, nx + 1)) @property - def meshgrid(self): + def vertices(self): """Return coordinates of mesh vertices Returns ------- - coordinates : numpy.ndarray or tuple of numpy.ndarray - Returns a numpy.ndarray for each dimension representing the mesh - vertex coordinates. Each array has an identical shape equal to - (nx + 1, ny + 1, nz + 1) where nx, ny, nz = dimension. + vertices : numpy.ndarray + Returns a numpy.ndarray representing the coordinates of mesh + vertices with a shape equal to (nx + 1, ny + 1, nz + 1, 3) + where nx, ny, nz = dimension in 3D. For 2D the shape is + (nx + 1, ny + 1, 2), and for 1D the shape is (nx,). """ ndim = len(self._dimension) @@ -264,14 +265,16 @@ class RegularMesh(MeshBase): xarr = np.linspace(x0, x1, nx + 1) yarr = np.linspace(y0, y1, ny + 1) zarr = np.linspace(z0, z1, nz + 1) - return np.meshgrid(xarr, yarr, zarr, indexing='ij') + xx, yy, zz = np.meshgrid(xarr, yarr, zarr, indexing='ij') + return np.stack((xx, yy, zz), axis=-1) elif ndim == 2: x0, y0 = self.lower_left x1, y1 = self.upper_right nx, ny = self.dimension xarr = np.linspace(x0, x1, nx + 1) yarr = np.linspace(y0, y1, ny + 1) - return np.meshgrid(xarr, yarr, indexing='ij') + xx, yy = np.meshgrid(xarr, yarr, indexing='ij') + return np.stack((xx, yy), axis=-1) else: nx, = self.dimension x0, = self.lower_left @@ -291,22 +294,13 @@ class RegularMesh(MeshBase): """ ndim = len(self._dimension) - meshgrid = self.meshgrid + vertices = self.vertices if ndim == 3: - xarr, yarr, zarr = meshgrid - xc = (xarr[:-1, :-1, :-1] + xarr[1:, 1:, 1:]) / 2 - yc = (yarr[:-1, :-1, :-1] + yarr[1:, 1:, 1:]) / 2 - zc = (zarr[:-1, :-1, :-1] + zarr[1:, 1:, 1:]) / 2 - return xc, yc, zc + return (vertices[:-1, :-1, :-1, :] + vertices[1:, 1:, 1:, :]) / 2 elif ndim == 2: - xarr, yarr = meshgrid - xc = (xarr[:-1, :-1] + xarr[1:, 1:]) / 2 - yc = (yarr[:-1, :-1] + yarr[1:, 1:]) / 2 - return xc, yc + return (vertices[:-1, :-1, :] + vertices[1:, 1:, :]) / 2 else: - xarr = meshgrid - xc = (xarr[:-1] + xarr[1:]) / 2 - return xc + return (vertices[:-1] + vertices[1:]) / 2 @dimension.setter def dimension(self, dimension): @@ -694,18 +688,19 @@ class RectilinearMesh(MeshBase): for x in range(1, nx + 1)) @property - def meshgrid(self): + def vertices(self): """Return coordinates of mesh vertices Returns ------- - coordinates : 3-tuple of numpy.ndarray - Returns a numpy.ndarray for each dimension representing the mesh - vertex coordinates. Each array has an identical shape equal to - (nx + 1, ny + 1, nz + 1) where nx, ny, nz = dimension. + vertices : numpy.ndarray + Returns a numpy.ndarray representing the coordinates of mesh + vertices with a shape equal to (nx + 1, ny + 1, nz + 1, 3) + where nx, ny, nz = dimension. """ - return np.meshgrid(self.x_grid, self.y_grid, self.z_grid, indexing='ij') + xx, yy, zz = np.meshgrid(self.x_grid, self.y_grid, self.z_grid, indexing='ij') + return np.stack((xx, yy, zz), axis=-1) @property def centroids(self): @@ -719,7 +714,7 @@ class RectilinearMesh(MeshBase): equal to (nx, ny, nz) where nx, ny, nz = n_dimension. """ - xx, yy, zz = self.meshgrid + xx, yy, zz = self.vertices xc = (xx[:-1, :-1, :-1] + xx[1:, 1:, 1:]) / 2 yc = (yy[:-1, :-1, :-1] + yy[1:, 1:, 1:]) / 2 zc = (zz[:-1, :-1, :-1] + zz[1:, 1:, 1:]) / 2 @@ -885,6 +880,10 @@ class CylindricalMesh(MeshBase): def z_grid(self): return self._z_grid + @property + def grids(self): + return (self.r_grid, self.phi_grid, self.z_grid) + @property def indices(self): nr, np, nz = self.dimension @@ -896,18 +895,18 @@ class CylindricalMesh(MeshBase): for r in range(1, nr + 1)) @property - def meshgrid(self): + def vertices(self): """Return coordinates of mesh vertices Returns ------- - coordinates : 3-tuple of numpy.ndarray - Returns a numpy.ndarray for each dimension representing the mesh - vertex coordinates. Each array has an identical shape equal to - (nr + 1, nphi + 1, nz + 1) where nr, nphi, nz = dimension. + vertices : numpy.ndarray + Returns a numpy.ndarray representing the coordinates of mesh + vertices with a shape equal to (nr + 1, nphi + 1, nz + 1, 3) + where nr, nphi, nz = dimension. """ - return np.meshgrid(self.r_grid, self.phi_grid, self.z_grid, indexing='ij') + return np.stack(np.meshgrid(*self.grids, indexing='ij'), axis=-1) @property def centroids(self): @@ -921,7 +920,7 @@ class CylindricalMesh(MeshBase): equal to (nx, nphi, nz) where nx, nphi, nz = n_dimension. """ - rr, pp, zz = self.meshgrid + rr, pp, zz = self.vertices rc = (rr[:-1, :-1, :-1] + rr[1:, 1:, 1:]) / 2 pc = (pp[:-1, :-1, :-1] + pp[1:, 1:, 1:]) / 2 zc = (zz[:-1, :-1, :-1] + zz[1:, 1:, 1:]) / 2 @@ -1116,18 +1115,22 @@ class SphericalMesh(MeshBase): for r in range(1, nr + 1)) @property - def meshgrid(self): + def vertices(self): """Return coordinates of mesh vertices Returns ------- - coordinates : 3-tuple of numpy.ndarray - Returns a numpy.ndarray for each dimension representing the mesh - vertex coordinates. Each array has an identical shape equal to - (nr + 1, ntheta + 1, nphi + 1) where nr, ntheta, nphi = dimension. + vertices : numpy.ndarray + Returns a numpy.ndarray representing the coordinates of mesh + vertices with a shape equal to (nr + 1, ntheta + 1, nphi + 1, 3) + where nr, ntheta, nphi = dimension. """ - return np.meshgrid(self.r_grid, self.phi_grid, self.z_grid, indexing='ij') + rr, tt, pp = np.meshgrid(self.r_grid, + self.theta_grid, + self.phi_grid, + indexing='ij') + return np.stack((rr, tt, pp), axis=-1) @property def centroids(self): @@ -1141,7 +1144,7 @@ class SphericalMesh(MeshBase): equal to (nx, ntheta, nphi) where nx, ntheta, nphi = n_dimension. """ - rr, tt, pp = self.meshgrid + rr, tt, pp = self.vertices rc = (rr[:-1, :-1, :-1] + rr[1:, 1:, 1:]) / 2 tc = (tt[:-1, :-1, :-1] + tt[1:, 1:, 1:]) / 2 pc = (pp[:-1, :-1, :-1] + pp[1:, 1:, 1:]) / 2 From 6167ae84dc57e3263cca79233a7b886ffae83849 Mon Sep 17 00:00:00 2001 From: Ethan Peterson Date: Thu, 17 Mar 2022 13:55:29 -0400 Subject: [PATCH 3/8] trying to define a clearer interface --- openmc/mesh.py | 198 +++++++++++++++---------------------------------- 1 file changed, 58 insertions(+), 140 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 056dd34acb..d81afb7253 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -45,6 +45,51 @@ class MeshBase(IDManagerMixin, ABC): def name(self): return self._name + @property + @abstractmethod + def dimension(self): + pass + + @property + @abstractmethod + def n_dimension(self): + pass + + @property + @abstractmethod + def _grids(self): + pass + + @property + def vertices(self): + """Return coordinates of mesh vertices. + + Returns + ------- + vertices : numpy.ndarray + Returns a numpy.ndarray representing the coordinates of the mesh + vertices with a shape equal to (dim1 + 1, ..., dimn + 1, ndim). + + """ + return np.stack(np.meshgrid(*self._grids, indexing='ij'), axis=-1) + + @property + def centroids(self): + """Return coordinates of mesh element centroids. + + Returns + ------- + centroids : numpy.ndarray + Returns a numpy.ndarray representing the mesh element centroid + coordinates with a shape equal to (dim1, ..., dimn, ndim). + + """ + ndim = len(self.dimension) + vertices = self.vertices + s0 = (slice(0, -1),)*ndim + (slice(None),) + s1 = (slice(1, None),)*ndim + (slice(None),) + return (vertices[s0] + vertices[s1]) / 2 + @name.setter def name(self, name): if name is not None: @@ -245,18 +290,7 @@ class RegularMesh(MeshBase): return ((x,) for x in range(1, nx + 1)) @property - def vertices(self): - """Return coordinates of mesh vertices - - Returns - ------- - vertices : numpy.ndarray - Returns a numpy.ndarray representing the coordinates of mesh - vertices with a shape equal to (nx + 1, ny + 1, nz + 1, 3) - where nx, ny, nz = dimension in 3D. For 2D the shape is - (nx + 1, ny + 1, 2), and for 1D the shape is (nx,). - - """ + def _grids(self): ndim = len(self._dimension) if ndim == 3: x0, y0, z0 = self.lower_left @@ -265,42 +299,19 @@ class RegularMesh(MeshBase): xarr = np.linspace(x0, x1, nx + 1) yarr = np.linspace(y0, y1, ny + 1) zarr = np.linspace(z0, z1, nz + 1) - xx, yy, zz = np.meshgrid(xarr, yarr, zarr, indexing='ij') - return np.stack((xx, yy, zz), axis=-1) + return (xarr, yarr, zarr) elif ndim == 2: x0, y0 = self.lower_left x1, y1 = self.upper_right nx, ny = self.dimension xarr = np.linspace(x0, x1, nx + 1) yarr = np.linspace(y0, y1, ny + 1) - xx, yy = np.meshgrid(xarr, yarr, indexing='ij') - return np.stack((xx, yy), axis=-1) + return (xarr, yarr) else: nx, = self.dimension x0, = self.lower_left x1, = self.upper_right - return np.linspace(x0, x1, nx + 1) - - @property - def centroids(self): - """Return coordinates of mesh element centroids - - Returns - ------- - coordinates : numpy.ndarray or tuple of numpy.ndarray - Returns a numpy.ndarray for each dimension representing the mesh - element centroid coordinates. Each array has an identical shape - equal to (nx, ny, nz) where nx, ny, nz = n_dimension. - - """ - ndim = len(self._dimension) - vertices = self.vertices - if ndim == 3: - return (vertices[:-1, :-1, :-1, :] + vertices[1:, 1:, 1:, :]) / 2 - elif ndim == 2: - return (vertices[:-1, :-1, :] + vertices[1:, 1:, :]) / 2 - else: - return (vertices[:-1] + vertices[1:]) / 2 + return (np.linspace(x0, x1, nx + 1),) @dimension.setter def dimension(self, dimension): @@ -656,6 +667,10 @@ class RectilinearMesh(MeshBase): def z_grid(self): return self._z_grid + @property + def _grids(self): + return (self.x_grid, self.y_grid, self.z_grid) + @property def volumes(self): """Return Volumes for every mesh cell @@ -687,39 +702,6 @@ class RectilinearMesh(MeshBase): for y in range(1, ny + 1) for x in range(1, nx + 1)) - @property - def vertices(self): - """Return coordinates of mesh vertices - - Returns - ------- - vertices : numpy.ndarray - Returns a numpy.ndarray representing the coordinates of mesh - vertices with a shape equal to (nx + 1, ny + 1, nz + 1, 3) - where nx, ny, nz = dimension. - - """ - xx, yy, zz = np.meshgrid(self.x_grid, self.y_grid, self.z_grid, indexing='ij') - return np.stack((xx, yy, zz), axis=-1) - - @property - def centroids(self): - """Return coordinates of mesh element centroids - - Returns - ------- - coordinates : 3-tuple of numpy.ndarray - Returns a numpy.ndarray for each dimension representing the mesh - element centroid coordinates. Each array has an identical shape - equal to (nx, ny, nz) where nx, ny, nz = n_dimension. - - """ - xx, yy, zz = self.vertices - xc = (xx[:-1, :-1, :-1] + xx[1:, 1:, 1:]) / 2 - yc = (yy[:-1, :-1, :-1] + yy[1:, 1:, 1:]) / 2 - zc = (zz[:-1, :-1, :-1] + zz[1:, 1:, 1:]) / 2 - return xc, yc, zc - @x_grid.setter def x_grid(self, grid): cv.check_type('mesh x_grid', grid, Iterable, Real) @@ -881,7 +863,7 @@ class CylindricalMesh(MeshBase): return self._z_grid @property - def grids(self): + def _grids(self): return (self.r_grid, self.phi_grid, self.z_grid) @property @@ -894,38 +876,6 @@ class CylindricalMesh(MeshBase): for p in range(1, np + 1) for r in range(1, nr + 1)) - @property - def vertices(self): - """Return coordinates of mesh vertices - - Returns - ------- - vertices : numpy.ndarray - Returns a numpy.ndarray representing the coordinates of mesh - vertices with a shape equal to (nr + 1, nphi + 1, nz + 1, 3) - where nr, nphi, nz = dimension. - - """ - return np.stack(np.meshgrid(*self.grids, indexing='ij'), axis=-1) - - @property - def centroids(self): - """Return coordinates of mesh element centroids - - Returns - ------- - coordinates : 3-tuple of numpy.ndarray - Returns a numpy.ndarray for each dimension representing the mesh - element centroid coordinates. Each array has an identical shape - equal to (nx, nphi, nz) where nx, nphi, nz = n_dimension. - - """ - rr, pp, zz = self.vertices - rc = (rr[:-1, :-1, :-1] + rr[1:, 1:, 1:]) / 2 - pc = (pp[:-1, :-1, :-1] + pp[1:, 1:, 1:]) / 2 - zc = (zz[:-1, :-1, :-1] + zz[1:, 1:, 1:]) / 2 - return rc, pc, zc - @r_grid.setter def r_grid(self, grid): cv.check_type('mesh r_grid', grid, Iterable, Real) @@ -1104,6 +1054,10 @@ class SphericalMesh(MeshBase): def phi_grid(self): return self._phi_grid + @property + def _grids(self): + return (self.r_grid, self.theta_grid, self.phi_grid) + @property def indices(self): nr, nt, np = self.dimension @@ -1114,42 +1068,6 @@ class SphericalMesh(MeshBase): for t in range(1, nt + 1) for r in range(1, nr + 1)) - @property - def vertices(self): - """Return coordinates of mesh vertices - - Returns - ------- - vertices : numpy.ndarray - Returns a numpy.ndarray representing the coordinates of mesh - vertices with a shape equal to (nr + 1, ntheta + 1, nphi + 1, 3) - where nr, ntheta, nphi = dimension. - - """ - rr, tt, pp = np.meshgrid(self.r_grid, - self.theta_grid, - self.phi_grid, - indexing='ij') - return np.stack((rr, tt, pp), axis=-1) - - @property - def centroids(self): - """Return coordinates of mesh element centroids - - Returns - ------- - coordinates : 3-tuple of numpy.ndarray - Returns a numpy.ndarray for each dimension representing the mesh - element centroid coordinates. Each array has an identical shape - equal to (nx, ntheta, nphi) where nx, ntheta, nphi = n_dimension. - - """ - rr, tt, pp = self.vertices - rc = (rr[:-1, :-1, :-1] + rr[1:, 1:, 1:]) / 2 - tc = (tt[:-1, :-1, :-1] + tt[1:, 1:, 1:]) / 2 - pc = (pp[:-1, :-1, :-1] + pp[1:, 1:, 1:]) / 2 - return rc, tc, pc - @r_grid.setter def r_grid(self, grid): cv.check_type('mesh r_grid', grid, Iterable, Real) From b5ed2b985f5142f985a04c152d707bb13d3f3f7b Mon Sep 17 00:00:00 2001 From: Ethan Peterson Date: Wed, 4 May 2022 13:47:40 -0400 Subject: [PATCH 4/8] cleaning up a few syntax things --- openmc/mesh.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index d81afb7253..c6fa7bb994 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -1,4 +1,4 @@ -from abc import ABC +from abc import ABC, abstractmethod from collections.abc import Iterable from math import pi from numbers import Real, Integral @@ -61,6 +61,7 @@ class MeshBase(IDManagerMixin, ABC): pass @property + @abstractmethod def vertices(self): """Return coordinates of mesh vertices. @@ -74,6 +75,7 @@ class MeshBase(IDManagerMixin, ABC): return np.stack(np.meshgrid(*self._grids, indexing='ij'), axis=-1) @property + @abstractmethod def centroids(self): """Return coordinates of mesh element centroids. @@ -84,7 +86,7 @@ class MeshBase(IDManagerMixin, ABC): coordinates with a shape equal to (dim1, ..., dimn, ndim). """ - ndim = len(self.dimension) + ndim = self.n_dimension vertices = self.vertices s0 = (slice(0, -1),)*ndim + (slice(None),) s1 = (slice(1, None),)*ndim + (slice(None),) @@ -105,7 +107,7 @@ class MeshBase(IDManagerMixin, ABC): return string def _volume_dim_check(self): - if len(self.dimension) != 3 or \ + if self.n_dimension != 3 or \ any([d == 0 for d in self.dimension]): raise RuntimeError(f'Mesh {self.id} is not 3D. ' 'Volumes cannot be provided.') @@ -509,7 +511,7 @@ class RegularMesh(MeshBase): for entry in bc: cv.check_value('bc', entry, _BOUNDARY_TYPES) - n_dim = len(self.dimension) + n_dim = self.n_dimension # Build the cell which will contain the lattice xplanes = [openmc.XPlane(self.lower_left[0], boundary_type=bc[0]), @@ -1227,7 +1229,7 @@ class UnstructuredMesh(MeshBase): (1.0, 1.0, 1.0), ...] """ def __init__(self, filename, library, mesh_id=None, name='', - length_multiplier=1.0): + length_multiplier=1.0): super().__init__(mesh_id, name) self.filename = filename self._volumes = None @@ -1322,6 +1324,19 @@ class UnstructuredMesh(MeshBase): Real) self._length_multiplier = length_multiplier + @property + def dimension(self): + return self.n_elements + + @property + def n_dimension(self): + return 3 + + @property + def vertices(self): + raise NotImplementedError("Vertices for UnstructuredMesh objects are " + "not yet available") + def __repr__(self): string = super().__repr__() string += '{: <16}=\t{}\n'.format('\tFilename', self.filename) @@ -1452,7 +1467,7 @@ class UnstructuredMesh(MeshBase): subelement.text = self.filename if self._length_multiplier != 1.0: - element.set("length_multiplier", str(self.length_multiplier)) + element.set("length_multiplier", str(self.length_multiplier)) return element From 606d9bf03eb967064e0c0235b486f4cca479c896 Mon Sep 17 00:00:00 2001 From: Ethan Peterson Date: Wed, 4 May 2022 14:04:15 -0400 Subject: [PATCH 5/8] fixed abstractmethod error --- openmc/mesh.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index c6fa7bb994..491419778f 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -61,7 +61,6 @@ class MeshBase(IDManagerMixin, ABC): pass @property - @abstractmethod def vertices(self): """Return coordinates of mesh vertices. @@ -75,7 +74,6 @@ class MeshBase(IDManagerMixin, ABC): return np.stack(np.meshgrid(*self._grids, indexing='ij'), axis=-1) @property - @abstractmethod def centroids(self): """Return coordinates of mesh element centroids. From 45cb513b15cc1ec3c4fb80104dfaca3853b53ac0 Mon Sep 17 00:00:00 2001 From: Ethan Peterson Date: Tue, 24 May 2022 16:08:42 -0400 Subject: [PATCH 6/8] adding ABC for structured mesh --- openmc/mesh.py | 116 +++++++++++++++++++++++++++++-------------------- 1 file changed, 70 insertions(+), 46 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 491419778f..3ae5ee43d7 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -45,51 +45,6 @@ class MeshBase(IDManagerMixin, ABC): def name(self): return self._name - @property - @abstractmethod - def dimension(self): - pass - - @property - @abstractmethod - def n_dimension(self): - pass - - @property - @abstractmethod - def _grids(self): - pass - - @property - def vertices(self): - """Return coordinates of mesh vertices. - - Returns - ------- - vertices : numpy.ndarray - Returns a numpy.ndarray representing the coordinates of the mesh - vertices with a shape equal to (dim1 + 1, ..., dimn + 1, ndim). - - """ - return np.stack(np.meshgrid(*self._grids, indexing='ij'), axis=-1) - - @property - def centroids(self): - """Return coordinates of mesh element centroids. - - Returns - ------- - centroids : numpy.ndarray - Returns a numpy.ndarray representing the mesh element centroid - coordinates with a shape equal to (dim1, ..., dimn, ndim). - - """ - ndim = self.n_dimension - vertices = self.vertices - s0 = (slice(0, -1),)*ndim + (slice(None),) - s1 = (slice(1, None),)*ndim + (slice(None),) - return (vertices[s0] + vertices[s1]) / 2 - @name.setter def name(self, name): if name is not None: @@ -171,7 +126,76 @@ class MeshBase(IDManagerMixin, ABC): raise ValueError(f'Unrecognized mesh type "{mesh_type}" found.') -class RegularMesh(MeshBase): +class StructuredMesh(ABC): + """A mixin for structured mesh functionality + + 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 + + """ + + def __init__(self, **kwargs): + super().__init__(**kwargs) + + @property + @abstractmethod + def dimension(self): + pass + + @property + @abstractmethod + def n_dimension(self): + pass + + @property + @abstractmethod + def _grids(self): + pass + + @property + def vertices(self): + """Return coordinates of mesh vertices. + + Returns + ------- + vertices : numpy.ndarray + Returns a numpy.ndarray representing the coordinates of the mesh + vertices with a shape equal to (dim1 + 1, ..., dimn + 1, ndim). + + """ + return np.stack(np.meshgrid(*self._grids, indexing='ij'), axis=-1) + + @property + def centroids(self): + """Return coordinates of mesh element centroids. + + Returns + ------- + centroids : numpy.ndarray + Returns a numpy.ndarray representing the mesh element centroid + coordinates with a shape equal to (dim1, ..., dimn, ndim). + + """ + ndim = self.n_dimension + vertices = self.vertices + s0 = (slice(0, -1),)*ndim + (slice(None),) + s1 = (slice(1, None),)*ndim + (slice(None),) + return (vertices[s0] + vertices[s1]) / 2 + + + +class RegularMesh(StructuredMesh, MeshBase): """A regular Cartesian mesh in one, two, or three dimensions Parameters From e1d91bb4c624034e7c8c28a7e41a88b16129e0ce Mon Sep 17 00:00:00 2001 From: Ethan Peterson Date: Thu, 26 May 2022 12:59:31 -0400 Subject: [PATCH 7/8] Update openmc/mesh.py Co-authored-by: Patrick Shriwise --- openmc/mesh.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 3ae5ee43d7..d812f27ac7 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -145,8 +145,8 @@ class StructuredMesh(ABC): """ - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) @property @abstractmethod From bf6e26600914320b0a5c2a4eb766d8faa01cbec8 Mon Sep 17 00:00:00 2001 From: Ethan Peterson Date: Tue, 31 May 2022 10:34:19 -0400 Subject: [PATCH 8/8] changed to single inheritance --- openmc/mesh.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index d812f27ac7..7f7b078795 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -126,8 +126,8 @@ class MeshBase(IDManagerMixin, ABC): raise ValueError(f'Unrecognized mesh type "{mesh_type}" found.') -class StructuredMesh(ABC): - """A mixin for structured mesh functionality +class StructuredMesh(MeshBase): + """A base class for structured mesh functionality Parameters ---------- @@ -194,8 +194,7 @@ class StructuredMesh(ABC): return (vertices[s0] + vertices[s1]) / 2 - -class RegularMesh(StructuredMesh, MeshBase): +class RegularMesh(StructuredMesh): """A regular Cartesian mesh in one, two, or three dimensions Parameters @@ -630,7 +629,7 @@ def Mesh(*args, **kwargs): return RegularMesh(*args, **kwargs) -class RectilinearMesh(MeshBase): +class RectilinearMesh(StructuredMesh): """A 3D rectilinear Cartesian mesh Parameters @@ -823,7 +822,7 @@ class RectilinearMesh(MeshBase): return element -class CylindricalMesh(MeshBase): +class CylindricalMesh(StructuredMesh): """A 3D cylindrical mesh Parameters @@ -1014,7 +1013,7 @@ class CylindricalMesh(MeshBase): return np.multiply.outer(np.outer(V_r, V_p), V_z) -class SphericalMesh(MeshBase): +class SphericalMesh(StructuredMesh): """A 3D spherical mesh Parameters