Skip to content

Commit 668f955

Browse files
committed
Merge branch 'anisotropic_chi' of https://github.com/magpylib/magpylib-material-response into anisotropic_chi
2 parents e666bb9 + ee56ab3 commit 668f955

File tree

2 files changed

+168
-60
lines changed

2 files changed

+168
-60
lines changed

example_comparison_ansys_magpylib.py

Lines changed: 154 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import numpy as np
21
import matplotlib.pyplot as plt
2+
import numpy as np
33

44
cells = 15625
55

@@ -11,10 +11,10 @@
1111
anisotropic_results_magpylib = np.load('anisotropic_results_magpylib_%d.npy' % cells)
1212

1313

14-
isotropic_results_ansys = isotropic_results_ansys.reshape((6,-1,3))
15-
anisotropic_results_ansys = anisotropic_results_ansys.reshape((6,-1,3))
16-
isotropic_results_magpylib = isotropic_results_magpylib.reshape((6,-1,3))
17-
anisotropic_results_magpylib = anisotropic_results_magpylib.reshape((6,-1,3))
14+
isotropic_results_ansys = isotropic_results_ansys.reshape((6, -1, 3))
15+
anisotropic_results_ansys = anisotropic_results_ansys.reshape((6, -1, 3))
16+
isotropic_results_magpylib = isotropic_results_magpylib.reshape((6, -1, 3))
17+
anisotropic_results_magpylib = anisotropic_results_magpylib.reshape((6, -1, 3))
1818

1919

2020
isotropic_results_ansys_abs = np.linalg.norm(isotropic_results_ansys, axis=-1)
@@ -25,58 +25,167 @@
2525

2626
for i in range(6):
2727
fig, (ax1, ax2) = plt.subplots(1, 2)
28-
print('evaluation line ', i)
29-
ax1.plot(isotropic_results_ansys[i,:,0], label='isotropic ansys x', color='C0', linestyle='-')
30-
ax1.plot(isotropic_results_ansys[i,:,1], label='isotropic ansys y', color='C0', linestyle='--')
31-
ax1.plot(isotropic_results_ansys[i,:,2], label='isotropic ansys z', color='C0', linestyle='-.')
32-
ax1.plot(isotropic_results_magpylib[i,:,0], label='isotropic magpylib x', color='C1', linestyle='-')
33-
ax1.plot(isotropic_results_magpylib[i,:,1], label='isotropic magpylib y', color='C1', linestyle='--')
34-
ax1.plot(isotropic_results_magpylib[i,:,2], label='isotropic magpylib z', color='C1', linestyle='-.')
35-
ax1.plot(anisotropic_results_ansys[i,:,0], label='anisotropic ansys x', color='C2', linestyle='-')
36-
ax1.plot(anisotropic_results_ansys[i,:,1], label='anisotropic ansys y', color='C2', linestyle='--')
37-
ax1.plot(anisotropic_results_ansys[i,:,2], label='anisotropic ansys z', color='C2', linestyle='-.')
38-
ax1.plot(anisotropic_results_magpylib[i,:,0], label='anisotropic magpylib x', color='C3', linestyle='-')
39-
ax1.plot(anisotropic_results_magpylib[i,:,1], label='anisotropic magpylib y', color='C3', linestyle='--')
40-
ax1.plot(anisotropic_results_magpylib[i,:,2], label='anisotropic magpylib z', color='C3', linestyle='-.')
41-
ax1.set_xlabel('point along avaluation line')
42-
ax1.set_ylabel('field components [T]')
28+
print("evaluation line ", i)
29+
ax1.plot(
30+
isotropic_results_ansys[i, :, 0],
31+
label="isotropic ansys x",
32+
color="C0",
33+
linestyle="-",
34+
)
35+
ax1.plot(
36+
isotropic_results_ansys[i, :, 1],
37+
label="isotropic ansys y",
38+
color="C0",
39+
linestyle="--",
40+
)
41+
ax1.plot(
42+
isotropic_results_ansys[i, :, 2],
43+
label="isotropic ansys z",
44+
color="C0",
45+
linestyle="-.",
46+
)
47+
ax1.plot(
48+
isotropic_results_magpylib[i, :, 0],
49+
label="isotropic magpylib x",
50+
color="C1",
51+
linestyle="-",
52+
)
53+
ax1.plot(
54+
isotropic_results_magpylib[i, :, 1],
55+
label="isotropic magpylib y",
56+
color="C1",
57+
linestyle="--",
58+
)
59+
ax1.plot(
60+
isotropic_results_magpylib[i, :, 2],
61+
label="isotropic magpylib z",
62+
color="C1",
63+
linestyle="-.",
64+
)
65+
ax1.plot(
66+
anisotropic_results_ansys[i, :, 0],
67+
label="anisotropic ansys x",
68+
color="C2",
69+
linestyle="-",
70+
)
71+
ax1.plot(
72+
anisotropic_results_ansys[i, :, 1],
73+
label="anisotropic ansys y",
74+
color="C2",
75+
linestyle="--",
76+
)
77+
ax1.plot(
78+
anisotropic_results_ansys[i, :, 2],
79+
label="anisotropic ansys z",
80+
color="C2",
81+
linestyle="-.",
82+
)
83+
ax1.plot(
84+
anisotropic_results_magpylib[i, :, 0],
85+
label="anisotropic magpylib x",
86+
color="C3",
87+
linestyle="-",
88+
)
89+
ax1.plot(
90+
anisotropic_results_magpylib[i, :, 1],
91+
label="anisotropic magpylib y",
92+
color="C3",
93+
linestyle="--",
94+
)
95+
ax1.plot(
96+
anisotropic_results_magpylib[i, :, 2],
97+
label="anisotropic magpylib z",
98+
color="C3",
99+
linestyle="-.",
100+
)
101+
ax1.set_xlabel("point along avaluation line")
102+
ax1.set_ylabel("field components [T]")
43103
ax1.grid()
44104
ax1.legend()
45105

