Skip to content

Commit 23ea2a4

Browse files
committed
Clean up of the BESSY II tune example.
1 parent 675f982 commit 23ea2a4

File tree

2 files changed

+54
-124
lines changed

2 files changed

+54
-124
lines changed

examples/BESSY2_example/bessy2-tune.ipynb

Lines changed: 26 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
{
1515
"cell_type": "code",
16-
"execution_count": 1,
16+
"execution_count": null,
1717
"id": "1a092dbb",
1818
"metadata": {},
1919
"outputs": [],
@@ -23,7 +23,7 @@
2323
"import numpy as np\n",
2424
"\n",
2525
"from pyaml.accelerator import Accelerator\n",
26-
"from pyaml.common.constants import ACTION_RESTORE\n",
26+
"from pyaml.common.constants import ACTION_MEASURE\n",
2727
"from pyaml.magnet.magnet import Magnet"
2828
]
2929
},
@@ -32,15 +32,7 @@
3232
"execution_count": null,
3333
"id": "9b63d253",
3434
"metadata": {},
35-
"outputs": [
36-
{
37-
"name": "stdout",
38-
"output_type": "stream",
39-
"text": [
40-
"28 Jan% 2026, 14:16:39 | WARNING | PyAML OA control system binding (0.1.1) initialized with name 'live' and prefix='a3744:'\n"
41-
]
42-
}
43-
],
35+
"outputs": [],
4436
"source": [
4537
"# ----- Load the configuration -----\n",
4638
"# Remember to change the prefix for the live mode to the one matching\n",
@@ -51,7 +43,7 @@
5143
},
5244
{
5345
"cell_type": "code",
54-
"execution_count": 3,
46+
"execution_count": null,
5547
"id": "bf603b5f",
5648
"metadata": {},
5749
"outputs": [],
@@ -61,96 +53,31 @@
6153
"\n",
6254
"\n",
6355
"def tune_callback(step: int, action: int, m: Magnet, dtune: np.array):\n",
64-
" if action == ACTION_RESTORE:\n",
65-
" # On action restore, the measured delta tune is passed as argument\n",
56+
" if action == ACTION_MEASURE:\n",
57+
" # On action measure, the measured dq / dk is passed as argument\n",
6658
" print(f\"Tune response: #{step} {m.get_name()} {dtune}\")\n",
6759
" return True"
6860
]
6961
},
7062
{
7163
"cell_type": "code",
72-
"execution_count": 4,
64+
"execution_count": null,
7365
"id": "d1593276",
7466
"metadata": {},
75-
"outputs": [
76-
{
77-
"name": "stdout",
78-
"output_type": "stream",
79-
"text": [
80-
"Tune response: #0 Q3M2D1R [ 0.2948122 -1.59761611]\n",
81-
"Tune response: #1 Q3M1T1R [ 0.31455894 -1.62602422]\n",
82-
"Tune response: #2 Q3M2T1R [ 0.39697699 -1.488687 ]\n",
83-
"Tune response: #3 Q3M1D2R [ 0.20694739 -1.66483728]\n",
84-
"Tune response: #4 Q3M2D2R [ 0.2012383 -1.5957427]\n",
85-
"Tune response: #5 Q3M1T2R [ 0.41065785 -1.45769981]\n",
86-
"Tune response: #6 Q3M2T2R [ 0.37243911 -1.47809081]\n",
87-
"Tune response: #7 Q3M1D3R [ 0.17999635 -1.57290023]\n",
88-
"Tune response: #8 Q3M2D3R [ 0.16824072 -1.69184086]\n",
89-
"Tune response: #9 Q3M1T3R [ 0.40143766 -1.53697391]\n",
90-
"Tune response: #10 Q3M2T3R [ 0.42532148 -1.48886172]\n",
91-
"Tune response: #11 Q3M1D4R [ 0.17907349 -1.74931195]\n",
92-
"Tune response: #12 Q3M2D4R [ 0.18700724 -1.51871493]\n",
93-
"Tune response: #13 Q3M1T4R [ 0.40510134 -1.37702393]\n",
94-
"Tune response: #14 Q3M2T4R [ 0.39620807 -1.34034338]\n",
95-
"Tune response: #15 Q3M1D5R [ 0.1820804 -1.56154372]\n",
96-
"Tune response: #16 Q3M2D5R [ 0.18853823 -1.6711882 ]\n",
97-
"Tune response: #17 Q3M1T5R [ 0.38067984 -1.39262069]\n",
98-
"Tune response: #18 Q3M2T5R [ 0.38563368 -1.53580491]\n",
99-
"Tune response: #19 Q3M1D6R [ 0.19110111 -1.51036189]\n",
100-
"Tune response: #20 Q3M2D6R [ 0.18082548 -1.58655204]\n",
101-
"Tune response: #21 Q3M1T6R [ 0.4128393 -1.57413822]\n",
102-
"Tune response: #22 Q3M2T6R [ 0.31077626 -1.37265153]\n",
103-
"Tune response: #23 Q3M1D7R [ 0.20870053 -1.51740208]\n",
104-
"Tune response: #24 Q3M2D7R [ 0.15146622 -1.71353181]\n",
105-
"Tune response: #25 Q3M1T7R [ 0.44789044 -1.46868931]\n",
106-
"Tune response: #26 Q3M2T7R [ 0.49459732 -1.47043094]\n",
107-
"Tune response: #27 Q3M1D8R [ 0.17966521 -1.71068117]\n",
108-
"Tune response: #28 Q3M2D8R [ 0.22093453 -1.55708902]\n",
109-
"Tune response: #29 Q3M1T8R [ 0.3893039 -1.39671459]\n",
110-
"Tune response: #30 Q3M2T8R [ 0.28904045 -1.57182273]\n",
111-
"Tune response: #31 Q3M1D1R [ 0.30996919 -1.51133886]\n",
112-
"Tune response: #32 Q4M2D1R [ 1.23318442 -0.62485904]\n",
113-
"Tune response: #33 Q4M1T1R [ 1.28947607 -0.49815711]\n",
114-
"Tune response: #34 Q4M2T1R [ 1.53574346 -0.46181013]\n",
115-
"Tune response: #35 Q4M1D2R [ 0.91291767 -0.6207083 ]\n",
116-
"Tune response: #36 Q4M2D2R [ 0.89526347 -0.59472661]\n",
117-
"Tune response: #37 Q4M1T2R [ 1.58046247 -0.45848444]\n",
118-
"Tune response: #38 Q4M2T2R [ 1.38596634 -0.46708279]\n",
119-
"Tune response: #39 Q4M1D3R [ 0.75228193 -0.58611061]\n",
120-
"Tune response: #40 Q4M2D3R [ 0.71557388 -0.63069453]\n",
121-
"Tune response: #41 Q4M1T3R [ 1.4825063 -0.48867921]\n",
122-
"Tune response: #42 Q4M2T3R [ 1.60207757 -0.46810845]\n",
123-
"Tune response: #43 Q4M1D4R [ 0.79461217 -0.6544545 ]\n",
124-
"Tune response: #44 Q4M2D4R [ 0.81971865 -0.5683347 ]\n",
125-
"Tune response: #45 Q4M1T4R [ 1.53281506 -0.4400919 ]\n",
126-
"Tune response: #46 Q4M2T4R [ 1.48838393 -0.42459024]\n",
127-
"Tune response: #47 Q4M1D5R [ 0.78662138 -0.59217602]\n",
128-
"Tune response: #48 Q4M2D5R [ 0.80669683 -0.63335949]\n",
129-
"Tune response: #49 Q4M1T5R [ 1.43702316 -0.43218978]\n",
130-
"Tune response: #50 Q4M2T5R [ 1.46247077 -0.49232655]\n",
131-
"Tune response: #51 Q4M1D6R [ 0.82225627 -0.56286775]\n",
132-
"Tune response: #52 Q4M2D6R [ 0.79021783 -0.5910496 ]\n",
133-
"Tune response: #53 Q4M1T6R [ 1.66415844 -0.40783638]\n",
134-
"Tune response: #54 Q4M2T6R [ 1.14304288 -0.46839169]\n",
135-
"Tune response: #55 Q4M1D7R [ 0.83707757 -0.57513168]\n",
136-
"Tune response: #56 Q4M2D7R [ 0.66071841 -0.64897922]\n",
137-
"Tune response: #57 Q4M1T7R [ 1.64291867 -0.46348414]\n",
138-
"Tune response: #58 Q4M2T7R [ 1.87993967 -0.46420292]\n",
139-
"Tune response: #59 Q4M1D8R [ 0.84390192 -0.6385394 ]\n",
140-
"Tune response: #60 Q4M2D8R [ 0.97428121 -0.58117844]\n",
141-
"Tune response: #61 Q4M1T8R [ 1.52427497 -0.43332046]\n",
142-
"Tune response: #62 Q4M2T8R [ 1.19367528 -0.48346323]\n",
143-
"Tune response: #63 Q4M1D1R [ 1.27555778 -0.59139844]\n"
144-
]
145-
}
146-
],
67+
"outputs": [],
14768
"source": [
14869
"# ----- Measure the tune response matrix-----\n",
14970
"# You can measure the tune response matrix on either the design or live mode.\n",
71+
"# At the moment they don't give the same result. This will be fixed later by\n",
72+
"# switching to using SerializedMagnet.\n",
73+
"\n",
74+
"# Choose which backend to use.\n",
75+
"SR = sr.design\n",
15076
"\n",
151-
"# sr.design.get_lattice().disable_6d()\n",
15277
"tune_adjust = sr.design.tune\n",
153-
"tune_adjust.response.measure(callback=tune_callback)\n",
78+
"tune_adjust.response.measure(\n",
79+
" callback=tune_callback, set_wait_time=0.0 if SR == sr.design else 2.0\n",
80+
")\n",
15481
"tune_adjust.response.save_json(\"tune-response.json\")"
15582
]
15683
},
@@ -165,42 +92,28 @@
16592
"# The example does the correction for the live mode\n",
16693
"# but it can also be done on the design mode.\n",
16794
"\n",
168-
"sr.design.tune.response.load_json(\"tune-response.json\")"
95+
"sr.live.tune.response.load_json(\"tune-response.json\")"
16996
]
17097
},
17198
{
17299
"cell_type": "code",
173100
"execution_count": null,
174101
"id": "153cefd1",
175102
"metadata": {},
176-
"outputs": [
177-
{
178-
"name": "stdout",
179-
"output_type": "stream",
180-
"text": [
181-
"\n",
182-
"Run tune correction:\n",
183-
"Initial tunes: [0.848070 0.726778]\n",
184-
"Final tunes: [0.830001 0.839958]\n"
185-
]
186-
}
187-
],
103+
"outputs": [],
188104
"source": [
189105
"# ----- Correct the tune -----\n",
190106
"\n",
191107
"print(\"\\nRun tune correction:\")\n",
192-
"print(\n",
193-
" f\"Initial tunes: {\n",
194-
" np.array2string(sr.design.tune.readback(), precision=6, floatmode='fixed')\n",
195-
" }\"\n",
196-
")\n",
197-
"sr.design.tune.set([0.83, 0.84], iter=2, wait_time=3)\n",
108+
"\n",
109+
"initial_tunes = np.array2string(sr.live.tune.readback(), precision=6, floatmode=\"fixed\")\n",
110+
"print(f\"Initial tunes: {initial_tunes}\")\n",
111+
"\n",
112+
"sr.live.tune.set([0.83, 0.84], iter=2, wait_time=3)\n",
198113
"time.sleep(3)\n",
199-
"print(\n",
200-
" f\"Final tunes: {\n",
201-
" np.array2string(sr.design.tune.readback(), precision=6, floatmode='fixed')\n",
202-
" }\"\n",
203-
")"
114+
"\n",
115+
"final_tunes = np.array2string(sr.live.tune.readback(), precision=6, floatmode=\"fixed\")\n",
116+
"print(f\"Final tunes: {final_tunes}\")"
204117
]
205118
}
206119
],
Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Tune correction
22
3-
This example shows how to run tune correction using the simulator or the virtual accelerator.
4-
If you want to test the virtual accelerator you need to start the container before running the script.
3+
This example shows how to run tune correction using the simulator
4+
or the virtual accelerator.
5+
If you want to test the virtual accelerator you need to start the
6+
container before running the script.
57
68
"""
79

@@ -10,40 +12,55 @@
1012
import numpy as np
1113

1214
from pyaml.accelerator import Accelerator
13-
from pyaml.common.constants import ACTION_RESTORE
15+
from pyaml.common.constants import ACTION_MEASURE
1416
from pyaml.magnet.magnet import Magnet
1517

1618
# ----- Load the configuration -----
17-
# Remember to change the prefix for the live mode to the one matching your virtual accelerator before loading.
19+
# Remember to change the prefix for the live mode to the one matching
20+
# your virtual accelerator before loading.
1821

1922
sr = Accelerator.load("BESSY2Tune.yaml")
2023

2124
# ----- Define a callback -----
2225
# This callback is used to print output during the tune response measurement.
2326

27+
2428
def tune_callback(step: int, action: int, m: Magnet, dtune: np.array):
25-
if action == ACTION_RESTORE:
26-
# On action restore, the measured delta tune is passed as argument
29+
if action == ACTION_MEASURE:
30+
# On action measure, the measured dq / dk is passed as argument
2731
print(f"Tune response: #{step} {m.get_name()} {dtune}")
2832
return True
2933

34+
3035
# ----- Measure the tune response matrix-----
3136
# You can measure the tune response matrix on either the design or live mode.
37+
# At the moment they don't give the same result.
38+
# This will be fixed later by switching to using SerializedMagnet.
39+
40+
# Choose which backend to use.
41+
SR = sr.design
3242

33-
#sr.design.get_lattice().disable_6d()
3443
tune_adjust = sr.design.tune
35-
tune_adjust.response.measure(callback=tune_callback)
44+
tune_adjust.response.measure(
45+
callback=tune_callback, set_wait_time=0.0 if SR == sr.design else 2.0
46+
)
3647
tune_adjust.response.save_json("tune-response.json")
3748

3849
# ----- Load the response matrix -----
39-
# The example does the correction for the live mode but it can also be done on the design mode.
50+
# The example does the correction for the live mode but it can also be done
51+
# on the design mode.
4052

4153
sr.live.tune.response.load_json("tune-response.json")
4254

4355
# ----- Correct the tune -----
4456

4557
print("\nRun tune correction:")
46-
print(f"Initial tunes: {np.array2string(sr.live.tune.readback(), precision=6, floatmode='fixed')}")
58+
59+
initial_tunes = np.array2string(sr.live.tune.readback(), precision=6, floatmode="fixed")
60+
print(f"Initial tunes: {initial_tunes}")
61+
4762
sr.live.tune.set([0.83, 0.84], iter=2, wait_time=3)
4863
time.sleep(3)
49-
print(f"Final tunes: {np.array2string(sr.live.tune.readback(), precision=6, floatmode='fixed')}")
64+
65+
final_tunes = np.array2string(sr.live.tune.readback(), precision=6, floatmode="fixed")
66+
print(f"Final tunes: {final_tunes}")

0 commit comments

Comments
 (0)