Modelcore
Bloch equations
Also called Bloch equation.
The differential equations for the nuclear magnetization in a magnetic field, with precession and T1 and T2 relaxation. They predict the MR signal for a sample of independent spins.
The Bloch equations describe how the nuclear magnetization of a sample moves in a magnetic field. The magnetization turns about the field at the Larmor frequency, its transverse part decays with the time constant , and its longitudinal part returns to equilibrium with the time constant . Felix Bloch wrote them in 1946. In MRI they are the physics model: they say what signal each small region of the body gives after the radio frequency pulses and gradients of a scan. The MRI signal equation follows from them when relaxation during the readout is ignored. They are a model of nature, not a law that always holds: they treat the spins as independent and the relaxation as single exponentials.
The machine
The state. The magnetization of a small volume, a vector in . The axis is along the main field . The transverse part is written as the complex number .
The parameters.
- The gyromagnetic ratio of the nucleus. For the proton, MHz/T.
- The field : the main field , the radio frequency field in the transverse plane, the gradient and the field error .
- The relaxation times and the equilibrium magnetization , which is proportional to the proton density.
The equations. With ,
The first term is precession, the second is relaxation. In the frame that turns with the radio frequency about , the same equation holds with replaced by , where is now written in the turning frame. On resonance, the component is zero and a pulse is a pure rotation about the direction.
Where it holds. The equations describe the average moment of many spins that do not interact, in fields that change slowly compared with the Larmor period. They fit water protons in most tissues well.
Where it fails.
- Diffusion moves spins through the gradient; the Bloch-Torrey equation adds a diffusion term.
- Many tissues have more than one water compartment, so the decay is a sum of exponentials, not one.
- Exchange between pools (magnetization transfer, chemical exchange) needs coupled equations for each pool.
- Coupled spins (J coupling) and solids with very short need the quantum density matrix.
- In a voxel with an uneven field, the signal decays with , which is not a property of the equation for one point but of the sum over the voxel.
A numerical solution. For a general pulse, the equation is solved numerically. The implementation uses the classical Runge-Kutta method with a fixed step .
BLOCH-RK4(M, omega(.), T1, T2, M0, h, K)- 1
f(t, v) = v x omega(t) - (v_x/T2, v_y/T2, (v_z - M0)/T1) - 2
for s = 0 .. K - 1 do - 3
t <- s h - 4
a <- f(t, M); b <- f(t + h/2, M + (h/2) a) - 5
c <- f(t + h/2, M + (h/2) b); d <- f(t + h, M + h c) - 6
M <- M + h (a + 2 b + 2 c + d) / 6 - 7
return M
Each step costs four evaluations of , so a run of steps costs operations. For a piecewise constant field, the exact solution of each piece (a rotation followed by relaxation) is faster and has no step error; the closed forms below give it.
What it counts
- The Larmor frequency : 63.87 MHz at 1.5 T and 127.73 MHz at 3 T for the proton.
- The free induction decay. After a 90 degree pulse, , the signal that the coil picks up.
- Recovery and inversion. After an inversion pulse, . For s the signal is zero at s, which is how inversion recovery suppresses one tissue.
- The signal of a pulse sequence. For a spoiled gradient echo with flip angle and repetition time , the steady state signal is with . For ms and s, it is at the best angle degrees, and at 90 degrees.
- The image contrast. , and of each tissue, through such signal formulas, give the contrast of and weighted images. MR fingerprinting fits these parameters to simulated signals.
Relation to other models
- MRI signal equation. If relaxation during the readout is ignored and the field is , the transverse magnetization at is with . Summed over the body after demodulation, this is the signal equation. A field error adds the phase used in conjugate phase reconstruction.
- Quantum mechanics. For a spin 1/2 in a field, the expected value of the spin follows the precession term exactly. The relaxation terms are a phenomenological addition for an ensemble.
- Bloch-Torrey. Adds diffusion, . It gives the signal loss used in diffusion imaging.
- Bloch-McConnell. Couples several pools with exchange rates.
- Linear time-invariant pieces. On an interval where is constant, the equation is with a constant matrix , so its solution is a matrix exponential. This gives the exact solvers used for long sequences.
Known results in the model
Existence and uniqueness. For a field that is continuous in pieces, the equation has exactly one solution for each start, because the right side is linear in (the Picard-Lindelöf theorem).
Free precession (theorem). For , and . For the transverse part turns clockwise when seen from . The proof gives the details.
Pulses are rotations (theorem). Without relaxation and with a constant , is turned by the angle about , given by the Rodrigues formula. The length stays constant. On resonance, a pulse along with takes to .
Relaxation shrinks the transverse part (theorem). With relaxation, when is along , and in general when there is no radio frequency field, so the equilibrium attracts every solution.
Proof. With along , the precession term is orthogonal to , because it is orthogonal to and has no component. The relaxation term gives .
The Ernst angle (theorem, under perfect spoiling). If the transverse magnetization is fully destroyed before each pulse, the steady state just before a pulse is . The signal is largest for .
Proof. Let be the steady value before a pulse. The pulse leaves , and free recovery over gives , which must equal . Solving gives the formula. The derivative of in has the numerator , which changes sign from positive to negative at .
Accuracy of the integrator (measurement). With s over 0.05 s of free precession, the RK4 solution agreed with the closed form within ; with s, the 90 degree pulse agreed within .
Proof: Closed form solutions of the Bloch equation used to check the integratorfull
The equation is, with and the angular frequency vector ,
where or is read as 0 when the time is infinite. The right side is linear in plus a constant, with coefficients that are continuous in when is continuous, so it is Lipschitz in . By the Picard-Lindelöf theorem, (1) has exactly one solution for each start . So any function that satisfies (1) and the start is the solution. The integrator of the page is the classical Runge-Kutta method, whose global error is for a smooth right side; the tests compare it with the closed forms below.
Theorem 1 (free precession and relaxation). Let be constant. Write . Then
Proof. With , the cross product is . So (1) reads , and . Then
This linear equation with constant coefficient has the stated solution, and so does the equation for .
For a proton, , so with along the transverse magnetization turns clockwise when seen from .
Corollary (two check values). With and no field, , which is at . With , , which is zero at .
Proof. Put the starts into Theorem 1. For the second, gives .
Theorem 2 (rotation without relaxation). Let and let be constant, with and . Then , where is the rotation by the angle about by the right-hand rule, given by the formula of Rodrigues
In particular for all .
Proof. Put , so . Write and . Differentiating the formula in gives
On the other side, , because . With for a unit vector , the two expressions are equal. So , and by the chain rule
which is (1) without relaxation. By uniqueness, . A rotation keeps the length.
Corollary (a hard pulse). On resonance in the rotating frame, with along , . Starting from , after the time with the magnetization is : a 90 degree pulse about puts it along .
Proof. Theorem 2 with , and : , , so .
Remarks.
- The tests run the integrator with s for 0.05 s of free precession, and with s for the pulse, and compare with these formulas.
- Without relaxation the exact solution keeps . The classical Runge-Kutta method does not keep it exactly, but with a small step the change is far below the test tolerance.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/bloch-equations/. The authors are listed in prov/YQ1113.jsonl.
"""The Bloch equation, solved by the classical Runge-Kutta method and in closed form.
State: the magnetization M = (Mx, My, Mz). Field: the angular frequency
vector omega(t) = gamma B(t) in rad/s, given in the frame of the caller (in
the rotating frame, omega = (gamma B1x, gamma B1y, Delta omega)). The
equation is
dM/dt = M x omega - (Mx / T2, My / T2, (Mz - M0) / T1).
For a proton (gamma > 0) and omega = (0, 0, w0), the transverse part
Mxy = Mx + i My turns clockwise seen from +z: Mxy(t) = Mxy(0) exp(-i w0 t - t / T2).
The Larmor frequency is f = gammabar B0 with gammabar = 42.577478461 MHz/T
for the free proton (the value listed by NIST, physics.nist.gov, constant gammapbar).
"""
import cmath
import math
from typing import Callable, Sequence
GAMMA_BAR_PROTON = 42.577478461e6 # Hz/T
GAMMA_PROTON = 2.0 * math.pi * GAMMA_BAR_PROTON # rad/s/T
Vec3 = tuple[float, float, float]
def larmor_hz(b0: float) -> float:
"""The proton Larmor frequency in Hz at field b0 in tesla."""
return GAMMA_BAR_PROTON * b0
def cross(a: Sequence[float], b: Sequence[float]) -> Vec3:
"""The cross product a x b."""
return (a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0])
def bloch_rhs(m: Sequence[float], omega: Sequence[float], t1: float, t2: float, m0: float) -> Vec3:
"""dM/dt of the Bloch equation. Use math.inf for no relaxation."""
c = cross(m, omega)
r2 = 0.0 if math.isinf(t2) else 1.0 / t2
r1 = 0.0 if math.isinf(t1) else 1.0 / t1
return (c[0] - m[0] * r2, c[1] - m[1] * r2, c[2] - (m[2] - m0) * r1)
def bloch_rk4(
m: Sequence[float],
omega: Callable[[float], Sequence[float]],
t1: float,
t2: float,
m0: float,
h: float,
steps: int,
) -> Vec3:
"""Integrate the Bloch equation from t = 0 with steps of size h (classical RK4)."""
def f(t: float, v: Sequence[float]) -> Vec3:
return bloch_rhs(v, omega(t), t1, t2, m0) # @line L1
def add(v: Sequence[float], d: Sequence[float], s: float) -> Vec3:
return (v[0] + s * d[0], v[1] + s * d[1], v[2] + s * d[2])
cur: Vec3 = (float(m[0]), float(m[1]), float(m[2]))
for s in range(steps): # @line L2
t = s * h # @line L3
a = f(t, cur) # @line L4
b = f(t + h / 2, add(cur, a, h / 2))
c = f(t + h / 2, add(cur, b, h / 2)) # @line L5
d = f(t + h, add(cur, c, h))
cur = tuple(cur[i] + h * (a[i] + 2 * b[i] + 2 * c[i] + d[i]) / 6 for i in range(3)) # @line L6
return cur # @line L7
def free_precession(m: Sequence[float], w0: float, t1: float, t2: float, m0: float, t: float) -> Vec3:
"""Closed form for omega = (0, 0, w0): Mxy decays by exp(-t/T2) and turns by -w0 t; Mz recovers to M0."""
mxy = complex(m[0], m[1]) * cmath.exp(-1j * w0 * t) * math.exp(-t / t2)
mz = m0 + (m[2] - m0) * math.exp(-t / t1)
return (mxy.real, mxy.imag, mz)
def rotate(v: Sequence[float], axis: Sequence[float], angle: float) -> Vec3:
"""Rodrigues: v turned by angle about the unit axis (right-hand rule)."""
c, s = math.cos(angle), math.sin(angle)
k = axis
kv = cross(k, v)
kd = k[0] * v[0] + k[1] * v[1] + k[2] * v[2]
return tuple(v[i] * c + kv[i] * s + k[i] * kd * (1 - c) for i in range(3))
test_bloch_equations.py 61 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/bloch-equations/. The authors are listed in prov/YQ1113.jsonl.
import math
import random
import pytest
from bloch_equations import bloch_rk4, free_precession, larmor_hz, rotate
def close(a, b, tol):
return max(abs(x - y) for x, y in zip(a, b)) < tol
def test_larmor_check_values():
assert larmor_hz(1.5) / 1e6 == pytest.approx(63.87, abs=0.005)
assert larmor_hz(3.0) / 1e6 == pytest.approx(127.73, abs=0.005)
def test_free_precession_matches_closed_form():
rng = random.Random(31)
for _ in range(5):
m = (rng.uniform(-1, 1), rng.uniform(-1, 1), rng.uniform(-1, 1))
w0 = 2 * math.pi * rng.uniform(-50, 50)
t1, t2 = rng.uniform(0.3, 1.5), rng.uniform(0.03, 0.2)
h, steps = 1e-4, 500
got = bloch_rk4(m, lambda t: (0.0, 0.0, w0), t1, t2, 1.0, h, steps)
want = free_precession(m, w0, t1, t2, 1.0, h * steps)
assert close(got, want, 1e-7)
def test_hard_pulse_is_a_rotation():
# On resonance with B1 along x, dM/dt = M x (w1, 0, 0) turns M by -w1 t about x.
w1 = 2 * math.pi * 250.0
tau = 1e-3 # 90 degree pulse: w1 tau = pi / 2
got = bloch_rk4((0.0, 0.0, 1.0), lambda t: (w1, 0.0, 0.0), math.inf, math.inf, 1.0, 1e-6, 1000)
assert close(got, rotate((0.0, 0.0, 1.0), (1.0, 0.0, 0.0), -w1 * tau), 1e-10)
assert close(got, (0.0, 1.0, 0.0), 1e-10)
def test_length_is_kept_without_relaxation():
rng = random.Random(32)
m = (0.3, -0.4, 0.5)
w = (rng.uniform(-900, 900), rng.uniform(-900, 900), rng.uniform(-900, 900))
got = bloch_rk4(m, lambda t: w, math.inf, math.inf, 1.0, 1e-5, 1000)
assert math.sqrt(sum(v * v for v in got)) == pytest.approx(math.sqrt(0.5), rel=1e-9)
# The same motion in closed form: a turn by -|w| t about w / |w|.
nw = math.sqrt(sum(v * v for v in w))
want = rotate(m, tuple(v / nw for v in w), -nw * 1e-2)
assert close(got, want, 1e-9)
def test_t2_decay_and_inversion_null():
got = bloch_rk4((1.0, 0.0, 0.0), lambda t: (0.0, 0.0, 0.0), 1.0, 0.1, 1.0, 1e-4, 500)
assert math.hypot(got[0], got[1]) == pytest.approx(math.exp(-0.5), rel=1e-10)
t1 = 0.8
tnull = t1 * math.log(2.0)
steps = 1000
got = bloch_rk4((0.0, 0.0, -1.0), lambda t: (0.0, 0.0, 0.0), t1, 0.05, 1.0, tnull / steps, steps)
assert abs(got[2]) < 1e-12
References
- Felix Bloch, Nuclear induction, Physical Review 70(7-8), 1946, 460 to 474, the equations of motion of the macroscopic nuclear moment with the longitudinal and transverse relaxation times (general locator)
- Zhi-Pei Liang and Paul C. Lauterbur, Principles of Magnetic Resonance Imaging, A Signal Processing Perspective, IEEE Press, 2000, the chapter on signal generation and detection (the Bloch equation, the rotating frame, excitation and relaxation; chapter number not confirmed)
- E. Mark Haacke, Robert W. Brown, Michael R. Thompson and Ramesh Venkatesan, Magnetic Resonance Imaging, Physical Principles and Sequence Design, Wiley-Liss, 1999, the chapters on the classical response of the magnetization, rotating frame and relaxation (general locator)
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H2 full proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag YQ1113 Edit this page History Report an error Markdown