Source code for ember.cut

r"""Cuts through a grid along a curve in the meridional plane.

The cut is the surface of revolution swept by an :math:`(x, r)` curve, carried
on straight past both ends. Three functions take it, each from a grid or a
single block:

* :func:`structured_traverse` gives a blade-free traverse plane as one
  structured 2D :class:`~ember.block.Block`, hub to casing by once round the
  pitch, ready for the averages in :mod:`ember.average`.
* :func:`structured_multiblock` gives any cut, through blades and tip gaps
  too, as a list of structured 2D blocks, split at each solid and between
  blade rows.
* :func:`unstructured` gives any cut as triangles from marching cubes, a list
  of :class:`~ember.unstructured.UnstructuredBlock`, one per blade row it
  crosses. It makes no demands of the grid's topology, but triangles are
  harder to plot and to work along than a structured sheet.

The structured cuts are built from the grid's own lines, every node
interpolated along one grid edge where the grid lines cross the cut cleanly,
and need the grid's periodic pairs to coincide, as
:meth:`ember.grid.Grid.snap_periodic` makes them. :func:`structured_multiblock`
and :func:`unstructured` cut a grid of several blade rows, separated by mixing
planes, a row at a time from inlet to outlet, so each piece carries one row's
blade count and rotation.
:meth:`UnstructuredBlock.from_structured
<ember.unstructured.UnstructuredBlock.from_structured>` turns a structured 2D
cut into triangles.

Cuts along grid surfaces by index
---------------------------------

The functions here extract cuts along an arbitrary meridional curve. To take a
cut along a grid surface of constant index, none of them are needed: a
:class:`~ember.block.Block` supports numpy-style indexing, so ``block[i]`` or
``block[:, j]`` returns a lower-dimensional structured cut that shares the
parent's data as a zero-copy view. See the indexing section of
:mod:`ember.block` for the full rules.
"""

import numpy as np

import ember._traverse
import ember.fortran
import ember.grid
from ember.block import Block
from ember.unstructured import UnstructuredBlock, _nodes_like

