

fenicsx-beat
Cardiac electrophysiology research uses computational modeling to study heart rhythm disorders and test therapies.
This tool, fenicsx-beat, is a cardiac electrophysiology simulator built specifically for the FEniCSx platform. It provides a dedicated and easy-to-use tool for researchers already using FEniCSx to perform simulations based on the Monodomain model.
Left: a spiral wave in a tissue slab (demo). Right: one beat (activation and repolarization) of a heart from the UK Biobank atlas, with many small random stimuli on the endocardium mimicking the Purkinje network (demo).
Install
You can install the library with pip
python3 -m pip install fenicsx-beat
or with conda
conda install -c conda-forge fenicsx-beat
Note that installing with pip requires FEniCSx already installed
Also that to run most of the examples you will need to install additional dependencies which can be done using the command
python3 -m pip install fenicsx-beat[demos]
Claude Code skill
fenicsx-beat also ships a Claude Code plugin with a skill that helps
you set up and run simulations: it asks what you want to simulate, suggests alternatives, and writes
and checks a beat config or script for you. In Claude Code, run
/plugin marketplace add finsberg/fenicsx-beat
/plugin install fenicsx-beat@fenicsx-beat
or copy the folder plugins/fenicsx-beat/skills/beat-simulation
into ~/.claude/skills/. See "Using fenicsx-beat with Claude Code" below.
Getting started
The following minimal example demonstrates simulating the Monodomain model on a unit square domain using a modified FitzHugh-Nagumo model
import shutil
import matplotlib.pyplot as plt
import numpy as np
from mpi4py import MPI
import dolfinx
import ufl
import beat
# MPI communicator
comm = MPI.COMM_WORLD
# Create mesh
mesh = dolfinx.mesh.create_unit_square(comm, 32, 32, dolfinx.cpp.mesh.CellType.triangle)
# Create a variable for time
time = dolfinx.fem.Constant(mesh, dolfinx.default_scalar_type(0.0))
# Define forward euler scheme for solving the ODEs
# This just needs to be a function that takes the current time, states, parameters and dt
# and returns the new states
def fitzhughnagumo_forward_euler(t, states, parameters, dt):
s, v = states
(
c_1,
c_2,
c_3,
a,
b,
v_amp,
v_rest,
v_peak,
stim_amplitude,
stim_duration,
stim_start,
) = parameters
i_app = np.where(
np.logical_and(t > stim_start, t < stim_start + stim_duration),
stim_amplitude,
0,
)
values = np.zeros_like(states)
ds_dt = b * (-c_3 * s + (v - v_rest))
values[0] = ds_dt * dt + s
v_th = v_amp * a + v_rest
I = -s * (c_2 / v_amp) * (v - v_rest) + (((c_1 / v_amp**2) * (v - v_rest)) * (v - v_th)) * (
-v + v_peak
)
dV_dt = I + i_app
values[1] = v + dV_dt * dt
return values
# Define space for the ODEs
ode_space = dolfinx.fem.functionspace(mesh, ("P", 1))
# Define parameters for the ODEs
a = 0.13
b = 0.013
c1 = 0.26
c2 = 0.1
c3 = 1.0
v_peak = 40.0
v_rest = -85.0
stim_amplitude = 100.0
stim_duration = 1
stim_start = 0.0
# Collect the parameter in a numpy array
parameters = np.array(
[
c1,
c2,
c3,
a,
b,
v_peak - v_rest,
v_rest,
v_peak,
stim_amplitude,
stim_duration,
stim_start,
],
dtype=np.float64,
)
# Define the initial states
init_states = np.array([0.0, -85], dtype=np.float64)
# Specify the index of state for the membrane potential
# which will also inform the PDE solver later
v_index = 1