Note
Go to the end to download the full example code.
Spherical harmonics-generating coil design¶
Example showing a basic biplanar coil producing a field profile defined by spherical harmonics. We use the surface harmonics basis for the stream function, and optimize the coupling to spherical harmonics components, thus creating a compact optimization problem that can be solved very quickly.
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, 15, 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))
# To spice things up, let's distort the planes a bit
joined_planes.vertices = (
joined_planes.vertices
- 0.5
* np.linalg.norm(joined_planes.vertices, axis=1)[:, None]
* np.sign(joined_planes.vertices[:, 1])[:, None]
* joined_planes.vertex_normals
)
joined_planes.vertices = (
joined_planes.vertices
- 0.5
* np.linalg.norm(joined_planes.vertices, axis=1)[:, None]
* np.sign(joined_planes.vertices[:, 1])[:, None]
* joined_planes.vertex_normals
)
# Create mesh class object
coil = MeshConductor(
mesh_obj=joined_planes,
fix_normals=True,
basis_name="suh",
N_suh=100,
sph_radius=0.2,
sph_normalization="energy",
)
Calculating surface harmonics expansion...
Computing the laplacian matrix...
Computing the mass matrix...
Set up target spherical harmonics components
target_alms = np.zeros((coil.opts["N_sph"] * (coil.opts["N_sph"] + 2),))
target_blms = np.zeros((coil.opts["N_sph"] * (coil.opts["N_sph"] + 2),))
target_blms[4] += 1
Create bfield specifications used when optimizing the coil geometry
target_spec = {
"coupling": coil.sph_couplings[1],
"abs_error": 0.01,
"target": target_blms,
}
Computing coupling matrices
l = 1 computed
l = 2 computed
l = 3 computed
l = 4 computed
l = 5 computed
Run QP solver
coil.s, prob = optimize_streamfunctions(
coil,
[target_spec],
objective="minimum_ohmic_power",
solver="CLARABEL",
)
Computing the resistance matrix...
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 = 70
nnz(P) = 5043
nnz(A) = 6974
cones (total) = 1
: Nonnegative = 1, numel = 70
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.4812e+00 -2.1309e+04 4.76e+03 6.09e-01 7.87e-20 1.00e+00 6.43e+02 ------
1 +2.3335e+01 -1.8364e+03 7.97e+01 6.01e-02 7.74e-19 1.06e+01 8.12e+01 8.98e-01
2 +1.2797e+01 -6.8766e+01 6.37e+00 2.26e-03 1.91e-17 5.01e-01 4.02e+00 9.71e-01
3 +1.2761e+01 +9.8404e+00 2.97e-01 7.75e-05 2.96e-16 7.41e-03 1.61e-01 9.62e-01
4 +1.2628e+01 +1.2441e+01 1.50e-02 4.96e-06 1.88e-16 8.42e-04 1.06e-02 9.45e-01
5 +1.2609e+01 +1.2607e+01 1.51e-04 5.03e-08 2.47e-17 8.68e-06 1.09e-04 9.90e-01
6 +1.2609e+01 +1.2609e+01 1.53e-06 5.11e-10 5.23e-18 8.82e-08 1.10e-06 9.90e-01
7 +1.2609e+01 +1.2609e+01 2.02e-08 6.61e-12 2.25e-14 1.16e-09 1.46e-08 9.87e-01
8 +1.2609e+01 +1.2609e+01 2.05e-10 6.46e-14 6.41e-15 1.18e-11 1.47e-10 9.90e-01
---------------------------------------------------------------------------------------------
Terminated with status = Solved
solve time = 6.749081ms
-------------------------------------------------------------------------------
Summary
-------------------------------------------------------------------------------
Plot coil windings

Processing contour, value: -1342706.2520301396
Processing contour, value: -805480.3923439882
Processing contour, value: -268254.53265783703
Processing contour, value: 268971.32702831435
Processing contour, value: 806197.1867144655
Processing contour, value: 1343423.0464006166
<pyvista.plotting.plotter.Plotter object at 0x7f893d349f50>
Now, let’s change the spherical harmonics inner expansion radius (i.e. the target region radius) and optimize a new coil (with the same target sph component)
coil.set_sph_options(sph_radius=1.4)
target_spec = {
"coupling": coil.sph_couplings[1],
"abs_error": 0.01,
"target": target_blms,
}
Computing coupling matrices
l = 1 computed
l = 2 computed
l = 3 computed
l = 4 computed
l = 5 computed
Run QP solver
coil.s2, prob = optimize_streamfunctions(
coil,
[target_spec],
objective="minimum_ohmic_power",
solver="CLARABEL",
)
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 = 70
nnz(P) = 5043
nnz(A) = 6968
cones (total) = 1
: Nonnegative = 1, numel = 70
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.9946e-01 -3.3788e+03 3.38e+03 7.36e-01 9.28e-19 1.00e+00 3.17e+02 ------
1 +1.1294e+00 -3.0213e+02 2.69e+02 1.59e-01 9.03e-18 7.27e-01 3.60e+01 8.89e-01
2 +1.1957e+00 -5.9578e+01 5.08e+01 3.63e-02 1.60e-17 1.34e+00 2.41e+01 4.80e-01
3 +1.2001e+00 -3.7386e+00 4.12e+00 2.99e-03 1.00e-16 2.62e-02 2.53e+00 9.00e-01
4 +1.1185e+00 +5.5405e-01 5.65e-01 3.45e-04 3.52e-16 7.17e-03 3.49e-01 9.42e-01
5 +1.0894e+00 +1.0554e+00 3.23e-02 2.07e-05 5.70e-17 4.65e-04 2.13e-02 9.71e-01
6 +1.0883e+00 +1.0879e+00 3.36e-04 2.07e-07 7.99e-18 4.95e-06 2.28e-04 9.90e-01
7 +1.0883e+00 +1.0883e+00 3.57e-06 2.19e-09 2.97e-14 5.26e-08 2.43e-06 9.89e-01
8 +1.0883e+00 +1.0883e+00 3.60e-08 2.20e-11 2.21e-14 5.30e-10 2.45e-08 9.90e-01
9 +1.0883e+00 +1.0883e+00 3.60e-10 2.20e-13 4.77e-16 5.30e-12 2.45e-10 9.90e-01
---------------------------------------------------------------------------------------------
Terminated with status = Solved
solve time = 7.420901ms
-------------------------------------------------------------------------------
Summary
-------------------------------------------------------------------------------
Plot coil windings

Processing contour, value: -17866.6041647386
Processing contour, value: -10736.677448555234
Processing contour, value: -3606.7507323718673
Processing contour, value: 3523.1759838114995
Processing contour, value: 10653.102699994863
Processing contour, value: 17783.029416178233
<pyvista.plotting.plotter.Plotter object at 0x7f8944f775d0>
Total running time of the script: (0 minutes 31.159 seconds)