77class RWMagnetStrength (ReadWriteFloatArray ):
88
99 def __init__ (self , name :str , magnets :list [Magnet ]):
10- self .__magnets = magnets
1110 self .__name = name
12- self .aggregator :ScalarAggregator = None
11+ self .__magnets = magnets
12+ self .__nb = len (self .__magnets )
13+ self .__aggregator :ScalarAggregator = None
1314
1415 # Gets the values
1516 def get (self ) -> np .array :
16- if not self .aggregator :
17+ if not self .__aggregator :
1718 return np .array ([m .strength .get () for m in self .__magnets ])
1819 else :
19- allHardwareValues = self .aggregator .get () # Read all hardware setpoints
20- allStrength = np .zeros (len (self .__magnets ))
21- mIdx = 0
22- idx = 0
23- for m in self .__magnets :
24- nbDev = len (m .model .get_devices ())
25- allStrength [mIdx ] = m .model .compute_strengths (allHardwareValues [idx :idx + nbDev ])[m .strength .index ()]
26- mIdx += 1
27- idx += nbDev
28- return allStrength
20+ return self .__aggregator .get ()
2921
3022 # Sets the values
3123 def set (self , value :np .array ):
32- if not self .aggregator :
24+ nvalue = np .ones (self .__nb ) * value if isinstance (value ,float ) else value
25+ if not self .__aggregator :
3326 for idx ,m in enumerate (self .__magnets ):
34- m .strength .set (value [idx ])
27+ m .strength .set (nvalue [idx ])
3528 else :
36- # TODO: if the array does not contains mappings to combined function
37- # magnets, the algorithm below can be optimized
38- allHardwareValues = self .aggregator .get () # Read all hardware setpoints
39- newHardwareValues = np .zeros (len (self .aggregator ))
40- mIdx = 0
41- idx = 0
42- for m in self .__magnets :
43- # m is a single function magnet or a mapping to a
44- # combined function magnet (RWMapper)
45- nbDev = len (m .model .get_devices ())
46- mStrengths = m .model .compute_strengths ( allHardwareValues [idx :idx + nbDev ] )
47- mStrengths [m .strength .index ()] = value [mIdx ]
48- newHardwareValues [idx :idx + nbDev ] = m .model .compute_hardware_values (mStrengths )
49- mIdx += 1
50- idx += nbDev
51- self .aggregator .set (newHardwareValues )
29+ self .__aggregator .set (nvalue )
5230
5331 # Sets the values and waits that the read values reach their setpoint
5432 def set_and_wait (self , value :np .array ):
@@ -60,36 +38,31 @@ def unit(self) -> list[str]:
6038
6139 # Set the aggregator (Control system only)
6240 def set_aggregator (self ,agg :ScalarAggregator ):
63- self .aggregator = agg
41+ self .__aggregator = agg
6442
6543class RWMagnetHardware (ReadWriteFloatArray ):
6644
6745 def __init__ (self , name :str , magnets :list [Magnet ]):
6846 self .__name = name
6947 self .__magnets = magnets
70- self .aggregator : ScalarAggregator = None
71- self .hasHardwareMapping = True
48+ self .__nb = len ( self . __magnets )
49+ self .__aggregator : ScalarAggregator = None
7250
7351 # Gets the values
7452 def get (self ) -> np .array :
75- if not self .aggregator :
53+ if not self .__aggregator :
7654 return np .array ([m .hardware .get () for m in self .__magnets ])
7755 else :
78- if not self .hasHardwareMapping :
79- raise Exception (f"Array { self .__name } contains elements that that do not support hardware units" )
80- else :
81- return self .aggregator .get ()
56+ return self .__aggregator .get ()
8257
8358 # Sets the values
8459 def set (self , value :np .array ):
85- if not self .aggregator :
60+ nvalue = np .ones (self .__nb ) * value if isinstance (value ,float ) else value
61+ if not self .__aggregator :
8662 for idx ,m in enumerate (self .__magnets ):
8763 m .hardware .set (value [idx ])
8864 else :
89- if not self .hasHardwareMapping :
90- raise Exception (f"Array { self .__name } contains elements that that do not support hardware units" )
91- else :
92- self .aggregator .set (value )
65+ self .__aggregator .set (value )
9366
9467 # Sets the values and waits that the read values reach their setpoint
9568 def set_and_wait (self , value :np .array ):
@@ -101,16 +74,14 @@ def unit(self) -> list[str]:
10174
10275 # Set the aggregator
10376 def set_aggregator (self ,agg :ScalarAggregator ):
104- self .aggregator = agg
105- for m in self .__magnets :
106- self .hasHardwareMapping |= m .model .has_hardware ()
77+ self .__aggregator = agg
10778
10879class MagnetArray (list [Magnet ]):
10980 """
11081 Class that implements access to a magnet array
11182 """
11283
113- def __init__ (self ,arrayName :str ,magnets :list [Magnet ],holder ):
84+ def __init__ (self ,arrayName :str ,magnets :list [Magnet ],holder = None ):
11485 """
11586 Construct a magnet array
11687
@@ -121,16 +92,18 @@ def __init__(self,arrayName:str,magnets:list[Magnet],holder):
12192 magnets: list[Magnet]
12293 Magnet iterator
12394 holder : Element holder
124- Holder that contains element of this array (Simulator or Control System)
95+ Holder (Simulator or Control System) that contains element of this array used for aggregator
12596 """
12697 super ().__init__ (i for i in magnets )
12798 self .__name = arrayName
12899 self .__rwstrengths = RWMagnetStrength (arrayName ,magnets )
129100 self .__rwhardwares = RWMagnetHardware (arrayName ,magnets )
130101
131- agg = holder .create_magnet_aggregator (magnets )
132- self .__rwstrengths .set_aggregator (agg )
133- self .__rwhardwares .set_aggregator (agg )
102+ if holder is not None :
103+ aggs = holder .create_magnet_strength_aggregator (magnets )
104+ aggh = holder .create_magnet_harddware_aggregator (magnets )
105+ self .__rwstrengths .set_aggregator (aggs )
106+ self .__rwhardwares .set_aggregator (aggh )
134107
135108 @property
136109 def strengths (self ) -> RWMagnetStrength :
0 commit comments