-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw_multi_field.py
More file actions
executable file
·110 lines (89 loc) · 2.38 KB
/
Copy pathdraw_multi_field.py
File metadata and controls
executable file
·110 lines (89 loc) · 2.38 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/python
## MODULES
from time import perf_counter
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm
import bfield
v_steps = 30 # circles around the magnet
cir_steps = 100 # steps around the circle
a = 0.02 # radius of the magnet in meters
b = 0.005 # length of the magnet in meters
m = 1 # magnetization in Am^2
magnet_pos = np.array([[0, -0.02], [0, 0.02]])
magnet_ori = np.array([1, -1])
grid = 500
grid_size = 0.064
x = np.linspace(-grid_size / 2, grid_size / 2, grid)
z = np.linspace(-grid_size / 2, grid_size / 2, grid)
X, Z = np.meshgrid(x, z)
Bx, Bz = np.meshgrid(np.zeros(grid), np.zeros(grid))
print("Solving Biot-Savart Array")
t1_start = perf_counter()
for mag_num, mpos in enumerate(magnet_pos):
for i in tqdm(range(grid)):
for y in range(grid):
xd = magnet_ori[mag_num] * bfield.solution(
np.array([x[i] - mpos[0], 0, z[y] - mpos[1]]),
moment=m,
mradius=a,
mheight=b,
accuracy=[v_steps, cir_steps],
)
Bx[y, i] += xd[0]
Bz[y, i] += xd[2]
t1_stop = perf_counter()
print("Elapsed time:", (t1_stop - t1_start), "s")
B_mag = np.log10(np.sqrt(Bx**2 + Bz**2))
# Plot the results
fig, ax = plt.subplots(figsize=(10, 10))
# Plot colored background showing field intensity
bg = ax.pcolormesh(
X,
Z,
B_mag,
cmap="inferno",
shading="auto",
alpha=0.75,
zorder=0,
)
# Plot the B-field streamlines on top
print("Rendering Stream Plot")
stream = ax.streamplot(
X,
Z,
Bx,
Bz,
density=2,
color="black",
linewidth=1,
arrowsize=0.8,
broken_streamlines=True,
zorder=1,
)
# Plot the magnet
for magnet_p in magnet_pos:
ax.add_patch(
plt.Rectangle(
(magnet_p[0] - a, magnet_p[1] - b / 2),
2 * a,
b,
fill=True,
facecolor="grey",
edgecolor="black",
zorder=2,
)
)
# Add colorbars
cbar_bg = fig.colorbar(bg, ax=ax, fraction=0.046, pad=0.1)
cbar_bg.set_label("Log Magnetic Field strength (T)")
ax.set_xlabel("x (m)")
ax.set_ylabel("z (m)")
ax.set_title("B-field (log) Around a Cylindrical Magnet")
ax.set_aspect("equal")
ax.set_xlim(-grid_size / 2, grid_size / 2)
ax.set_ylim(-grid_size / 2, grid_size / 2)
# plt.tight_layout()
print("Finished")
plt.tight_layout()
plt.show()