Skip to content

Commit 627a2ce

Browse files
committed
test and example
1 parent 5d7cf0f commit 627a2ce

7 files changed

+1405
-0
lines changed
10.1 KB
Binary file not shown.

example_comparison_ansys_magpylib.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
isotropic_results_ansys = np.loadtxt('tests/testdata/isotropic_results_ansys.txt', skiprows=1)
5+
isotropic_results_ansys = isotropic_results_ansys[:,3:]
6+
anisotropic_results_ansys = np.loadtxt('tests/testdata/anisotropic_results_ansys.txt', skiprows=1)
7+
anisotropic_results_ansys = anisotropic_results_ansys[:,3:]
8+
isotropic_results_magpylib = np.load('isotropic_results_magpylib_15625.npy')
9+
anisotropic_results_magpylib = np.load('anisotropic_results_magpylib_15625.npy')
10+
11+
12+
isotropic_results_ansys = isotropic_results_ansys.reshape((6,-1,3))
13+
anisotropic_results_ansys = anisotropic_results_ansys.reshape((6,-1,3))
14+
isotropic_results_magpylib = isotropic_results_magpylib.reshape((6,-1,3))
15+
anisotropic_results_magpylib = anisotropic_results_magpylib.reshape((6,-1,3))
16+
17+
18+
isotropic_results_ansys_abs = np.linalg.norm(isotropic_results_ansys, axis=-1)
19+
anisotropic_results_ansys_abs = np.linalg.norm(anisotropic_results_ansys, axis=-1)
20+
isotropic_results_magpylib_abs = np.linalg.norm(isotropic_results_magpylib, axis=-1)
21+
anisotropic_results_magpylib_abs = np.linalg.norm(anisotropic_results_magpylib, axis=-1)
22+
23+
24+
for i in range(6):
25+
fig, (ax1, ax2) = plt.subplots(1, 2)
26+
print('evaluation line ', i)
27+
ax1.plot(isotropic_results_ansys[i,:,0], label='isotropic ansys x', color='C0', linestyle='-')
28+
ax1.plot(isotropic_results_ansys[i,:,1], label='isotropic ansys y', color='C0', linestyle='--')
29+
ax1.plot(isotropic_results_ansys[i,:,2], label='isotropic ansys z', color='C0', linestyle='-.')
30+
ax1.plot(isotropic_results_magpylib[i,:,0], label='isotropic magpylib x', color='C1', linestyle='-')
31+
ax1.plot(isotropic_results_magpylib[i,:,1], label='isotropic magpylib y', color='C1', linestyle='--')
32+
ax1.plot(isotropic_results_magpylib[i,:,2], label='isotropic magpylib z', color='C1', linestyle='-.')
33+
ax1.plot(anisotropic_results_ansys[i,:,0], label='anisotropic ansys x', color='C2', linestyle='-')
34+
ax1.plot(anisotropic_results_ansys[i,:,1], label='anisotropic ansys y', color='C2', linestyle='--')
35+
ax1.plot(anisotropic_results_ansys[i,:,2], label='anisotropic ansys z', color='C2', linestyle='-.')
36+
ax1.plot(anisotropic_results_magpylib[i,:,0], label='anisotropic magpylib x', color='C3', linestyle='-')
37+
ax1.plot(anisotropic_results_magpylib[i,:,1], label='anisotropic magpylib y', color='C3', linestyle='--')
38+
ax1.plot(anisotropic_results_magpylib[i,:,2], label='anisotropic magpylib z', color='C3', linestyle='-.')
39+
ax1.set_xlabel('point along avaluation line')
40+
ax1.set_ylabel('field components [T]')
41+
ax1.grid()
42+
ax1.legend()
43+
44+
45+
ax2.plot(isotropic_results_magpylib[i,:,0]-isotropic_results_ansys[i,:,0], label='isotropic error x', color='C4', linestyle='-')
46+
ax2.plot(isotropic_results_magpylib[i,:,1]-isotropic_results_ansys[i,:,1], label='isotropic error y', color='C4', linestyle='--')
47+
ax2.plot(isotropic_results_magpylib[i,:,2]-isotropic_results_ansys[i,:,2], label='isotropic error z', color='C4', linestyle='-.')
48+
ax2.plot(anisotropic_results_magpylib[i,:,0]-anisotropic_results_ansys[i,:,0], label='anisotropic error x', color='C5', linestyle='-')
49+
ax2.plot(anisotropic_results_magpylib[i,:,1]-anisotropic_results_ansys[i,:,1], label='anisotropic error y', color='C5', linestyle='--')
50+
ax2.plot(anisotropic_results_magpylib[i,:,2]-anisotropic_results_ansys[i,:,2], label='anisotropic error z', color='C5', linestyle='-.')
51+
ax2.set_xlabel('point along avaluation line')
52+
ax2.set_ylabel('field components difference [T]')
53+
ax2.grid()
54+
ax2.legend()
55+
fig.suptitle('evaluation line %d' % i)
56+
plt.show()
57+
58+
59+
for i in range(6):
60+
fig, (ax1, ax2) = plt.subplots(1, 2)
61+
print('evaluation line ', i)
62+
ax1.plot(isotropic_results_ansys_abs[i,:], label='isotropic_results', color='C0')
63+
ax1.plot(isotropic_results_magpylib_abs[i,:], label='isotropic_results_magpylib', color='C1')
64+
ax1.plot(anisotropic_results_ansys_abs[i,:], label='anisotropic_results', color='C2')
65+
ax1.plot(anisotropic_results_magpylib_abs[i,:], label='anisotropic_results_magpylib', color='C3')
66+
ax1.set_xlabel('point along avaluation line')
67+
ax1.set_ylabel('field amplitude [T]')
68+
ax1.grid()
69+
ax1.legend()
70+
71+
ax2.plot((isotropic_results_magpylib_abs[i,:]-isotropic_results_ansys_abs[i,:])/isotropic_results_ansys_abs[i,:]*100, label='isotropic_results_magpylib', color='C4')
72+
ax2.plot((anisotropic_results_magpylib_abs[i,:]-anisotropic_results_ansys_abs[i,:])/anisotropic_results_ansys_abs[i,:]*100, label='anisotropic_results_magpylib', color='C5')
73+
ax2.set_xlabel('point along avaluation line')
74+
ax2.set_ylabel('field amplitude difference [%]')
75+
ax2.grid()
76+
ax2.legend()
77+
fig.suptitle('evaluation line %d' % i)
78+
plt.show()
79+
80+

