Metadata-Version: 2.4
Name: simcardemsx
Version: 0.4.0
Summary: Next generation cardiac electro-mechanics solver based on FEniCSx
Author-email: Henrik Finsberg <henriknf@simula.no>
License-Expression: MIT
Project-URL: Homepage, https://computationalphysiology.github.io/simcardemsx
Project-URL: Documentation, https://computationalphysiology.github.io/simcardemsx
Project-URL: Source, https://github.com/ComputationalPhysiology/simcardemsx
Project-URL: Issues, https://github.com/ComputationalPhysiology/simcardemsx/issues
Keywords: cardiac,electromechanics,finite element,fenicsx,electrophysiology
Classifier: Programming Language :: Python :: 3
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering :: Physics
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Requires-Python: >=3.12.0
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fenics-dolfinx
Requires-Dist: fenicsx-pulse>=0.13.1
Requires-Dist: fenicsx-beat>=0.16.0
Requires-Dist: crossbridge>=0.3.4
Requires-Dist: io4dolfinx[h5py]
Requires-Dist: scipy
Requires-Dist: gotranx>=2.11.0
Requires-Dist: toml
Requires-Dist: pydantic
Requires-Dist: pydantic_pint
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Requires-Dist: cardiac-geometriesx; extra == "test"
Requires-Dist: circulation>=0.4.0; extra == "test"
Requires-Dist: zero-mech>=0.3.0; extra == "test"
Requires-Dist: odezoo; extra == "test"
Provides-Extra: dev
Requires-Dist: pdbpp; extra == "dev"
Requires-Dist: ipython; extra == "dev"
Requires-Dist: bump2version; extra == "dev"
Requires-Dist: mypy; extra == "dev"
Requires-Dist: types-toml; extra == "dev"
Requires-Dist: pre-commit; extra == "dev"
Provides-Extra: demo
Requires-Dist: matplotlib; extra == "demo"
Requires-Dist: cardiac-geometriesx; extra == "demo"
Requires-Dist: fenicsx-ldrb; extra == "demo"
Requires-Dist: ukb-atlas; extra == "demo"
Requires-Dist: circulation>=0.4.0; extra == "demo"
Requires-Dist: gotranx>=2.11.0; extra == "demo"
Requires-Dist: odezoo; extra == "demo"
Requires-Dist: pyvista; extra == "demo"
Requires-Dist: imageio; extra == "demo"
Requires-Dist: io4dolfinx[h5py]; extra == "demo"
Provides-Extra: docs
Requires-Dist: mystmd; extra == "docs"
Requires-Dist: jupytext; extra == "docs"
Requires-Dist: ipykernel; extra == "docs"
Requires-Dist: jupyter-server; extra == "docs"
Requires-Dist: griffe>=2; extra == "docs"
Requires-Dist: zero-mech>=0.3.0; extra == "docs"
Requires-Dist: odezoo; extra == "docs"
Requires-Dist: gotranx>=2.11.0; extra == "docs"
Requires-Dist: pyvista; extra == "docs"
Requires-Dist: imageio; extra == "docs"
Requires-Dist: trame-vtk>=2.11.15; extra == "docs"
Requires-Dist: simcardemsx[demo]; extra == "docs"
Provides-Extra: benchmark
Requires-Dist: pytest-codspeed; extra == "benchmark"
Requires-Dist: simcardemsx[test]; extra == "benchmark"
Provides-Extra: all
Requires-Dist: simcardemsx[test]; extra == "all"
Requires-Dist: simcardemsx[docs]; extra == "all"
Requires-Dist: simcardemsx[dev]; extra == "all"
Requires-Dist: simcardemsx[demo]; extra == "all"
Dynamic: license-file

# simcardemsx

