|
3 | 3 | import numpy as np |
4 | 4 |
|
5 | 5 | from pyaml.accelerator import Accelerator |
| 6 | +from pyaml.common.constants import ACTION_RESTORE |
| 7 | +from pyaml.magnet.magnet import Magnet |
6 | 8 |
|
7 | 9 | # Get the directory of the current script |
8 | 10 | script_dir = os.path.dirname(__file__) |
|
16 | 18 | sr: Accelerator = Accelerator.load(absolute_path) |
17 | 19 | sr.design.get_lattice().disable_6d() |
18 | 20 |
|
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