Skip to content

Commit 15b2bd1

Browse files
committed
update
1 parent f7dbf37 commit 15b2bd1

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

magpylib_material_response/demag.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,32 @@ def get_susceptibilities(*sources, susceptibility=None):
4343
if src.parent is None:
4444
raise ValueError("No susceptibility defined in any parent collection")
4545
susceptibilities.extend(get_susceptibilities(src.parent))
46-
else:
46+
elif not hasattr(susceptibility, "__len__"):
47+
susceptibilities.append((susceptibility, susceptibility, susceptibility))
48+
elif len(susceptibility) == 3:
4749
susceptibilities.append(susceptibility)
50+
else:
51+
raise ValueError("susceptibility is not scalar or array fo length 3")
4852
return susceptibilities
4953

54+
def get_H_ext(*sources, H_ext=None):
55+
"""Return a list of length (len(sources)) with H_ext values
56+
Priority is given at the source level, hovever if value is not found, it is searched up the
57+
the parent tree, if available. Sets H_ext to zero if no value is found when reached the top
58+
level of the tree"""
59+
H_exts = []
60+
for src in sources:
61+
H_ext = getattr(src, "H_ext", None)
62+
if H_ext is None:
63+
if src.parent is None:
64+
#print("Warning: No value for H_ext defined in any parent collection. H_ext set to zero.")
65+
H_exts.append((0.0,0.0,0.0))
66+
else:
67+
H_exts.extend(get_H_ext(src.parent))
68+
else:
69+
H_exts.append(H_ext)
70+
return H_exts
71+
5072

5173
def demag_tensor(
5274
src_list,
@@ -348,7 +370,19 @@ def apply_demag(
348370
raise ValueError(
349371
"Apply_demag input collection and susceptibility must have same length."
350372
)
351-
S = np.diag(np.tile(susceptibility, 3)) # shape ii, jj
373+
susceptibility = np.reshape(
374+
susceptibility, 3 * n, order="F"
375+
)
376+
S = np.diag(susceptibility) # shape ii, jj
377+
378+
# set up H_ext
379+
H_ext = get_H_ext(*magnets_list)
380+
H_ext = np.array(H_ext)
381+
if len(H_ext) != n:
382+
raise ValueError("Apply_demag input collection and H_ext must have same length.")
383+
H_ext = np.reshape(
384+
H_ext, (3 * n, 1), order="F"
385+
)
352386

353387
# set up T (3 pol unit, n cells, n positions, 3 Bxyz)
354388
with timelog("Demagnetization tensor calculation", min_log_time=min_log_time):
@@ -362,7 +396,7 @@ def apply_demag(
362396
T *= magpy.mu_0
363397
T = T.swapaxes(2, 3).reshape((3 * n, 3 * n)).T # shape ii, jj
364398

365-
pol_tolal = pol_magnets
399+
pol_total = pol_magnets
366400

367401
if currents_list:
368402
with timelog(
@@ -371,14 +405,14 @@ def apply_demag(
371405
pos = np.array([src.position for src in magnets_list])
372406
pol_currents = magpy.getB(currents_list, pos, sumup=True)
373407
pol_currents = np.reshape(pol_currents, (3 * n, 1), order="F")
374-
pol_tolal += np.matmul(S, pol_currents)
408+
pol_total += np.matmul(S, pol_currents)
375409

376410
# set up Q
377411
Q = np.eye(3 * n) - np.matmul(S, T)
378412

379413
# determine new polarization vectors
380414
with timelog("Solving of linear system", min_log_time=1):
381-
pol_new = np.linalg.solve(Q, pol_tolal)
415+
pol_new = np.linalg.solve(Q, pol_total + np.matmul(S, H_ext))
382416

383417
pol_new = np.reshape(pol_new, (n, 3), order="F")
384418
# pol_new *= .4*np.pi
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import magpylib as magpy
2+
from meshing import mesh_Cuboid
3+
from demag import apply_demag
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
elements = 4
8+
9+
#hollow cylinder magnet
10+
cuboid = magpy.magnet.Cuboid(polarization=(1,2,3), dimension=(2,2,2))
11+
coll = mesh_Cuboid(cuboid, elements)
12+
#coll.susceptibility = (1,2,3,4)
13+
for i in range(len(coll)):
14+
coll[i].susceptibility = (i,i,i*10)
15+
#coll[i].susceptibility = i
16+
#coll.H_ext = (-10,-10,-10)
17+
coll = apply_demag(coll)
18+
19+
fig, ax = plt.subplots()
20+
21+
ts = np.linspace(0, 1, 9)
22+
grid = np.array([[(x, 0.1, z) for x in ts] for z in ts])
23+
24+
B = coll.getM(grid)
25+
26+
# Display the B-field with streamplot using log10-scaled
27+
# color function and linewidth
28+
splt = ax.quiver(
29+
grid[:, :, 0],
30+
grid[:, :, 2],
31+
B[:, :, 0],
32+
B[:, :, 2]
33+
)
34+
35+
print(B)
36+
37+
38+
# Figure styling
39+
ax.set(
40+
xlabel="x-position (mm)",
41+
ylabel="z-position (mm)",
42+
)
43+
44+
plt.tight_layout()
45+
plt.show()

0 commit comments

Comments
 (0)