# 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])

coil.plot_mesh(opacity=0.2)
mlab.quiver3d(*target_points.T, *sphfield.T)

target_spec = {
    "coupling": coil.B_coupling(target_points),
    "abs_error": 0.1,
    "target": target_field,
}

#%%
# Run QP solver
import mosek

coil.s, prob = optimize_streamfunctions(
    coil,
    [target_spec],
Exemplo n.º 2
0
stray_points_mesh = trimesh.creation.icosphere(subdivisions=3,
                                               radius=stray_radius)
stray_points = stray_points_mesh.vertices + center

n_stray_points = len(stray_points)

#%%
# Plot geometry
if PLOT:
    f = mlab.figure(None,
                    bgcolor=(1, 1, 1),
                    fgcolor=(0.5, 0.5, 0.5),
                    size=(800, 800))

    coil.plot_mesh(representation="wireframe",
                   opacity=0.1,
                   color=(0, 0, 0),
                   figure=f)
    coil.plot_mesh(representation="surface",
                   opacity=0.1,
                   color=(0, 0, 0),
                   figure=f)
    mlab.points3d(*target_points.T, color=(0, 0, 1))
    mlab.points3d(*stray_points.T, scale_factor=0.3, color=(1, 0, 0))

    f.scene.isometric_view()
    f.scene.camera.zoom(1.5)

    if SAVE_FIGURES:
        mlab.savefig(
            SAVE_DIR + "biplanar_geometry.png",
            figure=f,
Exemplo n.º 3
0
                             process=False)

mesh1 = combine_meshes((coil_plus, coil_minus))
mesh2 = mesh1.copy()
mesh2.apply_scale(1.4)

coil = MeshConductor(mesh_obj=mesh1, basis_name="inner", N_sph=4)
shieldcoil = MeshConductor(mesh_obj=mesh2, basis_name="inner", N_sph=4)

#%%
# Plot geometry
f = mlab.figure(None,
                bgcolor=(1, 1, 1),
                fgcolor=(0.5, 0.5, 0.5),
                size=(800, 800))
coil.plot_mesh(opacity=0.2, figure=f)
shieldcoil.plot_mesh(opacity=0.2, figure=f)

#%%
# Compute inductances and coupling

M11 = coil.inductance
M22 = shieldcoil.inductance
M21 = shieldcoil.mutual_inductance(coil)

# Mapping from I1 to I2, constraining flux through shieldcoil to zero
P = -np.linalg.solve(M22, M21)

A1, Beta1 = coil.sph_couplings
A2, Beta2 = shieldcoil.sph_couplings
Exemplo n.º 4
0
if SAVE:
    mlab.savefig(
        SAVE_DIR + "surface_harmonics.png",
        figure=f,
        magnification=4,
    )

    mlab.close()

f = mlab.figure(None,
                bgcolor=(1, 1, 1),
                fgcolor=(0.5, 0.5, 0.5),
                size=(750, 600))

c.plot_mesh(representation="wireframe", figure=f)
c.plot_mesh(opacity=0.2, figure=f)
#
# f.scene.camera.parallel_projection=1
# mlab.view(0,160)
f.scene.camera.zoom(1.5)
f.scene.z_plus_view()
# f.scene.camera.roll(270)

if SAVE:
    mlab.savefig(
        SAVE_DIR + "suhmesh.png",
        figure=f,
        magnification=6,
    )
    mlab.close()
Exemplo n.º 5
0
import mosek

coil.s, prob = optimize_streamfunctions(
    coil,
    [target_spec],
    objective="minimum_ohmic_power",
    solver="MOSEK",
    solver_opts={"mosek_params": {
        mosek.iparam.num_threads: 8
    }},
)

#%%
# Plot coil windings

f = coil.plot_mesh(opacity=0.2)

loops = coil.s.discretize(N_contours=6)

loops.plot_loops(figure=f)

#%%
# 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,
Exemplo n.º 6
0
# s[63] += 2

s = StreamFunction(s, c)

from mayavi import mlab
from mayavi.api import Engine

engine = Engine()
engine.start()

f = mlab.figure(None,
                bgcolor=(1, 1, 1),
                fgcolor=(0.5, 0.5, 0.5),
                size=(800, 700))
s.plot(figure=f, ncolors=256)
c.plot_mesh(representation="wireframe", figure=f)

j = gradient(s.vert, c.mesh, rotated=True)

Len = np.log(np.linalg.norm(j, axis=0))

vectors = mlab.quiver3d(*c.mesh.triangles_center.T,
                        *j,
                        mode="arrow",
                        colormap="Greens",
                        scalars=Len)

# vectors = engine.scenes[0].children[2].children[0].children[0]
vectors.glyph.glyph.scale_mode = "scale_by_scalar"
vectors.glyph.glyph.scale_factor = 0.6
f.scene.z_plus_view()
Exemplo n.º 7
0
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)

# Plot coil, shield and target points
if PLOT:
    f = mlab.figure(None,
                    bgcolor=(1, 1, 1),
                    fgcolor=(0.5, 0.5, 0.5),
                    size=(800, 800))
    coil.plot_mesh(figure=f, opacity=0.2)
    shield.plot_mesh(figure=f, opacity=0.2)
    mlab.points3d(*target_points.T)

#%%
# Compute C matrices that are used to compute the generated magnetic field

mutual_inductance = coil.mutual_inductance(shield)

# Take into account the field produced by currents induced into the shield
# NB! This expression is for instantaneous step-function switching of coil current, see Eq. 18 in G.N. Peeren, 2003.

shield.M_coupling = np.linalg.solve(-shield.inductance, mutual_inductance.T)
secondary_C = shield.B_coupling(target_points) @ -shield.M_coupling

#%%
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)

# Plot coil, shield and target points

f = mlab.figure(None,
                bgcolor=(1, 1, 1),
                fgcolor=(0.5, 0.5, 0.5),
                size=(800, 800))

coil.plot_mesh(representation="surface", figure=f, opacity=0.5)
shield.plot_mesh(representation="surface", opacity=0.2, figure=f)
mlab.points3d(*target_points.T)

f.scene.isometric_view()
f.scene.camera.zoom(1.1)

#%%
# Let's design a coil without taking the magnetic shield into account

# The absolute target field amplitude is not of importance,
# and it is scaled to match the C matrix in the optimization function
target_field = np.zeros(target_points.shape)
target_field[:, 0] = target_field[:, 0] + 1  # Homogeneous Y-field

target_abs_error = np.zeros_like(target_field)