-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_meshing.py
239 lines (190 loc) · 8.47 KB
/
test_meshing.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
"""Test meshing functions"""
import magpylib as magpy
import numpy as np
import pytest
from magpylib_material_response.meshing import (
mesh_all,
mesh_Cuboid,
mesh_Cylinder,
mesh_thin_CylinderSegment_with_cuboids,
slice_Cuboid,
)
def test_mesh_Cuboid():
# create cuboid with rotated path
N = 5
opacity = 0.5
c = magpy.magnet.Cuboid((1000, 0, 0), (1, 2, 3), position=[4, 5, 6])
c.style.label = "Cuboid 1"
c.style.opacity = 0 # will be overriden by mesh kwargs lower
c.rotate_from_angax(np.linspace(0, 76, N), "z", anchor=0, start=0)
c.move(np.linspace((0, 0, 0), (0, 0, 10.5), N), start=0)
c.xi = 3999
cm = mesh_Cuboid(c, target_elems=8, style_opacity=opacity)
# test if meshed cuboid collection provides the same field as original cuboid
np.testing.assert_allclose(c.getB([0, 0, 0]), cm.getB([0, 0, 0]))
# test if all children sources got the right xi value
assert all(s.xi == c.xi for s in cm.sources_all)
# test style kwargs
assert cm.style.label == c.style.label
assert cm.style.opacity == opacity
with pytest.raises(TypeError):
mesh_Cuboid(magpy.magnet.Cylinder(), target_elems=8)
def test_mesh_Cuboid2():
# test for custom target elems per dimension
c = magpy.magnet.Cuboid(dimension=(1, 1, 1))
target_elems = [1, 2, 3]
cm = mesh_Cuboid(c, target_elems=target_elems)
poss = np.array([o.position for o in cm])
assert target_elems == [len(np.unique(poss[:, i])) for i in range(3)]
def test_slice_Cuboid():
# create cuboid with rotated path
N = 5
opacity = 0.5
c = magpy.magnet.Cuboid((1000, 0, 0), (1, 2, 3), position=[4, 5, 6])
c.style.label = "Cuboid 1"
c.style.opacity = 0 # will be overriden by mesh kwargs lower
c.rotate_from_angax(np.linspace(0, 76, N), "z", anchor=0, start=0)
c.move(np.linspace((0, 0, 0), (0, 0, 10.5), N), start=0)
c.xi = 3999
cm = slice_Cuboid(c, shift=0.2, axis="y", style_opacity=opacity)
# test if meshed cuboid collection provides the same field as original cuboid
np.testing.assert_allclose(c.getB([0, 0, 0]), cm.getB([0, 0, 0]))
# test if all children sources got the right xi value
assert all(s.xi == c.xi for s in cm.sources_all)
# test style kwargs
assert cm.style.label == c.style.label
assert cm.style.opacity == opacity
with pytest.raises(TypeError):
slice_Cuboid(magpy.magnet.Cylinder(), target_elems=8)
def test_slice_Cuboid2():
c = magpy.magnet.Cuboid(dimension=(1, 1, 1))
# axis x
cm = slice_Cuboid(c, shift=0.5, axis="x")
np.testing.assert_allclose(cm[0].dimension, [0.5, 1.0, 1.0])
np.testing.assert_allclose(cm[1].dimension, [0.5, 1.0, 1.0])
np.testing.assert_allclose(cm[0].position, [-0.25, 0.0, 0.0])
np.testing.assert_allclose(cm[1].position, [0.25, 0.0, 0.0])
# axis y
cm = slice_Cuboid(c, shift=0.2, axis="y")
np.testing.assert_allclose(cm[0].dimension, [1.0, 0.2, 1.0])
np.testing.assert_allclose(cm[1].dimension, [1.0, 0.8, 1.0])
np.testing.assert_allclose(cm[0].position, [0.0, -0.4, 0.0])
np.testing.assert_allclose(cm[1].position, [0.0, 0.1, 0.0])
cm = slice_Cuboid(c, shift=0.1, axis="z")
# axis z
np.testing.assert_allclose(cm[0].dimension, [1.0, 1.0, 0.1])
np.testing.assert_allclose(cm[1].dimension, [1.0, 1.0, 0.9])
np.testing.assert_allclose(cm[0].position, [0.0, 0.0, -0.45])
np.testing.assert_allclose(cm[1].position, [0.0, 0.0, 0.05])
with pytest.raises(ValueError):
slice_Cuboid(c, shift=0, axis="y")
def test_mesh_Cylinder():
# create CylinderSegment with rotated path
N = 3
opacity = 0.5
c = magpy.magnet.CylinderSegment(
magnetization=(1000, 0, 0),
dimension=(1, 1.1, 1, 0, 270),
position=[1, 2, 1],
)
c.style.label = "CylinderSegment 1"
c.style.opacity = 0 # will be overriden by mesh kwargs lower
c.rotate_from_angax(np.linspace(0, 90, N), "z", anchor=0, start=0)
c.rotate_from_angax(np.linspace(0, 90, N), "x", anchor=0, start=0)
c.move(np.linspace((0, 0, 0), (0, 0, 1), N), start=0)
c.style.magnetization.show = False
c.xi = 3999
cm = mesh_Cylinder(c, target_elems=20, style_opacity=opacity)
# test if meshed cuboid collection provides the same field as original cuboid
np.testing.assert_allclose(c.getB([0, 0, 0]), cm.getB([0, 0, 0]))
# test if all children sources got the right xi value
assert all(s.xi == c.xi for s in cm.sources_all)
# test style kwargs
assert cm.style.label == c.style.label
assert cm.style.opacity == opacity
def test_mesh_Cylinder2():
# test for innermost is cylinder when closed and at least 3 layers
c = magpy.magnet.Cylinder(dimension=(1, 1))
cm = mesh_Cylinder(c, target_elems=200)
cyls = [o for o in cm if isinstance(o, magpy.magnet.Cylinder)]
cyl_segs = [o for o in cm if isinstance(o, magpy.magnet.CylinderSegment)]
assert len(cyls) == 7
assert len(cyl_segs) == 231
def test_mesh_thin_CylinderSegment_with_cuboids():
# create thin CylinderSegment with rotated path
N = 3
opacity = 0.5
c = magpy.magnet.CylinderSegment(
(1000, 0, 0), (1, 1.1, 1, 0, 270), position=[1, 2, 1], style_opacity=0.2
)
c.rotate_from_angax(np.linspace(0, 90, N), "z", anchor=0, start=0)
c.rotate_from_angax(np.linspace(0, 90, N), "x", anchor=0, start=0)
c.move(np.linspace((0, 0, 0), (0, 0, 1), N), start=0)
c.xi = 3999
cm = mesh_thin_CylinderSegment_with_cuboids(
c, target_elems=100, match_volume=False, style_opacity=opacity
)
# test if meshed cuboid collection provides the same field as original cuboid
# needs rtol because it is an approximation.
np.testing.assert_allclose(c.getB([0, 0, 0]), cm.getB([0, 0, 0]), rtol=0.06)
cm = mesh_thin_CylinderSegment_with_cuboids(
c, target_elems=100, style_opacity=opacity
)
# test if meshed cuboid collection provides the same field as original cuboid
# needs rtol because it is an approximation.
# by default match_volume=True, delivers better accuracy, hence lower rtol
np.testing.assert_allclose(c.getB([0, 0, 0]), cm.getB([0, 0, 0]), rtol=0.01)
# test if all children sources got the right xi value
assert all(s.xi == c.xi for s in cm.sources_all)
# test style kwargs
assert cm.style.label == c.style.label
assert cm.style.opacity == opacity
with pytest.raises(TypeError):
mesh_Cuboid(magpy.magnet.Cylinder(), target_elems=8)
def test_mesh_all():
xi = 3999
c = magpy.magnet.Cuboid(
(1000, 0, 0), (1, 2, 3), position=[4, 5, 6], style_label="C1"
)
c.xi = xi
cy = magpy.magnet.Cylinder((1, 1, 1), (4, 1))
cy.xi = c.xi
c2 = magpy.Collection(c.copy(dimension=c.dimension / 2).move((5, 0, 0)))
c2.style.label = "C2 super"
# currents and sensors and sensors should be just silently ignored by meshing
curr = magpy.current.Loop(current=1, diameter=1)
sens = magpy.Sensor()
c0 = magpy.Collection(c, c2, cy, sens, curr)
# fail on other unsupported magnets
c1 = c0.copy()
c1.add(magpy.magnet.Sphere((0, 0, 1000), 1))
with pytest.raises(TypeError):
mesh_all(c1, target_elems=20)
# mesh per total target elems (by default)
cm = mesh_all(c0, target_elems=20)
cubs = [o for o in cm.sources_all if isinstance(o, magpy.magnet.Cuboid)]
cyls = [o for o in cm.sources_all if isinstance(o, magpy.magnet.Cylinder)]
cylsegs = [o for o in cm.sources_all if isinstance(o, magpy.magnet.CylinderSegment)]
assert len(cubs) == 16
assert len(cyls) == 0
assert len(cylsegs) == 18
# mesh per child elems
cm = mesh_all(c0, per_child_elems=True, target_elems=20)
cubs = [o for o in cm.sources_all if isinstance(o, magpy.magnet.Cuboid)]
cyls = [o for o in cm.sources_all if isinstance(o, magpy.magnet.Cylinder)]
cylsegs = [o for o in cm.sources_all if isinstance(o, magpy.magnet.CylinderSegment)]
assert len(cubs) == 40
assert len(cyls) == 1
assert len(cylsegs) == 23
# mesh on copy (by default)
cm = mesh_all(c0, target_elems=20)
assert cm is not c0
# test if all children sources got the right xi value
mags = [s for s in cm.sources_all if not isinstance(s, magpy.current.Loop)]
assert all(s.xi == c.xi for s in mags)
# mesh inplace
cm = mesh_all(c0, target_elems=20, inplace=True)
assert cm is c0
# test if all children sources got the right xi value
mags = [s for s in cm.sources_all if not isinstance(s, magpy.current.Loop)]
assert all(s.xi == c.xi for s in mags)