Metadata-Version: 2.4
Name: crossbridge
Version: 0.7.0
Summary: Cardiac crossbridge models
Author-email: Henrik Finsberg <henriknf@simula.no>
License-Expression: MIT
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: scipy
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Requires-Dist: gotranx>=2.4.0; extra == "test"
Provides-Extra: fast
Requires-Dist: numba; extra == "fast"
Provides-Extra: demos
Requires-Dist: matplotlib; extra == "demos"
Requires-Dist: zero-mech; extra == "demos"
Requires-Dist: gotranx>=2.4.0; extra == "demos"
Requires-Dist: scipy; extra == "demos"
Requires-Dist: tqdm; extra == "demos"
Requires-Dist: numba; extra == "demos"
Provides-Extra: docs
Requires-Dist: jupyter-book<2.0; extra == "docs"
Requires-Dist: jupytext; extra == "docs"
Requires-Dist: crossbridge[demos]; extra == "docs"
Requires-Dist: sphinxcontrib-bibtex; extra == "docs"
Provides-Extra: dev
Requires-Dist: pdbpp; extra == "dev"
Requires-Dist: ipython; extra == "dev"
Requires-Dist: bump-my-version; extra == "dev"
Requires-Dist: pre-commit; extra == "dev"
Provides-Extra: all
Requires-Dist: crossbridge[test]; extra == "all"
Requires-Dist: crossbridge[fast]; extra == "all"
Requires-Dist: crossbridge[demos]; extra == "all"
Requires-Dist: crossbridge[docs]; extra == "all"
Requires-Dist: crossbridge[dev]; extra == "all"
Dynamic: license-file

# crossbridge

**crossbridge** is a Python library for simulating cardiac myofilament activation and crossbridge dynamics.

## The Mathematical Models
`crossbridge` implements several reduced-order models of cardiac myofilament activation
([RDQ18](docs/models/rdq18.md), [RDQ20-MF](docs/models/rdq20mf.md), [Land2017](docs/models/land2017.md),
[Lewalle2024](docs/models/lewalle2024.md), [Tewari2016](docs/models/tewari2016.md),
[Marzban2020](docs/models/marzban2020.md), [Musgrave2025](docs/models/musgrave2025.md), [Rice2008](docs/models/rice2008.md), [Tran2010](docs/models/tran2010.md)), all sharing a common interface so that a coupled electromechanics simulation can swap between them with minimal code
changes. See [docs/models](docs/models/index.md) for a description and reference for each model.

## Installation

You can install the base package and its dependencies using `pip`.

To install the library you can use pip:
```bash
python3 -m pip install crossbridge
```

To install with optional dependencies (for running demos, tests, or building docs):
```bash
python3 -m pip install "crossbridge[demos]"  # Installs scipy, matplotlib, gotranx, numba, zero-mech, etc.
python3 -m pip install "crossbridge[test]"   # Installs pytest and coverage tools
python3 -m pip install "crossbridge[docs]"   # Installs jupyter-book and sphinx plugins
python3 -m pip install "crossbridge[all]"    # Installs everything
```

### AI agent skill

