1+ from ..common .abstract import ReadWriteFloatArray
2+ from ..magnet .cfm_magnet import CombinedFunctionMagnet
3+ from .element_array import get_peer_from_array
4+ import numpy as np
5+
6+ #TODO handle aggregator for CFM
7+
8+ class RWMagnetStrengths (ReadWriteFloatArray ):
9+
10+ def __init__ (self , name :str , magnets :list [CombinedFunctionMagnet ]):
11+ self .__name = name
12+ self .__magnets = magnets
13+ self .__nb = sum (m .nb_multipole () for m in magnets )
14+
15+ # Gets the values
16+ def get (self ) -> np .array :
17+ r = np .zeros (self .__nb )
18+ idx = 0
19+ for m in self .__magnets :
20+ r [idx :idx + m .nb_multipole ()] = m .strengths .get ()
21+ idx += m .nb_multipole ()
22+ return r
23+
24+ # Sets the values
25+ def set (self , value :np .array ):
26+ nvalue = np .ones (self .__nb ) * value if isinstance (value ,float ) else value
27+ idx = 0
28+ for m in self .__magnets :
29+ m .strengths .set (nvalue [idx :idx + m .nb_multipole ()])
30+ idx += m .nb_multipole ()
31+
32+ # Sets the values and waits that the read values reach their setpoint
33+ def set_and_wait (self , value :np .array ):
34+ raise NotImplementedError ("Not implemented yet." )
35+
36+ # Gets the unit of the values
37+ def unit (self ) -> list [str ]:
38+ r = []
39+ for m in self .__magnets :
40+ r .extend (m .strengths .unit ())
41+ return r
42+
43+ class RWMagnetHardwares (ReadWriteFloatArray ):
44+
45+ def __init__ (self , name :str , magnets :list [CombinedFunctionMagnet ]):
46+ self .__name = name
47+ self .__magnets = magnets
48+ self .__nb = sum (m .nb_multipole () for m in magnets )
49+
50+ # Gets the values
51+ def get (self ) -> np .array :
52+ r = np .zeros (self .__nb )
53+ idx = 0
54+ for m in self .__magnets :
55+ r [idx :idx + m .nb_multipole ()] = m .hardwares .get ()
56+ idx += m .nb_multipole ()
57+ return r
58+
59+ # Sets the values
60+ def set (self , value :np .array ):
61+ nvalue = np .ones (self .__nb ) * value if isinstance (value ,float ) else value
62+ idx = 0
63+ for m in self .__magnets :
64+ m .hardwares .set (nvalue [idx :idx + m .nb_multipole ()])
65+ idx += m .nb_multipole ()
66+
67+ # Sets the values and waits that the read values reach their setpoint
68+ def set_and_wait (self , value :np .array ):
69+ raise NotImplementedError ("Not implemented yet." )
70+
71+ # Gets the unit of the values
72+ def unit (self ) -> list [str ]:
73+ r = []
74+ for m in self .__magnets :
75+ r .extend (m .hardwares .unit ())
76+ return r
77+
78+
79+ class CombinedFunctionMagnetArray (list [CombinedFunctionMagnet ]):
80+ """
81+ Class that implements access to a magnet array
82+ """
83+
84+ def __init__ (self ,arrayName :str ,magnets :list [CombinedFunctionMagnet ],use_aggregator = False ):
85+ """
86+ Construct a magnet array
87+
88+ Parameters
89+ ----------
90+ arrayName : str
91+ Array name
92+ magnets: list[Magnet]
93+ Magnet list, all elements must be attached to the same instance of
94+ either a Simulator or a ControlSystem.
95+ use_aggregator : bool
96+ Use aggregator to increase performance by using paralell access to underlying devices.
97+ """
98+ super ().__init__ (i for i in magnets )
99+ self .__name = arrayName
100+ holder = get_peer_from_array (self )
101+
102+ self .__rwstrengths = RWMagnetStrengths (arrayName ,magnets )
103+ self .__rwhardwares = RWMagnetHardwares (arrayName ,magnets )
104+
105+ if use_aggregator :
106+ raise ("Aggregator not implemented for CombinedFunctionMagnetArray" )
107+
108+ def get_name (self ) -> str :
109+ return self .__name
110+
111+ @property
112+ def strengths (self ) -> RWMagnetStrengths :
113+ """
114+ Give access to strength of each magnet of this array
115+ """
116+ return self .__rwstrengths
117+
118+ @property
119+ def hardwares (self ) -> RWMagnetHardwares :
120+ """
121+ Give access to hardware value of each magnet of this array
122+ """
123+ return self .__rwhardwares
124+
125+
126+
127+
128+
129+
0 commit comments