Skip to content

Backends ​

livn supports three simulation backends, each providing a full implementation of the Env protocol. Select a backend via the LIVN_BACKEND environment variable before importing livn:

sh
export LIVN_BACKEND=brian2     # requires livn[brian2] dependencies
export LIVN_BACKEND=diffrax    # requires livn[diffrax] dependencies
export LIVN_BACKEND=neuron     # requires livn[neuron] dependencies and MPI

When no LIVN_BACKEND is set, livn uses a neutral default backend that provides the full Env interface without running any simulation. This is useful for working with systems, I/O, and datasets without installing a simulation engine. To run actual simulations, set one of the backends above.

All backends share the same user-facing API - you write your simulation code once and switch backends by changing the environment variable.

brian2 ​

brian2 is a lightweight backend suitable for rapid prototyping and small-to-medium systems. It models neurons as point processes using brian2's equation-based description language.

Strengths:

  • Fast setup, no external system libraries needed
  • Good for systems up to ~1,000 neurons on a single machine
  • Includes Izhikevich and Leaky Integrate-and-Fire (LIF) models
python
import os
os.environ["LIVN_BACKEND"] = "brian2"

from livn import make

env = make("EI2")
it, t, iv, v, *_ = env.run(100)

GSL integration ​

By default, brian2 uses the forward Euler method with a small timestep (0.005 ms) for numerical integration. For improved accuracy and performance, you can enable the GNU Scientific Library (GSL) adaptive solver by setting the LIVN_USE_LIBGSL environment variable:

sh
export LIVN_USE_LIBGSL=1

When enabled, the integrator switches to gsl_rkf45 (adaptive Runge-Kutta-Fehlberg 4(5)) with a base timestep of 0.025 ms. The adaptive method automatically adjusts step sizes to maintain accuracy, allowing a larger base timestep while preserving numerical stability for stiff biophysical equations.

This requires GSL to be installed on your system:

sh
# Ubuntu/Debian
sudo apt install libgsl-dev

# macOS
brew install gsl

Jax ​

A JAX-based backend that enables differentiable simulations through Diffrax and Equinox. This allows you to compute exact gradients through the simulation and use gradient-based optimization to learn stimulus parameters, decode neural activity, or train surrogate models end-to-end.

Strengths:

  • End-to-end differentiable: backpropagate through the full simulation
  • GPU-accelerated via JAX
  • JIT-compiled for fast repeated evaluation

Install with:

sh
pip install livn[diffrax]
python
import os
os.environ["LIVN_BACKEND"] = "diffrax"

from livn import make
import jax

env = make("EI2")
# Gradients through the simulation are now available

See the Differentiable Simulation example for a full walkthrough.

NEURON ​

The NEURON backend provides high-fidelity, multi-compartment biophysical simulations with MPI-based parallelism. It integrates with the MiV-Simulator for large-scale network simulations on HPC infrastructure.

Strengths:

  • Detailed biophysical neuron models (multi-compartment, ion channels, calcium dynamics)
  • MPI-parallel: scales to millions of neurons on supercomputers
  • Best choice for generating realistic synthetic data

Requires system-level MPI and HDF5 libraries. See Installation for setup instructions.

sh
pip install livn[neuron]
python
import os
os.environ["LIVN_BACKEND"] = "neuron"

from livn import make

env = make("EI2")

Comparison ​

Featurebrian2DiffraxNEURON
DifferentiableNoYesNo
GPU supportNoYesNo
Multi-compartment modelsNoYesYes
Built-in opsinsNoYes (RhO3c)Yes (RhO3c, RhO6c)
MPI parallelismNoNoYes
Setup complexityLowMediumHigh
Ideal scale≤1,000 neurons≤10,000 neurons≤millions

Released under the MIT License.