Skip to content

Define integrated strengths in multipoles - #1001

Merged
lfarv merged 18 commits into
masterfrom
integr_polynoms
Oct 29, 2025
Merged

lfarv merged 18 commits into
masterfrom
integr_polynoms

Conversation

@lfarv

@lfarv lfarv commented Oct 20, 2025

Copy link
Copy Markdown
Contributor

This PR defined 2 new properties of Multipoles and derivatives:

  • IntegratedPolynomA = Length * PolynomA: skew strength
  • IntegratedPolynomB = Length * PolynomB: straight strength

@lfarv lfarv added enhancement Python For python AT code labels Oct 20, 2025
@swhite2401

Copy link
Copy Markdown
Contributor

@lfarv this is the minimal version of what I started in #970.
There was nevertheless much more to it and some open questions, in particular:
-The definition of Strength and Integrated Strength, corresponding to the DefaultOrder
-And to be done the introduction of KN1, KN1L, KS1, KS1L, etc.... because using K and H is really unsual....

I would very much prefer that we treat this problem as a whole instead of going for this minimal approach that has very limited added value.

@lfarv

lfarv commented Oct 21, 2025 •

Copy link
Copy Markdown
Contributor Author

@swhite: Once the Integrated polynoms are set, the rest follows immediately !

I added the properties
Kn0, Ks0, Kn0L, Ks0L and following up to the octupolar component (Kn3)

I also added Strength and IntegratedStrength, with the following rule:

  • for specific magnets (Dipole…), the order is determined by DefaultOrder,
  • for generic magnets (ThinMultipole, Multipole), by default an AttributeError is raised. However, the order can be selected by setting a DefautOrder attribute on the magnet instance. Example:
>>> from at import Multipole
>>> mult = Multipole("M1", 0.5, [], [0., 1., 2., 3.])
>>> mult.Strength
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/laurent/dev/libraries/at/pyat/at/lattice/elements/magnet_elements.py", line 57, in kget
    raise AttributeError(msg)
AttributeError: Undefined order. You need to set a 'DefaultOrder' attribute
>>>
>>> mult.DefaultOrder = 1
>>> mult.Strength
1.0
>>>
>>> mult.DefaultOrder = 2
>>> mult.Strength
2.0
>>> 
>>> mult.IntegratedStrength
1.0
>>> mult.IntegratedStrength = 2
>>> mult
Multipole('M1', 0.5, array([0., 0., 0., 0.]), array([0., 1., 4., 3.]), DefaultOrder=2)

@lfarv

lfarv commented Oct 21, 2025

Copy link
Copy Markdown
Contributor Author

The "integrated" properties (Kn.L, Ks.L, IntegratedStrength) are defined in ThinMultipole and are inherited by all derivatives (thin or thick)

The "local" properties (Kn., Ks., Strength) are defined in Multipole and inherited by its derivatives (they make no sense for thin magnets)

@JeanLucPons

Copy link
Copy Markdown
Collaborator

Hello,
Is it possible to add only a set attribute (or property).
I mean it is possible to define a Multipole(s) object that have for instance only Kn0L,Ks0L,Ks1L,Kn2L ?
It would be nice to detect functions of a combined function magnet.

@lfarv

lfarv commented Oct 21, 2025

Copy link
Copy Markdown
Contributor Author

@JeanLucPons:
The real field definition is PolynomA/B, there is no change here. All the new properties introduced here are just views into this polynomial. So the "active" functions of a magnet are the ones for which PolynomA/B[order] is non-zero. A way to identify them could be:

[n for n, v in enumerate(magnet.PolynomB[:magnet.MaxOrder+1]) if v != 0.0]

which gives a list of "active" orders.

@JeanLucPons

JeanLucPons commented Oct 21, 2025 •

Copy link
Copy Markdown
Collaborator

OK, detecting 0 is not reliable.
For instance on this sextupole KickAngle is defined.

Sextupole:
       FamName: SD1E-C02
        Length: 0.17989
    PassMethod: StrMPoleSymplectic4Pass
    EApertures: [0.025 0.01 ]
     KickAngle: [0. 0.]
      MaxOrder: 2
   NumIntSteps: 20
      PolynomA: [0. 0. 0.]
      PolynomB: [  0.           0.         -81.58486037]

Would it be possible to have:

