Source code for monai_physio.aorta.workflow_simulate_flow_fsi_ns

"""Workflow for 3D Navier-Stokes (NS) blood flow in an aorta with a moving wall.

The workflow prepares an aorta labelmap with :class:`AortaProcessGeometry`
(or takes a prepared geometry), meshes the lumen and a wall shell around it
together with netgen (:meth:`AortaProcessGeometry.fsi_mesh`) and couples the
flow model of :class:`AortaWorkflowSimulateFlow3DNS` to a quasi-static
Neo-Hookean wall with :class:`WarpFluidStructureNS` (fluid-structure
interaction, FSI), the model of
``experiments/BloodFlow-3D-FSI-Simpleware-Warp``: the wall is prestressed so
that the imaged lumen is its shape at the outlets' initial pressure,
clamped in a collar around every port and tethered on its outer surface,
and every time step iterates the blood and the wall to agreement with an
interface quasi-Newton method.

The solver needs a CUDA GPU and NVIDIA cuDSS
(:meth:`monai_physio.WarpCuDssFactor.find_library`).

Typical usage::

    import itk
    from monai_physio import SegmentNVSegmentCTMRI
    from monai_physio.aorta import (
        AortaFlowParameters,
        AortaProcessFlow,
        AortaVisualizeFlowNS,
        AortaWorkflowSimulateFlowFSINS,
    )

    # Label ids of the segmenter that wrote the labelmaps.
    taxonomy = SegmentNVSegmentCTMRI().taxonomy
    # The patient's measured blood pressure, an input of the simulation.
    flow_parameters = AortaFlowParameters(
        systolic_pressure_mmhg=124.0, diastolic_pressure_mmhg=82.0
    )
    # The generator reads one frame at a time: a long series of labelmaps
    # may not fit in memory.
    lv_volume_ml = AortaProcessFlow.lv_volumes_ml(
        (itk.imread(f) for f in frame_files), [taxonomy.label_id("ventricle_left")]
    )
    inflow, interval_s = AortaProcessFlow.lv_inflow(
        lv_volume_ml, flow_parameters.heart_rate_bpm
    )
    workflow = AortaWorkflowSimulateFlowFSINS(flow_parameters)
    result = workflow.process(
        itk.imread("labelmap.mha"),
        aorta_label_ids=(taxonomy.label_id("aorta"),),
        branch_label_ids={
            "BCT": taxonomy.label_id("brachiocephalic_trunk"),
            "LCCA": taxonomy.label_id("common_carotid_artery_left"),
            "LSA": taxonomy.label_id("subclavian_artery_left"),
        },
        inflow_m3_s=inflow,
        inflow_interval_s=interval_s,
    )
    visualize = AortaVisualizeFlowNS()
    visualize.save_results_3d(result, "./out")
    visualize.save_plots(result, "./out")
"""

from __future__ import annotations

import logging
import time
from collections.abc import Mapping, Sequence
from typing import Any, ClassVar, Optional

import itk
import numpy as np
from scipy.spatial import cKDTree

from ..process_flow_boundaries import ProcessFlowBoundaries
from ..process_volumetric_mesh import ProcessVolumetricMesh
from ..warp_flow_ns import WarpFlowNS
from ..warp_fluid_structure_ns import WarpFluidStructureNS
from ..warp_hyperelastic_wall import WarpHyperelasticWall
from .flow_parameters import AortaFlowParameters
from .workflow_simulate_flow_3d_ns import AortaWorkflowSimulateFlow3DNS


[docs] class AortaWorkflowSimulateFlowFSINS(AortaWorkflowSimulateFlow3DNS): """Simulate 3D blood flow in an aorta whose wall moves with the blood. The blood model, its inflow, outlets and summary are those of :class:`AortaWorkflowSimulateFlow3DNS`; this class adds the wall. ``flow_parameters`` also supplies the wall's Young's modulus, Poisson ratio, thickness and tether stiffness. **Wall** A compressible Neo-Hookean solid of the given thickness around the lumen, quasi-static (massless), prestressed so that the imaged geometry is its equilibrium at :attr:`AortaFlowParameters.initial_pressure_pa`, the pressure where the simulation starts (with calibrated outlets, the calibrated periodic pressure at the start of the beats), so the run starts at rest in equilibrium; clamped within ``collar_mm`` of every port and held by springs on its outer surface (:class:`WarpHyperelasticWall`). Netgen fills a wall shell reliably only when it is at least :attr:`MIN_WALL_CELLS` grid spacings thick, so a thinner wall is prepared that thick and its Young's modulus scaled by the physiological over the meshed thickness. Keeping modulus times thickness is a thin-wall approximation: the Lame thick-wall solution makes the default 1.5 mm, 1 MPa wall meshed 3.75 mm thick at 0.4 MPa about 20-35% more compliant for inner radii of 12-7.5 mm. The reported wall motion, strain, distensibility and wave speed describe that surrogate wall. The run records the thickness and modulus meshed. **Input pressures and wall compliance** As in the rigid workflows the patient's systolic and diastolic pressures are inputs, but here the wall stores part of each stroke itself and smooths the root pressure before the outlets see it. :meth:`process` therefore first measures the wall's compliance ``dV/dP`` with a quasi-static solve under uniform pressure, prestressed at the diastolic input and loaded with the systolic one, and then calibrates the outlets with that compliance in front of them (:meth:`AortaProcessFlow.calibrated` with ``wall_compliance_m3_pa``). The total compliance reported is the outlets' plus the wall's. The lumen is somewhat more compliant in the flow than in this static, uniform-pressure estimate (compare the summary's ``lumen_compliance_m3_pa``), so the simulated pulse and systolic pressure come out a little low. ``calibrate_outlets=False`` uses the outlet totals of ``flow_parameters`` as given. """ _DEFAULT_DT_S = 0.0025
[docs] def __init__( self, flow_parameters: Optional[AortaFlowParameters] = None, log_level: int | str = logging.INFO, ) -> None: """Initialize the workflow; see :class:`AortaWorkflowSimulateFlowBase`.""" super().__init__(flow_parameters=flow_parameters, log_level=log_level) self.spacing_mm = 1.25 self.collar_mm = 2.0
[docs] def set_spacing_mm(self, spacing_mm: float) -> None: """Set the grid spacing of the prepared geometry. It sets the mesh size; the run time grows about as its inverse cube. Args: spacing_mm: Spacing in millimeters. Default: 1.25 Raises: ValueError: If it is not positive. """ if not spacing_mm > 0: raise ValueError(f"spacing_mm must be > 0, got {spacing_mm}") self.spacing_mm = spacing_mm
[docs] def set_collar_mm(self, collar_mm: float) -> None: """Clamp wall points this close to a port. Args: collar_mm: Distance in millimeters. Default: 2.0 Raises: ValueError: If it is negative. """ if collar_mm < 0: raise ValueError(f"collar_mm must be >= 0, got {collar_mm}") self.collar_mm = collar_mm
TITLE: ClassVar[str] = "3D FSI aorta flow" #: Interface quasi-Newton settings of :class:`WarpFluidStructureNS`, #: those of ``experiments/BloodFlow-3D-FSI-Simpleware-Warp`` except a #: looser absolute tolerance, since float32 blood solves hold the #: interface residual above the experiment's 1 nm. COUPLING: ClassVar[dict[str, Any]] = { "relaxation": 0.1, "reuse": 2, "tolerance": 1e-3, "absolute_tolerance": 1e-8, "max_iterations": 20, } #: Fewest grid spacings across the wall shell that netgen fills reliably. MIN_WALL_CELLS = 3.0 #: Relative residual the static wall solve of the compliance must reach. WALL_TOLERANCE: ClassVar[float] = 1e-6 MESH_KEYS: ClassVar[tuple[str, ...]] = ( *AortaWorkflowSimulateFlow3DNS.MESH_KEYS, "wall_points_mm", "wall_tetrahedra", "wall_clamped", "wall_tether_faces", ) #: ``gmres_iterations`` and ``residual`` are those of the step's last #: blood solve; ``total_gmres_iterations`` sums every blood solve. STEP_KEYS: ClassVar[dict[str, tuple[str, Any]]] = { **AortaWorkflowSimulateFlow3DNS.STEP_KEYS, "total_gmres_iterations": ("gmres_iterations", np.int64), "converged": ("converged", bool), **{ key: (key, np.int64) for key in ( "coupling_iterations", "wall_iterations", "wall_tangent_updates", ) }, **{ key: (key, np.float64) for key in ( "coupling_residual", "coupling_residual_m", "max_displacement_m", "mean_displacement_m", "volume_m3", "volume_rate_m3_s", ) }, } LOG_STEPS: ClassVar[int] = 20 # ─────────────────────────── Main workflow ─────────────────────────────
[docs] def process( self, labelmap_image: Optional[itk.Image] = None, *, inflow_m3_s: np.ndarray, inflow_interval_s: float, geometry: Optional[Mapping[str, Any]] = None, mesh: Optional[Mapping[str, Any]] = None, ) -> dict[str, Any]: """Simulate coupled flow and wall motion in an aorta. Args: labelmap_image: Labelmap holding the aorta and, optionally, its branches; prepared by :meth:`AortaProcessGeometry.prepare` at *spacing_mm* with the wall thickness to mesh (see *Wall* above). Give this or *geometry*. inflow_m3_s: Mean inflow of each interval of a periodic series, for example from :meth:`AortaProcessFlow.lv_inflow`. inflow_interval_s: Duration of each inflow interval in seconds. geometry: A :meth:`AortaProcessGeometry.prepare` result, used in place of preparing *labelmap_image*; its spacing replaces *spacing_mm*, and the Young's modulus is scaled to its ``wall_thickness_mm``. mesh: Lumen and wall meshes with the keys of :meth:`AortaProcessGeometry.fsi_mesh`, used in place of meshing the geometry: the lumen keys of :meth:`AortaWorkflowSimulateFlow3DNS.process` plus ``"wall_points_mm"``, ``"wall_tetrahedra"``, ``"wall_clamped"`` and ``"wall_tether_faces"``. Every lumen point on a wall triangle (region 0) must be a wall point. Returns: ``dict`` with the keys of :meth:`AortaWorkflowSimulateFlow3DNS.process`, where ``'mesh'`` holds the lumen and wall meshes (millimetres), the velocity is at the P2 nodes of the lumen mesh as given, every field belongs to the mesh moved by the frame's displacement, and ``'gmres_iterations'`` and ``'residual'`` are those of the last blood solve of each step. Added: - ``'series'`` - (T,) ``'total_gmres_iterations'``, ``'coupling_iterations'``, ``'converged'``, ``'coupling_residual'``, ``'coupling_residual_m'``, ``'wall_iterations'`` and ``'wall_tangent_updates'``, the coupling statistics of every step (:meth:`WarpFluidStructureNS.step`); ``'max_displacement_m'`` and ``'mean_displacement_m'``, the largest and mean interface displacement; ``'volume_m3'`` and ``'volume_rate_m3_s'``, the lumen volume and its rate of change. - ``'frames'`` - (F, points, 3) ``'displacement_m'`` of the lumen mesh points (the wall's on the wall, its harmonic extension inside). - ``'summary'`` - the 3D summary plus the total, wall and outlet compliances, the wall's metrics over the last beat (peak displacement and outward normal displacement in mm, lumen volume and mean diameter strain in percent, the lumen's compliance fitted to the root pressure, the distensibility and the Bramwell-Hill pulse-wave speed) and the coupling statistics of the whole run (``max_coupling_residual`` relative to the step's interface motion, ``max_coupling_residual_m`` the RMS residual in metres). ``max_net_port_flow_relative`` counts the lumen volume change as outflow. Raises: ValueError: If not exactly one of *labelmap_image* and *geometry* is given, if *beats* is below two, if *frame_interval_s* is longer than a beat, if *dt_s* or the inflow series does not fit the heart rate (see ``_check_timing``), if *mesh* lacks a key or a port, if the input pressures do not satisfy ``0 < diastolic < systolic``, if the inflow has no ejection, if an outlet has no flow fraction, or if no outlet resistance and compliance give the input pressures, as when the wall alone is too compliant. RuntimeError: If a linear solve or a coupled step diverges, or if the static wall solve of the compliance does not converge. """ aorta_label_ids = self.aorta_label_ids branch_label_ids = self.branch_label_ids beats = self.beats dt_s = self.dt_s calibrate_outlets = self.calibrate_outlets element_size_mm = self.element_size_mm precision = self.precision frame_interval_s = self.frame_interval_s spacing_mm = self.spacing_mm collar_mm = self.collar_mm wall_thickness_mm = max( self.flow_parameters.wall_thickness_mm, self.MIN_WALL_CELLS * spacing_mm ) return self._simulate( labelmap_image, aorta_label_ids, branch_label_ids, inflow_m3_s=inflow_m3_s, inflow_interval_s=inflow_interval_s, geometry=geometry, beats=beats, dt_s=dt_s, frame_interval_s=frame_interval_s, calibrate_outlets=calibrate_outlets, prepare_options={ "spacing_mm": spacing_mm, "wall_thickness_mm": wall_thickness_mm, }, domain_options={ "mesh": mesh, "element_size_mm": element_size_mm, "collar_mm": collar_mm, }, solver_options={"precision": precision}, )
# ─────────────────────────── Model hooks ─────────────────────────────── def _mesh( self, geometry: Mapping[str, Any], element_size_mm: float, **options: Any ) -> dict[str, Any]: """Mesh the lumen and wall (:meth:`AortaProcessGeometry.fsi_mesh`).""" return self._geometry_tools.fsi_mesh(geometry, element_size_mm, **options) def _build_stepper( self, geometry: Mapping[str, Any], domain: Any, outlets: Sequence[str], inflow: tuple[np.ndarray, float], *, dt_s: float, calibrate_outlets: bool, **options: Any, ) -> tuple[Any, AortaFlowParameters, dict[str, Any]]: """Build the wall, measure its compliance, calibrate and couple. *options* holds the blood solver ``precision``. """ mesh = domain given = self.flow_parameters # The wall compliance is measured between the input pressures. self.flow_tools.check_input_pressures(given) thickness_mm = float(geometry["wall_thickness_mm"]) young_modulus_pa = ( given.young_modulus_pa * given.wall_thickness_mm / thickness_mm ) if thickness_mm != given.wall_thickness_mm: self.log_info( "Meshing the %.3g mm wall %.3g mm thick with Young's modulus " "%.3g Pa, keeping modulus x thickness", given.wall_thickness_mm, thickness_mm, young_modulus_pa, ) wall = WarpHyperelasticWall( np.asarray(mesh["wall_points_mm"]) * 1e-3, mesh["wall_tetrahedra"], mesh["wall_clamped"], young_modulus=young_modulus_pa, poisson_ratio=given.poisson_ratio, tether_faces=mesh["wall_tether_faces"], tether_stiffness=given.tether_stiffness_pa_m, ) solver: Optional[WarpFlowNS] = None try: wall_compliance = self._wall_compliance( wall, np.asarray(mesh["points_mm"]) * 1e-3, mesh["boundary_faces"], mesh["boundary_regions"], ( given.diastolic_pressure_mmhg * ProcessFlowBoundaries.MMHG_PA, given.systolic_pressure_mmhg * ProcessFlowBoundaries.MMHG_PA, ), ) flow_parameters = self._calibrate( *inflow, dt_s, calibrate_outlets, wall_compliance_m3_pa=wall_compliance ) solver = self._navier_stokes( mesh, geometry["ports"], outlets, flow_parameters, dt_s, options["precision"], ) coupled = WarpFluidStructureNS( solver, wall, mesh["boundary_regions"], reference_pressure=flow_parameters.initial_pressure_pa, **self.COUPLING, ) except BaseException: wall.close() if solver is not None: solver.close() raise run: dict[str, Any] = { "spacing_mm": float(geometry["labelmap"].GetSpacing()[0]), "wall_thickness_mm": given.wall_thickness_mm, "meshed_wall_thickness_mm": thickness_mm, "meshed_young_modulus_pa": young_modulus_pa, "reference_pressure_mmhg": flow_parameters.initial_pressure_pa / ProcessFlowBoundaries.MMHG_PA, "coupling": dict(self.COUPLING), "wall_nodes": len(wall.dof_points), "wall_tetrahedra": len(wall.tetrahedra), "wall_clamped_points": int(np.sum(mesh["wall_clamped"])), "nonconforming_faces": mesh.get("nonconforming_faces"), "total_compliance_m3_pa": flow_parameters.total_compliance_m3_pa + wall_compliance, "wall_compliance_m3_pa": wall_compliance, } return coupled, flow_parameters, run def _frame_extras(self, stepper: Any) -> dict[str, np.ndarray]: """Return the frame's displacement of the lumen mesh points.""" return {"displacement_m": stepper.mesh_displacement.copy()} def _step_message(self, step: Mapping[str, Any]) -> str: """Return the wall motion and coupling statistics of a step.""" return ( f"wall={step['max_displacement_m'] * 1e3:.3f} mm " f"coupling={step['coupling_iterations']}" f"{'' if step['converged'] else ' (not converged)'} " f"r={step['coupling_residual']:.1e} GMRES={step['gmres_iterations']}" ) def _wall_compliance( self, wall: WarpHyperelasticWall, points_m: np.ndarray, faces: np.ndarray, regions: np.ndarray, pressures_pa: tuple[float, float], ) -> float: """Return the lumen's dV/dP in m^3/Pa from a quasi-static wall solve. The wall is prestressed to the first pressure, so that the imaged lumen is its shape there, and solved under the second, uniform over the lumen's wall faces; the compliance is the secant between the two. The ports do not move. """ started = time.perf_counter() low_pa, high_pa = pressures_pa lumen_wall = faces[regions == 0] moving = np.unique(lumen_wall) distance, to_wall = cKDTree(wall.points).query(points_m[moving]) if np.max(distance) > 1e-9: raise ValueError("Lumen wall points are not wall points") index = np.zeros(len(points_m), dtype=np.int64) index[moving] = to_wall wall_faces = index[lumen_wall] wall.prestress(wall.pressure_load(wall_faces, low_pa)) solve = wall.solve( wall.pressure_load(wall_faces, high_pa), tolerance=self.WALL_TOLERANCE ) if solve["residual"] > self.WALL_TOLERANCE: raise RuntimeError( "The static wall solve for the compliance did not converge: " f"relative residual {solve['residual']:.3g} after " f"{solve['iterations']} iterations" ) moved = points_m.copy() moved[moving] += wall.displacement()[to_wall] volumes = [ ProcessVolumetricMesh.enclosed_volume(points_m, faces), ProcessVolumetricMesh.enclosed_volume(moved, faces), ] change_m3 = volumes[1] - volumes[0] compliance = change_m3 / (high_pa - low_pa) self.log_info( "Wall compliance %.4g m^3/Pa: lumen volume %.2f mL grows %.3f mL " "from %.1f to %.1f mmHg, displacement up to %.3f mm; %d Newton " "iterations, %.1f s", compliance, volumes[0] * 1e6, change_m3 * 1e6, low_pa / ProcessFlowBoundaries.MMHG_PA, high_pa / ProcessFlowBoundaries.MMHG_PA, float(np.linalg.norm(wall.displacement()[to_wall], axis=1).max()) * 1e3, solve["iterations"], time.perf_counter() - started, ) return float(compliance) # ─────────────────────────── Summary ─────────────────────────────────── def _summarize( self, result: Mapping[str, Any], near_outlet: np.ndarray ) -> dict[str, Any]: """Return the metrics of the last simulated beat (see :meth:`process`).""" summary = super()._summarize(result, near_outlet) flow_parameters = result["flow_parameters"] series = result["series"] last = self._last_beat(series["times_s"], 60.0 / flow_parameters.heart_rate_bpm) inflow = np.asarray(series["inlet_flow_m3_s"]) outflow = sum(np.asarray(v) for v in series["outlet_flow_m3_s"].values()) net = ( np.asarray(series["root_flow_m3_s"]) - outflow - np.asarray(series["volume_rate_m3_s"]) ) summary["max_net_port_flow_relative"] = float( np.abs(net[last]).max() / inflow[last].max() ) # Outward displacement of the lumen's wall points, on their normals. mesh = result["mesh"] points = np.asarray(mesh["points_mm"]) wall_faces = np.asarray(mesh["boundary_faces"])[ np.asarray(mesh["boundary_regions"]) == 0 ] corners = points[wall_faces] area = np.cross(corners[:, 1] - corners[:, 0], corners[:, 2] - corners[:, 0]) normals = np.zeros_like(points) for k in range(3): np.add.at(normals, wall_faces[:, k], area) moving = np.unique(wall_faces) normals = normals[moving] / np.linalg.norm(normals[moving], axis=1)[:, None] displacement = np.asarray(result["frames"]["displacement_m"])[:, moving] outward = np.einsum("fpk,pk->fp", displacement, normals) volume = np.asarray(series["volume_m3"])[last] root = np.asarray(series["root_pressure_pa"])[last] # Lumen compliance: least-squares slope of volume against root pressure. lumen_compliance = float(np.polyfit(root, volume, 1)[0]) distensibility_pa = lumen_compliance / float(volume.mean()) coupling = np.asarray(series["coupling_iterations"]) converged = np.asarray(series["converged"], dtype=bool) run = result["run"] summary.update( { "total_compliance_m3_pa": run["total_compliance_m3_pa"], "wall_compliance_m3_pa": run["wall_compliance_m3_pa"], "peak_wall_displacement_mm": float( np.asarray(series["max_displacement_m"])[last].max() * 1e3 ), "peak_outward_wall_displacement_mm": float(outward.max() * 1e3), "lumen_volume_ml": float(volume.min() * 1e6), "volume_strain_percent": float(np.ptp(volume) / volume.min() * 100), "diameter_strain_percent": float( (np.sqrt(volume.max() / volume.min()) - 1) * 100 ), "lumen_compliance_m3_pa": lumen_compliance, "distensibility_per_mmhg": distensibility_pa * ProcessFlowBoundaries.MMHG_PA, "bramwell_hill_wave_speed_m_s": float( 1.0 / np.sqrt(flow_parameters.density_kg_m3 * distensibility_pa) ), "mean_coupling_iterations": float(coupling.mean()), "max_coupling_iterations": int(coupling.max()), "capped_coupling_steps": int( np.sum(coupling > run["coupling"]["max_iterations"]) ), "unconverged_coupling_steps": int(np.sum(~converged)), "max_coupling_residual": float( np.max(np.asarray(series["coupling_residual"])) ), "max_coupling_residual_m": float( np.max(np.asarray(series["coupling_residual_m"])) ), } ) return summary