Create a full-heart model#

This example shows how to process a case from Rodero et al. (2021) into a simulation-ready heart model.

Perform the required imports#

Import the required modules and set relevant paths, including that of the working directory and generated model.

import json
import os
from pathlib import Path

import ansys.health.heart.models as models
from ansys.health.heart.pre.database_utils import get_compatible_input
from ansys.health.heart.utils.download import download_case_from_zenodo, unpack_case

# specify a download directory
download_folder = Path.home() / "pyansys-heart" / "downloads"

# Download a compatible case from the Zenodo database.
tar_file = download_case_from_zenodo("Rodero2021", 1, download_folder, overwrite=False)
# Unpack the case to get the input CASE or VTK file.
case_file = unpack_case(tar_file)

# Specify the working directory. This code uses the directory of the CASE file.
workdir = os.path.join(os.path.dirname(case_file), "FullHeart")

if not os.path.isdir(workdir):
    os.makedirs(workdir)

# Specify paths to the model, input, and part definitions.
path_to_model = os.path.join(workdir, "heart_model.vtu")
path_to_input = os.path.join(workdir, "input_model.vtp")
path_to_part_definitions = os.path.join(workdir, "part_definitions.json")

Note

You can also manually download the CASE or VTK files from the Strocchi 2020 and Rodero 2021 databases. For more information, see:

Alternatively, you can simply click one of the buttons at the bottom of this page to download a CASE file for the Rodero 2021 database in an IPYNB, PY, or ZIP format.

Convert the VTK file to a compatible input format#

input_geom, part_definitions = get_compatible_input(
    case_file, model_type="FullHeart", database="Rodero2021"
)

# Note that the input model and part definitions can be saved for later use.
# Save input geometry and part definitions.
input_geom.save(path_to_input)
with open(path_to_part_definitions, "w") as f:
    json.dump(part_definitions, f, indent=True)

Create a heart model#

Create a full-heart model.

model = models.FullHeart(working_directory=workdir)

# Load input model generated in an earlier step.
model.load_input(input_geom, part_definitions, "surface-id")

# Mesh the volume of all structural parts.
model.mesh_volume(use_wrapper=True, global_mesh_size=2.0, _global_wrap_size=2.0)

# Update the model and extract the required anatomical features.
model.update()

# Optionally save the simulation mesh as a VTK object for "offline" inspection.
model.mesh.save(os.path.join(model.workdir, "simulation-mesh.vtu"))
model.save_model(os.path.join(model.workdir, "heart_model.vtu"))

# Print some information about the processed model.
print(model)

# Print part names.
print(model.part_names)
GENERAL:
  total_num_tets: 330412
  total_num_nodes: 72001
PARTS:
  Left ventricle:
    num_tets: 140746
    SURFACES:
      Left ventricle endocardium:
        num_faces: 7438
      Left ventricle epicardium:
        num_faces: 8190
      Left ventricle endocardium septum:
        num_faces: 0
    CAPS:
      mitral-valve:
        num_nodes: 63
      aortic-valve:
        num_nodes: 41
  Right ventricle:
    num_tets: 67126
    SURFACES:
      Right ventricle endocardium:
        num_faces: 8877
      Right ventricle epicardium:
        num_faces: 9324
      Right ventricle endocardium septum:
        num_faces: 2771
    CAPS:
      tricuspid-valve:
        num_nodes: 92
      pulmonary-valve:
        num_nodes: 56
  Septum:
    num_tets: 42665
    SURFACES: {}
    CAPS: {}
  Left atrium:
    num_tets: 31344
    SURFACES:
      Left atrium endocardium:
        num_faces: 5661
      Left atrium epicardium:
        num_faces: 4389
    CAPS:
      mitral-valve-atrium:
        num_nodes: 44
      right-superior-pulmonary-vein:
        num_nodes: 39
      left-atrium-appendage:
        num_nodes: 34
      left-superior-pulmonary-vein:
        num_nodes: 31
      right-inferior-pulmonary-vein:
        num_nodes: 27
      left-inferior-pulmonary-vein:
        num_nodes: 24
  Right atrium:
    num_tets: 31557
    SURFACES:
      Right atrium endocardium:
        num_faces: 7104
      Right atrium epicardium:
        num_faces: 5572
    CAPS:
      tricuspid-valve-atrium:
        num_nodes: 74
      inferior-vena-cava:
        num_nodes: 34
      superior-vena-cava:
        num_nodes: 30
  Aorta:
    num_tets: 12611
    SURFACES:
      Aorta wall:
        num_faces: 4194
    CAPS: {}
  Pulmonary artery:
    num_tets: 4363
    SURFACES:
      Pulmonary artery wall:
        num_faces: 1355
    CAPS: {}
CAVITIES:
  Left ventricle cavity:
    volume: 120427.92341232538
  Right ventricle cavity:
    volume: 185706.02086772228
  Left atrium cavity:
    volume: 67925.9203447678
  Right atrium cavity:
    volume: 110995.74769401149

['Left ventricle', 'Right ventricle', 'Septum', 'Left atrium', 'Right atrium', 'Aorta', 'Pulmonary artery']

Visualize results#

Visualize and inspect the components of the model by accessing various properties or attributes and invoking methods.

print(f"Volume of LV cavity: {model.left_ventricle.cavity.volume} mm^3")
print(f"Volume of LV cavity: {model.left_atrium.cavity.volume} mm^3")

# Plot the remeshed model.
model.plot_mesh(show_edges=False)

# Plot the endocardial surface of the left ventricle.
model.left_ventricle.endocardium.plot(show_edges=True, color="r")

# Loop over all cavities and plot them in a single window with PyVista.
import pyvista as pv

cavities = pv.PolyData()
for c in model.cavities:
    cavities += c.surface
cavities.plot(show_edges=True)
preprocess fullheart
preprocess fullheart
preprocess fullheart
Volume of LV cavity: 120427.92341232538 mm^3
Volume of LV cavity: 67925.9203447678 mm^3

Total running time of the script: (8 minutes 29.356 seconds)

Gallery generated by Sphinx-Gallery