Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 69a8033

Browse files
committedNov 22, 2019
testing fields added
1 parent 2c12184 commit 69a8033

File tree

9 files changed

+581
-4
lines changed

9 files changed

+581
-4
lines changed
 

‎tests/test_fields/test_CurrentLine.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from magpylib._lib.fields.Current_Line import Bfield_CurrentLine
2+
from numpy import array
3+
import pytest
4+
5+
6+
# -------------------------------------------------------------------------------
7+
def test_Bfield_Zero_Length_segment():
8+
# Check if Zero-length segments in vertices return valid
9+
errMsg = "Field sample outside of Line is unexpected"
10+
mockResult = [0,0.72951356,0]
11+
12+
current = 5
13+
pos = [0,0,0]
14+
15+
vertices = array([[-1,0,0],[1,0,5],[1,0,5]])
16+
with pytest.warns(RuntimeWarning):
17+
results=Bfield_CurrentLine(pos,vertices,current)
18+
rounding = 4
19+
20+
for i in range(0,3):
21+
assert round(mockResult[i],rounding)==round(results[i],rounding), errMsg
22+
23+
# -------------------------------------------------------------------------------
24+
def test_Bfield_CurrentLine_outside():
25+
# Fundamental Positions in every 8 Octants
26+
errMsg = "Field sample outside of Line is unexpected"
27+
mockResults = [ [-15.426123, -42.10796, -12.922307],
28+
[67.176642, -3.154985, -10.209148],
29+
[-52.57675, 14.702422, 16.730058],
30+
[12.5054, 15.171589, -22.647928],
31+
[33.504425, -104.324783, 93.824852],
32+
[17.274412, 31.725278, -41.418518],
33+
[-22.39969, 56.344393, -3.576432],
34+
[-11.270571, -9.00747, 3.640508],]
35+
36+
testPosOut = array([[5.5,6,7],[6,7,-8],[7,-8,9],
37+
[-8,9,10],[7,-6,-5],[-8,7,-6],
38+
[-9,-8,7],[-10,-9,-8]])
39+
40+
#check field values to be within [1,100] adjust magnetization
41+
42+
current = -11111
43+
44+
vertices = array([ [-4,-4,-3],[3.5,-3.5,-2],[3,3,-1],
45+
[-2.5,2.5,0],[-2,-2,1],[1.5,-1.5,2],[1,1,3]])
46+
47+
results=[Bfield_CurrentLine(pos,vertices,current) for pos in testPosOut]
48+
rounding = 4
49+
for i in range(0,len(mockResults)):
50+
for j in range(0,3):
51+
assert round(mockResults[i][j],rounding)==round(results[i][j],rounding), errMsg
52+
53+
# -------------------------------------------------------------------------------
54+
def test_Bfield_singularity():
55+
# Test the result fo field sampleS on the line current
56+
# Each origin vertix, collinear center point
57+
# Expected: [nan,nan,nan]
58+
from magpylib import source,Collection
59+
from numpy import array, isnan
60+
vertices = [array([1,2,2]),array([1,2,30])]
61+
current = 5
62+
63+
origin1 = vertices[0]
64+
origin2 = vertices[1]
65+
middle = ((vertices[0]) + (vertices[1])) / 2
66+
67+
testPos = [origin1,origin2,middle]
68+
with pytest.warns(RuntimeWarning):
69+
results = [Bfield_CurrentLine(pos,vertices,current) for pos in testPos]
70+
assert all(all(isnan(val) for val in result) for result in results), "Results from getB is not NaN"
71+
72+
# -------------------------------------------------------------------------------
73+
def test_Bfield_onLine():
74+
# Check if points that are on the line but
75+
# not on the segment still return valid results
76+
# Expected for collinear points: [0,0,0]
77+
errMsg = "Points on Line (not on segment) are not being calculated"
78+
from magpylib import source,Collection
79+
from numpy import array
80+
vertices = [array([1,2,2]),array([1,2,30])]
81+
current = 5
82+
mockResults = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
83+
84+
points = [vertices[0] + [0,0,-3], vertices[1] + [0,0,3]]
85+
86+
87+
results = [Bfield_CurrentLine(point,vertices,current) for point in points]
88+
rounding = 4
89+
for i in range(0,len(mockResults)):
90+
for j in range(0,3):
91+
assert round(mockResults[i][j],rounding)==round(results[i][j],rounding), errMsg

‎tests/test_fields/test_CurrentLoop.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from magpylib._lib.fields.Current_CircularLoop import Bfield_CircularCurrentLoop
2+
from numpy import array, isnan
3+
import pytest
4+
5+
# -------------------------------------------------------------------------------
6+
def test_Bfield_singularity():
7+
# Test the result for a field sample on the circular loop
8+
# Expected: NaN
9+
10+
# Definitions
11+
current = 5
12+
diameter = 5
13+
calcPos = [0,2.5,0]
14+
15+
# Run
16+
with pytest.warns(RuntimeWarning):
17+
results = Bfield_CircularCurrentLoop(current,diameter,calcPos)
18+
assert all(isnan(axis) for axis in results)
19+
20+
# -------------------------------------------------------------------------------
21+
def test_CircularGetB_OuterLines():
22+
errMsg = "Results from getB are unexpected"
23+
mockResults = [ [-0.0, -51.469971, 30.236739],
24+
[0.0, 51.469971, 30.236739],
25+
[0.0, 63.447185, -2.422005],
26+
[0.0, -63.447185, -2.422005],
27+
[-0.0, -63.447185, -2.422005],
28+
[-0.0, 63.447185, -2.422005],
29+
[-0.0, -36.0076, 68.740859],
30+
[0.0, 36.0076, 68.740859],
31+
[0.0, 0.0, -133.812661], ]
32+
33+
sideSurface = [0,3,0]
34+
upperSurface = [0,1.5,1.5]
35+
lowerSurface = [0,1.5,-1.5]
36+
edgeBot = [0,2.5,1.5]
37+
edgeTop = [0,2.5,-1.5]
38+
edge = [0,3,1]
39+
edge2 = [0,-3,1]
40+
edge3 = [0,3,-1]
41+
edge4 = [0,-3,-1]
42+
43+
testPosOut = array([ edgeTop, edgeBot, edge, edge2,
44+
edge3, edge4, lowerSurface,upperSurface,
45+
sideSurface])
46+
47+
current = 500
48+
diameter = 5
49+
50+
results=[Bfield_CircularCurrentLoop(current,diameter,pos) for pos in testPosOut]
51+
rounding = 4
52+
for i in range(0,len(mockResults)):
53+
for j in range(0,3):
54+
assert round(mockResults[i][j],rounding)==round(results[i][j],rounding), errMsg
55+
56+
57+
# -------------------------------------------------------------------------------
58+
def test_Bfield_outside():
59+
# Fundamental Positions in every 8 Octants
60+
errMsg = "Field sample outside of Box is unexpected"
61+
mockResults = [ [-28.275526, -30.846029, -8.08718],
62+
[18.54694, 21.638096, -5.704383],
63+
[-12.588134, 14.386439, -3.348427],
64+
[8.919783, -10.034756, -2.091007],
65+
[29.112211, -24.953324, 9.416569],
66+
[-18.649955, 16.318711, 5.173529],
67+
[12.635187, 11.231277, 3.070725],
68+
[-8.943273, -8.048946, 1.934808],
69+
[0.0, 0.0, -69813.100267],]
70+
71+
testPosOut = array([ [5.5,6,7],[6,7,-8],[7,-8,9],
72+
[-8,9,10],[7,-6,-5],[-8,7,-6],
73+
[-9,-8,7],[-10,-9,-8],[0,0,0] ])
74+
75+
#check field values to be within [1,100] adjust magnetization
76+
current = -111111
77+
diameter = 2
78+
results=[Bfield_CircularCurrentLoop(current,diameter,pos) for pos in testPosOut]
79+
rounding = 4
80+
for i in range(0,len(mockResults)):
81+
for j in range(0,3):
82+
assert round(mockResults[i][j],rounding)==round(results[i][j],rounding), errMsg
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from magpylib._lib.fields.Moment_Dipole import Bfield_Dipole
2+
from numpy import array, isnan
3+
import pytest
4+
5+
# -------------------------------------------------------------------------------
6+
def test_Bfield_singularity():
7+
# Test the result for a field sample on the dipole itself
8+
# Expected: NaN
9+
10+
# Definitions
11+
mag=array([-1,2,-3])
12+
calcPos = array([0,0,0])
13+
14+
# Run
15+
with pytest.warns(RuntimeWarning):
16+
results = Bfield_Dipole(mag,calcPos)
17+
assert all(isnan(axis) for axis in results)
18+
19+
# -------------------------------------------------------------------------------
20+
def test_Bfield_outside():
21+
# Fundamental Positions in every 8 Octants
22+
errMsg = "Field sample outside of Box is unexpected"
23+
mockResults = [ [-20.105974, -24.142649, -5.059827],
24+
[15.050978, 16.020022, -4.835365],
25+
[-10.051157, 11.206562, -3.526921],
26+
[6.41919, -7.423334, -0.818756],
27+
[19.933508, -17.961747, 9.301348],
28+
[-15.331571, 12.868214, 2.721131],
29+
[10.209707, 8.129991, 2.130329],
30+
[-6.319435, -6.356132, 1.677024],]
31+
32+
testPosOut = array([[5.5,6,7],[6,7,-8],[7,-8,9],
33+
[-8,9,10],[7,-6,-5],[-8,7,-6],
34+
[-9,-8,7],[-10,-9,-8]])
35+
36+
#check field values to be within [1,100] adjust magnetization
37+
38+
39+
mag=array([-11111,22222,-333333])
40+
41+
results=[Bfield_Dipole(mag,pos) for pos in testPosOut]
42+
rounding = 4
43+
for i in range(0,len(mockResults)):
44+
for j in range(0,3):
45+
assert round(mockResults[i][j],rounding)==round(results[i][j],rounding), errMsg

‎tests/test_fields/test_PMBox.py

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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

Comments
 (0)