|
31 | 31 | logger.configure(**config)
|
32 | 32 |
|
33 | 33 |
|
34 |
| -def get_susceptibilities(*sources, susceptibility=None): |
| 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 |
| - susceptibilities = [] |
40 |
| - for src in sources: |
41 |
| - susceptibility = getattr(src, "susceptibility", None) |
42 |
| - if susceptibility is None: |
43 |
| - if src.parent is None: |
44 |
| - raise ValueError("No susceptibility defined in any parent collection") |
45 |
| - susceptibilities.extend(get_susceptibilities(src.parent)) |
46 |
| - elif not hasattr(susceptibility, "__len__"): |
47 |
| - susceptibilities.append((susceptibility, susceptibility, susceptibility)) |
48 |
| - elif len(susceptibility) == 3: |
49 |
| - susceptibilities.append(susceptibility) |
50 |
| - else: |
51 |
| - raise ValueError("susceptibility is not scalar or array fo length 3") |
52 |
| - return susceptibilities |
| 39 | + |
| 40 | + # susceptibilities from source attributes |
| 41 | + if susceptibility is None: |
| 42 | + susceptibilities = [] |
| 43 | + for src in sources: |
| 44 | + susceptibility = getattr(src, "susceptibility", None) |
| 45 | + if susceptibility is None: |
| 46 | + if src.parent is None: |
| 47 | + raise ValueError("No susceptibility defined in any parent collection") |
| 48 | + susceptibilities.extend(get_susceptibilities(src.parent)) |
| 49 | + elif not hasattr(susceptibility, "__len__"): |
| 50 | + susceptibilities.append((susceptibility, susceptibility, susceptibility)) |
| 51 | + elif len(susceptibility) == 3: |
| 52 | + susceptibilities.append(susceptibility) |
| 53 | + else: |
| 54 | + 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 | + ) |
| 68 | + 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 | + |
| 83 | + |
| 84 | + |
| 85 | + return np.array(susceptibilities) |
| 86 | + |
| 87 | + |
53 | 88 |
|
54 | 89 |
|
55 | 90 | def get_H_ext(*sources, H_ext=None):
|
@@ -364,15 +399,8 @@ def apply_demag(
|
364 | 399 | ) # shape ii = x1, ... xn, y1, ... yn, z1, ... zn
|
365 | 400 |
|
366 | 401 | # set up S
|
367 |
| - if susceptibility is None: |
368 |
| - susceptibility = get_susceptibilities(*magnets_list) |
369 |
| - susceptibility = np.array(susceptibility) |
370 |
| - if len(susceptibility) != n: |
371 |
| - raise ValueError( |
372 |
| - "Apply_demag input collection and susceptibility must have same length." |
373 |
| - ) |
374 |
| - susceptibility = np.reshape(susceptibility, 3 * n, order="F") |
375 |
| - S = np.diag(susceptibility) # shape ii, jj |
| 402 | + sus = get_susceptibilities(magnets_list, susceptibility) |
| 403 | + S = np.diag(sus) # shape ii, jj |
376 | 404 |
|
377 | 405 | # set up H_ext
|
378 | 406 | H_ext = get_H_ext(*magnets_list)
|
|
0 commit comments