Skip to content

Commit d36a68e

Browse files
committed
Update tune example
1 parent 1ee9ca3 commit d36a68e

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

examples/ESRF_tune_example/esrf_tune_example.py

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import numpy as np
44

55
from pyaml.accelerator import Accelerator
6+
from pyaml.common.constants import ACTION_RESTORE
7+
from pyaml.magnet.magnet import Magnet
68

79
# Get the directory of the current script
810
script_dir = os.path.dirname(__file__)
@@ -16,37 +18,24 @@
1618
sr: Accelerator = Accelerator.load(absolute_path)
1719
sr.design.get_lattice().disable_6d()
1820

19-
quadForTuneDesign = sr.design.get_magnets("QForTune")
20-
quadForTuneLive = sr.live.get_magnets("QForTune")
21-
22-
# Build tune response matrix
23-
tune = sr.design.get_lattice().get_tune()
24-
print(tune)
25-
tunemat = np.zeros((len(quadForTuneDesign), 2))
26-
27-
for idx, m in enumerate(quadForTuneDesign):
28-
str = m.strength.get()
29-
m.strength.set(str + 1e-4)
30-
dq = sr.design.get_lattice().get_tune() - tune
31-
tunemat[idx] = dq * 1e4
32-
m.strength.set(str)
33-
34-
# Compute correction matrix
35-
correctionmat = np.linalg.pinv(tunemat.T)
36-
37-
# Correct tune
38-
strs = quadForTuneDesign.strengths.get()
39-
strs += np.matmul(correctionmat, [0.1, 0.05]) # Ask for correction [dqx,dqy]
40-
quadForTuneDesign.strengths.set(strs)
41-
newTune = sr.design.get_lattice().get_tune()
42-
diffTune = newTune - tune
43-
print(diffTune)
44-
assert np.abs(diffTune[0] - 0.1) < 1e-3
45-
assert np.abs(diffTune[1] - 0.05) < 1e-3
46-
47-
if False:
48-
# Correct the tune on live (need a Virutal Accelerator)
49-
quadForTuneLive = sr.live.get_magnets("QForTune")
50-
strs = quadForTuneLive.strengths.get()
51-
strs += np.matmul(correctionmat, [0.1, 0.05]) # Ask for correction [dqx,dqy]
52-
quadForTuneLive.strengths.set(strs)
21+
22+
# Callback exectued after each magnet strenght setting
23+
# during the tune response matrix measurement
24+
def tune_callback(step: int, action: int, m: Magnet, dtune: np.array):
25+
if action == ACTION_RESTORE:
26+
# On action restore, the delta tune is passed as argument
27+
print(f"Tune response: #{step} {m.get_name()} {dtune}")
28+
return True
29+
30+
31+
# Compute tune response matrix
32+
tune_adjust_design = sr.design.get_tune_tuning("TUNE")
33+
tune_adjust_design.response.measure(callback=tune_callback)
34+
tune_adjust_design.response.save_json("tunemat.json")
35+
36+
# Correct tune on live
37+
tune_adjust_live = sr.live.get_tune_tuning("TUNE")
38+
tune_adjust_live.response.load_json("tunemat.json")
39+
print(tune_adjust_live.readback())
40+
tune_adjust_live.set([0.17, 0.32], iter=2, wait_time=10)
41+
print(tune_adjust_live.readback())

0 commit comments

Comments
 (0)