_TRITABLE = np.array(
    [
        [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [1, 8, 3, 9, 8, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [0, 8, 3, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [9, 2, 10, 0, 2, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [2, 8, 3, 2, 10, 8, 10, 9, 8, -1, -1, -1, -1, -1, -1, -1],
        [3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [0, 11, 2, 8, 11, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [1, 9, 0, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [1, 11, 2, 1, 9, 11, 9, 8, 11, -1, -1, -1, -1, -1, -1, -1],
        [3, 10, 1, 11, 10, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [0, 10, 1, 0, 8, 10, 8, 11, 10, -1, -1, -1, -1, -1, -1, -1],
        [3, 9, 0, 3, 11, 9, 11, 10, 9, -1, -1, -1, -1, -1, -1, -1],
        [9, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [4, 3, 0, 7, 3, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [0, 1, 9, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [4, 1, 9, 4, 7, 1, 7, 3, 1, -1, -1, -1, -1, -1, -1, -1],
        [1, 2, 10, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [3, 4, 7, 3, 0, 4, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1],
        [9, 2, 10, 9, 0, 2, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1],
        [2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4, -1, -1, -1, -1],
        [8, 4, 7, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [11, 4, 7, 11, 2, 4, 2, 0, 4, -1, -1, -1, -1, -1, -1, -1],
        [9, 0, 1, 8, 4, 7, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1],
        [4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1, -1, -1, -1, -1],
        [3, 10, 1, 3, 11, 10, 7, 8, 4, -1, -1, -1, -1, -1, -1, -1],
        [1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4, -1, -1, -1, -1],
        [4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3, -1, -1, -1, -1],
        [4, 7, 11, 4, 11, 9, 9, 11, 10, -1, -1, -1, -1, -1, -1, -1],
        [9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [9, 5, 4, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [0, 5, 4, 1, 5, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [8, 5, 4, 8, 3, 5, 3, 1, 5, -1, -1, -1, -1, -1, -1, -1],
        [1, 2, 10, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [3, 0, 8, 1, 2, 10, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1],
        [5, 2, 10, 5, 4, 2, 4, 0, 2, -1, -1, -1, -1, -1, -1, -1],
        [2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8, -1, -1, -1, -1],
        [9, 5, 4, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [0, 11, 2, 0, 8, 11, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1],
        [0, 5, 4, 0, 1, 5, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1],
        [2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5, -1, -1, -1, -1],
        [10, 3, 11, 10, 1, 3, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1],
        [4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10, -1, -1, -1, -1],
        [5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3, -1, -1, -1, -1],
        [5, 4, 8, 5, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1],
        [9, 7, 8, 5, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [9, 3, 0, 9, 5, 3, 5, 7, 3, -1, -1, -1, -1, -1, -1, -1],
        [0, 7, 8, 0, 1, 7, 1, 5, 7, -1, -1, -1, -1, -1, -1, -1],
        [1, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [9, 7, 8, 9, 5, 7, 10, 1, 2, -1, -1, -1, -1, -1, -1, -1],
        [10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3, -1, -1, -1, -1],
        [8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2, -1, -1, -1, -1],
        [2, 10, 5, 2, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1],
        [7, 9, 5, 7, 8, 9, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1],
        [9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11, -1, -1, -1, -1],
        [2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7, -1, -1, -1, -1],
        [11, 2, 1, 11, 1, 7, 7, 1, 5, -1, -1, -1, -1, -1, -1, -1],
        [9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11, -1, -1, -1, -1],
        [5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0, -1],
        [11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0, -1],
        [11, 10, 5, 7, 11, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [0, 8, 3, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [9, 0, 1, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [1, 8, 3, 1, 9, 8, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1],
        [1, 6, 5, 2, 6, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [1, 6, 5, 1, 2, 6, 3, 0, 8, -1, -1, -1, -1, -1, -1, -1],
        [9, 6, 5, 9, 0, 6, 0, 2, 6, -1, -1, -1, -1, -1, -1, -1],
        [5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8, -1, -1, -1, -1],
        [2, 3, 11, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [11, 0, 8, 11, 2, 0, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1],
        [0, 1, 9, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1],
        [5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11, -1, -1, -1, -1],
        [6, 3, 11, 6, 5, 3, 5, 1, 3, -1, -1, -1, -1, -1, -1, -1],
        [0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6, -1, -1, -1, -1],
        [3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9, -1, -1, -1, -1],
        [6, 5, 9, 6, 9, 11, 11, 9, 8, -1, -1, -1, -1, -1, -1, -1],
        [5, 10, 6, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [4, 3, 0, 4, 7, 3, 6, 5, 10, -1, -1, -1, -1, -1, -1, -1],
        [1, 9, 0, 5, 10, 6, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1],
        [10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4, -1, -1, -1, -1],
        [6, 1, 2, 6, 5, 1, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1],
        [1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7, -1, -1, -1, -1],
        [8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6, -1, -1, -1, -1],
        [7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9, -1],
        [3, 11, 2, 7, 8, 4, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1],
        [5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11, -1, -1, -1, -1],
        [0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1],
        [9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6, -1],
        [8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6, -1, -1, -1, -1],
        [5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11, -1],
        [0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7, -1],
        [6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9, -1, -1, -1, -1],
        [10, 4, 9, 6, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [4, 10, 6, 4, 9, 10, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1],
        [10, 0, 1, 10, 6, 0, 6, 4, 0, -1, -1, -1, -1, -1, -1, -1],
        [8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10, -1, -1, -1, -1],
        [1, 4, 9, 1, 2, 4, 2, 6, 4, -1, -1, -1, -1, -1, -1, -1],
        [3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4, -1, -1, -1, -1],
        [0, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [8, 3, 2, 8, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1],
        [10, 4, 9, 10, 6, 4, 11, 2, 3, -1, -1, -1, -1, -1, -1, -1],
        [0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6, -1, -1, -1, -1],
        [3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10, -1, -1, -1, -1],
        [6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1, -1],
        [9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3, -1, -1, -1, -1],
        [8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1, -1],
        [3, 11, 6, 3, 6, 0, 0, 6, 4, -1, -1, -1, -1, -1, -1, -1],
        [6, 4, 8, 11, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [7, 10, 6, 7, 8, 10, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1],
        [0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10, -1, -1, -1, -1],
        [10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0, -1, -1, -1, -1],
        [10, 6, 7, 10, 7, 1, 1, 7, 3, -1, -1, -1, -1, -1, -1, -1],
        [1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7, -1, -1, -1, -1],
        [2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9, -1],
        [7, 8, 0, 7, 0, 6, 6, 0, 2, -1, -1, -1, -1, -1, -1, -1],
        [7, 3, 2, 6, 7, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7, -1, -1, -1, -1],
        [2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7, -1],
        [1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11, -1],
        [11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1, -1, -1, -1, -1],
        [8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6, -1],
        [0, 9, 1, 11, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0, -1, -1, -1, -1],
        [7, 11, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [3, 0, 8, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [0, 1, 9, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [8, 1, 9, 8, 3, 1, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1],
        [10, 1, 2, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [1, 2, 10, 3, 0, 8, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1],
        [2, 9, 0, 2, 10, 9, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1],
        [6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8, -1, -1, -1, -1],
        [7, 2, 3, 6, 2, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [7, 0, 8, 7, 6, 0, 6, 2, 0, -1, -1, -1, -1, -1, -1, -1],
        [2, 7, 6, 2, 3, 7, 0, 1, 9, -1, -1, -1, -1, -1, -1, -1],
        [1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6, -1, -1, -1, -1],
        [10, 7, 6, 10, 1, 7, 1, 3, 7, -1, -1, -1, -1, -1, -1, -1],
        [10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8, -1, -1, -1, -1],
        [0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7, -1, -1, -1, -1],
        [7, 6, 10, 7, 10, 8, 8, 10, 9, -1, -1, -1, -1, -1, -1, -1],
        [6, 8, 4, 11, 8, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [3, 6, 11, 3, 0, 6, 0, 4, 6, -1, -1, -1, -1, -1, -1, -1],
        [8, 6, 11, 8, 4, 6, 9, 0, 1, -1, -1, -1, -1, -1, -1, -1],
        [9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6, -1, -1, -1, -1],
        [6, 8, 4, 6, 11, 8, 2, 10, 1, -1, -1, -1, -1, -1, -1, -1],
        [1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6, -1, -1, -1, -1],
        [4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9, -1, -1, -1, -1],
        [10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3, -1],
        [8, 2, 3, 8, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1],
        [0, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8, -1, -1, -1, -1],
        [1, 9, 4, 1, 4, 2, 2, 4, 6, -1, -1, -1, -1, -1, -1, -1],
        [8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1, -1, -1, -1, -1],
        [10, 1, 0, 10, 0, 6, 6, 0, 4, -1, -1, -1, -1, -1, -1, -1],
        [4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3, -1],
        [10, 9, 4, 6, 10, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [4, 9, 5, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [0, 8, 3, 4, 9, 5, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1],
        [5, 0, 1, 5, 4, 0, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1],
        [11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5, -1, -1, -1, -1],
        [9, 5, 4, 10, 1, 2, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1],
        [6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5, -1, -1, -1, -1],
        [7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2, -1, -1, -1, -1],
        [3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6, -1],
        [7, 2, 3, 7, 6, 2, 5, 4, 9, -1, -1, -1, -1, -1, -1, -1],
        [9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7, -1, -1, -1, -1],
        [3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0, -1, -1, -1, -1],
        [6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8, -1],
        [9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7, -1, -1, -1, -1],
        [1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4, -1],
        [4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10, -1],
        [7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10, -1, -1, -1, -1],
        [6, 9, 5, 6, 11, 9, 11, 8, 9, -1, -1, -1, -1, -1, -1, -1],
        [3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5, -1, -1, -1, -1],
        [0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11, -1, -1, -1, -1],
        [6, 11, 3, 6, 3, 5, 5, 3, 1, -1, -1, -1, -1, -1, -1, -1],
        [1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6, -1, -1, -1, -1],
        [0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10, -1],
        [11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5, -1],
        [6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3, -1, -1, -1, -1],
        [5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2, -1, -1, -1, -1],
        [9, 5, 6, 9, 6, 0, 0, 6, 2, -1, -1, -1, -1, -1, -1, -1],
        [1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8, -1],
        [1, 5, 6, 2, 1, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6, -1],
        [10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0, -1, -1, -1, -1],
        [0, 3, 8, 5, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [10, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [11, 5, 10, 7, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [11, 5, 10, 11, 7, 5, 8, 3, 0, -1, -1, -1, -1, -1, -1, -1],
        [5, 11, 7, 5, 10, 11, 1, 9, 0, -1, -1, -1, -1, -1, -1, -1],
        [10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1, -1, -1, -1, -1],
        [11, 1, 2, 11, 7, 1, 7, 5, 1, -1, -1, -1, -1, -1, -1, -1],
        [0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11, -1, -1, -1, -1],
        [9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7, -1, -1, -1, -1],
        [7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2, -1],
        [2, 5, 10, 2, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1],
        [8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5, -1, -1, -1, -1],
        [9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2, -1, -1, -1, -1],
        [9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2, -1],
        [1, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [0, 8, 7, 0, 7, 1, 1, 7, 5, -1, -1, -1, -1, -1, -1, -1],
        [9, 0, 3, 9, 3, 5, 5, 3, 7, -1, -1, -1, -1, -1, -1, -1],
        [9, 8, 7, 5, 9, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [5, 8, 4, 5, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1],
        [5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0, -1, -1, -1, -1],
        [0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5, -1, -1, -1, -1],
        [10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4, -1],
        [2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8, -1, -1, -1, -1],
        [0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11, -1],
        [0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5, -1],
        [9, 4, 5, 2, 11, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4, -1, -1, -1, -1],
        [5, 10, 2, 5, 2, 4, 4, 2, 0, -1, -1, -1, -1, -1, -1, -1],
        [3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9, -1],
        [5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, -1, -1, -1, -1],
        [8, 4, 5, 8, 5, 3, 3, 5, 1, -1, -1, -1, -1, -1, -1, -1],
        [0, 4, 5, 1, 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, -1, -1, -1, -1],
        [9, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [4, 11, 7, 4, 9, 11, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1],
        [0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11, -1, -1, -1, -1],
        [1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11, -1, -1, -1, -1],
        [3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4, -1],
        [4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2, -1, -1, -1, -1],
        [9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3, -1],
        [11, 7, 4, 11, 4, 2, 2, 4, 0, -1, -1, -1, -1, -1, -1, -1],
        [11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4, -1, -1, -1, -1],
        [2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9, -1, -1, -1, -1],
        [9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7, -1],
        [3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10, -1],
        [1, 10, 2, 8, 7, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [4, 9, 1, 4, 1, 7, 7, 1, 3, -1, -1, -1, -1, -1, -1, -1],
        [4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, -1, -1, -1, -1],
        [4, 0, 3, 7, 4, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [4, 8, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [9, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [3, 0, 9, 3, 9, 11, 11, 9, 10, -1, -1, -1, -1, -1, -1, -1],
        [0, 1, 10, 0, 10, 8, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1],
        [3, 1, 10, 11, 3, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [1, 2, 11, 1, 11, 9, 9, 11, 8, -1, -1, -1, -1, -1, -1, -1],
        [3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9, -1, -1, -1, -1],
        [0, 2, 11, 8, 0, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [3, 2, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [2, 3, 8, 2, 8, 10, 10, 8, 9, -1, -1, -1, -1, -1, -1, -1],
        [9, 10, 2, 0, 9, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8, -1, -1, -1, -1],
        [1, 10, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [1, 3, 8, 9, 1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [0, 9, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
        [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
    ],
    dtype=int,
)


def _vijk(shape, v):
    """Get (ni-1,nj-1, nk-1) indexing vectors that will extract vertex number v
    from a 3D matrix of size (ni, nj, nk)."""
    ni, nj, nk = shape
    if v == 0:
        return slice(0, ni - 1), slice(0, nj - 1), slice(0, nk - 1)
    elif v == 1:
        return slice(1, ni), slice(0, nj - 1), slice(0, nk - 1)
    elif v == 2:
        return slice(1, ni), slice(1, nj), slice(0, nk - 1)
    elif v == 3:
        return slice(0, ni - 1), slice(1, nj), slice(0, nk - 1)
    elif v == 4:
        return slice(0, ni - 1), slice(0, nj - 1), slice(1, nk)
    elif v == 5:
        return slice(1, ni), slice(0, nj - 1), slice(1, nk)
    elif v == 6:
        return slice(1, ni), slice(1, nj), slice(1, nk)
    elif v == 7:
        return slice(0, ni - 1), slice(1, nj), slice(1, nk)


def _eijk(i, j, k, e):
    """Get 2 (i,j,k) indexing vectors that will extract the start and end
    points of edge e for cell i, j, k of a 3D matrix."""
    if e == 0:
        return (i, j, k), (i + 1, j, k)
    elif e == 1:
        return (i + 1, j, k), (i + 1, j + 1, k)
    elif e == 2:
        return (i + 1, j + 1, k), (i, j + 1, k)
    elif e == 3:
        return (i, j + 1, k), (i, j, k)
    elif e == 4:
        return (i, j, k + 1), (i + 1, j, k + 1)
    elif e == 5:
        return (i + 1, j, k + 1), (i + 1, j + 1, k + 1)
    elif e == 6:
        return (i + 1, j + 1, k + 1), (i, j + 1, k + 1)
    elif e == 7:
        return (i, j + 1, k + 1), (i, j, k + 1)
    elif e == 8:
        return (i, j, k), (i, j, k + 1)
    elif e == 9:
        return (i + 1, j, k), (i + 1, j, k + 1)
    elif e == 10:
        return (i + 1, j + 1, k), (i + 1, j + 1, k + 1)
    elif e == 11:
        return (i, j + 1, k), (i, j + 1, k + 1)


# How many triangles each corner sign pattern makes, and the corner offsets
# of a cell's 8 vertices and of the two ends of its 12 edges. All are derived
# from the table and helpers above rather than written out again, and are
# handed to the Fortran kernels so that they hold no copy of their own.
_VERT_IJK = np.array(
    [[s.start for s in _vijk((2, 2, 2), v)] for v in range(8)], dtype=np.int32
)
_EDGE_IJK = np.array([_eijk(0, 0, 0, e) for e in range(12)], dtype=np.int32)


def _marching_cubes(data, a, b=None, coeffs=(1.0, 0.0, 0.0)):
    """Take an unstructured cut of 3D data using a signed distance field.

    The kernel forms the distance ``na * a + nb * b - c0`` at each node as it
    goes. With only `a` given, that is `a` itself, a distance field already
    worked out; a straight cut instead passes the meridional coordinates and
    its line's coefficients, and no field is ever built.

    Parameters
    ----------
    data: (ni, nj, nk, nvar) array
        Data variables arranged on a three-dimensional structured grid
        following ember convention with components in last axis.
    a, b: (ni, nj, nk) array
        Fields the signed distance is formed from, zero on the cut.
    coeffs: tuple of float
        ``(na, nb, c0)``. The default makes the distance `a`.

    Returns
    -------
    verts: (n_vert, nvar) array
        Data variables at each vertex of the cut, one vertex per cut edge
        however many triangles share it.
    faces: (n_face, 3) int32 array
        Indices into `verts` of the three vertices of each triangle.

    Returns None if nothing is cut.
    """
    a = np.asfortranarray(a, dtype=np.float32)
    b = a if b is None else np.asfortranarray(b, dtype=np.float32)
    na, nb, c0 = coeffs

    # Room for one triangle per cell, which a cut through a 3D block, crossing
    # a few percent of its cells, comes nowhere near. Pages of an empty array
    # are only mapped in when written, so the room left over costs nothing.
    # A cut that does make more is counted in full, and then run again with
    # room for all of it.
    cap = max(int(np.prod(np.subtract(a.shape, 1))), 1)
    ic_row = np.empty(a.shape[0] - 1, dtype=np.int32)
    i_cut = np.empty_like(ic_row)
    while True:
        lo = np.empty((3, cap), dtype=np.int32, order="F")
        hi = np.empty((3, cap), dtype=np.int32, order="F")
        frac = np.empty((3, cap), dtype=np.float32, order="F")
        n_face = ember.fortran.marching_cubes(
            a,
            b,
            na,
            nb,
            c0,
            _VERT_IJK,
            _TRITABLE,
            _EDGE_IJK,
            lo,
            hi,
            frac,
            ic_row,
            i_cut,
        )
        if n_face <= cap:
            break
        cap = n_face

    # Nothing is cut
    if n_face == 0:
        return None

    # One vertex per cut edge, however many triangles share it. The kernel
    # gives each edge as its pair of nodes, lower first, so the pair is the
    # same from every cell around the edge and keys it. Transposed, the
    # vertices run face by face.
    lo = lo[:, :n_face].T.ravel()
    hi = hi[:, :n_face].T.ravel()
    frac = frac[:, :n_face].T.ravel()
    _, first, inverse = np.unique(
        lo.astype(np.int64) * a.size + hi, return_index=True, return_inverse=True
    )
    faces = inverse.reshape(n_face, 3).astype(np.int32)

    # Interpolate every variable onto the vertices the kernel located. Done
    # here rather than in the kernel so that the data keeps whatever precision
    # it arrived in, and never has to be handed over in bulk. The node indices
    # flatten i fastest, as a block's data is stored, so this is a view.
    flat = data.reshape(-1, data.shape[-1], order="F")
    data_lo = flat[lo[first]]
    data_hi = flat[hi[first]]

    return data_lo + (data_hi - data_lo) * frac[first, np.newaxis], faces


def _line_normal(xr_cut):
    """Unit normal and offset of the line through a single segment.

    Returns ``(nx, nr, c)`` such that ``nx * x + nr * r - c`` is the signed
    distance from the line, positive on its left, looking along it.
    """
    (x0, r0), (x1, r1) = np.asarray(xr_cut, dtype=np.float64)
    bx, br = x1 - x0, r1 - r0
    length = np.sqrt(max(bx * bx + br * br, 1e-9))
    nx, nr = -br / length, bx / length
    return nx, nr, x0 * nx + r0 * nr


def _line_distance(xr_cut, xr_query):
    """Signed distance from the line through a single segment, past both ends.

    Positive on the left of the segment, looking from its first point to its
    second, as :func:`_cut_field` signs a cut. Linear in the coordinates, so
    a few array passes rather than the kernel's search over segments. In
    single precision, as the coordinates are.
    """
    nx, nr, c = (np.float32(v) for v in _line_normal(xr_cut))
    xr_query = np.asarray(xr_query, dtype=np.float32)
    d = xr_query[..., 0] * nx
    d += xr_query[..., 1] * nr
    d -= c
    return d


def _distance_args(xr_cut, block):
    """Fields and coefficients the kernels form a block's cut distance from.

    The cut is the zero level of a signed distance. Only its sign and the
    fraction along a grid edge where it vanishes are used, neither of which a
    uniform scaling changes, so it is measured in the block's nondimensional
    coordinates, a view of its data, rather than in scaled copies of them.

    The cut runs on past both ends of the curve, straight along its end
    segments, so through a block it reaches it is the curve and those two
    rays, and the distance is to all three. A single straight segment is
    then just its line, whose distance is linear in the coordinates, so the
    kernels form it node by node from ``(x, r)`` and the line's coefficients
    and no field is built. A polyline cut is :func:`_cut_field`, which the
    default coefficients take as it is.

    Returns the positional arguments ``(a, [b, coeffs])`` that follow the
    data in :func:`_marching_cubes`.
    """
    xr_cut_nd = xr_cut / block.L_ref
    xrt_nd = block.xrt_nd
    if len(xr_cut) == 2:
        return xrt_nd[..., 0], xrt_nd[..., 1], _line_normal(xr_cut_nd)
    return (_cut_field(xr_cut_nd, xrt_nd[..., :2]),)


def _cut_field(xr_cut, xr_query):
    """Signed distance to a cut curve and its end rays, exact near the cut.

    The curve is carried on past both ends as straight rays along its end
    segments. Each node is signed by the side of the curve and rays it lies
    on, positive on the left of the direction the curve runs. Unlike a sign
    taken from the segment nearest a node, it changes only across the cut
    itself, never where the nearest segment switches.

    The distance is exact wherever the cutting kernels use it, at the nodes
    of every grid edge the cut crosses, and at all nodes near the cut. Further
    off only the sign is worked out, and the magnitude is the largest single
    precision number. See ``cut_distance`` in ``_fortran/distance.f90``.

    Parameters
    ----------
    xr_cut : array_like, shape (n_points, 2)
        Cut curve, of at least two points; neither it nor its rays may cross
        themselves.
    xr_query : Array, shape (ni, nj, nk, 2)
        Meridional coordinates of a block's nodes.

    Returns
    -------
    Array, shape (ni, nj, nk)
        Signed distance, in single precision.
    """
    xr_cut = np.asfortranarray(xr_cut, dtype=np.float32)
    n_piece = xr_cut.shape[0] + 1
    keep = np.empty(n_piece, dtype=np.int32)
    pieces = np.empty((n_piece, 14), dtype=np.float32, order="F")
    return ember.fortran.cut_distance(
        xr_cut, np.asfortranarray(xr_query, dtype=np.float32), keep, pieces
    )


def _cut_reaches_block(xr_cut, block):
    """Whether a cut curve comes near enough to a block to be worth evaluating.

    Clips each segment of the curve against the block's meridional bounding
    box. A curve that reaches no block's box cannot cross that block, so its
    distance field need never be built.

    The screen is conservative: a segment that enters the bounding box but
    misses the block itself is still accepted, and the sign of the distance
    field then rejects it as before.

    Parameters
    ----------
    xr_cut : array_like, shape (n_segments, 2)
        Meridional :math:`(x, r)` curve segments defining the cut surface.
    block : Block
        Block to test the curve against.

    Returns
    -------
    bool
        False only when no segment of the curve enters the block's box.

    """
    # Taken from the nondimensional coordinates and scaled as four scalars,
    # rather than from block.xrt, which would copy and scale every node
    xrt_nd = block.xrt_nd
    lo = np.array([xrt_nd[..., 0].min(), xrt_nd[..., 1].min()]) * block.L_ref
    hi = np.array([xrt_nd[..., 0].max(), xrt_nd[..., 1].max()]) * block.L_ref

    xr_cut = np.asarray(xr_cut)

    # Checked here because the screen sees the curve first
    assert xr_cut.shape[-1] == 2, "Segments must have shape (..., 2)"
    assert xr_cut.ndim >= 2, "Segments must be at least 2D"

    # A single segment cuts along the whole of its line, which reaches the box
    # if the box's corners are not all on one side of it
    if len(xr_cut) == 2:
        corners = np.array([[lo[0], lo[1]], [lo[0], hi[1]], [hi[0], lo[1]], hi])
        d = _line_distance(xr_cut, corners)
        return bool(d.min() <= 0.0 <= d.max())

    seg_start = xr_cut[:-1]
    seg_delta = xr_cut[1:] - seg_start

    # Clip the segment parameter, which runs over [0, 1] along the segment, to
    # the slab between each pair of opposite box faces in turn. What survives
    # in both components at once is the part of the segment inside the box.
    t_in = np.zeros(len(seg_start))
    t_out = np.ones(len(seg_start))
    for c in range(2):
        # A segment constant in this component never crosses either face, so
        # it either lies within the slab for its whole length or misses it
        constant = seg_delta[:, c] == 0.0
        with np.errstate(divide="ignore", invalid="ignore"):
            t_lo = (lo[c] - seg_start[:, c]) / seg_delta[:, c]
            t_hi = (hi[c] - seg_start[:, c]) / seg_delta[:, c]

        t_in = np.maximum(t_in, np.where(constant, 0.0, np.minimum(t_lo, t_hi)))
        t_out = np.minimum(t_out, np.where(constant, 1.0, np.maximum(t_lo, t_hi)))

        outside = constant & ((seg_start[:, c] < lo[c]) | (seg_start[:, c] > hi[c]))
        t_in = np.where(outside, 1.0, t_in)
        t_out = np.where(outside, 0.0, t_out)

    # An empty interval on every segment means the curve misses the box. A
    # curve of a single node spans no segment and misses by the same token.
    return bool(np.any(t_in <= t_out))


# How far past each end of a cut curve the cuts look for the blocks it
# reaches, as a fraction of the end segment's length. Enough to take in a
# block whose wall cells straddle the end of a hub-to-casing line; short
# enough not to reach back into the fluid. Within a block it does reach, the
# cut runs on along the end segments' lines regardless.
_CUT_EXTEND = 0.1


def _extend_ends(xr_cut, frac):
    """Return `xr_cut` with its first and last segments lengthened by `frac`."""
    if xr_cut.ndim != 2 or xr_cut.shape[0] < 2:
        return xr_cut
    xr_cut = xr_cut.copy()
    xr_cut[0] += frac * (xr_cut[0] - xr_cut[1])
    xr_cut[-1] += frac * (xr_cut[-1] - xr_cut[-2])
    return xr_cut


def _unstructured_row(blocks, xr_cut):
    """The marching-cubes cut through one blade row, or None if it misses.

    `xr_cut` has already been carried on past its ends. The blocks are taken
    to share their metadata, which the cut takes from the first block it
    crosses; a row whose blocks the cut crosses with different blade counts,
    rotation or length scales raises.
    """
    verts = []
    faces = []
    n_node = 0
    crossed = []
    for block in blocks:
        # Reject the blocks the curve does not reach before building any
        # distance field. This also keeps out a block that only the rays
        # running on from the curve's ends would cut.
        if not _cut_reaches_block(xr_cut, block):
            continue

        # Get triangles for this block, or None if the cut misses it
        cut_block = _marching_cubes(block._data, *_distance_args(xr_cut, block))

        # Add them to the list, numbering this block's nodes after those of
        # the blocks before. Nodes on an interface between blocks are kept
        # once per block.
        if cut_block is not None:
            verts.append(cut_block[0])
            faces.append(cut_block[1] + n_node)
            n_node += cut_block[0].shape[0]
            crossed.append(block)

    if not crossed:
        return None

    # The nodes are stored nondimensionally, so blocks of one cut must agree
    # on their scales as well as on what the averages read
    first = crossed[0]
    for block in crossed[1:]:
        if (block.Nb, block.Omega, block.L_ref) != (first.Nb, first.Omega, first.L_ref):
            raise ValueError(
                "The cut crosses blocks of one row with different blade counts, "
                "rotation or length scales."
            )

    nodes = _nodes_like(first, n_node)
    nodes._data[:] = np.concatenate(verts)
    return UnstructuredBlock(nodes, np.concatenate(faces))


[docs] def unstructured(grid, xr_cut): r"""Take an unstructured cut through a grid using marching cubes. Extracts a triangulated isosurface where the meridional cut curve intersects the grid, by running the marching cubes algorithm on the signed distance field of each block. Within each block the curve reaches, or comes within a tenth of its end segments' length of reaching, the cut runs on past both ends straight along those segments, so a curve ending on an endwall cuts the wall cells cleanly. A single straight segment is thus its whole line, which is also the quicker case to evaluate. A grid of several blade rows is cut a row at a time, from inlet to outlet, giving one unstructured block per row the cut crosses, with that row's fluid, blade count and rotation; rows the cut misses give none. Within a row, the triangles of every block the cut crosses are joined into one block, a node on an interface between blocks being kept once per block. Parameters ---------- grid : Grid or Block Grid containing blocks to cut, or a single block. xr_cut : array_like, shape (n_points, 2) Meridional :math:`(x, r)` curve defining the cut surface. Returns ------- list of UnstructuredBlock The triangulated cut through each row it crosses. Raises ------ ValueError If the cut misses every row of the grid, or crosses blocks of one row with different blade counts, rotation or length scales. """ if isinstance(grid, Block): grid = [grid] rows = grid.rows if isinstance(grid, ember.grid.Grid) else [grid] # A cut curve usually ends on an endwall, but where that wall is sloped the # wall cells straddling the cut have nodes past the end of the curve. The # cut runs on along the end segments in any block it reaches, so those # cells are cut on the line through the walls. Carry the curve on a little # before screening the blocks too, so that one whose wall cells are all # that straddle it is still reached. xr_cut = _extend_ends(np.asarray(xr_cut, dtype=float), _CUT_EXTEND) cuts = [_unstructured_row(row, xr_cut) for row in rows] cuts = [cut for cut in cuts if cut is not None] if not cuts: raise ValueError("The cut misses the grid.") return cuts
[docs] def structured_traverse(grid, xr_cut, factor=1, untwist=0): r"""Cut a blade-free traverse plane into one structured sheet. The sheet is built from the grid's own lines rather than resampled. Where one family of a block's grid lines each crosses the cut the same number of times, or a run of them crosses once from hub to casing, each node of the sheet is a linear interpolation along one grid edge, exactly as the nodes of :func:`unstructured` are. These exact patches are joined across interfaces and periodic faces. Between them, where the cut crosses the grid lines only part-way, as it does near an O-grid's nose, the sheet is filled from the cut's triangles along rows that start and end on the exact nodes either side. With no exact patch at all, as where an inclined cut crosses one block near the hub and the next near the casing, each spanwise surface crossed cleanly round the pitch gives an exact row on the grid lines of the row below; one crossed part-way, at the interface, is cut alone and interpolated round the pitch between its neighbours. Failing that, the sheet is filled between two copies of the periodic seam. The sheet runs once round the pitch. Axis 0 of the result runs from hub to casing and axis 1 up in :math:`\theta`, with the last column the first a pitch on. Rows follow the grid's own spanwise surfaces, so they lie at constant :math:`(x, r)` only where those surfaces are surfaces of revolution, and the spacing in :math:`\theta` follows the grid's. Grid lines twisted round the annulus twist the sheet's columns with them; `untwist` instead slides every row round to start at the hub's first :math:`\theta`, keeping its own spacing, so the boundary columns lie at constant :math:`\theta` and the others nearly so, with the data interpolated along each row. The grid's periodic pairs must coincide exactly, as :meth:`ember.grid.Grid.snap_periodic` makes them: nodes are matched by position, to within a few single precision steps. Parameters ---------- grid : Grid or Block Grid to cut, or a single block. xr_cut : array_like, shape (n_points, 2) Meridional :math:`(x, r)` curve defining the cut surface, carried on straight past both ends. factor : int, default 1 Cells across a filled region per grid cell along its endwalls. The exact patches and rows are the grid's own lines and are not refined. untwist : int, default 0 If not zero, slide the rows round so the columns lie at constant :math:`\theta`, each interval along a row split into this many, the data interpolated by monotone cubics. That costs accuracy where a flow feature twists with the grid lines, as near a leading edge, whose resolution the straight columns no longer follow: 2 is a reasonable choice for a plot. Zero, the default, keeps every node exact. Returns ------- Block 2D structured block, shape ``(n_span, n_pitch)``. Raises ------ ValueError If the cut misses the grid; meets a blade, tip gap or other solid, so does not close round the pitch or leaves a boundary that is not an endwall; crosses blocks of different blade counts; or meets grid pieces with different numbers of spanwise nodes. """ if isinstance(grid, Block): grid = [grid] return ember._traverse.traverse(grid, xr_cut, factor, untwist)
[docs] def structured_multiblock(grid, xr_cut, factor=1): r"""Cut a grid into structured 2D blocks, through blades as well. Where the cut makes one sheet, the result is exactly that of :func:`structured_traverse`. Where it meets a solid, it is the pieces of the same exact patches and filled holes, split at each solid: a blade or cusp in an H-mesh, between the two faces it leaves, and a blade inside an O-grid, between the patches either side of it. Pieces with fewer spanwise nodes than the rest, as the cut makes across a tip gap meshed in blocks of its own, are blocks of their own. Every node is either interpolated along one grid edge or filled from the cut's triangles as in :func:`structured_traverse`. A grid of several blade rows is cut a row at a time, from inlet to outlet, so a blade-to-blade cut gives at least one block per row, each with its own row's pitch; rows the cut misses give none. Each block runs along the cut curve on axis 0, from hub to casing for a traverse plane, and up in :math:`\theta` on axis 1. Pieces that meet share their nodes along the side between them. The blocks are labelled ``cut0``, ``cut1`` and so on, as a piece can span several of the grid's blocks. Parameters ---------- grid : Grid or Block Grid to cut, or a single block, its periodic pairs snapped as for :func:`structured_traverse`. xr_cut : array_like, shape (n_points, 2) Meridional :math:`(x, r)` curve defining the cut surface, carried on straight past both ends. factor : int, default 1 Cells across a filled region per grid cell along its endwalls. Returns ------- list of Block The 2D structured blocks of the cut. Raises ------ ValueError If the cut misses every row of the grid, or leaves a gap between its pieces that is neither fluid nor a solid. """ if isinstance(grid, Block): grid = [grid] blocks = ember._traverse.multiblock(grid, xr_cut, factor) # A piece can span several of the grid's blocks, so none of their labels for n, block in enumerate(blocks): block.set_label(f"cut{n}") return blocks