Vortex, Wall Shear & Flux

Three related BSD-3-Clause modules for fluid-mechanics metrics on Eulerian velocity fields.

flowtracks.vortex

Vortex-identification scalars from a 3-D velocity field: the Q-criterion and lambda2 (Jeong & Hussain 1995), plus enstrophy — all vectorized over the grid via batched eigenvalue decomposition.

flowtracks.wall_shear

Near-wall shear stress and cyclic wall-shear indices from a velocity field. General-purpose: works with any triangulated surface (an explicit wall mesh, or one extracted with flowtracks.wall_shear.occupancy_isosurface() when no explicit mesh exists). TAWSS/OSI/RRT are standard wall-shear indices; nothing here is specific to one dataset. Normals via scatter-add over faces, near-wall sampling via one batched grid interpolation, indices via cumulative sums over phase — no per-vertex or per-phase Python loops.

flowtracks.wall_shear.vertex_normals() computes per-vertex normals. flowtracks.wall_shear.near_wall_shear_stress() computes the shear stress magnitudes; flowtracks.wall_shear.tawss_osi_rrt() and flowtracks.wall_shear.running_tawss_osi_rrt() turn them into the wall-shear indices.

flowtracks.flux

Volume flow rate through cross-sections of an internal flow (pipe, channel, vessel). In an incompressible flow the rate through every cross-section of a rigid conduit is the same at any instant, so flow rate along the conduit is a truth-free check of the measured velocities. The pipeline, each step usable on its own:

flowtracks.flux.fluid_domain() → flowtracks.flux.domain_boundary() → flowtracks.flux.centerline() → flowtracks.flux.section_lumen() → flowtracks.flux.section_flux() / flowtracks.flux.field_flux().

flowtracks.nhist

Normalized histograms. flowtracks.nhist.nhist_scipy() is the vectorized normalized histogram; flowtracks.nhist.nhist() wraps it with plotting options.

Vortex-identification scalars from a 3-D velocity field.

Q-criterion, lambda2 (Jeong & Hussain 1995), and enstrophy – standard, non-proprietary turbulence-analysis quantities. Vectorized over the whole grid (and any extra dims, e.g. phase) via batched eigenvalue decomposition, rather than a per-voxel loop.

flowtracks.vortex.vortex_identification(u: ndarray, v: ndarray, w: ndarray, dx: float, dy: float, dz: float) → tuple[source]

Q-criterion, lambda2, and enstrophy for a velocity field on a regular grid.

u, v, w: arrays shaped (nx, ny, nz, …) – any trailing dims (e.g. phase) are treated independently, batched through the same eigendecomposition.

Returns (Q, lambda2, enstrophy), each shaped like u.

Near-wall shear stress and cyclic wall-shear indices from a velocity field.

General-purpose: works with any triangulated surface – an explicit wall mesh you already have, or one extracted from an occupancy/indicator field with occupancy_isosurface() when no explicit mesh exists. TAWSS/OSI/RRT are standard wall-shear indices (time-averaged WSS, oscillatory shear index, relative residence time); nothing here is specific to any one dataset or organ.

Vectorized throughout: normals via scatter-add over faces, near-wall sampling via one batched grid interpolation call, TAWSS/OSI/RRT via cumulative sums over phase – no per-vertex or per-phase Python loops.

flowtracks.wall_shear.vertex_normals(vertices: ndarray, faces: ndarray) → ndarray[source]

Per-vertex normals: area-weighted average of adjacent face normals.

vertices

flowtracks.wall_shear.occupancy_isosurface(occupancy: ndarray, x: ndarray, y: ndarray, z: ndarray, level: float = 0.5) → tuple[source]

Extract a triangulated surface from an occupancy/indicator field via marching cubes, for use as a wall/interface proxy when no explicit mesh is available (e.g. the boundary of a masked flow region).

Requires the optional vtk dependency (flowtracks[vtk]). x, y, z must be uniformly spaced (a vtkImageData assumption).

flowtracks.wall_shear.near_wall_shear_stress(points: ndarray, normals: ndarray, x: ndarray, y: ndarray, z: ndarray, u: ndarray, v: ndarray, w: ndarray, mu: float, offset: float) → tuple[source]

Estimate wall/interface shear stress from the near-surface velocity.

Thin-layer approximation: sample velocity at points - offset*normals, project onto the local tangent plane, and take tau = mu * v_tangential / offset.

Returns (tau_vec, tau_mag), shaped (n_points, 3) and (n_points,). NaN where the sample point falls outside the velocity field’s domain.

flowtracks.wall_shear.tawss_osi_rrt(tau_vec: ndarray, tau_mag: ndarray, eps: float = 1e-12) → tuple[source]

Time-averaged WSS, oscillatory shear index, and relative residence time over a full cycle.

tau_vec

Returns (TAWSS, OSI, RRT), each (n_points,).

flowtracks.wall_shear.running_tawss_osi_rrt(tau_vec: ndarray, tau_mag: ndarray, eps: float = 1e-12) → tuple[source]

Cumulative-in-phase TAWSS/OSI/RRT: value at phase k uses only phases 1..k.

Same inputs/shapes as tawss_osi_rrt; returns arrays shaped (n_points, n_phase).

Volume flow rate through cross-sections of an internal flow, from particles.

For Lagrangian (PTV) data in a pipe, channel or vessel: in an incompressible flow the rate through every cross-section of a rigid conduit is the same at any instant, so the flow rate along the conduit is a truth-free check of the measured velocities (in a compliant conduit the differences also contain the volume stored by wall motion).

Pipeline, each step usable on its own:

1. fluid_domain() – where the fluid is: voxel occupancy of the particle positions, smoothed, closed, largest connected component. 2. centerline() – the conduit axis: the longest path through the 3D skeleton of the domain, smoothed, with stations every spacing along it and their unit tangents (the section normals). Needs scikit-image (pip install flowtracks[flux]). 3. section_lumen() – one cross-section’s lumen: the convex hull of where particles passed through a slab around the plane over the whole record, clipped to the part of the domain the plane cuts around the station (so a plane crossing two branches keeps only its own). 4. section_flux() – the flow rate through that lumen per time window, sum over cells of mean(u . n) x lumen area inside the cell, from the samples in the slab. 5. field_flux() – the exact flow rate of a KNOWN velocity field through a section, to test 1-4 against ground truth.

Units are the caller’s: positions in any length unit L (e.g. mm), velocities in any unit V; areas come out in L^2 and flow rates in V * L^2 (positions in mm and velocities in m/s give mL/s).

flowtracks.flux.fluid_domain(points, voxel, min_density=3.0, smooth=1.0, margin=3)[source]

Fluid domain from particle positions (N, 3): voxels whose smoothed occupancy (Gaussian, smooth voxels) reaches min_density points, closed and hole-filled, largest connected component.

Pass positions of MOVING particles only: points that never move (dirt, reflections on a wall) are not fluid. Returns a Domain.

flowtracks.flux.domain_boundary(domain)[source]

Every voxel face between a fluid voxel and a non-fluid one (or the grid edge): the conduit walls plus its openings. Returns a dict of arrays

cell (N, 3) int – the fluid voxel owning the face axis (N,) int – 0/1/2: the face is normal to x/y/z side (N,) int – +1/-1: the outward normal is side * e_axis centre (N, 3) – face centre normal (N, 3) – outward unit normal area float – face area (voxel^2)

E.g. to mark which faces are openings (flow crosses) and which are walls.

flowtracks.flux.centerline(domain, spacing, smooth=7, tangent_span=None)[source]

Conduit axis: the longest path through the 3D skeleton of the domain (two breadth-first sweeps), smoothed over smooth voxels, with stations every spacing along it (starting spacing/2 in) and unit tangents from a central difference over tangent_span (default: two voxels each side). The orientation along the path is arbitrary; flip the normals if a positive flux should mean the other direction.

flowtracks.flux.section_lumen(points, domain, center, normal, slab, cell, rmax, refine=3)[source]

The lumen of the section (center, normal): the convex hull of the fine cells (cell / refine) that particles passed through within slab of the plane over the whole record, clipped to the connected part of the domain around center (dilated by one flux cell). Using all times keeps sparse near-wall sampling from shrinking the section; the domain clip keeps a non-convex conduit from spilling outside the fluid. Returns a Section, or None if the plane misses the domain.

flowtracks.flux.section_flux(section, points, velocities, times, edges, min_count=3)[source]

Flow rate through section in each time window [edges[w], edges[w+1]).

Samples

(N, 3) and times (N,). A flux cell counts in a window if it has at least min_count samples; it contributes mean(u . n) x the lumen area inside it. Returns a dict of per-window arrays:

measured – cells with data only (a lower bound where the missing area carries flow in the same direction) area_corrected – the measured mean flux scaled to the whole lumen coverage – lumen area with data / lumen area samples – samples in the lumen, per window

flowtracks.flux.field_flux(velocity, inside, center, normal, times, rmax, h)[source]

Exact flow rate of a KNOWN field through the section (center, normal): velocity(points, t) -> (N, 3) and inside(points) -> (N,) bool (the fluid) are evaluated on h-sized cells of the plane, over the connected part of the fluid around center. Returns (flow rate per time, section area); units as the caller’s (velocity unit x length unit^2).

flowtracks.nhist.nhist_scipy(y: ndarray, bins: int | ndarray = 20, plot: bool = True, density: bool = True, kde: bool = False) → Tuple[ndarray, ndarray][source]

Fast, robust normalized histogram (PDF) with optional KDE overlay.

Parameters

y : array-like Input data. bins : int or array-like, default 20 Number of bins or bin edges. plot : bool, default True If True, plot the PDF. density : bool, default True If True, normalize to form a PDF. kde : bool, default False If True, overlay a Gaussian KDE. ax : matplotlib Axes, optional Axis to plot on. **plot_kwargs : dict Additional plot arguments.

Returns

hist : ndarray PDF values. bin_centers : ndarray Bin centers.

flowtracks.nhist.nhist(y, x=None, *args, plot=True)[source]

Normalized histogram (PDF), Matlab nhist.m equivalent.

Parameters

y : array-like Input data (vector or matrix). x : int or array-like, optional If int, number of bins. If array, bin centers. *args : Additional arguments for matplotlib plot. plot : bool, default True If True, plot the histogram. If False, return data.

Returns

no : ndarray Normalized counts (PDF values). xo : ndarray Bin centers.