Skip to content

Dynamic elements arrays#60

Merged
JeanLucPons merged 7 commits intomainfrom
dynamic-elements-arrays
Nov 7, 2025
Merged

Dynamic elements arrays#60
JeanLucPons merged 7 commits intomainfrom
dynamic-elements-arrays

Conversation

@JeanLucPons
Copy link
Copy Markdown
Contributor

This PR propose dynamic arrays to answer #46

Here are few possible writing.
Improvement and ideas are welcome.
The selection code is located in pyaml/arrays/element_array.py

from pyaml.pyaml import pyaml,PyAML
from pyaml.configuration.factory import Factory
from pyaml.instrument import Instrument
from pyaml.arrays.element_array import ElementArray

ml:PyAML = pyaml("tests/config/EBSOrbit.yaml")
sr:Instrument = ml.get('sr')
ae = ElementArray("All",sr.design.get_all_elements())
acfm = ElementArray("AllCFM",sr.design.get_all_cfm_magnets(),use_aggregator=False)

bpmC5 = ae['BPM*'][10:20]
print(f"{type(bpmC5)}: {len(bpmC5)} items")  # All BPM C5
bpmc10 = ae['BPM*C10*'] # All BPM C10
print(f"{type(bpmc10)}: {len(bpmc10)} items")
magSHV = ae['SH*-V'] # All SH vertical corrector
print(f"{type(magSHV)}: {len(magSHV)} items")
magSH1A = ae['model_name:SH1A-*'] # All SH1A magnets (including the CFMs)
print(f"{type(magSH1A)}: {len(magSH1A)} items")
magSH1AC = acfm['model_name:SH1A-*'] # All SH1A magnets (CFMs only)
print(f"{type(magSH1AC)}: {len(magSH1AC)} items")
    
Factory.clear()

outputs

<class 'pyaml.arrays.bpm_array.BPMArray'>: 10 items
<class 'pyaml.arrays.bpm_array.BPMArray'>: 10 items
<class 'pyaml.arrays.magnet_array.MagnetArray'>: 96 items
<class 'pyaml.arrays.element_array.ElementArray'>: 129 items
<class 'pyaml.arrays.cfm_magnet_array.CombinedFunctionMagnetArray'>: 32 items

@gubaidulinvadim
Copy link
Copy Markdown
Contributor

This looks mostly good to me. However, when I test I notice something strange:

In [15]: magSH1AC.strengths.get().shape
Out[15]: (97,)

In [16]: magSH1AC[0].strengths.get().shape
Out[16]: (4,)

In [17]: len(magSH1AC)
Out[17]: 32

In [18]: 4*32
Out[18]: 128

I do not understand what strengths of an array of combined function magnets are returned. For me, I would naively think that it will be a 2D array with a shape of (32, 4). A single ESRF combined function magnet returns array of (4,) corresponding to A0, B0, A1, B2. So, what is returned by a combined function array?

Also, there's a small issue for the user. The element array has no strength attribute because it can contain elements that are not magnets. But the user would want to have an array of, let's say, all H correctors. Some will be single function magnets and some will be combined function magnets. Then if I want to print the strength of all H correctors, I need to manually loop over all the magnets and have something like:

for m in magSH1A:
    if isinstance(m, CombinedFunctionMagnet):
        print(m.strengths[0].get())
    elif isinstance(m, SingleFunctionMagnet):
        print(m.strength.get())

Is this correct?

@JeanLucPons
Copy link
Copy Markdown
Contributor Author

Yes this seems correct.
Our SH1A-C04 and SH3E-C03 can perform H,V,SQ,S while others SH perform only H,V,SQ.
So 32*3 + 4 = 97.
This is the problem with CombinedFunctionMagnet and why I am against using them directly.
A CombinedFunctionMagnet has a random number of strengths.

Virutal Magnet are there especially for this, and aggregators are there to prevent from setpoint duplications.

However, if you have a set of coherent combined function magnets then you can easily reshape the results.

@gubaidulinvadim
Copy link
Copy Markdown
Contributor

I see, I didn't check if there are different types of combined function magnets. The strengths array in this example then is not very useful because it had to be flattened and there's no way to tell to which field component an array element belongs.

For the user, it would definitely be good to have a way to change all H correctors (combined and not) in a single operation. For the naming and sorting-by-name part, this is already very good.

@JeanLucPons
Copy link
Copy Markdown
Contributor Author

I see, I didn't check if there are different types of combined function magnets. The strengths array in this example then is not very useful because it had to be flattened and there's no way to tell to which field component an array element belongs.

For the user, it would definitely be good to have a way to change all H correctors (combined and not) in a single operation. For the naming and sorting-by-name part, this is already very good.

This is possible. You can create an array of HCorrector having both virtual and simple Magnet and setting them all at once.
It is even possible to mix all and creating an array of all Magnet (including simple one and virtual one) and setting a big vector of strenghts. This will end in one and only one setpoint on each used power supply.

@JeanLucPons JeanLucPons merged commit a22051b into main Nov 7, 2025
2 checks passed
@gubaidulinvadim gubaidulinvadim linked an issue Dec 4, 2025 that may be closed by this pull request
@gubaidulinvadim gubaidulinvadim deleted the dynamic-elements-arrays branch December 17, 2025 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Add hardware_name and model_name to elements

2 participants