Skip to content

Commit 8dfbce0

Browse files
committed
Merge branch 'anisotropic_chi' of https://github.com/magpylib/magpylib-material-response into anisotropic_chi
2 parents cb442cf + c227ed6 commit 8dfbce0

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

magpylib_material_response/demag.py

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,56 +31,50 @@
3131
logger.configure(**config)
3232

3333

34-
def get_susceptibilities(*sources, susceptibility):
34+
def get_susceptibilities(sources, susceptibility):
3535
"""Return a list of length (len(sources)) with susceptibility values
3636
Priority is given at the source level, hovever if value is not found, it is searched
3737
up the parent tree, if available. Raises an error if no value is found when reached
3838
the top level of the tree."""
39-
39+
n = len(sources)
40+
4041
# susceptibilities from source attributes
4142
if susceptibility is None:
42-
susceptibilities = []
43+
susis = []
4344
for src in sources:
4445
susceptibility = getattr(src, "susceptibility", None)
4546
if susceptibility is None:
4647
if src.parent is None:
47-
raise ValueError(
48-
"No susceptibility defined in any parent collection"
49-
)
50-
susceptibilities.extend(get_susceptibilities(src.parent))
48+
raise ValueError("No susceptibility defined in any parent collection")
49+
susis.extend(get_susceptibilities(src.parent))
5150
elif not hasattr(susceptibility, "__len__"):
52-
susceptibilities.append(
53-
(susceptibility, susceptibility, susceptibility)
54-
)
51+
susis.append((susceptibility, susceptibility, susceptibility))
5552
elif len(susceptibility) == 3:
56-
susceptibilities.append(susceptibility)
53+
susis.append(susceptibility)
5754
else:
5855
raise ValueError("susceptibility is not scalar or array fo length 3")
59-
return susceptibilities
60-
61-
# susceptibilities as input to demag function
62-
n = len(sources)
63-
if np.isscalar(susceptibility):
64-
susceptibility = np.ones((n, 3)) * susceptibility
65-
elif len(susceptibility) == 3:
66-
susceptibility = np.tile(susceptibility, (n, 1))
67-
if n == 3:
68-
raise ValueError(
69-
"Apply_demag input susceptibility is ambiguous - either scalar list or vector single entry. "
70-
"Please choose different means of input or change the number of cells in the Collection."
71-
)
7256
else:
73-
if len(susceptibility) != n:
74-
raise ValueError(
75-
"Apply_demag input susceptibility must be scalar, 3-vector, or same length as input Collection."
76-
)
77-
susceptibility = np.array(susceptibility)
78-
if susceptibility.ndim == 1:
79-
susceptibility = np.repeat(susceptibility, 3).reshape(n, 3)
80-
81-
susceptibility = np.reshape(susceptibility, 3 * n, order="F")
57+
# susceptibilities as input to demag function
58+
if np.isscalar(susceptibility):
59+
susis = np.ones((n,3))*susceptibility
60+
elif len(susceptibility) == 3:
61+
susis = np.tile(susceptibility, (n,1))
62+
if n==3:
63+
raise ValueError(
64+
"Apply_demag input susceptibility is ambiguous - either scalar list or vector single entry. "
65+
"Please choose different means of input or change the number of cells in the Collection."
66+
)
67+
else:
68+
if len(susceptibility) != n:
69+
raise ValueError(
70+
"Apply_demag input susceptibility must be scalar, 3-vector, or same length as input Collection."
71+
)
72+
susis = np.array(susceptibility)
73+
if susis.ndim == 1:
74+
susis = np.repeat(susis,3).reshape(n,3)
8275

83-
return np.array(susceptibilities)
76+
susis = np.reshape(susis, 3 * n, order="F")
77+
return np.array(susis)
8478

8579

8680
def get_H_ext(*sources, H_ext=None):

0 commit comments

Comments
 (0)