Skip to content

A Halbach cylinder: the field of a ring of magnets

FEM halbach_magnets.py

A ring of permanent magnets whose magnetisation turns twice as fast as the position around the ring pushes all its flux into the bore: inside there is a strong, almost uniform field, outside almost none. Sixteen segments are solved with a vector potential and checked against the closed form for an ideal Halbach cylinder.

The field of a 16-segment Halbach ring on a dark-to-bright magma scale: the ring and the bore glow, the air outside is almost black. Thin white lines are the field lines, straight and parallel across the bore and closing inside the magnet ring; each of the sixteen outlined segments carries a white arrow along its magnetisation, which turns twice around the ring.

\(|B|\) in tesla, with field lines (contours of \(A_z\)) and the magnetisation of each segment.

The same ring while every magnet's magnetisation turns by the same angle, one full turn: the uniform field in the bore turns with it, its parallel field lines sweeping round, while the air outside the ring stays dark.

Turn every magnetisation by the same angle and the bore field turns with it. The problem is linear, so two solves (turned by \(0\) and \(90^\circ\)) give every frame exactly.

The problem

With remanence \(\mathbf B_r\) in the magnets and \(\mu_r=1\) everywhere, \(\nabla\times\mathbf H=0\) with \(\mathbf H=(\mathbf B-\mathbf B_r)/\mu_0\) and \(\mathbf B=(\partial_yA_z,-\partial_xA_z)\) gives the weak form

\[\int\nabla A_z\cdot\nabla v\,d\Omega-\int_{\text{magnets}}(B_{r,x}\,\partial_y v-B_{r,y}\,\partial_x v)\,d\Omega=0,\]

on a disk four times the magnet radius with \(A_z=0\) on its rim. Segment \(j\) at angle \(\theta_j\) is magnetised along \(2\theta_j\). An ideal continuous ring has the bore field \(B=B_r\ln(r_o/r_i)\); \(N\) segments of constant magnetisation keep the factor \(\sin(2\pi/N)/(2\pi/N)\) of it.

The jNO code

Each segment is a ring intersected with a wedge, its own named region; jno.shape.regions meshes them conforming with the bore and the air:

ring = jno.shape.disk(0, 0, RO, size=H) - jno.shape.disk(0, 0, RI)
segments = {f"m{j}": ring & wedge(a) for j, a in enumerate(phi)}
air = {"bore": jno.shape.disk(0, 0, RI, size=H), "air": jno.shape.disk(0, 0, RFAR, size=10 * H)}
return jno.shape.regions({**segments, **air}).domain(), phi

The Laplacian is written once per region and the source only on the magnets, each with its own direction:

for tag in [f"m{j}" for j in range(n)] + ["bore", "air"]:
    x, y, _ = d.variable(tag, split=True)
    a, w = A.bind(x=x, y=y), v.bind(x=x, y=y)
    terms.append(a.x * w.x + a.y * w.y)  # curl H = 0 in every region
for j, p in enumerate(phi):  # the magnets' source: remanence BR along 2 phi_j + turn
    x, y, _ = d.variable(f"m{j}", split=True)
    w = v.bind(x=x, y=y)
    terms.append(-BR * (np.cos(2 * p + turn) * w.y - np.sin(2 * p + turn) * w.x))
fem = jno.fem([*terms, A(*d.variable("boundary", split=True)) - 0.0])

\(\mathbf B\) is recovered with fem.eval of \(\int\partial_yA\,\varphi_i\) and \(\int\partial_xA\,\varphi_i\), divided by the lumped mass.

Result

\(B_r=1.2\) T, \(r_i=20\) mm, \(r_o=50\) mm (ideal ring: 1.0995 T); the field read for \(r<0.8\,r_i\):

segments \(N\) \(B\) in the bore segment formula spread of \(\lvert B\rvert\)
8 0.9901 T 0.9899 T 1.0 %
16 1.0716 T 1.0715 T 0.06 %
32 1.0926 T 1.0925 T \(7\times10^{-4}\) %

Sixteen segments lose 2.5 % of the ideal field, eight lose 10 %.

Full script

