Head gradient coil

Example showing a gradient coil designed on the surface of a MEG system helmet

import pyvista as pv
from bfieldtools._example_plots import vector_glyphs

pv.set_plot_theme("document")


import numpy as np

from bfieldtools.mesh_conductor import MeshConductor
from bfieldtools.coil_optimize import optimize_streamfunctions
from bfieldtools.utils import load_example_mesh
from bfieldtools import sphtools


# Load simple plane mesh that is centered on the origin
helmetmesh = load_example_mesh("meg_helmet")
coil = MeshConductor(mesh_obj=helmetmesh, fix_normals=True)

Set up target and stray field points. Here, the target points are on a volumetric grid within a sphere

offset = np.array([0, 0, 0.04])
center = offset

sidelength = 0.05
n = 12
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"
# and inner points
target_points = (
    target_points[
        (np.linalg.norm(target_points, axis=1) < sidelength / 2)
        * (np.linalg.norm(target_points, axis=1) > sidelength / 2 * 0.8)
    ]
    + center
)

Specify target field and run solver. Here, we specify the target field through the use of spherical harmonics. We want to produce the field corresponding to a specific beta_l,m-component.

lmax = 3
alm = np.zeros((lmax * (lmax + 2),))
blm = np.zeros((lmax * (lmax + 2),))

# Set one specific component to one
blm[3] += 1

sphfield = sphtools.field(target_points, alm, blm, lmax)

target_field = sphfield / np.max(sphfield[:, 0])

target_field[:, 2] = 0

plotter = coil.plot_mesh(opacity=0.5)
plotter.add_mesh(vector_glyphs(*target_points.T, *sphfield.T), show_scalar_bar=False)
plotter.view_isometric()

abs_error = np.zeros_like(target_field)
abs_error[:, 0] += 0.05
abs_error[:, 1:3] += 0.1


target_spec = {
    "coupling": coil.B_coupling(target_points),
    "abs_error": abs_error,
    "target": target_field,
}
head gradient coil design
Computing magnetic field coupling matrix, 2044 vertices by 312 target points... took 0.44 seconds.
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 16313 MiB required for 2044 by 2044 vertices...
Computing inductance matrix in 40 chunks (14357 MiB memory free),              when approx_far=True using more chunks is faster...
Computing triangle-coupling matrix
Inductance matrix computation took 14.09 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     = 1945
  constraints   = 1872
  nnz(P)        = 1892485
  nnz(A)        = 3641040
  cones (total) = 1
    : Nonnegative = 1,  numel = 1872

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  +3.7669e+03  -1.1817e+06  3.15e+02  4.60e-16  9.46e-16  1.00e+00  6.33e+02   ------
  1  +2.3304e+03  -6.1681e+04  2.75e+01  5.41e-16  1.82e-14  2.37e-01  3.41e+01  9.47e-01
  2  +9.5844e+02  -9.4241e+03  1.08e+01  7.16e-16  1.41e-14  4.03e-01  5.41e+00  8.68e-01
  3  +5.3543e+02  -8.7032e+02  2.63e+00  8.88e-16  5.59e-15  2.05e-01  6.90e-01  9.37e-01
  4  +4.1541e+02  -1.0248e+02  5.05e+00  8.75e-16  1.63e-15  1.23e-01  2.40e-01  7.58e-01
  5  +4.0406e+02  +7.4977e+01  4.39e+00  7.79e-16  6.68e-16  1.39e-01  1.50e-01  7.59e-01
  6  +3.9537e+02  +3.0318e+02  3.04e-01  8.21e-16  1.71e-16  4.62e-02  4.15e-02  9.22e-01
  7  +3.9365e+02  +3.3955e+02  1.59e-01  7.67e-16  1.12e-16  2.79e-02  2.42e-02  6.63e-01
  8  +3.9212e+02  +3.7627e+02  4.21e-02  8.50e-16  7.05e-17  8.18e-03  7.06e-03  7.21e-01
  9  +3.9190e+02  +3.8118e+02  2.81e-02  7.53e-16  6.46e-17  5.63e-03  4.75e-03  6.30e-01
 10  +3.9168e+02  +3.8915e+02  6.51e-03  8.70e-16  6.47e-17  1.34e-03  1.12e-03  9.05e-01
 11  +3.9163e+02  +3.9151e+02  2.95e-04  9.37e-16  6.93e-17  6.13e-05  5.10e-05  9.89e-01
 12  +3.9163e+02  +3.9162e+02  7.40e-06  9.55e-16  7.23e-17  1.53e-06  1.28e-06  9.75e-01
 13  +3.9163e+02  +3.9163e+02  7.41e-08  9.30e-16  7.38e-17  1.54e-08  1.28e-08  9.90e-01
 14  +3.9163e+02  +3.9163e+02  7.41e-10  9.01e-16  6.88e-17  1.54e-10  1.28e-10  9.90e-01
---------------------------------------------------------------------------------------------
Terminated with status = Solved
solve time = 9.035757358s
-------------------------------------------------------------------------------
                                    Summary
-------------------------------------------------------------------------------

Plot coil windings

loops = coil.s.discretize(N_contours=10)
loops.plot_loops()

# Display the assembled scene (Sphinx-Gallery captures it during docs builds).
if "plotter" in globals() and not plotter._closed:
    plotter.show()
head gradient coil design
Processing contour, value: -968298.7510700395
Processing contour, value: -756379.0787563832
Processing contour, value: -544459.4064427267
Processing contour, value: -332539.7341290704
Processing contour, value: -120620.061815414
Processing contour, value: 91299.61049824231
Processing contour, value: 303219.28281189874
Processing contour, value: 515138.95512555516
Processing contour, value: 727058.6274392114
Processing contour, value: 938978.2997528678

Total running time of the script: (0 minutes 33.164 seconds)

Gallery generated by Sphinx-Gallery