A high-performance Python implementation of the Bloch equation solver originally developed by Brian Hargreaves at Stanford University. This package provides a fast C-based core with Python bindings, parallel processing support, and an interactive GUI for MRI pulse sequence simulation.
The Bloch Simulator can be accessed from 3 different directions:
Standalone GUI:
Download and install built package (.exe, .app) here. Features:
- High performance GUI for nice visualization
- Easily modify and live update simulation:
- Sample Parameters: T1, T2, Polarization level (enables simulation of hyperpolarized magnetization), etc.
- RF Pulses: Duration, Tip Angle, B1, Frequency, Phase, Pulse type such as sinc, Gaussian, Adiabatic Half Passage, etc.
- Pulse Sequence: Blockpulse, Spin Echo, Slice Selection, SSFP, Inversion Recovery, etc.
- Export:
- Python compatible simulation results (.npy, .npz, .hdf5)
- Automically generated IPython (.ipynb) notebooks for repeatility and modifications
- Animations (.mp4, .gif) and figures (.svg, .png)
Python package blochsimulator
Install bloch simulator package from pypi.org via pip instal blochsimulator. Features:
- Access simulation capabilites from IPython notebooks and python scripts
- Highly customizable simulations
Online GUI
Access here online. Features:
- No installation required
- Simple UI, live simulation
- Simulate RF Pulse Parameters and Slice Selection Parameters
For detailed instructions on installation, GUI features, and Python API usage, please refer to the User Guide.
Recommended for most users. Package is availabe on pypi.org.
pip install blochsimulatorRecommended for researchers and developers.
To run from source, you need Python and a C Compiler installed.
👇 Click here for detailed setup instructions (Windows, macOS, Linux)
- Windows:
- Download the installer from python.org.
- Important: During installation, check the box "Add Python to PATH".
- macOS:
- Download from python.org OR use Homebrew:
brew install python.
- Download from python.org OR use Homebrew:
- Linux:
- Usually pre-installed. If not:
sudo apt install python3 python3-pip(Ubuntu/Debian) orsudo dnf install python3(Fedora).
- Usually pre-installed. If not:
Required to build the fast simulation core.
- Windows:
- Install Visual Studio Build Tools (free).
- Download from visualstudio.microsoft.com.
- In the installer, select "Desktop development with C++".
- macOS:
- Open Terminal and run:
xcode-select --install. - (Optional but recommended for speed) Install OpenMP:
brew install libomp.
- Open Terminal and run:
- Linux:
- Install GCC:
sudo apt install build-essential(Ubuntu/Debian) orsudo dnf groupinstall "Development Tools"(Fedora).
- Install GCC:
Steps:
- Navigate to the directory where you want to install the GUI in your terminal.
cd /path/to/the/directory/ - Clone or download the repository.
git clone git@github.com:LucaNagel/bloch_sim_gui.git
- Or if that fails:
git clone https://github.com/LucaNagel/bloch_sim_gui.git
- Navigate to the cloned repository.
cd bloch_sim_gui/ - Install in editable mode:
pip install -e .
Test the installation:
from blochsimulator import BlochSimulator, TissueParameters
sim = BlochSimulator()
tissue = TissueParameters.gray_matter(3.0)
print(f"T1: {tissue.t1:.3f}s, T2: {tissue.t2:.3f}s")No installation required. Recommended for non-technical users.
Download the most reason version for your OS in Releases
In case of the MacOS app, this requires you to manually remove the quarantine flag that macOS puts on the downloaded app. Only perform this step if you trust the distributor.
- Unzip the file and move
BlochSimulator.appto your Applications folder. - Launch BlochSimulator from your Applications folder and dimiss the warning.
- Go to System Settings > Privacy & Security, scroll down to the Security section, here you should see a message "BlochSimulator.app was blocked...". Click "Open Anyway".
- Launch BlochSimulator from your Applications folder.
Alternative Activation:
- Unzip the file and move
BlochSimulator.appto your Applications folder. - Crucial Step: Open Terminal and run this command to fix the "App is damaged" error:
xattr -cr /Applications/BlochSimulator.app
- Launch BlochSimulator from your Applications folder.
You can launch the interactive GUI directly from a cell in your Jupyter Notebook.
# 0. Install from PyPI (run once) if not done before
!pip install blochsimulator
# 1. Launch GUI
!blochsimulator-guiNote: This works if Jupyter is running on your local machine. It will not work on headless remote servers or Google Colab.
Once installed, you can launch the GUI from any terminal or shell:
blochsimulator-guiFeatures:
- Design RF pulses (rectangular, sinc, Gaussian)
- Configure tissue parameters (T1, T2)
- Select pulse sequences (spin echo, gradient echo, etc.)
- Real-time 3D magnetization visualization
- Signal analysis and frequency spectra
import numpy as np
from blochsimulator import BlochSimulator, TissueParameters
# Create simulator
sim = BlochSimulator(use_parallel=True, num_threads=4)
# Define tissue parameters
tissue = TissueParameters(
name="Gray Matter",
t1=1.33, # seconds
t2=0.083 # seconds
)
# Create a simple 90-degree pulse
ntime = 100
dt = 1e-5 # 10 microseconds
time = np.arange(ntime) * dt
b1 = np.zeros(ntime, dtype=complex)
b1[0] = 0.0235 # 90-degree hard pulse
gradients = np.zeros((ntime, 3)) # No gradients
# Run simulation
result = sim.simulate(
sequence=(b1, gradients, time),
tissue=tissue,
mode=2 # Time-resolved output
)
# Plot results
sim.plot_magnetization()from blochsimulator import BlochSimulator, SpinEcho, TissueParameters
sim = BlochSimulator()
# Create spin echo sequence
sequence = SpinEcho(te=20e-3, tr=500e-3) # 20ms TE, 500ms TR
# Simulate white matter
tissue = TissueParameters.white_matter(3.0)
# Run simulation with multiple frequencies (T2* effects)
frequencies = np.linspace(-50, 50, 11) # Hz
result = sim.simulate(sequence, tissue, frequencies=frequencies)
# Access magnetization components
mx, my, mz = result['mx'], result['my'], result['mz']
signal = result['signal']from blochsimulator import design_rf_pulse
# Design a sinc pulse
b1, time = design_rf_pulse(
pulse_type='sinc',
duration=2e-3, # 2 ms
flip_angle=180, # degrees
time_bw_product=4, # Time-bandwidth product
npoints=200
)
# Apply phase
phase = np.pi/4 # 45 degrees
b1_phased = b1 * np.exp(1j * phase)# Simulate multiple positions and frequencies in parallel
positions = np.random.randn(100, 3) * 0.01 # Random positions in 1cm cube
frequencies = np.linspace(-200, 200, 41) # 41 frequencies
result = sim.simulate(
sequence=sequence,
tissue=tissue,
positions=positions,
frequencies=frequencies,
mode=0 # Endpoint only (faster)
)
# Result shape: (100 positions, 41 frequencies)
print(f"Signal shape: {result['signal'].shape}")Pre-defined sequences are available:
from blochsimulator import SpinEcho, GradientEcho
# Spin Echo
se = SpinEcho(te=30e-3, tr=1.0)
# Gradient Echo
gre = GradientEcho(te=5e-3, tr=10e-3, flip_angle=30)
# Compile to waveforms
b1, gradients, time = se.compile(dt=1e-6)Common tissues at different field strengths:
from blochsimulator import TissueParameters
# 3T parameters
gm = TissueParameters.gray_matter(3.0)
wm = TissueParameters.white_matter(3.0)
csf = TissueParameters.csf(3.0)
# 7T parameters
gm_7t = TissueParameters.gray_matter(7.0)
# Custom tissue
liver = TissueParameters(
name="Liver",
t1=0.812,
t2=0.042,
t2_star=0.028,
density=0.9
)For detailed packaging, release workflows, and CI/CD info, see the Developer Guide.
Note: Standalone applications for macOS, Windows, and Linux are automatically built and attached to GitHub Releases whenever a new version tag is pushed. The instructions below are for manual/local builds.
One build per OS is required (macOS build won’t run on Windows/Linux).
- macOS: Xcode CLT;
brew install libomp. - Windows: Python 3.8+ and MSVC Build Tools (for C extension).
- Linux: gcc/g++; ensure
libgompavailable.
python -m pip install -r requirements.txt
python -m pip install pyinstaller
python setup.py build_ext --inplace
PYINSTALLER_CONFIG_DIR=.pyinstaller pyinstaller bloch_gui.spec --noconfirmArtifact: dist/BlochSimulator (single binary; .exe on Windows).
./scripts/build_pyinstaller.sh # creates a venv, installs deps, builds, packages- macOS/Linux:
./dist/BlochSimulator - Windows:
dist\\BlochSimulator.exe
rfpulses/is bundled automatically.- Exports default to per-user data dirs:
- macOS:
~/Library/Application Support/BlochSimulator/exports - Windows:
%APPDATA%\\BlochSimulator\\exports - Linux:
~/.local/share/BlochSimulator/exports
- macOS:
- Override with
BLOCH_APP_DIRorBLOCH_EXPORT_DIRif you need a custom location.
The simulator solves the Bloch equations:
Using:
- Rotation matrices for RF and gradient effects
- Exponential decay for relaxation
- Cayley-Klein parameters for efficient rotation calculation
- Missing compiler: Install gcc (Linux), Xcode (macOS), or Visual Studio (Windows)
- OpenMP not found: The code will still work but without parallelization
- Import error: Ensure the .so/.pyd file is in the same directory
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new features
- Submit a pull request
If you use this simulator in your research, please cite:
@software{blochsimulator_python,
title={Python Bloch Equation Simulator GUI and API},
author={Luca Nagel},
year={2026},
url={https://github.com/LucaNagel/bloch_sim_gui}
}This project is based on code originally developed by Brian Hargreaves at Stanford University. Currently (01/2026) it is unfortunately not available. A python adaption of this code can be found here.
- Original Bloch simulator by Brian Hargreaves, Stanford University
- NumPy and SciPy communities
- PyQt/PySide developers
- OpenMP project
- Built partially with codex, claude code and gemini cli
Luca Nagel
blochsimulator/
├── src/
│ └── blochsimulator/
│ ├── __init__.py
│ ├── simulator.py # Core Python API
│ ├── gui.py # PyQt5 GUI
│ ├── bloch_core_modified.c # C implementation
│ ├── bloch_core.h # C header
│ ├── bloch_wrapper.pyx # Cython wrapper
│ └── ...
├── tests/ # Unit tests
├── docs/ # Sphinx documentation
├── pyproject.toml # Modern build config
├── setup.py # C-extension build config
├── MANIFEST.in # Source dist manifest
└── README.md