Sextupole:
       FamName: SD1E-C02
        Length: 0.17989
    PassMethod: StrMPoleSymplectic4Pass
    EApertures: [0.025 0.01 ]
     KickAngle: [0. 0.]
          Kn0L: 0.
          Ks0L: 0.
          Ks1L: 0.
          Kn2L: -14.676300532
      MaxOrder: 2
   NumIntSteps: 20
      PolynomA: [0. 0. 0.]
      PolynomB: [  0.           0.         -81.58486037]

@lfarv

lfarv commented Oct 21, 2025

Copy link
Copy Markdown
Contributor Author

Oh, are you talking of the way the magnet is printed ? That's something else. At the moment, properties are normally not printed (K is an exception).

@JeanLucPons

Copy link
Copy Markdown
Collaborator

Yes, If property are well printed, then i can find what functions are available a bit as for KickAngle.

@lfarv

lfarv commented Oct 21, 2025

Copy link
Copy Markdown
Contributor Author

To comply with the lattice standard of PALS, the Kn coefficient must follow the MAD/PALS field expansion. This may be puzzling, so I added some documentation on field expansion. Look at this and give me your comments.

Apart from that, do you see anything more to be added ?

@JeanLucPons

Copy link
Copy Markdown
Collaborator

Thanks for the doc. It is clear.
My idea at the beginning was to be able to describe a combined function magnet (without speaking of multipolar errors).
For instance, be able to construct a "Multipoles" object that contains additional information on available controllable strengths.

@lfarv

lfarv commented Oct 22, 2025

Copy link
Copy Markdown
Contributor Author

@JeanLucPons : I do not see clearly what you mean: you can build any combined function magnet, and control any strength in the PolynomB or PolynomA arrays. By lengthening the polynoms, you can reach any high order multipole. So I think that all the information you need is in these 2 attributes.

Anyway this can be discussed in another PR, for the time being I would like to focus on the introduction of the integrated multipoles and the additional properties (but keep in mind that these are just views into PolynomA and PolynomB).

Is this OK or is there anything more related to that?

@JeanLucPons

Copy link
Copy Markdown
Collaborator

I agree. There is no limitation in AT to access the strength you want.
But I have no information on which strength I can control from the real hardware which could help in setting up a middle layer.
I agree this can be the purpose of an other PR.

@lfarv

lfarv commented Oct 22, 2025

Copy link
Copy Markdown
Contributor Author

@JeanLucPons : Ah, hardware ! Well, up to now, AT totally ignores hardware, but you are free to add any attribute to elements, concerning anything you like. Additional attributes (other than the ones documented) are totally ignored and harmless.

In addition, it would be possible to decide that some attributes become part of AT, even if they are not used, just to freeze their names and avoid name conflicts.

@lfarv

lfarv commented Oct 23, 2025

Copy link
Copy Markdown
Contributor Author

@JeanLucPons : to illustrate what I was writing above, here is an example:

>>> from at import Sextupole
>>> 
>>> # Create a sextupole
>>> sx = Sextupole("SX1", 0.5, 100.0)
>>> 
>>> # Add some hardware-specific attributes
>>> sx.ML_Kn0 = "SR/PS/COR-H/SX1"   # Horizontal corrector
>>> sx.ML_Ks0 = "SR/PS/COR-V/SX1   # Vertical corrector
>>> sx.ML_Ks1 = "SR/PS/COR-SKEW/SX1"   # Skew quadrupole
>>> 
>>> print(sx)
Sextupole:
       FamName: SX1
        Length: 0.5
    PassMethod: StrMPoleSymplectic4Pass
        ML_Kn0: SR/PS/COR-H/SX1
        ML_Ks0: SR/PS/COR-V/SX1
        ML_Ks1: SR/PS/COR-SKEW/SX1
      MaxOrder: 2
   NumIntSteps: 10
      PolynomA: [0. 0. 0.]
      PolynomB: [  0.   0. 100.]
>>> 

There you can see the hardware capabilities.
These "custom" attributes are totally ignored, and instead of a string, you can put any object like a device proxy

@JeanLucPons

Copy link
Copy Markdown
Collaborator

Thanks
In fact i would like to go a bit further. For instance having a new Multipoles class that inherits from Multipole where I can specify properties I want to see (and detect) for instance Kn0,Ks0,Ks1. It may be done by overriding def items(self) and give a list of properties to the Multipoles constructor.
It just an idea.

@lfarv

lfarv commented Oct 23, 2025 •

