-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagpy_cuboid.py
53 lines (43 loc) · 1.21 KB
/
magpy_cuboid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Field line visualization using pyvista
import magpylib as magpy
import pyvista as pv
# Create a magnet with Magpylib
magnet = magpy.magnet.Cuboid(polarization=(0, 0, 1), dimension=(0.005, 0.004, 0.006))
# Create a 3D grid with Pyvista
grid = pv.ImageData(
dimensions=(41, 41, 41),
spacing=(0.001, 0.001, 0.001),
origin=(-0.02, -0.02, -0.02),
)
# Compute B-field and add as data to grid
grid["B"] = magnet.getB(grid.points) * 1000 # T -> mT
# Compute the field lines
seed = pv.Disc(inner=0.001, outer=0.003, r_res=1, c_res=9)
strl = grid.streamlines_from_source(
seed,
vectors="B",
max_step_length=0.1,
max_time=0.02,
integration_direction="both",
)
# Create a Pyvista plotting scene
pl = pv.Plotter()
# Add magnet to scene
magpy.show(magnet, canvas=pl, backend="pyvista")
# Prepare legend parameters
legend_args = {
"title": "B (mT)",
"title_font_size": 20,
"color": "black",
"position_y": 0.25,
"vertical": True,
}
# Add streamlines and legend to scene
pl.add_mesh(
strl.tube(radius=0.0002),
cmap="bwr",
scalar_bar_args=legend_args,
)
# Prepare and show scene
pl.camera.position = (0.03, 0.03, 0.03)
pl.show()