The whole script, as it runs in the tests
"""A Halbach cylinder: 2-D magnetostatics of a ring of permanent magnets, with a vector potential.

The field B = curl(A_z e_z) = (A_y, -A_x) of magnets with remanence Br obeys curl H = 0, H = (B - Br) / mu0.
With mu_r = 1 everywhere (mu0 scaled out, B in tesla) the weak form is

    int grad A . grad v - (Br_x v_y - Br_y v_x) = 0 ,

the magnets entering as a source. A ring of N segments whose magnetisation rotates twice as fast as the
segment's position, phi_M = 2 theta, makes a uniform dipole field in the bore and almost none outside.
Reference: B = Br ln(ro / ri) * sin(2 pi / N) / (2 pi / N) for N segments (Halbach 1980), the continuous
ring at N -> infinity.
"""

import jax
import numpy as np

import jno

jax.config.update("jax_enable_x64", True)  # the potential is tiny against its gradient
run = jno.setup(__file__)
BR, RI, RO, RFAR = 1.2, 0.02, 0.05, 0.2  # remanence [T], bore and outer radius, truncation radius [m]
H = 0.0006  # element size in the magnets and the bore; the air outside is coarser


def halbach(n):
    """The domain of an n-segment ring and the magnetisation angle of each segment."""
    phi = 2 * np.pi * (np.arange(n) + 0.5) / n  # segment centres; segment j is magnetised along 2 phi_j
    wedge = lambda a: jno.shape.polygon(
        [(0, 0)] + [(3 * RO * np.cos(a + s), 3 * RO * np.sin(a + s)) for s in (-np.pi / n, np.pi / n)]
    )
    ring = jno.shape.disk(0, 0, RO, size=H) - jno.shape.disk(0, 0, RI)
    segments = {f"m{j}": ring & wedge(a) for j, a in enumerate(phi)}
    air = {"bore": jno.shape.disk(0, 0, RI, size=H), "air": jno.shape.disk(0, 0, RFAR, size=10 * H)}
    return jno.shape.regions({**segments, **air}).domain(), phi


def solve(n, turn=0.0):
    """A_z of the n-segment ring, and B = (A_y, -A_x) as nodal averages (L2 projection, lumped mass).
    `turn` rotates every magnetisation by the same angle."""
    d, phi = halbach(n)
    A = d.unknown()
    v = A.test()
    terms = []
    for tag in [f"m{j}" for j in range(n)] + ["bore", "air"]:
        x, y, _ = d.variable(tag, split=True)
        a, w = A.bind(x=x, y=y), v.bind(x=x, y=y)
        terms.append(a.x * w.x + a.y * w.y)  # curl H = 0 in every region
    for j, p in enumerate(phi):  # the magnets' source: remanence BR along 2 phi_j + turn
        x, y, _ = d.variable(f"m{j}", split=True)
        w = v.bind(x=x, y=y)
        terms.append(-BR * (np.cos(2 * p + turn) * w.y - np.sin(2 * p + turn) * w.x))
    fem = jno.fem([*terms, A(*d.variable("boundary", split=True)) - 0.0])
    A_h = fem.solve(linear=jno.solve.cg(), precond=jno.precond.jacobi())
    x, y, _ = d.variable("interior", split=True)
    a, w = A.bind(x=x, y=y), v.bind(x=x, y=y)
    mass = fem.eval(w, A_h)
    Bx, By = fem.eval(a.y * w, A_h) / mass, -fem.eval(a.x * w, A_h) / mass
    return d, fem, A_h, Bx, By


ideal = BR * np.log(RO / RI)  # the continuous ring, N -> infinity
solved = {n: solve(n) for n in (8, 16, 32)}
for n, (d, fem, A_h, Bx, By) in solved.items():
    pts = np.asarray(fem.points)
    bore = np.hypot(*pts.T) < 0.8 * RI  # the field is read well inside the bore
    B_bore = np.hypot(Bx[bore], By[bore])
    ref = ideal * np.sin(2 * np.pi / n) / (2 * np.pi / n)  # n segments of constant magnetisation
    print(
        f"N = {n:2d} ({fem.dofs} DOFs): B in the bore {B_bore.mean():.4f} T (spread {B_bore.std() / B_bore.mean():.1e}), "
        f"segment formula {ref:.4f} T, continuous ring {ideal:.4f} T"
    )
    assert abs(B_bore.mean() / ref - 1) < 1e-3  # the segment formula, to the mesh