1
1
# here all core functions should be tested properly - ideally against FEM
2
2
from __future__ import annotations
3
3
4
- import array_api_strict as xp
4
+ import array_api_strict
5
+ import dask .array
6
+ import jax
5
7
import numpy as np
8
+ import pytest
9
+ from array_api_extra .testing import lazy_xp_function , patch_lazy_xp_functions
6
10
7
11
from magpylib .core import (
8
12
current_circle_Hfield ,
16
20
triangle_Bfield ,
17
21
)
18
22
23
+ lazy_xp_function (current_circle_Hfield )
24
+ lazy_xp_function (current_polyline_Hfield )
25
+ lazy_xp_function (dipole_Hfield )
26
+ lazy_xp_function (magnet_cuboid_Bfield )
27
+ lazy_xp_function (magnet_cylinder_axial_Bfield )
28
+ lazy_xp_function (magnet_cylinder_diametral_Hfield )
29
+ lazy_xp_function (magnet_cylinder_segment_Hfield )
30
+ lazy_xp_function (magnet_sphere_Bfield )
31
+ lazy_xp_function (triangle_Bfield )
19
32
20
- def test_magnet_sphere_Bfield ():
33
+
34
+ @pytest .fixture (params = [np , array_api_strict , jax .numpy , dask .array ])
35
+ def xp (request , monkeypatch ):
36
+ patch_lazy_xp_functions (request , monkeypatch , xp = request .param )
37
+ return request .param
38
+
39
+
40
+ def test_magnet_sphere_Bfield (xp ):
21
41
"magnet_sphere_Bfield test"
22
42
B = magnet_sphere_Bfield (
23
43
observers = xp .asarray ([(0 , 0 , 0 )]),
24
44
diameters = xp .asarray ([1 ]),
25
45
polarizations = xp .asarray ([(0 , 0 , 1 )]),
26
46
)
27
47
Btest = xp .asarray ([(0 , 0 , 2 / 3 )])
28
- np .testing .assert_allclose (B , Btest )
48
+ np .testing .assert_allclose (B , Btest , rtol = 1e-4 )
29
49
30
50
31
- def test_current_circle_Hfield ():
51
+ def test_current_circle_Hfield (xp ):
32
52
Htest = xp .asarray ([[0.09098208 , 0.09415448 ], [0.0 , 0.0 ], [0.07677892 , 0.22625335 ]])
33
53
H = current_circle_Hfield (
34
54
r0 = xp .asarray ([1 , 2 ]),
35
55
r = xp .asarray ([1 , 1 ]),
36
56
z = xp .asarray ([1 , 2 ]),
37
57
i0 = xp .asarray ([1 , 3 ]),
38
58
)
39
- np .testing .assert_allclose (H , Htest )
59
+ np .testing .assert_allclose (H , Htest , rtol = 1e-4 )
40
60
41
61
42
- def test_current_polyline_Hfield ():
62
+ def test_current_polyline_Hfield (xp ):
43
63
Htest = xp .asarray ([[0.0 , - 2.29720373 , 2.29720373 ], [0.0 , 0.59785204 , - 0.59785204 ]])
44
64
45
65
H = current_polyline_Hfield (
@@ -48,10 +68,10 @@ def test_current_polyline_Hfield():
48
68
segments_end = xp .asarray ([(1 , 0 , 0 ), (- 1 , 0 , 0 )]),
49
69
currents = xp .asarray ([100 , 200 ]),
50
70
)
51
- np .testing .assert_allclose (H , Htest )
71
+ np .testing .assert_allclose (H , Htest , rtol = 1e-4 )
52
72
53
73
54
- def test_dipole_Hfield ():
74
+ def test_dipole_Hfield (xp ):
55
75
Htest = xp .asarray (
56
76
[
57
77
[2.89501155e-13 , 1.53146915e03 , 1.53146915e03 ],
@@ -62,35 +82,35 @@ def test_dipole_Hfield():
62
82
observers = xp .asarray ([(1 , 1 , 1 ), (2 , 2 , 2 )]),
63
83
moments = xp .asarray ([(1e5 , 0 , 0 ), (0 , 0 , 1e5 )]),
64
84
)
65
- np .testing .assert_allclose (H , Htest )
85
+ np .testing .assert_allclose (H , Htest , atol = 1e-6 )
66
86
67
87
68
- def test_magnet_cuboid_Bfield ():
88
+ def test_magnet_cuboid_Bfield (xp ):
69
89
Btest = xp .asarray (
70
90
[
71
- [1.56103722e -02 , 1.56103722e -02 , - 3.53394965e -17 ],
72
- [7.73243250e -03 , 6.54431406e -03 , 1.04789520e -02 ],
91
+ [1.5610372220113404e -02 , 1.5610372220113404e -02 , - 3.5339496460705743e -17 ],
92
+ [7.7324325000007145e -03 , 6.5443140645650129e -03 , 1.0478952028501879e -02 ],
73
93
]
74
94
)
75
95
B = magnet_cuboid_Bfield (
76
96
observers = xp .asarray ([(1 , 1 , 1 ), (2 , 2 , 2 )]),
77
97
dimensions = xp .asarray ([(1 , 1 , 1 ), (1 , 2 , 3 )]),
78
98
polarizations = xp .asarray ([(0 , 0 , 1 ), (0.5 , 0.5 , 0 )]),
79
99
)
80
- np .testing .assert_allclose (B , Btest )
100
+ np .testing .assert_allclose (B , Btest , atol = 1e-6 )
81
101
82
102
83
- def test_magnet_cylinder_axial_Bfield ():
103
+ def test_magnet_cylinder_axial_Bfield (xp ):
84
104
Btest = xp .asarray ([[0.05561469 , 0.04117919 ], [0.0 , 0.0 ], [0.06690167 , 0.01805674 ]])
85
105
B = magnet_cylinder_axial_Bfield (
86
106
z0 = xp .asarray ([1 , 2 ]),
87
107
r = xp .asarray ([1 , 2 ]),
88
108
z = xp .asarray ([2 , 3 ]),
89
109
)
90
- np .testing .assert_allclose (B , Btest )
110
+ np .testing .assert_allclose (B , Btest , rtol = 1e-4 )
91
111
92
112
93
- def test_magnet_cylinder_diametral_Hfield ():
113
+ def test_magnet_cylinder_diametral_Hfield (xp ):
94
114
Btest = xp .asarray (
95
115
[
96
116
[- 0.020742122169014 , 0.007307203574376 ],
@@ -104,10 +124,10 @@ def test_magnet_cylinder_diametral_Hfield():
104
124
z = xp .asarray ([2 , 3 ]),
105
125
phi = xp .asarray ([0.1 , np .pi / 4 ]),
106
126
)
107
- np .testing .assert_allclose (B , Btest )
127
+ np .testing .assert_allclose (B , Btest , rtol = 1e-4 )
108
128
109
129
110
- def test_magnet_cylinder_segment_Hfield ():
130
+ def test_magnet_cylinder_segment_Hfield (xp ):
111
131
Btest = xp .asarray (
112
132
[
113
133
[- 1948.14367497 , 32319.94437208 , 17616.88571231 ],
@@ -119,10 +139,10 @@ def test_magnet_cylinder_segment_Hfield():
119
139
dimensions = xp .asarray ([(1 , 2 , 0.1 , 0.2 , - 1 , 1 ), (1 , 2 , 0.3 , 0.9 , 0 , 1 )]),
120
140
magnetizations = xp .asarray ([(1e7 , 0.1 , 0.2 ), (1e6 , 1.1 , 2.2 )]),
121
141
)
122
- np .testing .assert_allclose (B , Btest )
142
+ np .testing .assert_allclose (B , Btest , rtol = 1e-4 )
123
143
124
144
125
- def test_triangle_Bfield ():
145
+ def test_triangle_Bfield (xp ):
126
146
Btest = xp .asarray (
127
147
[[7.45158965 , 4.61994866 , 3.13614132 ], [2.21345618 , 2.67710148 , 2.21345618 ]]
128
148
)
@@ -136,4 +156,4 @@ def test_triangle_Bfield():
136
156
),
137
157
polarizations = xp .asarray ([(1.0 , 1.0 , 1.0 ), (1.0 , 1.0 , 0.0 )]) * 1e3 ,
138
158
)
139
- np .testing .assert_allclose (B , Btest )
159
+ np .testing .assert_allclose (B , Btest , rtol = 1e-4 )
0 commit comments