From 26c70ebd204553110a6cdbf0dfdf72f3fc1decf5 Mon Sep 17 00:00:00 2001 From: kparasch Date: Mon, 23 Nov 2026 17:02:30 +0100 Subject: [PATCH] Bending angle treatment for simulator --- pyaml/lattice/abstract_impl.py | 36 ++++++++++++++++++---- tests/lattice/test_magnet_accessors.py | 41 ++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/pyaml/lattice/abstract_impl.py b/pyaml/lattice/abstract_impl.py index c0ecb817..04a5dc66 100644 --- a/pyaml/lattice/abstract_impl.py +++ b/pyaml/lattice/abstract_impl.py @@ -3,6 +3,12 @@ This module adapts Accelerator Toolbox lattice elements to PyAML accessors for magnet strengths, hardware values, BPM readings, RF parameters, and tune data. + +For ``PolynomB[0]``, integrated strength is +``sign * coefficient * effective_length + BendingAngle``. The bending angle is +outside the sign conversion and defaults to zero when absent. Writes subtract +the total bending angle before distributing the residual polynomial strength; +they never change the lattice elements' bending angles. """ import at @@ -36,6 +42,13 @@ def _effective_lengths(elements: list[at.Element]) -> list[float]: return lengths +def _bending_angle(element: at.Element, polynom: str, index: int) -> float: + """Return one element's current bend offset for the horizontal dipole component.""" + if polynom == "PolynomB" and index == 0: + return getattr(element, "BendingAngle", 0.0) + return 0.0 + + class RWHardwareScalar(abstract.ReadWriteFloatScalar): """ Provide read/write access to a simulated magnet in hardware units. @@ -78,6 +91,7 @@ def __init__(self, elements: list[at.Element], poly: PolynomInfo, model: MagnetM self._poly = [e.__getattribute__(poly.attName) for e in elements] self._sign = poly.sign self._polyIdx = poly.index + self._polyName = poly.attName self._is_thin = all(e.Length == 0 for e in elements) self._lengths = _effective_lengths(elements) self._length: float = sum(self._lengths) @@ -92,7 +106,7 @@ def is_thin(self) -> bool: def get(self) -> float: """Return the current value.""" - s = 0 + s = sum(_bending_angle(e, self._polyName, self._polyIdx) for e in self._elements) for idx, _ in enumerate(self._elements): s += self._poly[idx][self._polyIdx] * self._sign * self._lengths[idx] return self._model.compute_hardware_values([s])[0] @@ -107,6 +121,7 @@ def set(self, value: float): Hardware value to apply, in the unit reported by the magnet model. """ s = self._model.compute_strengths([value])[0] + s -= sum(_bending_angle(e, self._polyName, self._polyIdx) for e in self._elements) for idx, _ in enumerate(self._elements): self._poly[idx][self._polyIdx] = s / (self._length * self._sign) @@ -180,6 +195,7 @@ def __init__(self, elements: list[at.Element], poly: PolynomInfo, model: MagnetM self._poly = [e.__getattribute__(poly.attName) for e in elements] self._sign = poly.sign self._polyIdx = poly.index + self._polyName = poly.attName self._lengths = _effective_lengths(elements) self._length: float = sum(self._lengths) @@ -208,12 +224,13 @@ def get(self, polynom: str = None, polyidx: int = None) -> float: if polynom is None: pIdx = self._polyIdx poly = self._poly + polynom = self._polyName else: # Override strength access pIdx = polyidx poly = [e.__getattribute__(polynom) for e in self._elements] - s = 0 + s = sum(_bending_angle(e, polynom, pIdx) for e in self._elements) for idx, _ in enumerate(self._elements): s += poly[idx][pIdx] * self._sign * self._lengths[idx] return s @@ -236,11 +253,13 @@ def set(self, value: float, polynom: str = None, polyidx: int = None): if polynom is None: pIdx = self._polyIdx poly = self._poly + polynom = self._polyName else: # Override strength access pIdx = polyidx poly = [e.__getattribute__(polynom) for e in self._elements] + value -= sum(_bending_angle(e, polynom, pIdx) for e in self._elements) for idx, _ in enumerate(self._elements): poly[idx][pIdx] = value / (self._length * self._sign) @@ -524,12 +543,14 @@ def __init__(self, elements: list[at.Element], poly: list[PolynomInfo], model: M self.__elements = elements self.__poly = [] self.__polyIdx = [] + self.__polyName = [] self.__sign = [] self.__model = model self.__length = _effective_lengths(elements[:1])[0] for p in poly: self.__poly.append(elements[0].__getattribute__(p.attName)) self.__polyIdx.append(p.index) + self.__polyName.append(p.attName) self.__sign.append(p.sign) # Gets the value @@ -539,6 +560,7 @@ def get(self) -> np.array: s = np.zeros(nbStrength) for i in range(nbStrength): s[i] = self.__poly[i][self.__polyIdx[i]] * self.__sign[i] * self.__length + s[i] += _bending_angle(self.__elements[0], self.__polyName[i], self.__polyIdx[i]) return self.__model.compute_hardware_values(s) # Sets the value @@ -554,7 +576,8 @@ def set(self, value: np.array): nbStrength = len(self.__poly) s = self.__model.compute_strengths(value) for i in range(nbStrength): - self.__poly[i][self.__polyIdx[i]] = s[i] / (self.__length * self.__sign[i]) + offset = _bending_angle(self.__elements[0], self.__polyName[i], self.__polyIdx[i]) + self.__poly[i][self.__polyIdx[i]] = (s[i] - offset) / (self.__length * self.__sign[i]) # Sets the value and wait that the read value reach the setpoint def set_and_wait(self, value: np.array): @@ -615,12 +638,14 @@ def __init__(self, elements: list[at.Element], poly: list[PolynomInfo], model: M self.__elements = elements self.__poly = [] self.__polyIdx = [] + self.__polyName = [] self.__sign = [] self.__model = model self.__length = _effective_lengths(elements[:1])[0] for p in poly: self.__poly.append(elements[0].__getattribute__(p.attName)) self.__polyIdx.append(p.index) + self.__polyName.append(p.attName) self.__sign.append(p.sign) # Gets the value @@ -630,6 +655,7 @@ def get(self) -> np.array: s = np.zeros(nbStrength) for i in range(nbStrength): s[i] = self.__poly[i][self.__polyIdx[i]] * self.__sign[i] * self.__length + s[i] += _bending_angle(self.__elements[0], self.__polyName[i], self.__polyIdx[i]) return s # Sets the value @@ -643,9 +669,9 @@ def set(self, value: np.array): Integrated strengths to apply, one per magnet and ordered like the array. """ nbStrength = len(self.__poly) - s = np.zeros(nbStrength) for i in range(nbStrength): - self.__poly[i][self.__polyIdx[i]] = value[i] / (self.__length * self.__sign[i]) + offset = _bending_angle(self.__elements[0], self.__polyName[i], self.__polyIdx[i]) + self.__poly[i][self.__polyIdx[i]] = (value[i] - offset) / (self.__length * self.__sign[i]) # Sets the value and wait that the read value reach the setpoint def set_and_wait(self, value: np.array): diff --git a/tests/lattice/test_magnet_accessors.py b/tests/lattice/test_magnet_accessors.py index 28c7a924..b1d8ee78 100644 --- a/tests/lattice/test_magnet_accessors.py +++ b/tests/lattice/test_magnet_accessors.py @@ -9,6 +9,7 @@ RWStrengthArray, RWStrengthScalar, ) +from pyaml.lattice.polynom_info import PolynomInfo from pyaml.magnet.hcorrector import HCorrector from pyaml.magnet.identity_cfm_model import IdentityCFMagnetModel from pyaml.magnet.identity_model import IdentityMagnetModel @@ -90,3 +91,43 @@ def test_serialized_magnets_follow_the_strength_of_one_magnet(): assert elements[0].PolynomB[0] == pytest.approx(-1.0e-6) assert elements[1].PolynomB[0] * elements[1].Length == pytest.approx(-1.0e-6) assert serialized[1].index() == 1 + + +@pytest.mark.parametrize("accessor_type", [RWStrengthScalar, RWHardwareScalar]) +@pytest.mark.parametrize("sign", [-1.0, 1.0]) +@pytest.mark.parametrize("length", [0.0, 0.4]) +@pytest.mark.parametrize("angle", [0.0, 0.02]) +def test_scalar_bending_angle(accessor_type, sign, length, angle): + element = at.Dipole("BEND", length, angle) + element.PolynomB[0] = 0.003 + effective_length = length or 1.0 + model = IdentityMagnetModel(physics="COR", unit="rad") + accessor = accessor_type([element], PolynomInfo("PolynomB", 0, sign), model) + + assert accessor.get() == pytest.approx(sign * 0.003 * effective_length + angle) + accessor.set(0.025) + assert element.PolynomB[0] == pytest.approx((0.025 - angle) / (sign * effective_length)) + assert accessor.get() == pytest.approx(0.025) + assert element.BendingAngle == angle + + element.BendingAngle += 0.01 + assert accessor.get() == pytest.approx(0.035) + accessor.set(0.04) + assert element.PolynomB[0] == pytest.approx((0.04 - angle - 0.01) / (sign * effective_length)) + assert accessor.get() == pytest.approx(0.04) + + +def test_serialized_magnets_with_bending_angles(): + elements = [at.Dipole("B1", 0.2, 0.01), at.Dipole("B2", 0.4, 0.03)] + model = IdentityMagnetModel(physics="COR", unit="rad") + strengths = [RWStrengthScalar([e], HCorrector.polynom, model) for e in elements] + currents = [RWHardwareScalar([e], HCorrector.polynom, model) for e in elements] + serialized = [RWSerializedStrength(strengths, currents, i) for i in range(len(elements))] + + serialized[0].set(0.05) + + for accessor in serialized: + assert accessor.get() == pytest.approx(0.05) + assert elements[0].PolynomB[0] == pytest.approx(-0.2) + assert elements[1].PolynomB[0] == pytest.approx(-0.05) + assert [e.BendingAngle for e in elements] == [0.01, 0.03]