def test_scalar_contour_direction(): """ Test the direction of scalar_contour and the field direction generated by the scalar contour """ mesh, scalars = setup_contour_input() N = 10 polys, vals = contour.scalar_contour(mesh, scalars, N_contours=N, return_values=True) compare_contour_direction_to_rotated_gradient(mesh, scalars, polys[-1]) test_point = np.array([[0, 0, 1]]) compare_magnetic_field_directions(mesh, scalars, polys, test_point)
CU1 = coil.U_coupling(points) CU2 = shieldcoil.U_coupling(points) B1 = CB1 @ coil.s B2 = CB2 @ shieldcoil.s U1 = CU1 @ coil.s U2 = CU2 @ shieldcoil.s #%% # Now, plot the field streamlines and scalar potential from bfieldtools.contour import scalar_contour cc1 = scalar_contour(mesh1, mesh1.vertices[:, 2], contours=[-0.001]) cc2 = scalar_contour(mesh2, mesh2.vertices[:, 2], contours=[-0.001]) cx10 = cc1[0][:, 1] cy10 = cc1[0][:, 0] cx20 = cc2[0][:, 1] cy20 = cc2[0][:, 0] cx11 = np.vstack(cc1[1:])[:, 1] cy11 = np.vstack(cc1[1:])[:, 0] cx21 = np.vstack(cc2[1:])[:, 1] cy21 = np.vstack(cc2[1:])[:, 0] B = (B1.T + B2.T)[:2].reshape(2, x.shape[0], y.shape[0]) lw = np.sqrt(B[0]**2 + B[1]**2) lw = 2 * np.log(lw / np.max(lw) * np.e + 1.1)
coil.s, prob = optimize_streamfunctions( coil, [target_spec, stray_spec], objective=(0, 1), solver="MOSEK", solver_opts={"mosek_params": { mosek.iparam.num_threads: 8 }}, ) #%% # Plot coil windings and target points N_contours = 6 loops = scalar_contour(coil.mesh, coil.s.vert, N_contours=N_contours) if PLOT: f = mlab.figure(None, bgcolor=(1, 1, 1), fgcolor=(0.5, 0.5, 0.5), size=(650, 750)) mlab.clf() plot_3d_current_loops(loops, colors="auto", figure=f) # B_target = coil.B_coupling(target_points) @ coil.s # mlab.quiver3d(*target_points.T, *B_target.T, mode="arrow", scale_factor=1) f.scene.isometric_view()
s1 = StreamFunction(I1inner, coil1) s2 = StreamFunction(I2inner, coil2) # s = mlab.triangular_mesh(*mesh1.vertices.T, mesh1.faces, scalars=I1) # s.enable_contours=True # s = mlab.triangular_mesh(*mesh2.vertices.T, mesh2.faces, scalars=I2) # s.enable_contours=True B1 = CB1 @ s1 B2 = CB2 @ s2 U1 = CU1 @ s1 U2 = CU2 @ s2 #%% Plot cc1 = scalar_contour(mesh1, mesh1.vertices[:, 2], contours=[-0.001]) cc2 = scalar_contour(mesh2, mesh2.vertices[:, 2], contours=[-0.001]) cx10 = cc1[0][:, 1] cy10 = cc1[0][:, 0] cx20 = cc2[0][:, 1] cy20 = cc2[0][:, 0] cx11 = cc1[1][:, 1] cy11 = cc1[1][:, 0] cx21 = cc2[1][:, 1] cy21 = cc2[1][:, 0] B = (B1.T + B2.T)[:2].reshape(2, x.shape[0], y.shape[0]) lw = np.sqrt(B[0]**2 + B[1]**2) lw = 2 * np.log(lw / np.max(lw) * np.e + 1.1)
[target_spec] + induction_spec, objective="minimum_inductive_energy", solver="MOSEK", solver_opts={"mosek_params": { mosek.iparam.num_threads: 8 }}, ) from bfieldtools.mesh_conductor import StreamFunction shield.induced_s = StreamFunction(shield.M_coupling @ coil.s, shield) #%% # Plot coil windings and target points loops = scalar_contour(coil.mesh, coil.s.vert, N_contours=6) # loops = [simplify_contour(loop, min_edge=1e-2, angle_threshold=2e-2, smooth=True) for loop in loops] # loops = [loop for loop in loops if loop is not None] if PLOT: f = mlab.figure(None, bgcolor=(1, 1, 1), fgcolor=(0.5, 0.5, 0.5), size=(600, 500)) mlab.clf() plot_3d_current_loops(loops, colors="auto", figure=f, tube_radius=0.005) B_target = coil.B_coupling(target_points) @ coil.s
import mosek coil.s, coil.prob = optimize_streamfunctions( coil, [target_spec], objective="minimum_inductive_energy", solver="MOSEK", solver_opts={"mosek_params": { mosek.iparam.num_threads: 8 }}, ) #%% # Plot coil windings and target points loops = scalar_contour(coil.mesh, coil.s.vert, N_contours=10) f = mlab.figure(None, bgcolor=(1, 1, 1), fgcolor=(0.5, 0.5, 0.5), size=(800, 800)) mlab.clf() plot_3d_current_loops(loops, colors="auto", figure=f) B_target = coil.B_coupling(target_points) @ coil.s mlab.quiver3d(*target_points.T, *B_target.T, mode="arrow", scale_factor=0.75) f.scene.isometric_view() f.scene.camera.zoom(0.95)
bb = np.zeros(M.shape[1]) bb[: M11.shape[1]] = bi I = np.linalg.solve(M, bb) I1 = I[: M11.shape[1]] I2 = I[M11.shape[1] :] B1 = CB1 @ I1 B2 = CB2 @ I2 U1 = CU1 @ I1 U2 = CU2 @ I2 # Plot # Extract the cross-sections of the plane and the surfaces cc1 = scalar_contour(mesh1, mesh1.vertices[:, 2], contours=[-0.001])[0] cc2 = scalar_contour(mesh2, mesh2.vertices[:, 2], contours=[-0.001])[0] B = (B1.T + B2.T)[:2].reshape(2, x.shape[0], y.shape[0]) lw = np.sqrt(B[0] ** 2 + B[1] ** 2) lw = 2 * lw / np.max(lw) xx = np.linspace(-1, 1, 16) # seed_points = 0.51*np.array([xx, -np.sqrt(1-xx**2)]) # seed_points = np.hstack([seed_points, (0.51*np.array([xx, np.sqrt(1-xx**2)]))]) seed_points = np.array([cc1[:, 0], cc1[:, 1]]) * 1.01 # plt.streamplot(x,y, B[1], B[0], density=2, linewidth=lw, color='k', # start_points=seed_points.T, integration_direction='both') U = (U1 + U2).reshape(x.shape[0], y.shape[0]) U /= np.max(U) plt.figure() plt.contourf(X, Y, U.T, cmap="seismic", levels=40)
-0.28276020498745635, 0.33658483701858727, 0.898196701154387, ] scene.scene.camera.clipping_range = [16.139073445910277, 116.31572537292347] scene.scene.camera.compute_view_plane_normal() scene.scene.render() #%% U2_shield = shield.U_coupling(points) U2_prim = coil.U_coupling(points) #%% from bfieldtools.contour import scalar_contour cc1 = scalar_contour(planemesh, planemesh.vertices[:, 2], contours=[-0.1]) cc1 = np.vstack(cc1) cc1a = cc1[:cc1.shape[0] // 2] cc1b = cc1[cc1.shape[0] // 2:] cc2 = scalar_contour(shieldmesh, shieldmesh.vertices[:, 2], contours=[-0.1]) cc2 = np.vstack(cc2) cc2a = cc1[:cc2.shape[0] // 2] cc2b = cc1[cc2.shape[0] // 2:] import matplotlib.pyplot as plt import matplotlib.colors as colors def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=256): new_cmap = colors.LinearSegmentedColormap.from_list( "trunc({n},{a:.2f},{b:.2f})".format(n=cmap.name, a=minval, b=maxval),