r"""Flow data on an unstructured triangulated surface.
An :class:`UnstructuredBlock` joins a one-dimensional
:class:`~ember.block.Block` of flow data at a set of nodes to the triangular
faces that connect them. It is what :func:`ember.cut.unstructured` returns,
and it is accepted by the integrals and averages in :mod:`ember.average`
alongside a structured 2D cut.
Each node is stored once, however many faces share it. The flow is set and
read through :attr:`~UnstructuredBlock.nodes`, an ordinary
:class:`~ember.block.Block` of shape ``(n_node,)``; face quantities have
shape ``(n_face, ...)``.
The nodes are built as any other block, and then joined by their faces::
nodes = Block((n_node,))
nodes.set_xrt(xrt)
nodes.set_fluid(fluid)
nodes.set_P_T(P, T)
nodes.set_Vxrt(Vxrt)
u = UnstructuredBlock(nodes, i_face) # int, shape (n_face, 3)
.. autosummary::
UnstructuredBlock
"""
import numpy as np
import ember.block
__all__ = ["UnstructuredBlock"]
def _nodes_like(block, n_node):
"""New 1D :class:`~ember.block.Block` carrying the point metadata of `block`.
The fluid, length scale, rotation, blade count and label come across;
the patches of `block` do not. The data are left NaN for the caller to
fill, and are marked set, as :meth:`Block.empty
<ember._struct.StructuredData.empty>` marks them.
"""
out = ember.block.Block((n_node,))
out._metadata.update({k: v for k, v in block._metadata.items() if k != "patches"})
out._versions = block._versions.copy()
return out
[docs]
class UnstructuredBlock:
def __init__(self, nodes, i_face):
r"""Join flow data at a set of nodes into a triangulated surface.
Each face is a triple of node indices :math:`(A, B, C)`. Its area
vector, :attr:`dA`, follows the right-hand rule around
:math:`A \to B \to C` in the pseudo-Cartesian space
:math:`(x, r, r\theta)`, the same sense as the face areas of a
structured cut.
The nodes are held by reference, so the flow on them can be changed
in place through :attr:`nodes`. The faces are copied and stored
read-only; to change them, or the number of nodes, build a new
:class:`UnstructuredBlock`.
A volume mesh would extend this class rather than replace it: each
face would also store the cells either side of it, and the cells,
their volumes and their node lists would be derived from those.
Parameters
----------
nodes : Block, shape (n_node,)
Flow data at the nodes.
i_face : array_like of int, shape (n_face, 3)
Indices into `nodes` of the three nodes of each face, each in
``[0, n_node)``. Their order sets the direction of the face's
area vector by the right-hand rule.
"""
if not isinstance(nodes, ember.block.Block) or nodes.ndim != 1:
raise TypeError("Nodes must be a one-dimensional Block.")
n_node = nodes.shape[0]
i_face = np.asarray(i_face)
if not np.issubdtype(i_face.dtype, np.integer):
raise TypeError(f"Faces must be integer indices, got {i_face.dtype}.")
if i_face.ndim != 2 or i_face.shape[1] != 3:
raise ValueError(f"Faces must have shape (n_face, 3), got {i_face.shape}.")
if i_face.size and (i_face.min() < 0 or i_face.max() >= n_node):
raise ValueError(
f"Face indices must lie in [0, {n_node}), got "
f"[{i_face.min()}, {i_face.max()}]."
)
i_face = np.array(i_face, dtype=np.int32)
i_face.flags.writeable = False
self._nodes = nodes
self._i_face = i_face
def __setstate__(self, state):
"""Unpickle, locking the faces again (pickle drops the read-only flag)."""
self.__dict__.update(state)
self._i_face.flags.writeable = False
def _to_faces(self, nodal):
"""Average node values onto faces, shape ``(n_face, ...)``."""
return np.mean(np.asarray(nodal)[self._i_face], axis=1)
[docs]
@classmethod
def from_structured(cls, block):
"""Triangulate a structured 2D cut.
Every node of `block` becomes a node of the result, and every quad
face is split into two triangles, giving ``n_face = 2 (ni - 1) (nj -
1)`` faces. The metadata of `block` come across, but not its patches.
Parameters
----------
block : Block, shape (ni, nj)
2D structured block to triangulate.
Returns
-------
cut : UnstructuredBlock
Unstructured block with ``n_node = ni * nj`` nodes, node
``(i, j)`` of `block` being node ``i + ni * j``.
"""
if block.ndim != 2:
raise ValueError(f"Only a 2D block can be triangulated, got {block.ndim}D.")
# Every structured quad becomes two triangles:
#
# i,j+1 +----+ i+1, j+1
# |A / |
# | / B|
# i,j +----+ i+1, j
#
ni, nj = block.shape
nvar = block._data.shape[-1]
# Nodes in Fortran order, so node (i, j) is node i + ni * j
nodes = _nodes_like(block, ni * nj)
nodes._data[:] = block._data.reshape(ni * nj, nvar, order="F")
# Corner nodes of every quad, i varying slowest
i, j = np.meshgrid(np.arange(ni - 1), np.arange(nj - 1), indexing="ij")
v00 = (i + ni * j).ravel()
v01 = v00 + ni
v11 = v00 + ni + 1
v10 = v00 + 1
# Triangle A is (i,j), (i,j+1), (i+1,j+1); B is (i,j), (i+1,j+1),
# (i+1,j). Interleave A and B quad by quad.
i_face = np.empty((2 * v00.size, 3), dtype=np.int32)
i_face[0::2] = np.column_stack([v00, v01, v11])
i_face[1::2] = np.column_stack([v00, v11, v10])
return cls(nodes, i_face)
[docs]
def copy(self):
"""Create a copy with its own nodes.
The faces are shared, being read-only.
Returns
-------
out : UnstructuredBlock
A new block whose node data can be changed independently.
"""
return UnstructuredBlock(self._nodes.copy(), self._i_face)
@property
def dA(self):
r"""Face area vectors :math:`\delta\!A` [m^2], shape ``(n_face, 3)``.
See :attr:`dA_nd` for the nondimensional form and the geometry
reference.
"""
return self.dA_nd * self._nodes.L_ref**2
@property
def dA_nd(self):
r"""Face area vectors :math:`\delta\!A / L_\mathrm{ref}^2` [-], shape ``(n_face, 3)``.
Pseudo-Cartesian components :math:`(x, r, r\theta)` on the trailing
axis, directed by the right-hand rule around each face's nodes.
See :ref:`face-areas` for the calculation.
"""
return ember.block._get_da_tri(self._nodes._xrt_nd[self._i_face])
@property
def i_face(self):
"""Node indices of each face, read-only int32, shape ``(n_face, 3)``."""
return self._i_face
@property
def n_face(self):
"""Number of faces."""
return self._i_face.shape[0]
@property
def n_node(self):
"""Number of nodes."""
return self._nodes.shape[0]
@property
def nodes(self):
"""Flow data at the nodes, a :class:`~ember.block.Block` of shape ``(n_node,)``."""
return self._nodes