"""Views and movies of saved Navier-Stokes (NS) P2/P1 flow fields.
A results folder written by a :class:`WarpFlowNS` run holds ``mesh.npz``
(``points_m``, ``tetrahedra``, ``edges``) besides the files every
:class:`VisualizeFlowFieldsBase` reads; ``velocity_m_s.npy`` holds the
velocity at the canonical P2 nodes (the vertices, then the midpoints of the
sorted unique tetrahedron ``edges``). Fields are shown on genuine ten-node
quadratic tetrahedra.
"""
from __future__ import annotations
from pathlib import Path
from typing import ClassVar
import numpy as np
import pyvista as pv
from .visualize_flow_fields_base import VisualizeFlowFieldsBase
[docs]
class VisualizeFlowFieldsNS(VisualizeFlowFieldsBase):
"""Render saved P2/P1 velocity and pressure frames on quadratic tetrahedra.
Per-vertex values (pressure, wall displacement) are shown at the edge
nodes as the mean of the edge's two vertices.
"""
#: Vertex pairs of the six edge nodes of a VTK quadratic tetrahedron.
VTK_TET_EDGES: ClassVar[list[list[int]]] = [
[0, 1],
[1, 2],
[0, 2],
[0, 3],
[1, 3],
[2, 3],
]
#: Sorted unique tetrahedron edges of the mesh, kept by :meth:`_load_grid`.
edges: np.ndarray
def _load_grid(self, results: Path) -> pv.UnstructuredGrid:
"""Return the quadratic tetrahedra of ``mesh.npz`` at the P2 nodes.
Also keeps the mesh's ``edges``, which :meth:`_to_grid_points` uses.
"""
mesh = np.load(results / "mesh.npz")
points = mesh["points_m"]
# int64 keys: the vertex count squared overflows int32 above 46340
# vertices.
tets = np.asarray(mesh["tetrahedra"], dtype=np.int64)
self.edges = np.asarray(mesh["edges"], dtype=np.int64)
nodes = np.concatenate([points, points[self.edges].mean(axis=1)])
keys = self.edges[:, 0] * len(points) + self.edges[:, 1]
local = np.sort(tets[:, self.VTK_TET_EDGES], axis=2)
edge_ids = np.searchsorted(keys, local[:, :, 0] * len(points) + local[:, :, 1])
return pv.UnstructuredGrid(
np.c_[np.full(len(tets), 10), tets, len(points) + edge_ids].ravel(),
np.full(len(tets), pv.CellType.QUADRATIC_TETRA, dtype=np.uint8),
nodes,
)
def _to_grid_points(self, vertex_values: np.ndarray) -> np.ndarray:
"""Return per-vertex values at the P2 nodes, edge nodes averaged."""
return np.concatenate([vertex_values, vertex_values[self.edges].mean(axis=1)])