-
Notifications
You must be signed in to change notification settings - Fork 35
/
test_getBH_dict.py
350 lines (287 loc) · 9.95 KB
/
test_getBH_dict.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import pytest
import numpy as np
from scipy.spatial.transform import Rotation as R
import magpylib
from magpylib.magnet import Cuboid, Cylinder, Sphere
from magpylib import getB, getH
from magpylib._src.exceptions import MagpylibBadUserInput
def test_getB_dict1():
"""test field wrapper functions
"""
pos_obs = (11,2,2)
mag = [111,222,333]
dim = [3,3]
pm = Cylinder(mag, dim)
pm.move(np.linspace((.5,0,0), (7.5,0,0), 15), start=-1)
pm.rotate_from_angax(np.linspace(0,666,25), 'y', anchor=0)
pm.move([(0,x,0) for x in np.linspace(0,5,5)], start=-1)
B2 = pm.getB(pos_obs)
pos = pm.position
rot = pm.orientation
dic = {
'magnetization': mag,
'dimension': dim,
'position': pos,
'orientation':rot
}
B1 = getB('Cylinder', pos_obs, **dic)
assert np.allclose(B1, B2, rtol=1e-12, atol=1e-12)
def test_getB_dict2():
"""test field wrapper functions
"""
pos_obs = (11,2,2)
mag = [111,222,333]
dim = [3,3]
pos = [(1,1,1),(2,2,2),(3,3,3),(5,5,5)]
dic = {
'magnetization': mag,
'dimension': dim,
'position': pos
}
B1 = getB('Cylinder', pos_obs, **dic)
pm = Cylinder(mag, dim, position=pos)
B2 = getB([pm],pos_obs)
assert np.allclose(B1, B2, rtol=1e-12, atol=1e-12)
def test_getH_dict1():
"""test field wrapper functions
"""
pos_obs = (11,2,2)
mag = [111,222,333]
dim = [3,3]
dic = {
'magnetization': mag,
'dimension': dim,
}
B1 = getH('Cylinder', pos_obs, **dic)
pm = Cylinder(mag, dim)
B2 = pm.getH(pos_obs)
assert np.allclose(B1, B2, rtol=1e-12, atol=1e-12)
def test_getB_dict3():
"""test field wrapper functions
"""
n = 25
pos_obs = np.array([1,2,2])
mag = [[111,222,333],]*n
dim = [3,3,3]
pos = np.array([0,0,0])
rot = R.from_quat([(t,.2,.3,.4) for t in np.linspace(0,.1,n)])
dic = {
'magnetization': mag,
'dimension': dim,
'position': pos,
'orientation': rot
}
B1 = getB('Cuboid', pos_obs, **dic)
B2 = []
for i in range(n):
pm = Cuboid(mag[i],dim,pos,rot[i])
B2 += [pm.getB(pos_obs)]
B2 = np.array(B2)
print(B1-B2)
assert np.allclose(B1, B2, rtol=1e-12, atol=1e-12)
def test_getH_dict3():
"""test field wrapper functions
"""
pos_obs = (1,2,2)
mag = [[111,222,333],[22,2,2],[22,-33,-44]]
dim = 3
dic = {
'magnetization': mag,
'diameter': dim
}
B1 = getH('Sphere', pos_obs, **dic)
B2 = []
for i in range(3):
pm = Sphere(mag[i], dim)
B2 += [getH([pm], pos_obs)]
B2 = np.array(B2)
assert np.allclose(B1, B2, rtol=1e-12, atol=1e-12)
def test_getB_dict4():
"""test field wrapper functions
"""
n = 25
pos_obs = np.array([1,2,2])
mag = [[111,222,333],]*n
dim = 3
pos = np.array([0,0,0])
rot = R.from_quat([(t,.2,.3,.4) for t in np.linspace(0,.1,n)])
dic = {
'magnetization': mag,
'diameter': dim,
'position': pos,
'orientation': rot
}
B1 = getB('Sphere', pos_obs, **dic)
B2 = []
for i in range(n):
pm = Sphere(mag[i], dim, pos, rot[i])
B2 += [pm.getB(pos_obs)]
B2 = np.array(B2)
print(B1-B2)
assert np.allclose(B1, B2, rtol=1e-12, atol=1e-12)
def test_geBHv_dipole():
""" test if Dipole implementation gives correct output
"""
B = getB('Dipole', (1,1,1), moment=(1,2,3))
Btest = np.array([0.07657346,0.06125877,0.04594407])
assert np.allclose(B,Btest)
H = getH('Dipole', (1,1,1), moment=(1,2,3))
Htest = np.array([0.06093522,0.04874818,0.03656113])
assert np.allclose(H,Htest)
def test_geBHv_circular():
""" test if Loop implementation gives correct output
"""
B = getB('Loop', (0,0,0), current=1, diameter=2)
Btest = np.array([0,0,0.6283185307179586])
assert np.allclose(B,Btest)
H = getH('Loop', (0,0,0), current=1, diameter=2)
Htest = np.array([0,0,0.6283185307179586*10/4/np.pi])
assert np.allclose(H,Htest)
def test_getBHv_squeeze():
""" test if squeeze works
"""
B1 = getB('Loop', (0,0,0) , current=1, diameter=2)
B2 = getB('Loop', [(0,0,0)] , current=1, diameter=2)
B3 = getB('Loop', [(0,0,0)] , current=1, diameter=2, squeeze=False)
B4 = getB('Loop', [(0,0,0)]*2, current=1, diameter=2)
assert B1.ndim == 1
assert B2.ndim == 1
assert B3.ndim == 2
assert B4.ndim == 2
def test_getBHv_line():
""" test getBHv with Line
"""
H = getH(
'Line',
[(1,1,1),(1,2,3),(2,2,2)],
current=1,
segment_start=(0,0,0),
segment_end=[(0,0,0),(2,2,2),(2,2,2)])
x = np.array([[0,0,0], [0.02672612, -0.05345225, 0.02672612], [0,0,0]])*10/4/np.pi
assert np.allclose(x, H)
def test_getBHv_line2():
""" test line with pos and rot arguments
"""
x = 0.14142136
# z-line on x=1
B1 = getB('Line', (0,0,0), current=1,
segment_start=(1,0,-1), segment_end=(1,0,1))
assert np.allclose(B1, np.array([0,-x,0]))
# move z-line to x=-1
B2 = getB('Line', (0,0,0), position=(-2,0,0), current=1,
segment_start=(1,0,-1), segment_end=(1,0,1))
assert np.allclose(B2, np.array([0,x,0]))
# rotate 1
rot = R.from_euler('z', 90, degrees=True)
B3 = getB('Line', (0,0,0), orientation=rot, current=1,
segment_start=(1,0,-1), segment_end=(1,0,1))
assert np.allclose(B3, np.array([x,0,0]))
# rotate 2
rot = R.from_euler('x', 90, degrees=True)
B4 = getB('Line', (0,0,0), orientation=rot, current=1,
segment_start=(1,0,-1), segment_end=(1,0,1))
assert np.allclose(B4, np.array([0,0,-x]))
# rotate 3
rot = R.from_euler('y', 90, degrees=True)
B5 = getB('Line', (0,0,0), orientation=rot, current=1,
segment_start=(1,0,-1), segment_end=(1,0,1))
assert np.allclose(B5, np.array([0,-x,0]))
def test_BHv_Cylinder_FEM():
"""test against FEM"""
ts = np.linspace(0,2,31)
obsp = [(t,t,t) for t in ts]
Bfem = np.array([
(-0.0300346254954609,-0.00567085248536589,-0.0980899423563197),
(-0.0283398999697276,-0.00136726650574628,-0.10058277210005),
(-0.0279636086648847,0.00191033319772333,-0.102068667474779),
(-0.0287959403346942,0.00385627171155148,-0.102086609934239),
(-0.0298064414078247,0.00502298395545467,-0.101395051504575),
(-0.0309138327020785,0.00585315159763698,-0.0994210978208941),
(-0.0304478836262897,0.00637062970240076,-0.0956959733446996),
(-0.0294756102340511,0.00796586777139283,-0.0909716586168481),
(-0.0257014555198541,0.00901347002514088,-0.0839378050637996),
(-0.0203392379411272,0.0113401710780434,-0.0758447872526493),
(-0.0141186721748514,0.014275060463367,-0.0666447793887049),
(-0.00715638330645336,0.0169990957749629,-0.0567988806666027),
(-0.000315107745706201,0.0196025044167515,-0.0471345331233655),
(0.00570680487262037,0.0216935664564627,-0.0379802748006986),
(0.0106937560983821,0.0229598553802506,-0.029816827145783),
(0.0147153251512036,0.0237740278061223,-0.0226247514391129),
(0.0173457909761498,0.0240321714861875,-0.0167312828159773),
(0.0193755103218335,0.023674091804632,-0.0119446813034152),
(0.0204291390948416,0.0230735973599725,-0.00805340729977855),
(0.0207908036651642,0.0221875600164857,-0.00496582571560478),
(0.020692112773328,0.0211419193131436,-0.00269563642259977),
(0.0202607525969918,0.0199897027578393,-0.000891130303443818),
(0.0195698099586468,0.0187793271229261,0.000332964123866357),
(0.0187342589014612,0.0175395229794614,0.00128198337775133),
(0.0178090320514157,0.0163998590430951,0.00196979345612218),
(0.0168069297247124,0.0152418998801328,0.00243910426847474),
(0.0158127817011691,0.0141524929704775,0.00274664013462767),
(0.0148149313600427,0.013148844940711,0.00293212192295656),
(0.013878964772737,0.0121841676914905,0.00302995618189322),
(0.0129803941608119,0.0113011353152514,0.00305232762136824),
(0.0121250819870128,0.0104894041620816,0.00303690098080925)])
# compare against FEM
B = getB(
'CylinderSegment',
obsp,
dimension=(1,2,1,90,360),
magnetization=np.array((1,2,3))*1000/np.sqrt(14),
position = (0,0,0.5),
)
err = np.linalg.norm(B-Bfem*1000, axis=1)/np.linalg.norm(B,axis=1)
assert np.amax(err)<0.01
def test_BHv_solid_cylinder():
"""compare multiple solid-cylinder solutions against each other"""
# combine multiple slices to one big Cylinder
B1 = getB(
'CylinderSegment',
(1,2,3),
dimension=[(0,1,2,20,120), (0,1,2,120,220), (0,1,2,220,380)],
magnetization=(22,33,44),
)
B1 = np.sum(B1,axis=0)
# one big cylinder
B2 = getB(
'CylinderSegment',
(1,2,3),
dimension=(0,1,2,0,360),
magnetization=(22,33,44),
)
# compute with solid cylinder code
B3 = getB(
'Cylinder',
(1,2,3),
dimension=(2,2),
magnetization=(22,33,44),
)
assert np.allclose(B1,B2)
assert np.allclose(B1,B3)
def test_getB_dict_over_getB():
"""test field wrapper functions
"""
pos_obs = (11,2,2)
mag = [111,222,333]
dim = [3,3]
pm = Cylinder(mag, dim)
pm.move(np.linspace((.5,0,0),(7.5,0,0),15))
pm.rotate_from_angax(np.linspace(0,666,25), 'y', anchor=0)
pm.move([(0,x,0) for x in np.linspace(0,5,5)])
B2 = pm.getB(pos_obs)
pos = pm.position
rot = pm.orientation
dic = {
'sources': 'Cylinder',
'observers': pos_obs,
'magnetization': mag,
'dimension': dim,
'position': pos,
'orientation':rot
}
B1 = magpylib.getB(**dic)
assert np.allclose(B1, B2, rtol=1e-12, atol=1e-12)
# test for kwargs if sources is not a string
dic['sources'] = pm
with pytest.raises(MagpylibBadUserInput):
magpylib.getB(**dic)