1
+ from magpylib ._lib .fields .PM_Box import Bfield_Box
2
+ from numpy import array , isnan
3
+ import pytest
4
+
5
+ # -------------------------------------------------------------------------------
6
+ def test_BfieldBox_OLD ():
7
+ errMsg = "Wrong field calculation for BfieldBox"
8
+
9
+ mag = array ([5 ,5 ,5 ])
10
+ dim = array ([1 ,1 ,1 ])
11
+ rotatedPos = array ([- 19. , 1.2 , 8. ])
12
+ mockResults = array ([ 1.40028858e-05 , - 4.89208175e-05 , - 7.01030695e-05 ])
13
+
14
+ result = Bfield_Box (mag ,rotatedPos ,dim )
15
+ rounding = 4
16
+ for i in range (3 ):
17
+ assert round (result [i ],rounding )== round (mockResults [i ],rounding ), errMsg
18
+
19
+ # -------------------------------------------------------------------------------
20
+ def test_BfieldBox_Edges ():
21
+ from numpy import array ,array_equal ,append
22
+ from magpylib import source , Collection
23
+
24
+
25
+ mag = array ([- 111 ,222 ,- 333 ])
26
+ a ,b ,c = 2 ,3 ,4
27
+ dim = array ([a ,b ,c ])
28
+ testPosEdge = []
29
+ corners = array ([[a ,b ,c ],[- a ,b ,c ],[a ,- b ,c ],[a ,b ,- c ],[a ,- b ,- c ],[- a ,b ,- c ],[- a ,- b ,c ],[- a ,- b ,- c ]])/ 2
30
+
31
+ testPosEdge .extend (corners / array ([2 ,1 ,1 ])) # testPosEdgesX =
32
+ testPosEdge .extend (corners / array ([1 ,2 ,1 ])) # testPosEdgesY =
33
+ testPosEdge .extend (corners / array ([1 ,1 ,2 ])) # testPosEdgesZ =
34
+
35
+ with pytest .warns (RuntimeWarning ):
36
+ results = [Bfield_Box (mag ,pos ,dim ) for pos in testPosEdge ]
37
+ assert all (all (isnan (val ) for val in result ) for result in results ), "Results from getB is not NaN"
38
+
39
+ # -------------------------------------------------------------------------------
40
+ def test_BfieldBox_Faces ():
41
+ from numpy import array ,array_equal ,append
42
+ from magpylib import source , Collection
43
+
44
+ mag = array ([- 111 ,222 ,- 333 ])
45
+ a ,b ,c = 2 ,3 ,4
46
+ dim = array ([a ,b ,c ])
47
+ testPosFaces = []
48
+ corners = array ([[a ,b ,c ],[- a ,b ,c ],[a ,- b ,c ],[a ,b ,- c ],[a ,- b ,- c ],[- a ,b ,- c ],[- a ,- b ,c ],[- a ,- b ,- c ]])/ 2
49
+ testPosFaces .extend (corners / array ([2 ,2 ,1 ])) # testPosFaceX =
50
+ testPosFaces .extend (corners / array ([2 ,1 ,2 ])) # testPosFaceY =
51
+ testPosFaces .extend (corners / array ([1 ,2 ,2 ])) # testPosFaceZ =
52
+
53
+ with pytest .warns (RuntimeWarning ):
54
+ results = [Bfield_Box (mag ,pos ,dim ) for pos in testPosFaces ]
55
+ assert all (all (isnan (val ) for val in result ) for result in results ), "Results from getB is not NaN"
56
+
57
+ # -------------------------------------------------------------------------------
58
+ def test_BfieldBox_OuterLines ():
59
+ errMsg = "Unexpected Results for getB in Outer Lines"
60
+ from numpy import array ,array_equal ,append
61
+ from magpylib import source , Collection
62
+
63
+ mag = array ([- 111 ,222 ,- 333 ])
64
+ mockResults = [array ([ - 7.66913751 , - 11.43130392 , 3.90940536 ]), array ([ 0.5814601 , - 5.45527776 , 10.643622 ]),
65
+ array ([- 19.62118983 , 3.13850731 , - 1.81978469 ]), array ([12.53351242 , - 2.83751885 , 4.91443196 ]),
66
+ array ([ 0.5814601 , - 5.45527776 , 10.643622 ]), array ([- 19.62118983 , 3.13850731 , - 1.81978469 ]),
67
+ array ([12.53351242 , - 2.83751885 , 4.91443196 ]), array ([ - 7.66913751 , - 11.43130392 , 3.90940536 ]),
68
+ array ([ 2.40147269 , - 0.80424712 , 5.73409625 ]), array ([0.66977042 , 1.19674994 , 6.4908602 ]),
69
+ array ([- 1.60052144 , 10.81979527 , - 0.6812673 ]), array ([4.67176454 , 8.81879821 , 0.07549665 ]),
70
+ array ([0.66977042 , 1.19674994 , 6.4908602 ]), array ([- 1.60052144 , 10.81979527 , - 0.6812673 ]),
71
+ array ([4.67176454 , 8.81879821 , 0.07549665 ]), array ([ 2.40147269 , - 0.80424712 , 5.73409625 ]),
72
+ array ([- 0.30055594 , - 3.65531213 , - 4.07927409 ]), array ([ 2.06817563 , - 3.40748556 , - 3.12447919 ]),
73
+ array ([- 0.79620907 , 0.60480782 , - 6.75413634 ]), array ([ 2.56382876 , 0.35698125 , - 5.79934144 ]),
74
+ array ([ 2.06817563 , - 3.40748556 , - 3.12447919 ]), array ([- 0.79620907 , 0.60480782 , - 6.75413634 ]),
75
+ array ([ 2.56382876 , 0.35698125 , - 5.79934144 ]), array ([- 0.30055594 , - 3.65531213 , - 4.07927409 ])]
76
+ a ,b ,c = 2 ,3 ,4
77
+ dim = array ([a ,b ,c ])
78
+ testPos = []
79
+ corners = array ([[a ,b ,c ],[- a ,b ,c ],[a ,- b ,c ],[a ,b ,- c ],[a ,- b ,- c ],[- a ,b ,- c ],[- a ,- b ,c ],[- a ,- b ,- c ]])/ 2
80
+ testPos .extend (corners * array ([3 ,1 ,1 ])) # testPosOuterX =
81
+ testPos .extend (corners * array ([1 ,3 ,1 ])) # testPosOuterY =
82
+ testPos .extend (corners * array ([1 ,1 ,3 ])) # testPosOuterZ =
83
+
84
+
85
+ results = [Bfield_Box (mag ,pos ,dim ) for pos in testPos ]
86
+ rounding = 4
87
+ for i in range (0 ,len (mockResults )):
88
+ for j in range (0 ,3 ):
89
+ assert round (mockResults [i ][j ],rounding )== round (results [i ][j ],rounding ), errMsg
90
+
91
+ # -------------------------------------------------------------------------------
92
+ def test_BfieldBox_Corners ():
93
+ from numpy import array ,array_equal ,append
94
+ from magpylib import source , Collection
95
+
96
+ mag = array ([- 111 ,222 ,- 333 ])
97
+ a ,b ,c = 2 ,3 ,4
98
+ dim = array ([a ,b ,c ])
99
+
100
+ testPosCorners = array ([[a ,b ,c ],[- a ,b ,c ],[a ,- b ,c ],[a ,b ,- c ],[a ,- b ,- c ],[- a ,b ,- c ],[- a ,- b ,c ],[- a ,- b ,- c ]])/ 2
101
+
102
+ with pytest .warns (RuntimeWarning ):
103
+ results = [Bfield_Box (mag ,pos ,dim ) for pos in testPosCorners ]
104
+ assert all (all (isnan (val ) for val in result ) for result in results ), "Results from getB is not NaN"
105
+
106
+ # -------------------------------------------------------------------------------
107
+ def test_BfieldBox_outside ():
108
+ # Fundamental Positions in every 8 Octants, but inside
109
+ errMsg = "Field sample outside of Box is unexpected"
110
+ mockResults = [ [- 487.520576 , - 575.369828 , - 104.423566 ],
111
+ [364.861085 , 382.575024 , - 106.896362 ],
112
+ [- 243.065706 , 267.987035 , - 79.954987 ],
113
+ [154.533798 , - 177.245393 , - 17.067326 ],
114
+ [467.108616 , - 413.895715 , 234.294815 ],
115
+ [- 364.043702 , 300.956661 , 72.402694 ],
116
+ [242.976273 , 191.057477 , 54.841929 ],
117
+ [- 150.641259 , - 150.43341 , 42.180744 ],]
118
+
119
+ testPosOut = array ([[5.5 ,6 ,7 ],[6 ,7 ,- 8 ],[7 ,- 8 ,9 ],
120
+ [- 8 ,9 ,10 ],[7 ,- 6 ,- 5 ],[- 8 ,7 ,- 6 ],
121
+ [- 9 ,- 8 ,7 ],[- 10 ,- 9 ,- 8 ]])
122
+
123
+ #check field values to be within [1,100] adjust magnetization
124
+
125
+ mag = array ([- 11111 ,22222 ,- 333333 ])
126
+
127
+ a ,b ,c = 2 ,3 ,4
128
+ dim = array ([a ,b ,c ])
129
+
130
+ results = [Bfield_Box (mag ,pos ,dim ) for pos in testPosOut ]
131
+ rounding = 4
132
+ for i in range (0 ,len (mockResults )):
133
+ for j in range (0 ,3 ):
134
+ assert round (mockResults [i ][j ],rounding )== round (results [i ][j ],rounding ), errMsg
135
+
136
+ # -------------------------------------------------------------------------------
137
+ def test_BfieldBox_inside ():
138
+ # Fundamental Positions in every 8 Octants, but inside
139
+ errMsg = "Field sample inside of Box is unexpected"
140
+ mockResults = [ [- 57.457487 , 133.687466 , - 259.77011 ],
141
+ [- 56.028444 , 147.488799 , - 250.092873 ],
142
+ [- 85.060153 , 175.141795 , - 278.20544 ],
143
+ [- 28.425778 , 161.340462 , - 268.528204 ],
144
+ [- 56.028444 , 147.488799 , - 250.092873 ],
145
+ [- 85.060153 , 175.141795 , - 278.20544 ],
146
+ [- 28.425778 , 161.340462 , - 268.528204 ],
147
+ [- 57.457487 , 133.687466 , - 259.77011 ],]
148
+
149
+ mag = array ([- 111 ,222 ,- 333 ])
150
+
151
+ a ,b ,c = 2 ,3 ,4
152
+ dim = array ([a ,b ,c ])
153
+
154
+ testPosInside = array ([[a ,b ,c ],[- a ,b ,c ],[a ,- b ,c ],[a ,b ,- c ],[a ,- b ,- c ],[- a ,b ,- c ],[- a ,- b ,c ],[- a ,- b ,- c ]])/ 4
155
+
156
+ results = [Bfield_Box (mag ,pos ,dim ) for pos in testPosInside ]
157
+ rounding = 4
158
+ for i in range (0 ,len (mockResults )):
159
+ for j in range (0 ,3 ):
160
+ assert round (mockResults [i ][j ],rounding )== round (results [i ][j ],rounding ), errMsg
161
+
0 commit comments