-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
148 lines (111 loc) · 4.03 KB
/
test.py
File metadata and controls
148 lines (111 loc) · 4.03 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from jax import grad
import jax.numpy as jnp
def dm(x):
return r @ jnp.array([-jnp.sin(x), jnp.cos(x), 0.])
from jax import jacfwd, jacrev
f = lambda x: B(p, dm(x), r)
J = jacfwd(F, argnums=1)(p, 1.)
print(J)
x=0.
print(Jf(p, dm(1.)))
X = cp.Constant(np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]))
cons = cp.sum(X) == 6
print(cons.value())
ans = cp.sum(X, axis=0) <= 15
ans.value()
# Generating all combinations of angles
K=3
lins = [np.linspace(0, 1.5*np.pi, 2) for i in range(K)]
# lins.append(np.linspace(0, 2*np.pi, d) + np.pi/4)
angles = np.array(np.meshgrid(*lins)).T.reshape(-1, K)
print(angles)
import jax.numpy as jnp
def B(p_i, theta):
dm_i = r @ jnp.array([jnp.cos(theta), jnp.sin(theta), 0])
r_i = target - p_i
r_i_hat = r_i / jnp.linalg.norm(r_i)
return mu_0 * moment / (4 * jnp.pi * jnp.linalg.norm(r_i) ** 3) * ((3 * jnp.outer(r_i_hat, r_i_hat) - jnp.eye(3)) @ dm_i)
def F(p_i, theta):
dm_i = r @ jnp.array([jnp.cos(theta), jnp.sin(theta), 0])
r_i = target - p_i
r_i_hat = r_i / jnp.linalg.norm(r_i)
return 3 * mu_0 * moment / (4 * jnp.pi * jnp.linalg.norm(r_i) ** 4) \
* jnp.dot(
jnp.outer(dm_i, r_i_hat) +
jnp.outer(r_i_hat, dm_i) -
((5 * jnp.outer(r_i_hat, r_i_hat) - jnp.eye(3)) * jnp.dot(dm_i, r_i_hat))
, mt)
t = np.random.rand(1)[0] * 3.14
def dm(x):
return r @ jnp.array([-jnp.sin(x), jnp.cos(x), 0.])
from jax import jacfwd, jacrev
f = lambda x: B(p, dm(x), r)
J = jacfwd(F, argnums=1)(p, t)
print(J)
x=0.
print(Jf(p, dm(t)))
coll = magpy.Collection()
j = Br / mu_0
angles = res.x
xrange = 0.01
zmax = target[2] + xrange
zmin = target[2] - xrange
ymax = target[1] + xrange
ymin = target[1] - xrange
xmax = ymax
xmin = ymin
xrange2 = 0.05
xy
xz
xy
yz
xz
yz
gridxy = np.array([[(x, y, target[2]) for x in np.linspace(xmin, xmax, 50)] for y in np.linspace(ymin, ymax, 50)])
x_xy, y_xy, _ = np.moveaxis(gridxy, 2, 0)
gridxz = np.array([[(x, target[1], z) for x in np.linspace(xmin, xmax, 50)] for z in np.linspace(zmin, zmax, 50)])
# Create an observer grid in the xy-symmetry plane
grid = np.mgrid[0.2-xrange2:0.2+xrange2:50j, 0.2-xrange2:0.2+xrange2:50j, 0.55:0.55:1j].T[0]
X1, Y1, _ = np.moveaxis(grid, 2, 0)
# Create an observer grid in the yz-symmetry plane
grid2 = np.array([[(0.2, y, z) for y in np.linspace(xmin, xmax, 50)] for z in np.linspace(zmin, zmax, 50)])
_, Y2, Z2 = np.moveaxis(grid2, 2, 0)
# Create an observer grid in the yz-symmetry plane
grid3 = np.array([[(x, 0.2, z) for x in np.linspace(xmin, xmax, 50)] for z in np.linspace(zmin, zmax, 50)])
X3, _, Z3 = np.moveaxis(grid3, 2, 0)
for ind, i in enumerate(inds):
p, r = S[i]
# magpy.magnet.Cuboid(magnetization=(M,0,0), dimension=(0.02,0.01,0.05), position=(-0.074806,0,0))
coll.add(magpy.magnet.Cuboid(magnetization=(j, 0, 0), dimension=(l, l, l), position=p, orientation=R.from_matrix(r)).rotate_from_angax(angles[ind], axis=r.dot([0, 0, 1]), degrees=False))
print("Field at target: ", coll.getB(target))
b = coll.getB(grid)
Bx, By, _ = np.moveaxis(b, 2, 0)
b2 = coll.getB(grid2)
_, By2, Bz2 = np.moveaxis(b2, 2, 0)
b3 = coll.getB(grid3)
Bx3, By3, Bz3 = np.moveaxis(b3, 2, 0)
fig, [ax1,ax2] = plt.subplots(2, 1, figsize=(5, 10))
# splt = ax1.streamplot((X1-0.2)*100, (Y1-0.2)*100, Bx, By, color=1000*(norm(b, axis=2)), cmap="autumn_r")
# ax1.plot(0, 0, 'bo')
splt2 = ax2.streamplot((Y2-0.2)*100, (Z2-0.55)*100, By2, Bz2, color=1000*(norm(b2, axis=2)), cmap="spring_r")
ax2.plot(0, 0, 'bo')
splt = ax1.streamplot((X3-0.2)*100, (Z3-0.55)*100, Bx3, Bz3, color=1000*(norm(b3, axis=2)), cmap="autumn_r")
ax1.plot(0, 0, 'bo')
cb = fig.colorbar(splt.lines, ax=ax1, label="|B| (mT)")
fig.colorbar(splt2.lines, ax=ax2, label="|B| (mT)")
ax1.set(
xlabel="x-position (cm)",
ylabel="z-position (cm)",
yticks=[-1, -0.5, 0, 0.5, 1],
xlim=(-xrange*100, xrange*100),
ylim=(-xrange*100, xrange*100),
)
ax2.set(
xlabel="y-position (cm)",
ylabel="z-position (cm)",
yticks=[-1, -0.5, 0, 0.5, 1],
xlim=(-xrange*100, xrange*100),
ylim=(-xrange*100, xrange*100),
)
# magpy.show(coll, animation=False)
plt.show()