Esempio n. 1
0
def test_vs_analytic():

    obj = _fake_mesh_conductor("unit_disc")
    mesh = obj.mesh

    mesh.vertices, mesh.faces = trimesh.remesh.subdivide(mesh.vertices, mesh.faces)

    # Fix the simulation parameters
    d = 100e-6  # thickness
    sigma = 3.7e7  # conductivity
    res = 1 / sigma  # resistivity
    T = 300  # temperature
    kB = 1.38064852e-23  # Boltz
    mu0 = 4 * np.pi * 1e-7  # permeability of freespace

    # Compute the AC-current modes and visualize them
    vl, u = thermal_noise.compute_current_modes(
        obj=mesh, T=T, resistivity=res, thickness=d, mode="AC", return_eigenvals=True
    )

    # Define field points on z axis
    Np = 10
    z = np.linspace(0.2, 1, Np)
    fp = np.array((np.zeros(z.shape), np.zeros(z.shape), z)).T

    B_coupling = magnetic_field_coupling(
        mesh, fp, analytic=True
    )  # field coupling matrix

    # Compute noise variance
    B = np.sqrt(thermal_noise.noise_var(B_coupling, vl))

    # Next, we compute the DC noise without reference to the inductance
    vl_dc, u_dc = thermal_noise.compute_current_modes(
        obj=mesh, T=T, resistivity=res, thickness=d, mode="DC", return_eigenvals=True
    )

    # Compute noise variance
    B_dc = np.sqrt(thermal_noise.noise_var(B_coupling, vl_dc))

    # Calculate Bz noise using analytical formula and plot the results
    r = 1
    Ban = (
        mu0
        * np.sqrt(sigma * d * kB * T / (8 * np.pi * z ** 2))
        * (1 / (1 + z ** 2 / r ** 2))
    )

    assert_allclose(B_dc[:, 2], B[:, 2, 0])

    assert_allclose(Ban, B[:, 2, 0], rtol=5e-2)
Esempio n. 2
0
def test_magnetic_field_coupling():

    mesh = load_example_mesh("unit_disc")

    points = mesh.vertices + np.array([0, 0, 20])

    B_1_chunk = mesh_magnetics.magnetic_field_coupling(mesh, points, Nchunks=1)
    B_2_chunk = mesh_magnetics.magnetic_field_coupling(mesh, points, Nchunks=2)

    assert_allclose(B_1_chunk, B_2_chunk, atol=1e-16)

    B_q_1 = mesh_magnetics.magnetic_field_coupling(mesh, points, quad_degree=1)
    B_q_2 = mesh_magnetics.magnetic_field_coupling(mesh, points, quad_degree=2)
    B_q_3 = mesh_magnetics.magnetic_field_coupling(mesh, points, quad_degree=3)

    assert_allclose(B_q_1, B_q_2, atol=1e-3 * np.mean(np.abs(B_q_2)))
    assert_allclose(B_q_2, B_q_3, atol=1e-3 * np.mean(np.abs(B_q_2)))

    B_a = mesh_magnetics.magnetic_field_coupling(mesh, points, analytic=True)

    assert_allclose(B_a, B_q_1, atol=1e-3 * np.mean(np.abs(B_a)))
    assert_allclose(B_a, B_q_2, atol=1e-3 * np.mean(np.abs(B_a)))
    assert_allclose(B_a, B_q_3, atol=1e-3 * np.mean(np.abs(B_a)))
Esempio n. 3
0
# Stream functions
s = disc.vertices[:, 0]**2 + disc.vertices[:, 1]**2

#%% Create evaluation points and plot them
Np = 50
z = np.linspace(-1, 10, Np) + 0.001
points = np.zeros((Np, 3))
points[:, 2] = z
mlab.triangular_mesh(*disc.vertices.T,
                     disc.faces,
                     scalars=s,
                     colormap="viridis")
mlab.points3d(*points.T, scale_factor=0.1)

bfield_mesh = magnetic_field_coupling(disc, points, analytic=True) @ s

#%% Plot asymptotic error with different radii
Nr = 100
drs = np.linspace(-0.0035, -0.0034, Nr)
from matplotlib import cm

colors = cm.viridis(np.linspace(0, 1, Nr))
err = []
for c, dr in zip(colors, drs):
    err.append((abs((bfield_mesh[:, 2] - field_disc(z, 1 + dr)) /
                    field_disc(z, 1 + dr)))[-1])
plt.plot(1 + drs, err)
plt.ylabel("error")
plt.xlabel("disc radius")
#%% Determine the "most fit" radius and compare
Esempio n. 4
0
# Unit sphere
# ------------


Np = 10
radius = np.linspace(0.1, 1, Np)
fp = np.zeros((1, 3))