isotropic_results_magpylib_15625.npy

10.1 KB
Binary file not shown.

tests/test_isotropic_anisotropic.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import magpylib as magpy
2+
from magpylib_material_response import meshing
3+
from magpylib_material_response import demag
4+
import numpy as np
5+
6+
7+
def test_isotropic_susceptibility():
8+
9+
cells = 1000 #should be >=1000, otherwise discretization error too large
10+
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:]
15+
16+
#isotropic
17+
magnet.susceptibility = 0.1
18+
magnet_meshed = meshing.mesh_Cuboid(magnet, cells)
19+
20+
demag.apply_demag(magnet_meshed, inplace=True)
21+
22+
field_magpylib = magnet_meshed.getB(grid)
23+
24+
np.testing.assert_allclose(field_ansys, field_magpylib, rtol=0, atol=0.0012)
25+
26+
27+
28+
def test_anisotropic_susceptibility():
29+
30+
cells = 1000 #should be >=1000, otherwise discretization error too large
31+
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:]
36+
37+
#anisotropic
38+
magnet.susceptibility = (0.3, 0.2, 0.1)
39+
magnet_meshed = meshing.mesh_Cuboid(magnet, cells)
40+
41+
demag.apply_demag(magnet_meshed, inplace=True)
42+
43+
field_magpylib = magnet_meshed.getB(grid)
44+
45+
np.testing.assert_allclose(field_ansys, field_magpylib, rtol=0, atol=0.0012)

0 commit comments

Comments
 (0)