Skip to content

Commit 82e4fa3

Browse files
committed
allow multiple susceptibility input formats
1 parent ae8667d commit 82e4fa3

File tree

1 file changed

+27
-35
lines changed

1 file changed

+27
-35
lines changed

magpylib_material_response/demag.py

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,58 +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:
4748
raise ValueError("No susceptibility defined in any parent collection")
48-
susceptibilities.extend(get_susceptibilities(src.parent))
49+
susis.extend(get_susceptibilities(src.parent))
4950
elif not hasattr(susceptibility, "__len__"):
50-
susceptibilities.append((susceptibility, susceptibility, susceptibility))
51+
susis.append((susceptibility, susceptibility, susceptibility))
5152
elif len(susceptibility) == 3:
52-
susceptibilities.append(susceptibility)
53+
susis.append(susceptibility)
5354
else:
5455
raise ValueError("susceptibility is not scalar or array fo length 3")
55-
return susceptibilities
56-
57-
# susceptibilities as input to demag function
58-
n = len(sources)
59-
if np.isscalar(susceptibility):
60-
susceptibility = np.ones((n,3))*susceptibility
61-
elif len(susceptibility) == 3:
62-
susceptibility = np.tile(susceptibility, (n,1))
63-
if n==3:
64-
raise ValueError(
65-
"Apply_demag input susceptibility is ambiguous - either scalar list or vector single entry. "
66-
"Please choose different means of input or change the number of cells in the Collection."
67-
)
6856
else:
69-
if len(susceptibility) != n:
70-
raise ValueError(
71-
"Apply_demag input susceptibility must be scalar, 3-vector, or same length as input Collection."
72-
)
73-
susceptibility = np.array(susceptibility)
74-
if susceptibility.ndim == 1:
75-
susceptibility = np.repeat(susceptibility,3).reshape(n,3)
76-
77-
susceptibility = np.reshape(susceptibility, 3 * n, order="F")
78-
79-
80-
81-
82-
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)
8375

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

8779

8880

0 commit comments

Comments
 (0)