46-
47-
ax2.plot(isotropic_results_magpylib[i,:,0]-isotropic_results_ansys[i,:,0], label='isotropic error x', color='C4', linestyle='-')
48-
ax2.plot(isotropic_results_magpylib[i,:,1]-isotropic_results_ansys[i,:,1], label='isotropic error y', color='C4', linestyle='--')
49-
ax2.plot(isotropic_results_magpylib[i,:,2]-isotropic_results_ansys[i,:,2], label='isotropic error z', color='C4', linestyle='-.')
50-
ax2.plot(anisotropic_results_magpylib[i,:,0]-anisotropic_results_ansys[i,:,0], label='anisotropic error x', color='C5', linestyle='-')
51-
ax2.plot(anisotropic_results_magpylib[i,:,1]-anisotropic_results_ansys[i,:,1], label='anisotropic error y', color='C5', linestyle='--')
52-
ax2.plot(anisotropic_results_magpylib[i,:,2]-anisotropic_results_ansys[i,:,2], label='anisotropic error z', color='C5', linestyle='-.')
53-
ax2.set_xlabel('point along avaluation line')
54-
ax2.set_ylabel('field components difference [T]')
55-
ax2.grid()
106+
ax2.plot(
107+
isotropic_results_magpylib[i, :, 0] - isotropic_results_ansys[i, :, 0],
108+
label="isotropic error x",
109+
color="C4",
110+
linestyle="-",
111+
)
112+
ax2.plot(
113+
isotropic_results_magpylib[i, :, 1] - isotropic_results_ansys[i, :, 1],
114+
label="isotropic error y",
115+
color="C4",
116+
linestyle="--",
117+
)
118+
ax2.plot(
119+
isotropic_results_magpylib[i, :, 2] - isotropic_results_ansys[i, :, 2],
120+
label="isotropic error z",
121+
color="C4",
122+
linestyle="-.",
123+
)
124+
ax2.plot(
125+
anisotropic_results_magpylib[i, :, 0] - anisotropic_results_ansys[i, :, 0],
126+
label="anisotropic error x",
127+
color="C5",
128+
linestyle="-",
129+
)
130+
ax2.plot(
131+
anisotropic_results_magpylib[i, :, 1] - anisotropic_results_ansys[i, :, 1],
132+
label="anisotropic error y",
133+
color="C5",
134+
linestyle="--",
135+
)
136+
ax2.plot(
137+
anisotropic_results_magpylib[i, :, 2] - anisotropic_results_ansys[i, :, 2],
138+
label="anisotropic error z",
139+
color="C5",
140+
linestyle="-.",
141+
)
142+
ax2.set_xlabel("point along avaluation line")
143+
ax2.set_ylabel("field components difference [T]")
144+
ax2.grid()
56145
ax2.legend()
57-
fig.suptitle('evaluation line %d' % i)
146+
fig.suptitle("evaluation line %d" % i)
58147
plt.show()
59148

60149

61150
for i in range(6):
62151
fig, (ax1, ax2) = plt.subplots(1, 2)
63-
print('evaluation line ', i)
64-
ax1.plot(isotropic_results_ansys_abs[i,:], label='isotropic_results', color='C0')
65-
ax1.plot(isotropic_results_magpylib_abs[i,:], label='isotropic_results_magpylib', color='C1')
66-
ax1.plot(anisotropic_results_ansys_abs[i,:], label='anisotropic_results', color='C2')
67-
ax1.plot(anisotropic_results_magpylib_abs[i,:], label='anisotropic_results_magpylib', color='C3')
68-
ax1.set_xlabel('point along avaluation line')
69-
ax1.set_ylabel('field amplitude [T]')
152+
print("evaluation line ", i)
153+
ax1.plot(isotropic_results_ansys_abs[i, :], label="isotropic_results", color="C0")
154+
ax1.plot(
155+
isotropic_results_magpylib_abs[i, :],
156+
label="isotropic_results_magpylib",
157+
color="C1",
158+
)
159+
ax1.plot(
160+
anisotropic_results_ansys_abs[i, :], label="anisotropic_results", color="C2"
161+
)
162+
ax1.plot(
163+
anisotropic_results_magpylib_abs[i, :],
164+
label="anisotropic_results_magpylib",
165+
color="C3",
166+
)
167+
ax1.set_xlabel("point along avaluation line")
168+
ax1.set_ylabel("field amplitude [T]")
70169
ax1.grid()
71170
ax1.legend()
72171

