dt4acc-lib 0.4.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dt4acc_lib/__init__.py +4 -0
- dt4acc_lib/bl/__init__.py +0 -0
- dt4acc_lib/bl/command_rewritter.py +183 -0
- dt4acc_lib/bl/identity_conversion.py +16 -0
- dt4acc_lib/bl/liaison_manager.py +114 -0
- dt4acc_lib/bl/translator_service.py +135 -0
- dt4acc_lib/bl/tune_translator.py +29 -0
- dt4acc_lib/bl/unit_conversion.py +141 -0
- dt4acc_lib/bl/yellow_pages.py +48 -0
- dt4acc_lib/interfaces/__init__.py +0 -0
- dt4acc_lib/interfaces/backend/__init__.py +0 -0
- dt4acc_lib/interfaces/backend/backend.py +72 -0
- dt4acc_lib/interfaces/backend/calculation_states.py +25 -0
- dt4acc_lib/interfaces/simulator/__init__.py +0 -0
- dt4acc_lib/interfaces/simulator/accelerator_simulator.py +62 -0
- dt4acc_lib/interfaces/simulator/element.py +82 -0
- dt4acc_lib/interfaces/simulator/result_element.py +14 -0
- dt4acc_lib/interfaces/utils/__init__.py +0 -0
- dt4acc_lib/interfaces/utils/command_execution_engine.py +21 -0
- dt4acc_lib/interfaces/utils/command_rewritter.py +29 -0
- dt4acc_lib/interfaces/utils/liaison_manager.py +53 -0
- dt4acc_lib/interfaces/utils/state_conversion.py +29 -0
- dt4acc_lib/interfaces/utils/translator_service.py +21 -0
- dt4acc_lib/interfaces/utils/yellow_pages.py +31 -0
- dt4acc_lib/model/__init__.py +0 -0
- dt4acc_lib/model/output/__init__.py +0 -0
- dt4acc_lib/model/output/calculated_track.py +17 -0
- dt4acc_lib/model/output/result.py +137 -0
- dt4acc_lib/model/output/survey.py +13 -0
- dt4acc_lib/model/output/tune.py +58 -0
- dt4acc_lib/model/output/twiss.py +29 -0
- dt4acc_lib/model/utils/__init__.py +0 -0
- dt4acc_lib/model/utils/command.py +107 -0
- dt4acc_lib/model/utils/conv.py +23 -0
- dt4acc_lib/model/utils/identifiers.py +30 -0
- dt4acc_lib/model/utils/liaison_manager_lookup_table.py +81 -0
- dt4acc_lib/model/utils/tango_resource_locator.py +119 -0
- dt4acc_lib/model/utils/translator_manager_lookup_table.py +58 -0
- dt4acc_lib/pyat_simulator/__init__.py +0 -0
- dt4acc_lib/pyat_simulator/accelerator_simulator.py +112 -0
- dt4acc_lib/pyat_simulator/element_properties/__init__.py +0 -0
- dt4acc_lib/pyat_simulator/element_properties/cavity_properties.py +32 -0
- dt4acc_lib/pyat_simulator/element_properties/element_property_interface.py +37 -0
- dt4acc_lib/pyat_simulator/element_properties/geometric_properties.py +52 -0
- dt4acc_lib/pyat_simulator/element_properties/kick.py +42 -0
- dt4acc_lib/pyat_simulator/element_properties/main_strength.py +103 -0
- dt4acc_lib/pyat_simulator/element_properties/multipole.py +76 -0
- dt4acc_lib/pyat_simulator/element_properties/utils.py +149 -0
- dt4acc_lib/pyat_simulator/model/__init__.py +0 -0
- dt4acc_lib/pyat_simulator/proxies/__init__.py +0 -0
- dt4acc_lib/pyat_simulator/proxies/properties_proxy.py +101 -0
- dt4acc_lib/pyat_simulator/proxies/proxy_factory.py +103 -0
- dt4acc_lib/pyat_simulator/simulator_backend.py +399 -0
- dt4acc_lib-0.4.2.dist-info/METADATA +32 -0
- dt4acc_lib-0.4.2.dist-info/RECORD +57 -0
- dt4acc_lib-0.4.2.dist-info/WHEEL +4 -0
- dt4acc_lib-0.4.2.dist-info/licenses/LICENSE +674 -0
dt4acc_lib/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"""convert from lattice element change to device property
|
|
2
|
+
|
|
3
|
+
Please note:
|
|
4
|
+
here we have to map (lattice_name, property) -> (device_name, property)
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Sequence
|
|
8
|
+
|
|
9
|
+
from dt4acc_lib.model.utils.command import ReadCommand
|
|
10
|
+
from dt4acc_lib.interfaces.utils.command_rewritter import CommandRewriterBase
|
|
11
|
+
from dt4acc_lib.interfaces.utils.liaison_manager import LiaisonManagerBase
|
|
12
|
+
from dt4acc_lib.interfaces.utils.translator_service import TranslatorServiceBase
|
|
13
|
+
from dt4acc_lib.model.utils.command import Command
|
|
14
|
+
from dt4acc_lib.model.utils.identifiers import (
|
|
15
|
+
DevicePropertyID,
|
|
16
|
+
LatticeElementPropertyID,
|
|
17
|
+
ConversionID,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class CommandRewriter(CommandRewriterBase):
|
|
22
|
+
"""Command rewriter.
|
|
23
|
+
|
|
24
|
+
This module translates commands between two state spaces:
|
|
25
|
+
from a lattice element specification to a device property specification (forward)
|
|
26
|
+
and vice versa (inverse).
|
|
27
|
+
|
|
28
|
+
It uses a liaison manager to map identifiers and a translator service
|
|
29
|
+
to convert the command values between representations.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
liaison_manager: LiaisonManagerBase,
|
|
35
|
+
translation_service: TranslatorServiceBase,
|
|
36
|
+
):
|
|
37
|
+
"""
|
|
38
|
+
Initialize the CommandRewriter with the required services.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
liaison_manager: The manager responsible for mapping identifiers.
|
|
42
|
+
translator_service: The service providing value conversions.
|
|
43
|
+
"""
|
|
44
|
+
self.translator_service = translation_service
|
|
45
|
+
self.liaison_manager = liaison_manager
|
|
46
|
+
|
|
47
|
+
def inverse(self, cmd: Command) -> Sequence[Command]:
|
|
48
|
+
"""
|
|
49
|
+
Convert a command from a device property space back to the lattice space.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
:param cmd: The command to be transformed.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
A sequence of commands corresponding to the inverse translations.
|
|
56
|
+
"""
|
|
57
|
+
dev_prop_id = DevicePropertyID(device_name=cmd.id, property=cmd.property)
|
|
58
|
+
rcmd = self.inverse_read_command(cmd)
|
|
59
|
+
lat_prop_ids = [
|
|
60
|
+
LatticeElementPropertyID(element_name=r.id, property=r.property)
|
|
61
|
+
for r in rcmd
|
|
62
|
+
]
|
|
63
|
+
|
|
64
|
+
return [
|
|
65
|
+
self.inverse_translate_one(cmd, dev_prop_id, lat_prop_id)
|
|
66
|
+
for lat_prop_id in lat_prop_ids
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
def inverse_translate_one(
|
|
70
|
+
self,
|
|
71
|
+
cmd: Command,
|
|
72
|
+
dev_prop_id: DevicePropertyID,
|
|
73
|
+
lat_prop_id: LatticeElementPropertyID,
|
|
74
|
+
) -> Command:
|
|
75
|
+
"""
|
|
76
|
+
Perform a single inverse translation.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
:param dev_prop_id: The source device property identifier
|
|
80
|
+
:param cmd: The original command
|
|
81
|
+
:param lat_prop_id: The target lattice property identifier
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
A new Command with the value converted to the lattice state.
|
|
85
|
+
"""
|
|
86
|
+
translation_object = self.translator_service.get(
|
|
87
|
+
ConversionID(
|
|
88
|
+
lattice_property_id=lat_prop_id, device_property_id=dev_prop_id
|
|
89
|
+
)
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
if dev_prop_id.device_name is None:
|
|
93
|
+
raise ValueError(
|
|
94
|
+
"Device name cannot be None in device property identifier."
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
ncmd = Command(
|
|
98
|
+
id=lat_prop_id.element_name,
|
|
99
|
+
property=lat_prop_id.property,
|
|
100
|
+
value=translation_object.inverse(cmd.value),
|
|
101
|
+
behaviour_on_error=cmd.behaviour_on_error,
|
|
102
|
+
)
|
|
103
|
+
return ncmd
|
|
104
|
+
|
|
105
|
+
def forward(self, cmd: Command) -> Sequence[Command]:
|
|
106
|
+
rcmds = self.forward_read_command(cmd)
|
|
107
|
+
dev_prop_ids = [
|
|
108
|
+
DevicePropertyID(device_name=rcmd.id, property=rcmd.property)
|
|
109
|
+
for rcmd in rcmds
|
|
110
|
+
]
|
|
111
|
+
lat_prop_id = LatticeElementPropertyID(
|
|
112
|
+
element_name=cmd.id, property=cmd.property
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
return [
|
|
116
|
+
self.forward_translate_one(cmd, lat_prop_id, dev_prop_id)
|
|
117
|
+
for dev_prop_id in dev_prop_ids
|
|
118
|
+
]
|
|
119
|
+
|
|
120
|
+
def forward_translate_one(
|
|
121
|
+
self,
|
|
122
|
+
cmd: Command,
|
|
123
|
+
lat_prop_id : LatticeElementPropertyID,
|
|
124
|
+
dev_prop_id: DevicePropertyID):
|
|
125
|
+
translation_object = self.translator_service.get(
|
|
126
|
+
ConversionID(
|
|
127
|
+
lattice_property_id=lat_prop_id, device_property_id=dev_prop_id
|
|
128
|
+
)
|
|
129
|
+
)
|
|
130
|
+
ncmd = Command(
|
|
131
|
+
id=dev_prop_id.device_name,
|
|
132
|
+
property=dev_prop_id.property,
|
|
133
|
+
value=translation_object.forward(cmd.value),
|
|
134
|
+
behaviour_on_error=cmd.behaviour_on_error,
|
|
135
|
+
)
|
|
136
|
+
return ncmd
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def forward_read_command(self, command: ReadCommand) -> Sequence[ReadCommand]:
|
|
141
|
+
"""
|
|
142
|
+
|
|
143
|
+
Note:
|
|
144
|
+
the direction linkage is as in the context of
|
|
145
|
+
the update command.
|
|
146
|
+
|
|
147
|
+
The cooresponding translation object would have to
|
|
148
|
+
be called with inverse ...
|
|
149
|
+
|
|
150
|
+
Todo:
|
|
151
|
+
is there a use case where one lattice read command
|
|
152
|
+
goes to more than one device ?
|
|
153
|
+
"""
|
|
154
|
+
lat_prop_id = LatticeElementPropertyID(
|
|
155
|
+
element_name=command.id, property=command.property
|
|
156
|
+
)
|
|
157
|
+
dev_prop_ids = self.liaison_manager.forward(lat_prop_id)
|
|
158
|
+
return [
|
|
159
|
+
ReadCommand(id=dev_prop_id.device_name, property=dev_prop_id.property)
|
|
160
|
+
for dev_prop_id in dev_prop_ids
|
|
161
|
+
]
|
|
162
|
+
|
|
163
|
+
def inverse_read_command(self, command: ReadCommand) -> Sequence[ReadCommand]:
|
|
164
|
+
"""
|
|
165
|
+
Note:
|
|
166
|
+
the direction linkage is as in the context of
|
|
167
|
+
the update command.
|
|
168
|
+
|
|
169
|
+
The cooresponding translation object would have to
|
|
170
|
+
be called with forward ...
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
"""
|
|
174
|
+
dev_prop_id = DevicePropertyID(
|
|
175
|
+
device_name=command.id, property=command.property
|
|
176
|
+
)
|
|
177
|
+
lat_prop_ids = self.liaison_manager.inverse(dev_prop_id)
|
|
178
|
+
return [
|
|
179
|
+
ReadCommand(id=lp.element_name, property=lp.property) for lp in lat_prop_ids
|
|
180
|
+
]
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
__all__ = ["CommandRewriter"]
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from dt4acc_lib.interfaces.utils.state_conversion import StateConversion
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class IdentityConversion(StateConversion):
|
|
5
|
+
def inverse(self, state: object) -> object:
|
|
6
|
+
return state
|
|
7
|
+
|
|
8
|
+
def forward(self, state: object) -> object:
|
|
9
|
+
return state
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# as a singleton
|
|
13
|
+
identity_conversion = IdentityConversion()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
__all__ = ["identity_conversion", "IdentityConversion"]
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from typing import Sequence
|
|
3
|
+
|
|
4
|
+
from dt4acc_lib.interfaces.utils.liaison_manager import LiaisonManagerBase
|
|
5
|
+
from dt4acc_lib.model.utils.identifiers import (
|
|
6
|
+
LatticeElementPropertyID,
|
|
7
|
+
DevicePropertyID,
|
|
8
|
+
)
|
|
9
|
+
from dt4acc_lib.model.utils.liaison_manager_lookup_table import (
|
|
10
|
+
LiaisonManagerInverseLookupTable,
|
|
11
|
+
LiaisonManagerForwardLookupTable
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
logger = logging.getLogger("dt4acc_lib")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class LiaisonManager(LiaisonManagerBase):
|
|
18
|
+
"""
|
|
19
|
+
Todo:
|
|
20
|
+
consider internally to represent classes of devices with a certain functionallity
|
|
21
|
+
|
|
22
|
+
So internally have
|
|
23
|
+
* classes of devices providing similar properties
|
|
24
|
+
* this can be used when searching for suggesting alternatives to the
|
|
25
|
+
user when searching for it
|
|
26
|
+
|
|
27
|
+
Logging of internal knowledge? Should that be provided
|
|
28
|
+
|
|
29
|
+
Provide _repr_html_ or similar for better output on notebooks
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
forward_lut: LiaisonManagerForwardLookupTable,
|
|
35
|
+
inverse_lut: LiaisonManagerInverseLookupTable,
|
|
36
|
+
):
|
|
37
|
+
if forward_lut:
|
|
38
|
+
forward_lut.verify()
|
|
39
|
+
if inverse_lut:
|
|
40
|
+
inverse_lut.verify()
|
|
41
|
+
|
|
42
|
+
self.forward_lut = forward_lut
|
|
43
|
+
self.inverse_lut = inverse_lut
|
|
44
|
+
|
|
45
|
+
def __str__(self):
|
|
46
|
+
if self.forward_lut:
|
|
47
|
+
fwd_txt = f"fwd lut with {len(self.forward_lut.lut)} entries"
|
|
48
|
+
else:
|
|
49
|
+
fwd_txt = "no fwd lut"
|
|
50
|
+
if self.inverse_lut:
|
|
51
|
+
inv_txt = f"inv lut with {len(self.inverse_lut.lut)} entries"
|
|
52
|
+
else:
|
|
53
|
+
inv_txt = "no inv lut"
|
|
54
|
+
return f"{self.__class__.__name__}({fwd_txt}, {inv_txt})"
|
|
55
|
+
|
|
56
|
+
def __repr__(self):
|
|
57
|
+
return (
|
|
58
|
+
f"{self.__class__.__name__}("
|
|
59
|
+
f"fwd_lut={repr(self.forward_lut)}"
|
|
60
|
+
f", inv_lut={repr(self.inverse_lut)}"
|
|
61
|
+
")"
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
def forward(self, id_: LatticeElementPropertyID) -> DevicePropertyID:
|
|
65
|
+
devs_p = self.forward_lut.get(id_)
|
|
66
|
+
if devs_p is not None:
|
|
67
|
+
# assert len(devs_p) == 1, f"lattice id {id_} translated to {devs_p}. expected only 1"
|
|
68
|
+
return devs_p
|
|
69
|
+
|
|
70
|
+
logger.error(
|
|
71
|
+
f"{self.__class__.__name__} id {id_} not found in lookup table"
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
em = self.objects_for_lattice_element(id_.element_name)
|
|
75
|
+
logger.warning(f"{self.__class__.__name__}: For the element I know {em}")
|
|
76
|
+
raise KeyError(f"forward lut does not contain entry {id_}")
|
|
77
|
+
|
|
78
|
+
def inverse(self, id_: DevicePropertyID) -> Sequence[LatticeElementPropertyID]:
|
|
79
|
+
lp = self.inverse_lut.get(id_)
|
|
80
|
+
if lp is not None:
|
|
81
|
+
return lp
|
|
82
|
+
|
|
83
|
+
logger.error(
|
|
84
|
+
f"{self.__class__.__name__} id {id_} not found in lookup table"
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
od = self.objects_for_device(id_.device_name)
|
|
88
|
+
logger.warning(f"{self.__class__.__name__}: For the device I know {od}")
|
|
89
|
+
|
|
90
|
+
# Todo: give the user a hint what we know and what is close to what we know
|
|
91
|
+
raise KeyError(f"inverse lut does not contain entry {id_}")
|
|
92
|
+
|
|
93
|
+
def known_lattice_elements(self) -> Sequence[str]:
|
|
94
|
+
return [key.element_name for key in self.forward_lut.keys()]
|
|
95
|
+
|
|
96
|
+
def known_device_elements(self) -> Sequence[str]:
|
|
97
|
+
return [key.device_name for key in self.inverse_lut.keys()]
|
|
98
|
+
|
|
99
|
+
def objects_for_lattice_element(self, elem_name: str):
|
|
100
|
+
return {
|
|
101
|
+
key : self.forward_lut.get(key)
|
|
102
|
+
for key in self.forward_lut.keys()
|
|
103
|
+
if elem_name == key.element_name
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
def objects_for_device(self, dev_name: str):
|
|
107
|
+
return {
|
|
108
|
+
key : self.inverse_lut.get(key)
|
|
109
|
+
for key in self.inverse_lut.keys()
|
|
110
|
+
if dev_name == key.device_name
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
__all__ = ["LiaisonManager"]
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
import logging
|
|
3
|
+
from typing import Union
|
|
4
|
+
|
|
5
|
+
from .tune_translator import TuneConversion
|
|
6
|
+
from dt4acc_lib.bl.identity_conversion import identity_conversion
|
|
7
|
+
from dt4acc_lib.bl.unit_conversion import (
|
|
8
|
+
LinearUnitConversion,
|
|
9
|
+
EnergyDependentLinearUnitConversion,
|
|
10
|
+
)
|
|
11
|
+
from dt4acc_lib.interfaces.utils.state_conversion import StateConversion
|
|
12
|
+
from dt4acc_lib.interfaces.utils.translator_service import TranslatorServiceBase
|
|
13
|
+
from dt4acc_lib.model.utils.identifiers import ConversionID
|
|
14
|
+
from dt4acc_lib.model.utils.translator_manager_lookup_table import (
|
|
15
|
+
TranslatorLookupTable,
|
|
16
|
+
PolynomCoefficients,
|
|
17
|
+
TuneConversionCoefficients,
|
|
18
|
+
IdentityMapper,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
logger = logging.getLogger("dt4acc_lib")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class TranslatorService(TranslatorServiceBase):
|
|
26
|
+
"""
|
|
27
|
+
Todo:
|
|
28
|
+
review if translation objects should only be instaniated
|
|
29
|
+
when needed or during startup
|
|
30
|
+
|
|
31
|
+
Proper handling of brho!
|
|
32
|
+
|
|
33
|
+
Support _repr_html_ of similar for better output on jupyter notebooks
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(self, *, lut: TranslatorLookupTable, brho: float):
|
|
37
|
+
self.lut = lut
|
|
38
|
+
self.brho = brho
|
|
39
|
+
|
|
40
|
+
def __str__(self):
|
|
41
|
+
return f"{self.__class__.__name__}(brho=self.brho, lut with {len(self.lut.lut)} entries)"
|
|
42
|
+
|
|
43
|
+
def __repr__(self):
|
|
44
|
+
return (
|
|
45
|
+
f"{self.__class__.__name__}"
|
|
46
|
+
"("
|
|
47
|
+
f"brho={repr(self.brho)}"
|
|
48
|
+
f", lut={repr(self.lut)}"
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
@functools.lru_cache(maxsize=None)
|
|
52
|
+
def get(self, id_: ConversionID) -> Union[StateConversion, None]:
|
|
53
|
+
to = self._lookup_config(id_)
|
|
54
|
+
if to is None:
|
|
55
|
+
return None
|
|
56
|
+
|
|
57
|
+
# Should one use a factory down here ?
|
|
58
|
+
# Then it could be handled over and configured by the user
|
|
59
|
+
r = create_translation_object_for_data_model(to, self.brho)
|
|
60
|
+
return r
|
|
61
|
+
|
|
62
|
+
def _lookup_config(self, id_: ConversionID):
|
|
63
|
+
to = self.lut.get(id_)
|
|
64
|
+
if to is not None:
|
|
65
|
+
return to
|
|
66
|
+
|
|
67
|
+
logger.error(
|
|
68
|
+
f"{self.__class__.__name__}: I did not find id {id_} in lookup table"
|
|
69
|
+
)
|
|
70
|
+
od = self.objects_for_device(id_.device_property_id.device_name)
|
|
71
|
+
logger.warning(f"{self.__class__.__name__}: For the device I know {od}")
|
|
72
|
+
em = self.objects_for_lat_elem(id_.lattice_property_id.element_name)
|
|
73
|
+
logger.warning(
|
|
74
|
+
f"{self.__class__.__name__}: For the lattice element I know {em}"
|
|
75
|
+
)
|
|
76
|
+
raise KeyError(f"No translation object for {id_}")
|
|
77
|
+
|
|
78
|
+
def objects_for_lat_elem(self, elm_name: str):
|
|
79
|
+
return {
|
|
80
|
+
key: self.lut.get(key)
|
|
81
|
+
for key in self.lut.keys()
|
|
82
|
+
if elm_name == key.lattice_property_id.element_name
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
def objects_for_device(self, dev_name: str):
|
|
86
|
+
return {
|
|
87
|
+
key: self.lut.get(key)
|
|
88
|
+
for key in self.lut.keys()
|
|
89
|
+
if dev_name == key.device_property_id.device_name
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def create_translation_object_for_data_model(
|
|
94
|
+
data_model: Union[PolynomCoefficients | TuneConversionCoefficients], brho: float
|
|
95
|
+
):
|
|
96
|
+
if isinstance(data_model, IdentityMapper):
|
|
97
|
+
return identity_conversion
|
|
98
|
+
if isinstance(data_model, PolynomCoefficients):
|
|
99
|
+
return create_translation_object_for_polynom_coefficients(data_model, brho)
|
|
100
|
+
elif isinstance(data_model, TuneConversionCoefficients):
|
|
101
|
+
return create_translation_object_for_tune(data_model)
|
|
102
|
+
else:
|
|
103
|
+
# Todo: fix the error that is raised
|
|
104
|
+
raise AssertionError(
|
|
105
|
+
f"Don't know how to instantiate data_model of {type(data_model)} for {data_model}"
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def create_translation_object_for_tune(
|
|
110
|
+
data_model: TuneConversionCoefficients,
|
|
111
|
+
) -> TuneConversion:
|
|
112
|
+
assert (
|
|
113
|
+
not data_model.conversion.energy_dependent
|
|
114
|
+
), "Expect the tune conversion to be energy independent"
|
|
115
|
+
return TuneConversion(
|
|
116
|
+
create_translation_object_for_polynom_coefficients(
|
|
117
|
+
data_model.conversion, brho=None
|
|
118
|
+
)
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def create_translation_object_for_polynom_coefficients(
|
|
123
|
+
data_model: PolynomCoefficients,
|
|
124
|
+
brho: float,
|
|
125
|
+
) -> Union[LinearUnitConversion, EnergyDependentLinearUnitConversion]:
|
|
126
|
+
intercept, slope = data_model.coeffs
|
|
127
|
+
if data_model.energy_dependent:
|
|
128
|
+
return EnergyDependentLinearUnitConversion(
|
|
129
|
+
intercept=intercept, slope=slope, brho=brho
|
|
130
|
+
)
|
|
131
|
+
else:
|
|
132
|
+
return LinearUnitConversion(slope=slope, intercept=intercept)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
__all__ = ["TranslatorService"]
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from dt4acc_lib.model.output.tune import Tune
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class TuneConversion:
|
|
5
|
+
"""Convert from Floquet coordinates to frequency
|
|
6
|
+
|
|
7
|
+
Assuming that x and y use the same scaling. This is typically
|
|
8
|
+
the case as the calculation needs:
|
|
9
|
+
|
|
10
|
+
* (ring) revolution frequency
|
|
11
|
+
|
|
12
|
+
This is typically calculated from
|
|
13
|
+
|
|
14
|
+
* main RF / master clock frequency
|
|
15
|
+
* number of buckets
|
|
16
|
+
|
|
17
|
+
Thus, it should be the same for both planes
|
|
18
|
+
|
|
19
|
+
Warning:
|
|
20
|
+
Need to review connection to master clock
|
|
21
|
+
"""
|
|
22
|
+
def __init__(self, to):
|
|
23
|
+
self.to = to
|
|
24
|
+
|
|
25
|
+
def forward(self, inp: Tune) -> Tune:
|
|
26
|
+
return Tune(x=self.to.forward(inp.x), y=self.to.forward(inp.y))
|
|
27
|
+
|
|
28
|
+
def inverse(self, inp: Tune) -> Tune:
|
|
29
|
+
return Tune(x=self.to.forward(inp.x), y=self.to.forward(inp.y))
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from typing import Sequence
|
|
5
|
+
from dt4acc_lib.interfaces.utils.state_conversion import StateConversion
|
|
6
|
+
from dt4acc_lib.model.utils.identifiers import CurvePoint
|
|
7
|
+
from scipy.interpolate import interp1d
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger("dt4acc_lib")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class EnergyDependentLinearUnitConversion(StateConversion):
|
|
13
|
+
"""Typical example: magnet parameters"""
|
|
14
|
+
|
|
15
|
+
def __init__(self, *, intercept: float, slope: float, brho: float):
|
|
16
|
+
self.intercept = intercept
|
|
17
|
+
self.slope = slope
|
|
18
|
+
self.brho = brho
|
|
19
|
+
|
|
20
|
+
def forward(self, state: float) -> float:
|
|
21
|
+
logger.info(
|
|
22
|
+
"%s.forward: brho %s, intercept %s slope %s, state %s",
|
|
23
|
+
self.__class__.__name__,
|
|
24
|
+
self.brho,
|
|
25
|
+
self.intercept,
|
|
26
|
+
self.slope,
|
|
27
|
+
state,
|
|
28
|
+
)
|
|
29
|
+
intercept = self.intercept * self.brho
|
|
30
|
+
slope = self.slope * self.brho
|
|
31
|
+
return intercept + slope * state
|
|
32
|
+
|
|
33
|
+
def inverse(self, state: float) -> float:
|
|
34
|
+
logger.info(
|
|
35
|
+
"%s.inverse: brho %s, intercept %s slope %s, state %s",
|
|
36
|
+
self.__class__.__name__,
|
|
37
|
+
self.brho,
|
|
38
|
+
self.intercept,
|
|
39
|
+
self.slope,
|
|
40
|
+
state,
|
|
41
|
+
)
|
|
42
|
+
intercept = self.intercept * self.brho
|
|
43
|
+
slope = self.slope * self.brho
|
|
44
|
+
return (state - intercept) / slope
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class LinearUnitConversion(StateConversion):
|
|
48
|
+
"""Typical example: tune"""
|
|
49
|
+
|
|
50
|
+
def __init__(self, *, intercept: float, slope: float):
|
|
51
|
+
self.intercept = intercept
|
|
52
|
+
self.slope = slope
|
|
53
|
+
|
|
54
|
+
def forward(self, state: float) -> float:
|
|
55
|
+
logger.info(
|
|
56
|
+
"%s.forward: intercept %s slope %s, state %s",
|
|
57
|
+
self.__class__.__name__,
|
|
58
|
+
self.intercept,
|
|
59
|
+
self.slope,
|
|
60
|
+
state,
|
|
61
|
+
)
|
|
62
|
+
return self.intercept + self.slope * state
|
|
63
|
+
|
|
64
|
+
def inverse(self, state: float) -> float:
|
|
65
|
+
logger.info(
|
|
66
|
+
"%s.inverse: intercept %s slope %s, state %s",
|
|
67
|
+
self.__class__.__name__,
|
|
68
|
+
self.intercept,
|
|
69
|
+
self.slope,
|
|
70
|
+
state,
|
|
71
|
+
)
|
|
72
|
+
return (state - self.intercept) / self.slope
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class EnergyIndependentCurveUnitConversion(StateConversion):
|
|
77
|
+
"""
|
|
78
|
+
Interpolate a curve (independent -> dependent) using scipy.interpolate.interp1d
|
|
79
|
+
and scale by `brho`.
|
|
80
|
+
|
|
81
|
+
- points: sequence of 2-tuples (indep, dep) or objects with `indep` and `dep`.
|
|
82
|
+
- forward(x) -> interpolated_dep(x) * brho
|
|
83
|
+
- inverse(y) -> x such that interpolated_dep(x) == y / brho (searches segments;
|
|
84
|
+
works for non-monotonic curves by returning the first matching segment)
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
def __init__(
|
|
88
|
+
self,
|
|
89
|
+
*,
|
|
90
|
+
fwd_points: Sequence[CurvePoint],
|
|
91
|
+
bwd_points: Sequence[CurvePoint],
|
|
92
|
+
brho: float,
|
|
93
|
+
# todo: see how it was named at max iv
|
|
94
|
+
# iimplement it in a filter delegating implementation to this object
|
|
95
|
+
flip_dep_sign: False,
|
|
96
|
+
length : float
|
|
97
|
+
):
|
|
98
|
+
# forward interpolator: will raise if x out of bounds
|
|
99
|
+
# TODO: clean up it is a mess at the moment
|
|
100
|
+
self._fwd = interp1d(
|
|
101
|
+
[t.indep for t in fwd_points],
|
|
102
|
+
[t.dep for t in fwd_points],
|
|
103
|
+
kind="linear",
|
|
104
|
+
# Todo: change later to true ... or make user configurable
|
|
105
|
+
bounds_error=False,
|
|
106
|
+
)
|
|
107
|
+
self._bwd = interp1d(
|
|
108
|
+
[t.indep for t in bwd_points],
|
|
109
|
+
[t.dep for t in bwd_points],
|
|
110
|
+
kind="linear",
|
|
111
|
+
# Todo: change later to true ... or make user configurable
|
|
112
|
+
bounds_error=False,
|
|
113
|
+
)
|
|
114
|
+
self.brho = float(brho)
|
|
115
|
+
self.length = float(length) if length else 1.0
|
|
116
|
+
self.fwd_points = fwd_points
|
|
117
|
+
self.bwd_points = bwd_points
|
|
118
|
+
self.flip_dep_sign = flip_dep_sign
|
|
119
|
+
|
|
120
|
+
def forward(self, state: float) -> float:
|
|
121
|
+
logger.info(
|
|
122
|
+
"%s.forward: brho %s state %s", self.__class__.__name__, self.brho, state
|
|
123
|
+
)
|
|
124
|
+
x = float(state)
|
|
125
|
+
y = float(self._fwd(x)) # interp1d returns an array-like
|
|
126
|
+
if self.flip_dep_sign:
|
|
127
|
+
y = -y
|
|
128
|
+
return y * self.brho
|
|
129
|
+
|
|
130
|
+
def inverse(self, state: float) -> float:
|
|
131
|
+
# logger.info("%s.inverse: brho %s points %d state %s", self.__class__.__name__, self.brho, len(self._indep), state)
|
|
132
|
+
if self.brho == 0:
|
|
133
|
+
raise ValueError("brho must be non-zero for inversion")
|
|
134
|
+
target = float(state) / self.brho
|
|
135
|
+
y = float(self._bwd(target)) # interp1d returns an array-like
|
|
136
|
+
assert np.isfinite(y), "failed to inverse {state=} ({brho=}, {target=})"
|
|
137
|
+
if self.flip_dep_sign:
|
|
138
|
+
y = y * -1
|
|
139
|
+
return y / self.length
|
|
140
|
+
|
|
141
|
+
__all__ = ["EnergyDependentLinearUnitConversion", "LinearUnitConversion", "EnergyIndependentCurveUnitConversion"]
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
from typing import Sequence, Union
|
|
3
|
+
|
|
4
|
+
from dt4acc_lib.interfaces.utils.yellow_pages import YellowPagesBase
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class FamilyName(Enum):
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
Todo:
|
|
11
|
+
move to model or interface
|
|
12
|
+
"""
|
|
13
|
+
quadrupoles = "quadrupoles"
|
|
14
|
+
tune_correction_quadrupoles = "tune_correction_quadrupoles"
|
|
15
|
+
master_clock = "master_clock"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class YellowPages(YellowPagesBase):
|
|
19
|
+
"""
|
|
20
|
+
or use:
|
|
21
|
+
get(family_name: str)
|
|
22
|
+
separate yellow pages for lattice elements and devices
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, d: dict):
|
|
26
|
+
self._d = d
|
|
27
|
+
|
|
28
|
+
def get(self, family_name: Union[str, FamilyName]) -> Sequence[str]:
|
|
29
|
+
# check for valid key?
|
|
30
|
+
# key = str(FamilyName(family_name))
|
|
31
|
+
return self._d[family_name]
|
|
32
|
+
|
|
33
|
+
def quadrupole_names(self) -> Sequence[str]:
|
|
34
|
+
"""
|
|
35
|
+
Todo:
|
|
36
|
+
remove me: favour to only provide the .get interface
|
|
37
|
+
"""
|
|
38
|
+
return self.get("quadrupoles")
|
|
39
|
+
|
|
40
|
+
def tune_correction_quadrupole_names(self) -> Sequence[str]:
|
|
41
|
+
"""
|
|
42
|
+
Todo:
|
|
43
|
+
remove me: favour to only provide the .get interface
|
|
44
|
+
"""
|
|
45
|
+
return self.get("tune_correction_quadrupoles")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
__all__ = ["FamilyName", "YellowPages"]
|
|
File without changes
|
|
File without changes
|