Cardiac electro-mechanics in [FEniCSx](https://fenicsproject.org).

[![One beat of a biventricular heart in a closed-loop circulation](examples/circulation_biv/media/biv_clip.gif)](https://computationalphysiology.github.io/simcardemsx/circulation-biv/)

*One beat of a biventricular heart in a closed-loop circulation, cut along its long axis:
the membrane potential on a fine EP mesh (left) and the displacement on the mechanics mesh
(right). From the [BiV application](https://computationalphysiology.github.io/simcardemsx/circulation-biv/).*

simcardemsx couples a model of cardiac electrophysiology (EP) to a model of tissue
mechanics, with the contraction of each cell in between. It does not solve either
physics itself. It connects two solvers that do:

- [fenicsx-beat](https://github.com/finsberg/fenicsx-beat) for EP (the monodomain
  model, or reaction-eikonal EP);
- [fenicsx-pulse](https://github.com/finsberg/fenicsx-pulse) for the mechanics, from a
  single element to a beating heart in a circulation model.

The cell model, EP and contraction alike, is a single
[gotranx](https://github.com/finsberg/gotranx) `.ode` file. simcardemsx splits it in two
and generates code for each half. The EP half is NumPy code for beat. The contraction
half is UFL, which the mechanics solver can differentiate through, so the contraction
model can be stepped *inside* the mechanics Newton iteration. That removes the
instability the usual staggered coupling suffers from when the tissue is soft
(Regazzoni & Quarteroni, 2021). Models that cannot be written in UFL, such as the NumPy
models of [crossbridge](https://github.com/ComputationalPhysiology/crossbridge), are
coupled with the stabilized scheme of the same paper instead.

📖 **Documentation**: <https://computationalphysiology.github.io/simcardemsx>

## How it fits together

```mermaid
flowchart LR
    zoo["published models<br/>(odezoo) + glue"] -.->|"gotranx compose"| ode
    ode[".ode cell model<br/>EP + contraction"] -->|"split, generate"| ep["EP half<br/>NumPy or C"]
    ode -->|"split, generate"| ufl["contraction half<br/>UFL"]
    ep --> beat["beat: EP<br/>monodomain or reaction-eikonal<br/>on the EP mesh"]
    ufl --> backend["activation backend"]
    xb["crossbridge<br/>NumPy models"] --> backend
    beat <-->|"calcium, crossbridges, … ⇄ stretch, …"| backend
    backend --> pulse["pulse: mechanics<br/>static or dynamic<br/>on the mechanics mesh"]
    pulse <--> circ["0D circulation<br/>or cardiac cycle"]
```

A `SimulationController` takes the time steps: EP steps up to the next mechanics step,
what crosses is moved between the two meshes, and the mechanics is solved with the
contraction model inside its Newton iteration. The
[user guide](https://computationalphysiology.github.io/simcardemsx/overview/) explains each piece.

## Features

- One `.ode` file for the whole cell, written by hand or composed from published models
  with gotranx, and split anywhere between EP and contraction. Which variables cross
  between the two sides is read off the generated code, not configured by hand.
- Separate meshes and time steps for EP and mechanics, such as a fine EP mesh nested in a
  coarse mechanics mesh.
- EP from beat: the monodomain model or reaction-eikonal EP, activated by a stimulus, an
  eikonal solve or Durrer's endocardial sequence, with the cell model stepped in NumPy or
  in compiled C.
- Three coupling schemes: monolithic (the default), stabilized segregated, and the naive
  segregated scheme for comparison. The contraction model is the `.ode` file's own, or
  one of crossbridge's.
- Static and dynamic mechanics, prestressing to the unloaded state, closed-loop
  circulation (Regazzoni's 0D circuit) and open-loop cardiac cycles, through pulse.
- Rollback of a failed time step, and checkpoints that a run continues from in a new
  process (bit for bit under pulse's default direct solver).

## Gallery

Each image links to the page of the application that made it.

| | |
| --- | --- |
| [![A slab contracting under each of three splits of the cell model](examples/slab/media/splits.gif)](https://computationalphysiology.github.io/simcardemsx/slab/) | [![A Rodero biventricular heart through the cardiac cycle](examples/rodero_05/media/rodero_heart.gif)](https://computationalphysiology.github.io/simcardemsx/rodero/) |
| **[Slab](https://computationalphysiology.github.io/simcardemsx/slab/)**: ToR-ORd + Land composed from published models and split three ways, coloured by the active tension. | **[Rodero BiV](https://computationalphysiology.github.io/simcardemsx/rodero/)**: a heart from a virtual cohort through the five phases of the cardiac cycle, with reaction-eikonal activation. |

[![On one element, the error of each coupling scheme against a monolithic reference as the time step shrinks](examples/scheme_comparison/media/element_static-zetasplit_error_vs_dt.png)](https://computationalphysiology.github.io/simcardemsx/scheme-comparison/)

**[Coupling schemes](https://computationalphysiology.github.io/simcardemsx/scheme-comparison/)**: on one soft element, the error of each
scheme against a monolithic reference as the time step shrinks. The naive segregated scheme
(open squares) fails at every time step, while the stabilized one converges at first
order.

## Installation

simcardemsx needs [DOLFINx](https://github.com/FEniCS/dolfinx), which is not
installable from PyPI on its own. The simplest way to get it is the DOLFINx container:

```bash
docker run -ti -v $(pwd):/home/shared -w /home/shared ghcr.io/fenics/dolfinx/dolfinx:stable
```

Inside it, install simcardemsx with pip:

```bash
python3 -m pip install simcardemsx
```

or from source, with the dependencies of the examples:

```bash
git clone https://github.com/ComputationalPhysiology/simcardemsx.git
cd simcardemsx
python3 -m pip install -e ".[demo]"
```

The extras are `demo` (the examples), `test`, `docs` and `dev`.

## A first look

Generate the two halves of a cell model and see what crosses between them:

```python
from pathlib import Path

from simcardemsx.ode_model import load_ode_modules

odefile = Path("examples/odefiles/ToRORd_dynCl_endo_zetasplit.ode")
modules = load_ode_modules(odefile, Path("generated_odes"))

print(modules.mechanics.missing)  # what contraction needs from EP: {'XS': 0, 'XW': 1}
print(modules.ep.missing)  # what EP needs back: {'Zetas': 0, 'Zetaw': 1}
```

The contraction half becomes the active stress of a pulse model:

```python
from simcardemsx.backends import GeneratedActivation

backend = GeneratedActivation(modules.mechanics, mesh, f0, quadrature_degree=4)
model = pulse.CardiacModel(material=material, active=backend, compressibility=compressibility)
```

and `SimulationController` steps EP and mechanics together:

```python
from simcardemsx.controller import SimulationController

controller = SimulationController(
    mechanics=problem,  # a pulse.StaticProblem or DynamicProblem using `model`
    ep_solver=ep_solver,  # a beat.SplittingSolver
    backend=backend,
    ode_modules=modules,
    dt_mech=1.0,  # ms
    dt_ep=0.05,  # ms
)
while controller.t < 200.0:
    controller.step()
```

The [tutorials](https://computationalphysiology.github.io/simcardemsx/cell-model/) build this up step by step, from one cell to a
coupled slab.

## Examples

[`examples/`](https://github.com/ComputationalPhysiology/simcardemsx/tree/main/examples) holds eight short tutorials, executed when the
documentation is built, and four larger applications:

- [an electro-mechanical slab](https://computationalphysiology.github.io/simcardemsx/slab/), for any split, scheme or contraction model;
- [a biventricular heart from the Rodero cohort](https://computationalphysiology.github.io/simcardemsx/rodero/), driven through the
  cardiac cycle;
- [a convergence study of the coupling schemes](https://computationalphysiology.github.io/simcardemsx/scheme-comparison/);
- [a biventricular heart beating in a closed-loop circulation](https://computationalphysiology.github.io/simcardemsx/circulation-biv/).

## Development

```bash
python3 -m pip install -e ".[dev,test]"
python3 -m pytest                 # the test suite
python3 -m pytest -m "not slow"   # the fast part of it
pre-commit run --all              # ruff, mypy, cspell, …
```

The documentation is built with [MyST](https://mystmd.org). See
[`docs/README.md`](https://github.com/ComputationalPhysiology/simcardemsx/blob/main/docs/README.md).
[`CONTRIBUTING.md`](https://github.com/ComputationalPhysiology/simcardemsx/blob/main/CONTRIBUTING.md)
describes the development environment and how to open a pull request.

## References

- F. Regazzoni and A. Quarteroni. An oscillation-free fully staggered algorithm for
  velocity-dependent active models of cardiac mechanics. *Computer Methods in Applied
  Mechanics and Engineering* 373, 113506 (2021).
- J. Tomek et al. Development, calibration, and validation of a novel human ventricular
  myocyte model in health, disease, and drug block. *eLife* 8, e48890 (2019).
- S. Land et al. A model of cardiac contraction based on novel measurements of tension
  development in human cardiomyocytes. *Journal of Molecular and Cellular Cardiology*
  106, 68–83 (2017).

## Authors

Henrik Finsberg (<henriknf@simula.no>)

## License

MIT. See [LICENSE](https://github.com/ComputationalPhysiology/simcardemsx/blob/main/LICENSE).