B = np.zeros((Np, 3))
for i in range(Np):
    mesh = trimesh.load(
        pkg_resources.resource_filename("bfieldtools", "example_meshes/unit_sphere.stl")
    )
    mesh.apply_scale(radius[i])

    B_coupling = magnetic_field_coupling(mesh, fp, analytic=True)

    vl = compute_current_modes(
        obj=mesh,
        T=T,
        resistivity=1 / sigma,
        thickness=d,
        mode="AC",
        return_eigenvals=False,
    )

    #    scene = mlab.figure(None, bgcolor=(1, 1, 1), fgcolor=(0.5, 0.5, 0.5),
    #               size=(800, 800))
    #    visualize_current_modes(mesh,vl[:,:,0], 8, 1)

    vl[:, 0] = np.zeros(vl[:, 0].shape)  # fix DC-component
Esempio n. 5
0
    mesh, plane.vertices, multiply_coeff=False, approx_far=True
)

uprim = phi0x(plane.vertices)
usec = (mu_r - 1) / (4 * np.pi) * Uplane @ u
uplane = uprim + usec

# Meshgrid on the same plane for the bfield
X, Y = np.meshgrid(
    np.linspace(-1.5, 1.5, 50), np.linspace(-1.5, 1.5, 50), indexing="ij"
)
pp = np.zeros((50 * 50, 3))
pp[:, 0] = X.flatten()
pp[:, 1] = Y.flatten()

Bplane = magnetic_field_coupling(mesh, pp, analytic=True)
bprim = pp * 0  # copy pp
# add x directional field
mu0 = 1e-7 * 4 * np.pi
bprim[:, 0] = -1
# In this simulation mu0==1 is assumed
# magnetic_field_coupling uses mu0 in SI units
bplane = (mu_r - 1) / (4 * np.pi) / mu0 * Bplane @ u + bprim
# uplane[Uplane.sum(axis=1)]

#%% Plot the results
mlab.figure("Potential for B", bgcolor=(1, 1, 1), size=(1000, 1000))
m = mlab.triangular_mesh(*plane.vertices.T, plane.faces, scalars=uplane, colormap="bwr")
m.actor.mapper.interpolate_scalars_before_mapping = True
m.module_manager.scalar_lut_manager.number_of_colors = 32
# vectors = mlab.quiver3d(*(plane.triangles_center + np.array([0,0,0.001])).T,
Esempio n. 6
0
from bfieldtools.mesh_conductor import MeshConductor
import pkg_resources


# Load simple plane mesh that is centered on the origin
file_obj = pkg_resources.resource_filename(
    "bfieldtools", "example_meshes/10x10_plane.obj"
)
coilmesh = trimesh.load(file_obj, process=False)
coil = MeshConductor(mesh_obj=coilmesh)
weights = np.zeros(coilmesh.vertices.shape[0])
weights[coil.inner_vertices] = 1

test_points = coilmesh.vertices + np.array([0, 1, 0])

B0 = magnetic_field_coupling(coilmesh, test_points) @ weights
B1 = magnetic_field_coupling_analytic(coilmesh, test_points) @ weights


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

s = mlab.triangular_mesh(
    *coilmesh.vertices.T, coilmesh.faces, scalars=weights, colormap="viridis"
)
s.enable_contours = True
s.actor.property.render_lines_as_tubes = True
s.actor.property.line_width = 3.0

mlab.quiver3d(*test_points.T, *B0.T, color=(1, 0, 0))
mlab.quiver3d(*test_points.T, *B1.T, color=(0, 0, 1))
                              thickness=d,
                              mode="AC",
                              return_eigenvals=True)

scene = mlab.figure(None,
                    bgcolor=(1, 1, 1),
                    fgcolor=(0.5, 0.5, 0.5),
                    size=(800, 800))
visualize_current_modes(mesh, vl[:, :, 0], 42, 5, contours=True)

# Define field points on z axis
Np = 30
z = np.linspace(0.1, 1, Np)
fp = np.array((np.zeros(z.shape), np.zeros(z.shape), z)).T

B_coupling = magnetic_field_coupling(mesh, fp,
                                     analytic=True)  # field coupling matrix

# Compute noise variance
B = np.sqrt(noise_var(B_coupling, vl))

# Calculate Bz noise using analytical formula and plot the results
r = 1
Ban = (mu0 * np.sqrt(sigma * d * kB * T / (8 * np.pi * z**2)) *
       (1 / (1 + z**2 / r**2)))

plt.figure()
plt.subplot(2, 1, 1)
plt.semilogy(z, Ban, label="Analytic")
plt.semilogy(z, B[:, 2, 0], "x", label="Numerical")
plt.legend(frameon=False)
plt.xlabel("Distance d/R")