|
21 | 21 |
|
22 | 22 | logger = logging.getLogger(__name__) |
23 | 23 |
|
24 | | -# Relative spread above which the currents requested by members of a series are considered inconsistent |
| 24 | +# Relative gap above which strengths requested for a series are considered unreachable with a single hardware value |
25 | 25 | _SPREAD_RTOL = 1e-6 |
26 | 26 |
|
27 | 27 |
|
@@ -180,6 +180,8 @@ class LinearSerializedMagnetModel(MagnetModel, DynamicValidation): |
180 | 180 | Return the average strength of the series for a hardware value. |
181 | 181 | compute_hardware_from_mean_strength(strength) |
182 | 182 | Return the hardware value giving an average strength of the series. |
| 183 | + compute_hardware_from_strengths(strengths) |
| 184 | + Return the hardware value for a requested strength of each magnet of the series. |
183 | 185 | shares_power_supply() |
184 | 186 | Return True, all the magnets of the series share one power supply. |
185 | 187 | get_strength_units() |
@@ -355,37 +357,37 @@ def compute_strengths(self, currents: np.array) -> np.array: |
355 | 357 | return np.array([model.compute_strengths([current])[0] for model in self.__sub_models]) |
356 | 358 |
|
357 | 359 | def compute_hardware_values_partial( |
358 | | - self, current_hardware: np.ndarray | None, strengths_by_index: dict[int, float] |
| 360 | + self, current_hardware: np.ndarray, strengths_by_index: dict[int, float] |
359 | 361 | ) -> np.ndarray: |
360 | 362 | """ |
361 | 363 | Convert the strengths of some magnets of the series to the shared hardware value. |
362 | 364 |
|
363 | | - Each requested strength implies a hardware value through its own submodel. The power supply gets the mean of |
364 | | - the hardware values implied by the requested magnets only: the magnets that are not requested do not take part. |
365 | | - A warning is logged when the requested strengths are not consistent with a single hardware value. |
| 365 | + The magnets that are not requested keep the strength given by the current hardware value. The completed |
| 366 | + strengths are then converted by :meth:`compute_hardware_from_strengths`, which logs a warning when they |
| 367 | + cannot all be reached with a single hardware value. |
366 | 368 |
|
367 | 369 | Parameters |
368 | 370 | ---------- |
369 | | - current_hardware : np.array | None |
370 | | - Current hardware value of the series. Not used: the requested strengths define the hardware value. |
| 371 | + current_hardware : np.array |
| 372 | + Current hardware value of the series, one value per power supply. |
371 | 373 | strengths_by_index : dict[int, float] |
372 | 374 | Requested strengths, indexed by the position of the magnet in the series. |
373 | 375 |
|
374 | 376 | Returns |
375 | 377 | ------- |
376 | 378 | np.array |
377 | | - Hardware value of the power supply. |
| 379 | + Hardware value of the power supply, with the shape of ``current_hardware``. |
| 380 | +
|
| 381 | + Raises |
| 382 | + ------ |
| 383 | + PyAMLException |
| 384 | + If ``current_hardware`` does not hold one value per power supply of the series. |
378 | 385 | """ |
379 | | - currents = np.array( |
380 | | - [self.__sub_models[idx].compute_hardware_values([strength])[0] for idx, strength in strengths_by_index.items()] |
381 | | - ) |
382 | | - current = np.mean(currents) |
383 | | - if np.ptp(currents) > _SPREAD_RTOL * max(abs(current), 1.0): |
384 | | - logger.warning( |
385 | | - f"Inconsistent strengths requested for magnets {list(strengths_by_index.keys())} of a series on " |
386 | | - f"{self._powerconverter}: implied hardware values {currents}, the mean {current} is applied" |
387 | | - ) |
388 | | - return np.array([current]) |
| 386 | + _check_len(current_hardware, "current_hardware", len(self.get_device_names())) |
| 387 | + strengths = self.compute_strengths(current_hardware) |
| 388 | + for idx, strength in strengths_by_index.items(): |
| 389 | + strengths[idx] = strength |
| 390 | + return np.array([self.compute_hardware_from_strengths(strengths)]) |
389 | 391 |
|
390 | 392 | def compute_mean_strength(self, current: float) -> float: |
391 | 393 | """ |
@@ -436,6 +438,41 @@ def compute_hardware_from_mean_strength(self, strength: float) -> float: |
436 | 438 | ) |
437 | 439 | return float(np.interp(strength, strengths, currents)) |
438 | 440 |
|
| 441 | + def compute_hardware_from_strengths(self, strengths: np.ndarray) -> float: |
| 442 | + """ |
| 443 | + Return the hardware value for a requested strength of each magnet of the series. |
| 444 | +
|
| 445 | + The series has a single hardware value, so it solves ``sum(f_i(I)) == sum(k_i)`` for ``I``. When the requested |
| 446 | + strengths cannot all be reached with one hardware value, the result is a compromise and a warning is logged. |
| 447 | +
|
| 448 | + Parameters |
| 449 | + ---------- |
| 450 | + strengths : np.ndarray |
| 451 | + Requested strength ``k_i`` of each magnet of the series, in declaration order. |
| 452 | +
|
| 453 | + Returns |
| 454 | + ------- |
| 455 | + float |
| 456 | + Hardware value of the power supply. |
| 457 | +
|
| 458 | + Raises |
| 459 | + ------ |
| 460 | + PyAMLException |
| 461 | + If the number of strengths does not match the number of magnets, or if the average strength is not |
| 462 | + monotonic in the hardware value. |
| 463 | + """ |
| 464 | + strengths = np.asarray(strengths, dtype=float) |
| 465 | + _check_len(strengths, "strengths", self.__nbMagnets) |
| 466 | + current = self.compute_hardware_from_mean_strength(float(np.mean(strengths))) |
| 467 | + gaps = self.compute_strengths([current]) - strengths |
| 468 | + if np.any(np.abs(gaps) > _SPREAD_RTOL * np.maximum(np.abs(strengths), 1.0)): |
| 469 | + worst = int(np.argmax(np.abs(gaps))) |
| 470 | + logger.warning( |
| 471 | + f"Inconsistent strengths requested for the series on {self._powerconverter}: the hardware value " |
| 472 | + f"{current} misses the requested strengths by up to {gaps[worst]:.3g} (magnet {worst})" |
| 473 | + ) |
| 474 | + return current |
| 475 | + |
439 | 476 | def shares_power_supply(self) -> bool: |
440 | 477 | """Return True, all the magnets of the series share one power supply.""" |
441 | 478 | return True |
|
0 commit comments