Note
Go to the end to download the full example code.
MAMBA coil¶
Compact example of a biplanar coil producing homogeneous field in a number of target regions arranged in a grid. Meant to demonstrate the flexibility in target choice, inspired by the technique “multiple-acquisition micro B(0) array” (MAMBA) technique, see https://doi.org/10.1002/mrm.10464
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.contour import scalar_contour
from bfieldtools.viz import plot_3d_current_loops
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, 1.5, 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="inner")
Set up target and stray field points. Here, the target points are on a planar 4x4 grid slightly smaller than the coil dimensions.
center = np.array([0, 0, 0])
sidelength = 0.5
n = 4
height = 0.1
n_height = 2
xx = np.linspace(-sidelength / 2, sidelength / 2, n)
yy = np.linspace(-height / 2, height / 2, n_height)
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
grid_target_points = list()
target_field = list()
hori_offsets = [-3, -1, 1, 3]
vert_offsets = [-3, -1, 1, 3]
for i, offset_x in enumerate(hori_offsets):
for j, offset_y in enumerate(vert_offsets):
grid_target_points.append(target_points + np.array([offset_x, 0, offset_y]))
target_field.append((i + j - 3) * np.ones((len(target_points),)))
target_points = np.asarray(grid_target_points).reshape((-1, 3))
target_field = np.asarray(target_field).reshape((-1,))
target_field = np.array(
[np.zeros((len(target_field),)), target_field, np.zeros((len(target_field),))]
).T
target_abs_error = np.zeros_like(target_field)
target_abs_error[:, 1] += 0.1
target_abs_error[:, 0::2] += 0.1
Plot target points and mesh
plotter = coil.plot_mesh(opacity=0.1)
plotter.add_mesh(
vector_glyphs(*target_points.T, *target_field.T), show_scalar_bar=False
)

Actor (0x7f892cde9c00)
Center: (0.0, 0.0, 0.0)
Pickable: True
Position: (0.0, 0.0, 0.0)
Scale: (1.0, 1.0, 1.0)
Visible: True
X Bounds -3.296E+00, 3.296E+00
Y Bounds -5.096E-01, 5.096E-01
Z Bounds -3.296E+00, 3.296E+00
User matrix: Identity
Has mapper: True
Property (0x7f892cb96f80)
Ambient: 0.0
Ambient color: Color(name='light_blue', hex='#add8e6ff', opacity=255)
Anisotropy: 0.0
Color: Color(name='light_blue', hex='#add8e6ff', opacity=255)
Culling: "none"
Diffuse: 1.0
Diffuse color: Color(name='light_blue', hex='#add8e6ff', opacity=255)
Edge color: Color(name='black', hex='#000000ff', opacity=255)
Edge opacity: 1.0
Interpolation: InterpolationType.FLAT
Lighting: True
Line width: 1.0
Metallic: 0.0
Opacity: 1.0
Point size: 5.0
Render lines as tubes: False
Render points as spheres: False
Roughness: 0.5
Show edges: False
Specular: 0.0
Specular color: Color(name='light_blue', hex='#add8e6ff', opacity=255)
Specular power: 100.0
Style: "Surface"
DataSetMapper (0x7f892cb96f80)
Scalar visibility: True
Scalar range: (0.0, 3.0)
Interpolate before mapping: True
Scalar map mode: point
Color mode: map
Attached dataset:
PolyData (0x7f892cb969e0)
N Cells: 22016
N Points: 51712
N Strips: 0
X Bounds: -3.296e+00, 3.296e+00
Y Bounds: -5.096e-01, 5.096e-01
Z Bounds: -3.296e+00, 3.296e+00
N Arrays: 2
Compute coupling matrix that is used to compute the generated magnetic field, create field specification
target_spec = {
"coupling": coil.B_coupling(target_points),
"abs_error": target_abs_error,
"target": target_field,
}
Computing magnetic field coupling matrix, 3184 vertices by 512 target points... took 1.10 seconds.
Run QP solver, plot result
coil.s, prob = optimize_streamfunctions(
coil,
[target_spec],
objective="minimum_inductive_energy",
solver="CLARABEL",
)
plotter = coil.s.plot()
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()


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 (14009 MiB memory free), when approx_far=True using more chunks is faster...
Computing triangle-coupling matrix
Inductance matrix computation took 21.94 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 = 2896
constraints = 3072
nnz(P) = 4194856
nnz(A) = 8894794
cones (total) = 1
: Nonnegative = 1, numel = 3072
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 +4.2973e+02 -5.0071e+04 1.18e+02 1.97e-15 2.50e-15 1.00e+00 1.64e+01 ------
1 +2.9827e+02 -3.7065e+03 1.34e+01 1.94e-15 1.33e-14 9.73e-02 1.30e+00 9.23e-01
2 +1.9453e+02 -3.2763e+02 2.68e+00 2.21e-15 8.08e-15 2.68e-02 1.63e-01 9.09e-01
3 +1.5727e+02 +5.3108e+01 1.96e+00 2.23e-15 2.51e-15 7.89e-03 3.10e-02 8.51e-01
4 +1.5113e+02 +1.2591e+02 2.00e-01 2.31e-15 6.98e-16 3.44e-03 7.30e-03 8.37e-01
5 +1.4980e+02 +1.4756e+02 1.52e-02 2.48e-15 3.93e-16 3.15e-04 6.34e-04 9.29e-01
6 +1.4968e+02 +1.4960e+02 5.08e-04 2.58e-15 3.66e-16 1.17e-05 2.10e-05 9.90e-01
7 +1.4967e+02 +1.4967e+02 2.87e-05 2.42e-15 3.86e-16 5.27e-07 1.11e-06 9.90e-01
8 +1.4967e+02 +1.4967e+02 2.06e-06 2.35e-15 3.90e-16 3.21e-08 7.24e-08 9.90e-01
9 +1.4967e+02 +1.4967e+02 8.67e-08 2.63e-15 3.88e-16 1.26e-09 2.87e-09 9.90e-01
10 +1.4967e+02 +1.4967e+02 1.08e-09 2.27e-15 1.74e-14 1.58e-11 3.57e-11 9.90e-01
---------------------------------------------------------------------------------------------
Terminated with status = Solved
solve time = 19.747547113s
-------------------------------------------------------------------------------
Summary
-------------------------------------------------------------------------------
Processing contour, value: -12658097.660057528
Processing contour, value: -9845187.57939187
Processing contour, value: -7032277.498726213
Processing contour, value: -4219367.418060556
Processing contour, value: -1406457.3373949006
Processing contour, value: 1406452.7432707567
Processing contour, value: 4219362.823936412
Processing contour, value: 7032272.904602071
Processing contour, value: 9845182.985267727
Processing contour, value: 12658093.065933386
Total running time of the script: (0 minutes 55.604 seconds)