|
31 | 31 | logger.configure(**config) |
32 | 32 |
|
33 | 33 |
|
34 | | -def get_susceptibilities(*sources, susceptibility): |
| 34 | +def get_susceptibilities(sources, susceptibility): |
35 | 35 | """Return a list of length (len(sources)) with susceptibility values |
36 | 36 | Priority is given at the source level, hovever if value is not found, it is searched |
37 | 37 | up the parent tree, if available. Raises an error if no value is found when reached |
38 | 38 | the top level of the tree.""" |
39 | | - |
| 39 | + n = len(sources) |
| 40 | + |
40 | 41 | # susceptibilities from source attributes |
41 | 42 | if susceptibility is None: |
42 | | - susceptibilities = [] |
| 43 | + susis = [] |
43 | 44 | for src in sources: |
44 | 45 | susceptibility = getattr(src, "susceptibility", None) |
45 | 46 | if susceptibility is None: |
46 | 47 | 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)) |
51 | 50 | elif not hasattr(susceptibility, "__len__"): |
52 | | - susceptibilities.append( |
53 | | - (susceptibility, susceptibility, susceptibility) |
54 | | - ) |
| 51 | + susis.append((susceptibility, susceptibility, susceptibility)) |
55 | 52 | elif len(susceptibility) == 3: |
56 | | - susceptibilities.append(susceptibility) |
| 53 | + susis.append(susceptibility) |
57 | 54 | else: |
58 | 55 | 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 | | - ) |
72 | 56 | 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) |
82 | 75 |
|
83 | | - return np.array(susceptibilities) |
| 76 | + susis = np.reshape(susis, 3 * n, order="F") |
| 77 | + return np.array(susis) |
84 | 78 |
|
85 | 79 |
|
86 | 80 | def get_H_ext(*sources, H_ext=None): |
|
0 commit comments