Copy link
Copy Markdown
Contributor Author

I added the new Kn0L and Ks0L properties to the Corrector element. They are equivalent to the horizontal and vertical deviation angles.

With this, Kn0L and Ks0L, which are now available for all magnets (Corrector, ThinMultipole, Multipole) can be used everywhere instead of KickAngle. For instance in orbit response matrices, it avoids the necessity to create the KickAngle attribute in advance.

More exactly, for any magnet: $Kn0L = -x' = -\tan(hangle)$ and $Ks0L = y' = \tan(vangle)$.

@JeanLucPons

Copy link
Copy Markdown
Collaborator

I have a qestion concerning this tan(). Today:

    if (KickAngle) {   /* Convert corrector component to polynomial coefficients */
        B[0] -= sin(KickAngle[0])/le;
        A[0] += sin(KickAngle[1])/le;
    }

It uses a sin() so if you consider the pathlength for the hypotenuse, this should be a sin().
I know that it does not make big difference but what is correct ?

@lfarv

lfarv commented Oct 23, 2025 •

Copy link
Copy Markdown
Contributor Author

@JeanLucPons : I noticed that this morning. When applied to a straight magnet, the reference path is a straight line and the slope $x'$ at the exit of the magnet is indeed $\tan(angle)$. For me, the sine in the integrator is wrong, it should be a tangent… To be discussed.

@lfarv

lfarv commented Oct 23, 2025

Copy link
Copy Markdown
Contributor Author

@JeanLucPons

overriding def items(self):

I strongly discourage using items() for that. Among other things, items() is used in file storage of elements, and defines what needs to be stored, so that the element can be rebuilt identically. Normally, this is exactly the list of attributes (but not properties, which are derived from the attributes). The only exception is K, because for historical reasons in Matlab K and PolynomB are different attributes. So K is added to the list and both are stored.

So overriding items may cause storage problems…

But there are many other ways to answer your question

@lfarv

lfarv commented Oct 23, 2025

Copy link
Copy Markdown
Contributor Author

For instance, a solution which does not need any modification:

>>> from at import Element, Sextupole
>>> 
>>> # define a new attribute "ml_attributes" listing the "interesting" attributes/properties
>>> 
>>> # write a function using this attribute
>>> def ml_print(elem):
...     attrlist = getattr(elem, "ml_attributes", [])
...     for attr in attrlist:
...         print(f"{attr}: {getattr(elem, attr)}")
... 
>>>         
>>> # Add this user-defined method to the Element class
>>> Element.ml_print = ml_print
>>> 
>>> # Create a sextupole
>>> sx = Sextupole("sx1", 0.5, 100)
>>> # Select some attibutes
>>> sx.ml_attributes = ["Kn0", "Ks0", "Ks1"]
>>> 
>>> sx.ml_print()
Kn0: 0.0
Ks0: 0.0
Ks1: 0.0
>>>                 

You can add your own attributes and methods to the Element class. Of course, those which are of general interest can be included in AT itself.

@swhite2401

Copy link
Copy Markdown
Contributor

Thanks @lfarv for the many useful additions! On my side it looks ok.

@lfarv

lfarv commented Oct 28, 2025

Copy link
Copy Markdown
Contributor Author

@JeanLucPons, @swhite2401 : Do you approve ?

@lfarv
lfarv merged commit c632d9f into master Oct 29, 2025
@lfarv
lfarv deleted the integr_polynoms branch October 29, 2025 09:10
@swhite2401
swhite2401 restored the integr_polynoms branch October 30, 2025 10:07
@swhite2401

Copy link
Copy Markdown
Contributor

@lfarv, this is not ok, you have added the k[ns]l properties with a convention that is not consistent with AT. I understand you would like to stick the the PALS convention but it is in my opinion not acceptable to have 2 different convention in the same code, this is just too confusion for the user; either we chance everywhere or we stick to AT convention.

I have restore the branch so we can correct this.

@lfarv

lfarv commented Oct 30, 2025 •

Copy link
Copy Markdown
Contributor Author

@swhite2401

To comply with the lattice standard of PALS, the Kn coefficient must follow the MAD/PALS field expansion. This may be puzzling, so I added some documentation on field expansion. Look at this and give me your comments.

I was indeed wondering about the best option… But I got no feedback.

However I understand your concern, so #1010 switches to the AT convention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Python For python AT code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants