Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Anisotropic chi #9

Merged
merged 24 commits into from
Oct 16, 2024
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
15b2bd1
update
fslanovc May 3, 2024
408dd58
update folder
fslanovc May 3, 2024
1acbf88
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 5, 2024
6840233
add __temp tp gitignore
OrtnerMichael May 5, 2024
9ea92ea
comment out tests
OrtnerMichael May 6, 2024
ae8667d
intermediate
OrtnerMichael May 6, 2024
3121949
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 6, 2024
cb442cf
remove test_demag
fslanovc May 6, 2024
82e4fa3
allow multiple susceptibility input formats
OrtnerMichael May 6, 2024
c227ed6
Merge branch 'anisotropic_chi' of https://github.com/magpylib/magpyli…
OrtnerMichael May 6, 2024
8dfbce0
Merge branch 'anisotropic_chi' of https://github.com/magpylib/magpyli…
fslanovc May 6, 2024
b6c7a20
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 6, 2024
5374617
add bad test
OrtnerMichael May 7, 2024
5d7cf0f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 7, 2024
627a2ce
test and example
fslanovc Aug 27, 2024
ee56ab3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 27, 2024
e666bb9
files for difference cell numbers
fslanovc Aug 27, 2024
668f955
Merge branch 'anisotropic_chi' of https://github.com/magpylib/magpyli…
fslanovc Aug 27, 2024
5d82b89
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 27, 2024
987eef7
remove example from main folder
OrtnerMichael Aug 28, 2024
dfed8ff
changelog
OrtnerMichael Aug 28, 2024
aa1e8ce
Merge branch 'main' of https://github.com/magpylib/magpylib-material-…
OrtnerMichael Aug 28, 2024
d1e788e
example with negative susceptibility
fslanovc Aug 28, 2024
e4321bb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
update
  • Loading branch information
fslanovc committed May 3, 2024
commit 15b2bd10f3049256281f5b7800e4f3d0de6de8b8
44 changes: 39 additions & 5 deletions magpylib_material_response/demag.py
Original file line number Diff line number Diff line change
@@ -43,10 +43,32 @@ def get_susceptibilities(*sources, susceptibility=None):
if src.parent is None:
raise ValueError("No susceptibility defined in any parent collection")
susceptibilities.extend(get_susceptibilities(src.parent))
else:
elif not hasattr(susceptibility, "__len__"):
susceptibilities.append((susceptibility, susceptibility, susceptibility))
elif len(susceptibility) == 3:
susceptibilities.append(susceptibility)
else:
raise ValueError("susceptibility is not scalar or array fo length 3")
return susceptibilities

def get_H_ext(*sources, H_ext=None):
"""Return a list of length (len(sources)) with H_ext values
Priority is given at the source level, hovever if value is not found, it is searched up the
the parent tree, if available. Sets H_ext to zero if no value is found when reached the top
level of the tree"""
H_exts = []
for src in sources:
H_ext = getattr(src, "H_ext", None)
if H_ext is None:
if src.parent is None:
#print("Warning: No value for H_ext defined in any parent collection. H_ext set to zero.")
H_exts.append((0.0,0.0,0.0))
else:
H_exts.extend(get_H_ext(src.parent))
else:
H_exts.append(H_ext)
return H_exts


def demag_tensor(
src_list,
@@ -348,7 +370,19 @@ def apply_demag(
raise ValueError(
"Apply_demag input collection and susceptibility must have same length."
)
S = np.diag(np.tile(susceptibility, 3)) # shape ii, jj
susceptibility = np.reshape(
susceptibility, 3 * n, order="F"
)
S = np.diag(susceptibility) # shape ii, jj

# set up H_ext
H_ext = get_H_ext(*magnets_list)
H_ext = np.array(H_ext)
if len(H_ext) != n:
raise ValueError("Apply_demag input collection and H_ext must have same length.")
H_ext = np.reshape(
H_ext, (3 * n, 1), order="F"
)

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

pol_tolal = pol_magnets
pol_total = pol_magnets

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

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

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

pol_new = np.reshape(pol_new, (n, 3), order="F")
# pol_new *= .4*np.pi
45 changes: 45 additions & 0 deletions magpylib_material_response/test_demag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import magpylib as magpy
from meshing import mesh_Cuboid
from demag import apply_demag
import numpy as np
import matplotlib.pyplot as plt

elements = 4

#hollow cylinder magnet
cuboid = magpy.magnet.Cuboid(polarization=(1,2,3), dimension=(2,2,2))
coll = mesh_Cuboid(cuboid, elements)
#coll.susceptibility = (1,2,3,4)
for i in range(len(coll)):
coll[i].susceptibility = (i,i,i*10)
#coll[i].susceptibility = i
#coll.H_ext = (-10,-10,-10)
coll = apply_demag(coll)

fig, ax = plt.subplots()

ts = np.linspace(0, 1, 9)
grid = np.array([[(x, 0.1, z) for x in ts] for z in ts])

B = coll.getM(grid)

# Display the B-field with streamplot using log10-scaled
# color function and linewidth
splt = ax.quiver(
grid[:, :, 0],
grid[:, :, 2],
B[:, :, 0],
B[:, :, 2]
)

print(B)


# Figure styling
ax.set(
xlabel="x-position (mm)",
ylabel="z-position (mm)",
)

plt.tight_layout()
plt.show()
Loading
Oops, something went wrong.