|
| 1 | +import logging |
| 2 | +from pathlib import Path |
| 3 | +from typing import List, Literal, Optional |
| 4 | + |
| 5 | +from pydantic import BaseModel, ConfigDict |
| 6 | + |
| 7 | +from ..common.element_holder import ElementHolder |
| 8 | +from ..external.pySC import pySC |
| 9 | +from ..external.pySC.pySC import ResponseMatrix |
| 10 | +from ..external.pySC.pySC.apps import orbit_correction |
| 11 | +from ..external.pySC_interface import pySCInterface |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | +logging.getLogger("pyaml.external.pySC").setLevel(logging.WARNING) |
| 15 | + |
| 16 | +PYAMLCLASS = "Orbit" |
| 17 | + |
| 18 | + |
| 19 | +class ConfigModel(BaseModel): |
| 20 | + model_config = ConfigDict(arbitrary_types_allowed=True, extra="forbid") |
| 21 | + |
| 22 | + bpm_array_name: str |
| 23 | + hcorr_array_name: str |
| 24 | + vcorr_array_name: str |
| 25 | + singular_values: int |
| 26 | + response_matrix_file: str |
| 27 | + |
| 28 | + |
| 29 | +class Orbit(object): |
| 30 | + def __init__(self, element_holder: ElementHolder, cfg: ConfigModel): |
| 31 | + self._cfg = cfg |
| 32 | + |
| 33 | + self.element_holder = element_holder |
| 34 | + self.bpm_array_name = cfg.bpm_array_name |
| 35 | + self.hcorr_array_name = cfg.hcorr_array_name |
| 36 | + self.vcorr_array_name = cfg.vcorr_array_name |
| 37 | + self.singular_values = cfg.singular_values |
| 38 | + self.response_matrix = ResponseMatrix.from_json(Path(cfg.response_matrix_file)) |
| 39 | + |
| 40 | + def correct( |
| 41 | + self, |
| 42 | + reference=None, |
| 43 | + gain: float = 1.0, |
| 44 | + plane: Optional[Literal["H", "V"]] = None, |
| 45 | + ): |
| 46 | + interface = pySCInterface( |
| 47 | + element_holder=self.element_holder, |
| 48 | + bpm_array_name=self.bpm_array_name, |
| 49 | + hcorr_array_name=self.hcorr_array_name, |
| 50 | + vcorr_array_name=self.vcorr_array_name, |
| 51 | + ) |
| 52 | + |
| 53 | + if plane is None or plane == "H": |
| 54 | + trims_h = orbit_correction( |
| 55 | + interface=interface, |
| 56 | + response_matrix=self.response_matrix, |
| 57 | + method="svd_values", |
| 58 | + parameter=self.singular_values, |
| 59 | + zerosum=True, |
| 60 | + apply=False, |
| 61 | + plane="H", |
| 62 | + reference=reference, |
| 63 | + ) |
| 64 | + |
| 65 | + if plane is None or plane == "V": |
| 66 | + trims_v = orbit_correction( |
| 67 | + interface=interface, |
| 68 | + response_matrix=self.response_matrix, |
| 69 | + method="svd_values", |
| 70 | + parameter=self.singular_values, |
| 71 | + zerosum=False, |
| 72 | + apply=False, |
| 73 | + plane="V", |
| 74 | + reference=reference, |
| 75 | + ) |
| 76 | + |
| 77 | + # this is now the only place where interface.set_many and get_many are used. |
| 78 | + # if we can remove it from here then the pySCInterface will be even simpler. |
| 79 | + correctors = self.response_matrix.input_names |
| 80 | + data_to_send = interface.get_many(correctors) |
| 81 | + if plane is None or plane == "H": |
| 82 | + for name in trims_h.keys(): |
| 83 | + data_to_send[name] += trims_h[name] * gain |
| 84 | + if plane is None or plane == "V": |
| 85 | + for name in trims_v.keys(): |
| 86 | + data_to_send[name] += trims_v[name] * gain |
| 87 | + |
| 88 | + interface.set_many(data_to_send) |
| 89 | + |
| 90 | + return |
0 commit comments