73-
ax2.plot((isotropic_results_magpylib_abs[i,:]-isotropic_results_ansys_abs[i,:])/isotropic_results_ansys_abs[i,:]*100, label='isotropic_results_magpylib', color='C4')
74-
ax2.plot((anisotropic_results_magpylib_abs[i,:]-anisotropic_results_ansys_abs[i,:])/anisotropic_results_ansys_abs[i,:]*100, label='anisotropic_results_magpylib', color='C5')
75-
ax2.set_xlabel('point along avaluation line')
76-
ax2.set_ylabel('field amplitude difference [%]')
172+
ax2.plot(
173+
(isotropic_results_magpylib_abs[i, :] - isotropic_results_ansys_abs[i, :])
174+
/ isotropic_results_ansys_abs[i, :]
175+
* 100,
176+
label="isotropic_results_magpylib",
177+
color="C4",
178+
)
179+
ax2.plot(
180+
(anisotropic_results_magpylib_abs[i, :] - anisotropic_results_ansys_abs[i, :])
181+
/ anisotropic_results_ansys_abs[i, :]
182+
* 100,
183+
label="anisotropic_results_magpylib",
184+
color="C5",
185+
)
186+
ax2.set_xlabel("point along avaluation line")
187+
ax2.set_ylabel("field amplitude difference [%]")
77188
ax2.grid()
78189
ax2.legend()
79-
fig.suptitle('evaluation line %d' % i)
190+
fig.suptitle("evaluation line %d" % i)
80191
plt.show()
81-
82-

tests/test_isotropic_anisotropic.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import magpylib as magpy
2-
from magpylib_material_response import meshing
3-
from magpylib_material_response import demag
42
import numpy as np
53

4+
from magpylib_material_response import demag, meshing
5+
66

77
def test_isotropic_susceptibility():
88

9-
cells = 1000 #should be >=1000, otherwise discretization error too large
9+
cells = 1000 # should be >=1000, otherwise discretization error too large
1010

11-
magnet = magpy.magnet.Cuboid(dimension=(1e-3,1e-3,1e-3), polarization=(0,0,1.1))
12-
grid = np.loadtxt('tests/testdata/grid_points.pts')
13-
field_ansys = np.loadtxt('tests/testdata/isotropic_results_ansys.txt', skiprows=1)
14-
field_ansys = field_ansys[:,3:]
11+
magnet = magpy.magnet.Cuboid(dimension=(1e-3, 1e-3, 1e-3), polarization=(0, 0, 1.1))
12+
grid = np.loadtxt("tests/testdata/grid_points.pts")
13+
field_ansys = np.loadtxt("tests/testdata/isotropic_results_ansys.txt", skiprows=1)
14+
field_ansys = field_ansys[:, 3:]
1515

16-
#isotropic
16+
# isotropic
1717
magnet.susceptibility = 0.1
1818
magnet_meshed = meshing.mesh_Cuboid(magnet, cells)
1919

@@ -24,17 +24,16 @@ def test_isotropic_susceptibility():
2424
np.testing.assert_allclose(field_ansys, field_magpylib, rtol=0, atol=0.0012)
2525

2626

27-
2827
def test_anisotropic_susceptibility():
2928

30-
cells = 1000 #should be >=1000, otherwise discretization error too large
29+
cells = 1000 # should be >=1000, otherwise discretization error too large
3130

32-
magnet = magpy.magnet.Cuboid(dimension=(1e-3,1e-3,1e-3), polarization=(0,0,1.1))
33-
grid = np.loadtxt('tests/testdata/grid_points.pts')
34-
field_ansys = np.loadtxt('tests/testdata/anisotropic_results_ansys.txt', skiprows=1)
35-
field_ansys = field_ansys[:,3:]
31+
magnet = magpy.magnet.Cuboid(dimension=(1e-3, 1e-3, 1e-3), polarization=(0, 0, 1.1))
32+
grid = np.loadtxt("tests/testdata/grid_points.pts")
33+
field_ansys = np.loadtxt("tests/testdata/anisotropic_results_ansys.txt", skiprows=1)
34+
field_ansys = field_ansys[:, 3:]
3635

37-
#anisotropic
36+
# anisotropic
3837
magnet.susceptibility = (0.3, 0.2, 0.1)
3938
magnet_meshed = meshing.mesh_Cuboid(magnet, cells)
4039

0 commit comments

Comments
 (0)