Note
Go to the end to download the full example code.
High-order spherical harmonic biplanar coil design¶
Example showing a basic biplanar coil producing a high-order spherical harmonic field in a specific target region between the two coil planes.
import pyvista as pv
from bfieldtools._example_plots import vector_glyphs
pv.set_plot_theme("document")
import numpy as np
import trimesh
from bfieldtools.mesh_conductor import MeshConductor
from bfieldtools.coil_optimize import optimize_streamfunctions
from bfieldtools.utils import combine_meshes, load_example_mesh
# Load simple plane mesh that is centered on the origin
planemesh = load_example_mesh("10x10_plane_hires")
# Specify coil plane geometry
center_offset = np.array([0, 0, 0])
standoff = np.array([0, 3, 0])
# Create coil plane pairs
coil_plus = trimesh.Trimesh(
planemesh.vertices + center_offset + standoff, planemesh.faces, process=False
)
coil_minus = trimesh.Trimesh(
planemesh.vertices + center_offset - standoff, planemesh.faces, process=False
)
joined_planes = combine_meshes((coil_plus, coil_minus))
# Create mesh class object
coil = MeshConductor(
mesh_obj=joined_planes, fix_normals=True, basis_name="suh", N_suh=100
)
Calculating surface harmonics expansion...
Computing the laplacian matrix...
Computing the mass matrix...
Set up target and stray field points
# Here, the target points are on a volumetric grid within a sphere
center = np.array([0, 0, 0])
sidelength = 1.5
n = 8
xx = np.linspace(-sidelength / 2, sidelength / 2, n)
yy = np.linspace(-sidelength / 2, sidelength / 2, n)
zz = np.linspace(-sidelength / 2, sidelength / 2, n)
X, Y, Z = np.meshgrid(xx, yy, zz, indexing="ij")
x = X.ravel()
y = Y.ravel()
z = Z.ravel()
target_points = np.array([x, y, z]).T
# Turn cube into sphere by rejecting points "in the corners"
target_points = (
target_points[np.linalg.norm(target_points, axis=1) < sidelength / 2] + center
)
Create bfield specifications used when optimizing the coil geometry
from bfieldtools import sphtools
lmax = 4
alm = np.zeros((lmax * (lmax + 2),))
blm = np.zeros((lmax * (lmax + 2),))
# Set one specific component to one
blm[16] += 1
sphfield = sphtools.field(target_points, alm, blm, lmax)
target_field = sphfield / np.max(sphfield[:, 0])
plotter = coil.plot_mesh(opacity=0.2)
plotter.add_mesh(vector_glyphs(*target_points.T, *sphfield.T), show_scalar_bar=False)
target_spec = {
"coupling": coil.B_coupling(target_points),
"abs_error": 0.1,
"target": target_field,
}

Computing magnetic field coupling matrix, 3184 vertices by 160 target points... took 0.32 seconds.
Run QP solver
coil.s, prob = optimize_streamfunctions(
coil,
[target_spec],
objective="minimum_inductive_energy",
solver="CLARABEL",
)
Computing the inductance matrix...
Computing self-inductance matrix using rough quadrature (degree=2). For higher accuracy, set quad_degree to 4 or more.
Estimating 34964 MiB required for 3184 by 3184 vertices...
Computing inductance matrix in 60 chunks (14184 MiB memory free), when approx_far=True using more chunks is faster...
Computing triangle-coupling matrix
Inductance matrix computation took 21.77 seconds.
Passing problem to solver...
===============================================================================
CVXPY
v1.9.2
===============================================================================
-------------------------------------------------------------------------------
Compilation
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Numerical solver
-------------------------------------------------------------------------------
-------------------------------------------------------------
Clarabel.rs v0.11.1 - Clever Acronym
(c) Paul Goulart
University of Oxford, 2022
-------------------------------------------------------------
problem:
variables = 100
constraints = 960
nnz(P) = 5050
nnz(A) = 96000
cones (total) = 1
: Nonnegative = 1, numel = 960
settings:
linear algebra: direct / faer, precision: 64 bit (4 threads)
max iter = 200, time limit = Inf, max step = 0.990
tol_feas = 1.0e-8, tol_gap_abs = 1.0e-8, tol_gap_rel = 1.0e-8,
static reg : on, ϵ1 = 1.0e-8, ϵ2 = 4.9e-32
dynamic reg: on, ϵ = 1.0e-13, δ = 2.0e-7
iter refine: on, reltol = 1.0e-13, abstol = 1.0e-12,
max iter = 10, stop ratio = 5.0
equilibrate: on, min_scale = 1.0e-4, max_scale = 1.0e4
max iter = 10
iter pcost dcost gap pres dres k/t μ step
---------------------------------------------------------------------------------------------
0 +7.4831e+03 +1.3097e+04 7.50e-01 3.31e-01 1.70e-16 1.00e+00 6.12e+02 ------
1 +9.5169e+04 -3.6004e+03 2.74e+01 2.35e-02 2.53e-16 3.68e+01 3.93e+01 9.65e-01
2 +1.0941e+05 -3.1393e+04 4.49e+00 2.97e-02 2.34e-16 1.90e+02 3.54e+01 2.67e-01
3 +1.6238e+05 +8.2862e+04 9.60e-01 9.85e-03 7.68e-17 2.80e+02 1.27e+01 7.60e-01
4 +2.0726e+05 +1.1018e+05 8.81e-01 6.87e-03 2.40e-16 4.23e+02 6.35e+00 8.69e-01
5 +2.5951e+05 +2.1271e+05 2.20e-01 2.23e-03 7.11e-17 1.82e+02 2.58e+00 6.38e-01
6 +3.3085e+05 +2.0526e+05 6.12e-01 1.40e-03 4.50e-17 2.49e+02 1.35e+00 9.90e-01
7 +3.3299e+05 +3.1851e+05 4.54e-02 9.91e-05 8.86e-17 2.74e+01 1.45e-01 9.25e-01
8 +3.3383e+05 +3.3349e+05 9.97e-04 2.21e-06 5.95e-17 6.27e-01 3.39e-03 9.78e-01
9 +3.3385e+05 +3.3385e+05 9.98e-06 2.21e-08 3.19e-17 6.28e-03 3.40e-05 9.90e-01
10 +3.3385e+05 +3.3385e+05 9.98e-08 2.21e-10 6.24e-17 6.28e-05 3.40e-07 9.90e-01
11 +3.3385e+05 +3.3385e+05 9.98e-10 2.21e-12 5.56e-17 6.28e-07 3.40e-09 9.90e-01
---------------------------------------------------------------------------------------------
Terminated with status = Solved
solve time = 346.257873ms
-------------------------------------------------------------------------------
Summary
-------------------------------------------------------------------------------
Plot coil windings and target points
coil.s.discretize(N_contours=10).plot_loops()
# Display the assembled scene (Sphinx-Gallery captures it during docs builds).
if "plotter" in globals() and not plotter._closed:
plotter.show()

Processing contour, value: -984483385.4345521
Processing contour, value: -765708075.0985093
Processing contour, value: -546932764.7624667
Processing contour, value: -328157454.4264239
Processing contour, value: -109382144.09038126
Processing contour, value: 109393166.2456615
Processing contour, value: 328168476.58170414
Processing contour, value: 546943786.9177468
Processing contour, value: 765719097.2537897
Processing contour, value: 984494407.5898323
Total running time of the script: (0 minutes 27.284 seconds)