crossbridge ships an [agent skill](https://agentskills.io) that helps you choose and run a
contraction model: it checks your environment, asks what you need the tension for (tissue, calcium
source, length, scale), proposes a model and a coupling scheme, and builds and checks the
simulation.

- **Claude Code:** `/plugin marketplace add ComputationalPhysiology/crossbridge` then
  `/plugin install crossbridge@crossbridge`
- **Other agents** (Codex, Gemini CLI, GitHub Copilot, Cursor, OpenCode, ...):
  `npx skills add ComputationalPhysiology/crossbridge --skill crossbridge-models`
- **Manually:** copy [`plugins/crossbridge/skills/crossbridge-models`](https://github.com/ComputationalPhysiology/crossbridge/tree/main/plugins/crossbridge/skills/crossbridge-models)
  into your agent's skills directory (e.g. `~/.claude/skills/`).

See the section "Using crossbridge with an AI agent" below.

## Documentation
Documentation is available at [https://computationalphysiology.github.io/crossbridge](https://computationalphysiology.github.io/crossbridge). It includes a user guide, API reference, and model background/references.

## Basic Usage
Every model takes the intracellular calcium concentration [µM] and the sarcomere length [µm], and
returns the active tension [kPa]. One instance holds `num_cells` independent cells (for example the
integration points of a mesh), vectorized with NumPy. An isometric twitch with `Land2017`:

```python
import numpy as np
import matplotlib.pyplot as plt
from crossbridge import Land2017, calcium_trace

dt = 1e-3  # [s]
t = np.arange(0, 1, dt)
Ca = calcium_trace(t)  # a synthetic calcium transient [µM]

model = Land2017(num_cells=1)
Ta = np.zeros_like(t)
for i, Cai in enumerate(Ca):
    model.advance_step(dt, Cai, 2.2)  # dt [s], calcium [µM], sarcomere length [µm]
    Ta[i] = model.get_active_tension()[0]  # [kPa], one value per cell

fig, ax = plt.subplots(2, 1, sharex=True)
ax[0].plot(t, Ca)
ax[0].set_ylabel("Calcium [µM]")
ax[1].plot(t, Ta)
ax[1].set_ylabel("Active tension [kPa]")
ax[1].set_xlabel("Time [s]")
plt.show()
```

The tension peaks at 4.8 kPa. `Land2017`'s defaults are its calibration to skinned cells; for an
intact cell's calcium transient use `Land2017.whole_organ_parameters()` or
`Land2017.torord_land_parameters()` (`Land2017(num_cells=1, params=...)`). Any other model is used
the same way, by class or by name (`crossbridge.get_model("Rice2008")`); a `params` key that is not
in the model's `default_parameters()` raises `ValueError`.

## Coupling to Electrophysiology and Mechanics
In a coupled simulation the calcium comes from a cell model and the sarcomere length from a
mechanics solver, both as a scalar or as one value per cell, and `advance_step` is called once per
time step of the larger loop (it sub-steps internally, so 1 ms is fine):

```python
model = Land2017(num_cells=num_cells, params=Land2017.torord_land_parameters())
for t in time_steps:
    Cai = 1000.0 * compute_calcium(t)  # a cell model's calcium, mM -> µM
    model.advance_step(dt, Cai, SL_ref * stretch)  # dt in s, not ms
    Ta = model.get_active_tension()
    Ka = model.get_active_stiffness()
    stretch = solve_mechanics(Ta, Ka)
```

Cell models (for example from [gotranx](https://finsberg.github.io/gotranx) or
[odezoo](https://computationalphysiology.github.io/odezoo)) usually work in ms and mM; convert at
the boundary. Pass the active stiffness `Ka` to the mechanics solve as well: a segregated scheme
that holds the tension fixed oscillates, and more so at smaller time steps. For two-way calcium
coupling, `get_calcium_binding_rate()` gives the troponin flux to hand back to the cell model. See
the [documentation](https://computationalphysiology.github.io/crossbridge) for both.

## Examples & Demos

The `demo/` folder contains several scripts demonstrating how to couple the `crossbridge` model to different physics scales. See [demos](demo/index.md) for a description of each demo and how to run them.


## Testing and Development
We use `pytest` for unit testing. To run the test suite and check code coverage:
```bash
pytest
```
To run the pre-commit linters (Ruff and MyPy):
```bash
pre-commit run --all-files
```

## Using crossbridge with an AI agent

The `crossbridge-models` skill (installed as above) turns a coding agent into a guide for
contraction models. It

- checks that crossbridge is installed (by its models, not its version string), and what else the
  plan needs (numba, gotranx and a cell model, zero-mech, dolfinx with fenicsx-pulse or
  simcardemsx);
- asks you, one question at a time, about your goal, the tissue, where the calcium and the
  sarcomere length come from, and how many cells;
- proposes alternatives with measured trade-offs (which of the nine models, at what cost; which
  parameter set; segregated coupling with the active stiffness or a monolithic one; one- or
  two-way calcium coupling; the class or code generated from its `.ode` file) and recommends one;
- writes the simulation as a script, checks the tension, its convergence in `dt` and its physics,
  and hands over the numbers with the units at every boundary;
- knows the common pitfalls (calcium in mM, skinned-cell default parameters, `Ta_max` ignored by
  most models, misspelled parameters ignored, segregated coupling without `Ka`, troponin buffered
  twice);
- never installs anything without asking.

Describe what you want, for example

> Run an isometric twitch with the Land model driven by a ToR-ORd calcium transient

and the agent loads the skill on its own; or invoke it explicitly (`/crossbridge:crossbridge-models`
in Claude Code, `/crossbridge-models` if you copied the folder). Update it with
`/plugin marketplace update crossbridge` or `npx skills update`.

## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
