accelmo 0.0.1__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.
Files changed (95) hide show
  1. accelmo/__init__.py +8 -0
  2. accelmo/analysis/__init__.py +21 -0
  3. accelmo/analysis/degeneracy_checker.py +308 -0
  4. accelmo/backend/__init__.py +26 -0
  5. accelmo/backend/madng/__init__.py +5 -0
  6. accelmo/backend/madng/accelerator_hooks.py +171 -0
  7. accelmo/backend/madng/engines.py +535 -0
  8. accelmo/backend/madng/interface.py +600 -0
  9. accelmo/backend/madng/machine_state.py +62 -0
  10. accelmo/backend/madng/mad_scripts/run_closed_twiss_init.mad +399 -0
  11. accelmo/backend/madng/scripts.py +367 -0
  12. accelmo/backend/protocol.py +274 -0
  13. accelmo/backend/xtrack/__init__.py +1 -0
  14. accelmo/backend/xtrack/engines.py +366 -0
  15. accelmo/backend/xtrack/model.py +292 -0
  16. accelmo/config.py +169 -0
  17. accelmo/fitting/__init__.py +4 -0
  18. accelmo/fitting/bpm_gains.py +54 -0
  19. accelmo/fitting/config.py +44 -0
  20. accelmo/fitting/lifecycle.py +40 -0
  21. accelmo/fitting/lm_loop.py +212 -0
  22. accelmo/fitting/pool.py +75 -0
  23. accelmo/fitting/priors.py +58 -0
  24. accelmo/fitting/protocol.py +265 -0
  25. accelmo/fitting/reduction.py +65 -0
  26. accelmo/fitting/results.py +36 -0
  27. accelmo/fitting/setup.py +239 -0
  28. accelmo/fitting/shared_reference.py +102 -0
  29. accelmo/fitting/uncertainty.py +115 -0
  30. accelmo/fitting/weights.py +13 -0
  31. accelmo/fitting/worker.py +250 -0
  32. accelmo/machine/__init__.py +1 -0
  33. accelmo/machine/accelerators/__init__.py +27 -0
  34. accelmo/machine/accelerators/base.py +276 -0
  35. accelmo/machine/accelerators/fcc.py +82 -0
  36. accelmo/machine/accelerators/lhc.py +188 -0
  37. accelmo/machine/accelerators/magnet_grouping.py +149 -0
  38. accelmo/machine/accelerators/psb.py +202 -0
  39. accelmo/machine/accelerators/selection.py +109 -0
  40. accelmo/machine/accelerators/sps.py +125 -0
  41. accelmo/machine/knob_names.py +14 -0
  42. accelmo/machine/machine_state.py +65 -0
  43. accelmo/machine/patterns.py +96 -0
  44. accelmo/momentum_reference.py +271 -0
  45. accelmo/monitor/__init__.py +21 -0
  46. accelmo/monitor/__main__.py +52 -0
  47. accelmo/monitor/app.py +338 -0
  48. accelmo/monitor/cache.py +240 -0
  49. accelmo/monitor/panels.py +261 -0
  50. accelmo/monitor/source.py +52 -0
  51. accelmo/monitor/view.py +39 -0
  52. accelmo/optimisers/__init__.py +18 -0
  53. accelmo/optimisers/adam.py +136 -0
  54. accelmo/optimisers/base.py +84 -0
  55. accelmo/optimisers/lbfgs.py +208 -0
  56. accelmo/optimisers/levenberg_marquardt.py +253 -0
  57. accelmo/poco/__init__.py +34 -0
  58. accelmo/poco/calibrated.py +328 -0
  59. accelmo/poco/calibration.py +234 -0
  60. accelmo/poco/closed_orbit.py +335 -0
  61. accelmo/poco/fitter.py +410 -0
  62. accelmo/poco/workers/__init__.py +1 -0
  63. accelmo/poco/workers/calibrated.py +200 -0
  64. accelmo/poco/workers/closed_orbit.py +501 -0
  65. accelmo/poco/workers/closed_twiss.py +174 -0
  66. accelmo/runlog/__init__.py +25 -0
  67. accelmo/runlog/reader.py +127 -0
  68. accelmo/runlog/writer.py +150 -0
  69. accelmo/tracking/__init__.py +59 -0
  70. accelmo/tracking/config/__init__.py +29 -0
  71. accelmo/tracking/config/helpers.py +43 -0
  72. accelmo/tracking/config/models.py +66 -0
  73. accelmo/tracking/config/tracking.py +535 -0
  74. accelmo/tracking/data_manager.py +476 -0
  75. accelmo/tracking/dispatch/__init__.py +1 -0
  76. accelmo/tracking/dispatch/payloads.py +210 -0
  77. accelmo/tracking/dispatch/setup.py +212 -0
  78. accelmo/tracking/dispatch/turn_planner.py +196 -0
  79. accelmo/tracking/fitter.py +698 -0
  80. accelmo/tracking/ic_iteration.py +110 -0
  81. accelmo/tracking/lm_problem.py +252 -0
  82. accelmo/tracking/ranges.py +112 -0
  83. accelmo/tracking/session.py +301 -0
  84. accelmo/tracking/sgd/__init__.py +1 -0
  85. accelmo/tracking/sgd/checkpointing.py +229 -0
  86. accelmo/tracking/sgd/loop.py +327 -0
  87. accelmo/tracking/sgd/scheduler.py +70 -0
  88. accelmo/tracking/uncertainty.py +160 -0
  89. accelmo/tracking/worker.py +661 -0
  90. accelmo-0.0.1.dist-info/METADATA +208 -0
  91. accelmo-0.0.1.dist-info/RECORD +95 -0
  92. accelmo-0.0.1.dist-info/WHEEL +5 -0
  93. accelmo-0.0.1.dist-info/entry_points.txt +2 -0
  94. accelmo-0.0.1.dist-info/licenses/LICENSE +21 -0
  95. accelmo-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,399 @@
1
+ ! run_closed_twiss_init.mad
2
+ !
3
+ ! Sets up a parametric *closed* twiss: the optimisation knobs become TPSA
4
+ ! parameters on the sequence, so twiss returns the periodic solution together
5
+ ! with the derivative of every optical function with respect to every knob.
6
+ !
7
+ ! There is no seeded starting point. twiss runs cofind for the parametric closed
8
+ ! orbit and then normalises the parametric one-turn map, so the closed orbit,
9
+ ! beta, alpha, phase and dispersion at every BPM are the periodic solution of the
10
+ ! ring as a function of the knobs. Nothing is taken from the measurement except
11
+ ! the targets themselves.
12
+ !
13
+ ! The optical functions come from twiss's own ``trkopt``: each requested name is
14
+ ! added as an mtable column and filled with ``optfun(m, name, ..., mt, nf0)`` as
15
+ ! the map is tracked. That is strictly better than reading them back out of
16
+ ! a saved map afterwards - twiss shares one memoisation table per element (so the
17
+ ! beta/phase TPSA is built once, not once per knob), it references the phase to
18
+ ! the normal form at the origin, and the results arrive as plain columns instead
19
+ ! of one damap per BPM. Encoding the knob monomial in the requested name is what
20
+ ! turns each column into a derivative: ``beta11_`` is the value and
21
+ ! ``beta11_0..010..0`` is d(beta11)/d(knob i).
22
+ !
23
+ ! The closed orbit is the exception: ``x``/``y`` are not optical functions, so
24
+ ! their knob derivatives are recorded by an ``atexit`` hook (``save_orbit_jac``) as twiss
25
+ ! tracks, straight from each BPM's TPSA. No per-BPM maps are saved, which avoids copying
26
+ ! large damaps (many knobs, many BPMs).
27
+ !
28
+ ! When *only* orbit coordinates are observed (``optics_columns`` is empty) none of
29
+ ! that machinery is needed, and ``compute_closed_orbit`` replaces ``twiss``: a plain
30
+ ! ``cofind`` finds the closed orbit and ONE parametric pass gives its knob derivatives.
31
+ ! See ``compute_closed_orbit`` below.
32
+ !
33
+ ! Assumes the following globals are already defined (set by the worker):
34
+ ! loaded_sequence : the sequence to compute the closed twiss of
35
+ ! knob_names : ordered table of optimisation-knob variable names
36
+ ! optics_columns : ordered table of trkopt names (values then derivatives);
37
+ ! empty when only orbit coordinates are observed
38
+ ! orbit_coords : ordered table of closed-orbit coordinates to report
39
+ ! nbpms : number of observed BPMs (sizes the orbit buffers)
40
+ !
41
+ ! After this script runs the worker drives it with, per iteration:
42
+ ! loaded_sequence['<knob>']:set0(value) -- update the knob constant part
43
+ ! compute_closed_twiss() -- returns success flag (optics observed)
44
+ ! compute_closed_orbit() -- same, when only orbits are observed
45
+ ! <read optics_columns via closed_tbl:to_df>
46
+ ! send_orbit_jacobian() -- one matrix per orbit coordinate
47
+
48
+ local matrix, vector in MAD
49
+ local cofind, track in MAD
50
+
51
+ num_knobs = #knob_names
52
+ ! Jacobian buffers keep one (all zero) column when there are no knobs: the only fitted parameter may be the momentum pt, which
53
+ ! the worker handles outside MAD, and MAD vectors and matrices need at least one element.
54
+ jac_cols = math.max(num_knobs, 1)
55
+
56
+ ! Identity parametric map over the 6 phase-space coords + one parameter per knob.
57
+ !
58
+ ! With optical functions observed, mo=2 (not 1) is required: they are first order
59
+ ! in the phase space variables, so their knob derivatives are order-2 monomials
60
+ ! (one coordinate slot times one parameter slot). gphys asserts this directly,
61
+ ! e.g. nf_bet: "damap order is too low %d < %d" for maxord < 1 + mon:ord(). po=1
62
+ ! keeps the parameters first order, so no knob-knob cross terms are carried - the
63
+ ! Gauss-Newton Hessian only ever needs the first derivatives.
64
+ !
65
+ ! Orbit-only needs mo=1: d(orbit)/d(knob) is the pure-parameter monomial, which is
66
+ ! order 1, and the phase-space linearisation is order 1 as well.
67
+ orbit_only = #optics_columns == 0
68
+ x0map = MAD.damap {
69
+ nv = 6,
70
+ np = num_knobs,
71
+ mo = orbit_only and 1 or 2,
72
+ po = 1,
73
+ pn = knob_names,
74
+ }
75
+
76
+ ! Make each knob a TPSA (value + 1*parameter) so its derivative propagates, and
77
+ ! remember the monomial that selects d/d(knob_i) from a coordinate TPSA. The
78
+ ! monomial is kept in the string form that ``tpsa:get`` parses itself.
79
+ knob_mono = {}
80
+ for i, param in ipairs(knob_names) do
81
+ loaded_sequence[param] = loaded_sequence[param] + x0map[param]
82
+ knob_mono[i] = "000000" .. string.rep("0", i - 1) .. "1"
83
+ end
84
+
85
+ ! Orbit buffers, allocated once (first use) and only zeroed per computation.
86
+ ! ``orbit_val[coord]`` is a vector, ``orbit_jac[coord]`` an (nbpm x nknob) matrix and ``orbit_R[coord]`` the (nbpm x 4) transverse
87
+ ! map rows used by ``compute_closed_orbit``.
88
+ local orbit_n, orbit_names, orbit_val, orbit_jac, orbit_R, orbit_ready = 0, {}, {}, {}, {}, false
89
+ local orbit_row, orbit_row4 = vector(jac_cols), vector(4)
90
+
91
+ local function knob_row(tpsa)
92
+ if num_knobs == 0 then return orbit_row end
93
+ return tpsa:getvec(8, orbit_row)
94
+ end
95
+
96
+ local function reset_orbit()
97
+ if not orbit_ready then
98
+ orbit_ready = true
99
+ for _, coord in ipairs(orbit_coords) do
100
+ orbit_val[coord] = vector(nbpms)
101
+ orbit_jac[coord] = matrix(nbpms, jac_cols)
102
+ orbit_R[coord] = matrix(nbpms, 4)
103
+ end
104
+ end
105
+ orbit_n = 0
106
+ for _, coord in ipairs(orbit_coords) do
107
+ orbit_val[coord]:zeros()
108
+ orbit_jac[coord]:zeros()
109
+ end
110
+ end
111
+
112
+ ! Orbit values only (compute_orbit_plain). cofind calls the hook on each of its own
113
+ ! internal tracking passes, every one visiting all BPMs in order, so the row index
114
+ ! wraps and each pass overwrites the last; the final pass is the converged orbit.
115
+ local function save_orbit(elm, mflw, _, slc)
116
+ if slc ~= -4 or not elm:is_observed() then return end
117
+ orbit_n = orbit_n % nbpms + 1
118
+ for _, coord in ipairs(orbit_coords) do orbit_val[coord]:seti(orbit_n, mflw[1][coord]:get0()) end
119
+ end
120
+
121
+ ! Orbit values and, filled straight into the Jacobian rows, d(orbit)/d(knob) (compute_closed_orbit).
122
+ ! Also records the BPM names. Coefficient 0 of a coordinate TPSA is the constant, 1-6 the phase space, 7.. the knobs.
123
+ local function save_orbit_jac(elm, mflw, _, slc)
124
+ if slc ~= -4 or not elm:is_observed() then return end
125
+ orbit_n = orbit_n % nbpms + 1
126
+ orbit_names[orbit_n] = elm.name
127
+ for _, coord in ipairs(orbit_coords) do
128
+ local tpsa = mflw[1][coord]
129
+ orbit_val[coord]:seti(orbit_n, tpsa:get0())
130
+ orbit_jac[coord]:setrow(orbit_n, knob_row(tpsa))
131
+ end
132
+ end
133
+
134
+ ! Closed orbit with the knobs as plain numbers (call between knobs_to_plain() and knobs_to_param()): cofind from a plain
135
+ ! start builds its own 6-variable map. Returns the one-turn map; its status is 'stable' at a closed orbit.
136
+ -- Warm start of the next cofind: the last converged closed orbit (x, px, y, py) of the SAME machine state. The worker sets the
137
+ -- global ``co_key`` (series index, role, pt) before each solve, so every state keeps its own start and one series' orbit never
138
+ -- seeds another's (different corrector kick, other momentum). Without ``co_key`` every solve shares the key 'default'.
139
+ co_key = 'default'
140
+ local co_by_key = {}
141
+ local cofind_log = os.getenv('COFIND_LOG')
142
+ local function log_failure(stage, detail)
143
+ if not cofind_log then return end
144
+ local f = io.open(cofind_log, 'a')
145
+ if f then f:write(stage, ' ', tostring(detail), '\n') f:close() end
146
+ end
147
+
148
+ local function run_cofind(atexit, start)
149
+ local X0 = { pt = x0map.pt:get0() } -- the fixed momentum pinned by the worker on x0map
150
+ if start then X0.x, X0.px, X0.y, X0.py = start[1], start[2], start[3], start[4] end
151
+ local _, mflw = cofind{
152
+ sequence = loaded_sequence,
153
+ method = 6,
154
+ observe = 1,
155
+ X0 = X0,
156
+ save = false,
157
+ atexit = atexit,
158
+ }
159
+ return mflw[1]
160
+ end
161
+
162
+ -- Predicted start of the next closed-orbit solve. For every state (co_key) keep the last converged orbit X, the knob values K it
163
+ -- belongs to and, once a parametric solve has run, S = dX*/dk (4 x num_knobs). The next solve starts at X + S (k - K), which
164
+ -- is the closed orbit to first order in the knob change, so cofind needs far fewer passes or none at all.
165
+ local co_base = {}
166
+ local knob_now = vector(jac_cols)
167
+
168
+ local function read_knobs(out)
169
+ for i, p in ipairs(knob_names) do out:seti(i, loaded_sequence[p]) end
170
+ return out
171
+ end
172
+
173
+ local function remember(x, S)
174
+ local b = co_base[co_key]
175
+ if not b then b = { K = vector(jac_cols) } co_base[co_key] = b end
176
+ b.X = x
177
+ read_knobs(b.K)
178
+ if S then b.S = S end
179
+ end
180
+
181
+ local function predicted_start()
182
+ local b = co_base[co_key]
183
+ if not (b and b.S) then return nil end
184
+ read_knobs(knob_now)
185
+ local x = vector(6)
186
+ for i = 1, 6 do x:seti(i, b.X[i]) end
187
+ for r = 1, 4 do
188
+ local shift, row = 0, (r - 1) * jac_cols
189
+ for c = 1, num_knobs do shift = shift + b.S[row + c] * (knob_now[c] - b.K[c]) end
190
+ x:seti(r, x[r] + shift)
191
+ end
192
+ -- The stored orbit may be at another momentum (a fitted pt moves between solves): start at the pinned one.
193
+ x:seti(6, x0map.pt:get0())
194
+ return x
195
+ end
196
+
197
+ ! Warm start from the previous solution; a failed or unstable warm start retries from the zero orbit (the stored orbit is
198
+ ! only replaced by a converged one, so a lost trial does not poison the next).
199
+ local function plain_cofind(atexit)
200
+ local last_co = predicted_start() or co_by_key[co_key]
201
+ if last_co then
202
+ if atexit then reset_orbit() end
203
+ local ok, res = pcall(run_cofind, atexit, last_co)
204
+ if ok and res.status == 'stable' then
205
+ co_by_key[co_key] = res:get0()
206
+ remember(co_by_key[co_key])
207
+ return res
208
+ end
209
+ log_failure('warm_cofind_failed', ok and res.status or res)
210
+ if atexit then reset_orbit() end
211
+ end
212
+ local res = run_cofind(atexit, nil)
213
+ if res.status == 'stable' then co_by_key[co_key] = res:get0() remember(co_by_key[co_key]) else log_failure('cold_cofind_unstable', res.status) end
214
+ return res
215
+ end
216
+
217
+ ! Compute the parametric closed twiss. cofind can fail (lose the particle) when
218
+ ! the knobs are driven into an unstable region; report success so the worker can
219
+ ! flag the step as recoverable instead of using a stale/NaN table.
220
+ !
221
+ ! coupling=true is required for betx/bety/alfx/alfy: without it those columns
222
+ ! are unavailable, and the uncoupled Ripken beta11/beta22/alfa11/alfa22 are
223
+ ! blind to skew/roll knobs by construction (a coupling error does not move them
224
+ ! at first order), so they carry no leverage on those knobs regardless of any
225
+ ! fit weight. There is no equivalent physical-plane column for phase advance --
226
+ ! MAD-NG's optical-function names only offer mu/dmu with a mode-index suffix
227
+ ! (mu1/mu2/mu3), no mux/muy -- so mu1/mu2 stay mode-indexed and the same
228
+ ! blind-to-roll limitation applies to the phase-advance observable.
229
+ function compute_closed_twiss()
230
+ -- Capture into a local first: when twiss fails it returns nil, and reading
231
+ -- an undeclared/nil global closed_tbl would trip MAD-NG's strict-mode warning.
232
+ reset_orbit()
233
+ orbit_names = {}
234
+ local tws = twiss{
235
+ sequence = loaded_sequence,
236
+ observe = 1,
237
+ X0 = x0map,
238
+ trkopt = optics_columns,
239
+ method = 6,
240
+ coupling = true,
241
+ atexit = #orbit_coords > 0 and save_orbit_jac or nil,
242
+ }
243
+ closed_tbl = tws
244
+ local ok = tws ~= nil and (tws.lost == nil or tws.lost == 0) and (#orbit_coords == 0 or orbit_n == nbpms)
245
+ python:send(ok and true or false, true)
246
+ end
247
+
248
+ ! Closed orbit and its knob derivatives without ``twiss``, which also normalises the parametric one-turn map and tracks
249
+ ! the normal form to fill every optical-function column, none of which an orbit observable reads. One parametric track:
250
+ !
251
+ ! 1. ``cofind`` with the knobs as plain numbers finds the closed orbit X0.
252
+ ! 2. ``track`` of the parametric map from X0, with a hook recording at each BPM s the value, R_s = dM_s/dz (transverse
253
+ ! 4 columns) and P_s = dM_s/dk. After the turn the map itself is the one-turn map (R, P).
254
+ ! 3. The closed orbit moves with the knobs: X*(k) = M(X*(k), k) gives dX*/dk = S = (I - R)^-1 P on the transverse block
255
+ ! (pt is pinned, so t/pt do not enter). By the chain rule d(orbit_s)/dk = R_s S + P_s. No ``normal`` and no second track.
256
+ local tcoords = { 'x', 'px', 'y', 'py' }
257
+
258
+ local function save_orbit_chain(elm, mflw, _, slc)
259
+ if slc ~= -4 or not elm:is_observed() then return end
260
+ orbit_n = orbit_n + 1
261
+ orbit_names[orbit_n] = elm.name
262
+ for _, coord in ipairs(orbit_coords) do
263
+ local tpsa = mflw[1][coord]
264
+ orbit_val[coord]:seti(orbit_n, tpsa:get0())
265
+ orbit_R[coord]:setrow(orbit_n, tpsa:getvec(2, orbit_row4))
266
+ orbit_jac[coord]:setrow(orbit_n, knob_row(tpsa))
267
+ end
268
+ end
269
+
270
+ ! S = (I - R)^-1 P and the Newton step dx = (I - R)^-1 (M(X0) - X0) from the one-turn map ``turn`` tracked from the start ``x0``;
271
+ ! S is a 4 x num_knobs matrix, dx a 4 x 1 matrix.
272
+ local function fixed_point_shift(turn, x0)
273
+ local IR, P, res = matrix(4, 4), matrix(4, jac_cols), matrix(4, 1)
274
+ for r, coord in ipairs(tcoords) do
275
+ local tpsa = turn[coord]
276
+ tpsa:getvec(2, orbit_row4)
277
+ for c = 1, 4 do IR:seti((r - 1) * 4 + c, (r == c and 1 or 0) - orbit_row4[c]) end
278
+ P:setrow(r, knob_row(tpsa))
279
+ res:seti(r, tpsa:get0() - x0[r])
280
+ end
281
+ return IR:solve(P), IR:solve(res)
282
+ end
283
+
284
+ -- A solve whose Newton step is below this is accepted as the closed orbit without another cofind (the correction is second order).
285
+ local fast_tol = 1e-7
286
+
287
+ -- One parametric track of the one-turn map from the start ``x0`` (6 coordinates). Fills the orbit buffers with the values and
288
+ -- Jacobian at the Newton-corrected closed orbit; returns S, the largest Newton step and the corrected start, or nil if the particle is lost.
289
+ local function parametric_pass(x0)
290
+ reset_orbit()
291
+ orbit_names = {}
292
+ -- Fresh copy each call: ``nocopy`` makes the tracked map the one-turn map, which must not leak into x0map.
293
+ local start = x0map:copy()
294
+ start.nocopy = true
295
+ start:set0(x0)
296
+ local _, mflw = track{
297
+ sequence = loaded_sequence,
298
+ observe = 1,
299
+ X0 = start,
300
+ method = 6,
301
+ save = false,
302
+ atexit = save_orbit_chain,
303
+ }
304
+ if mflw[1].status == 'lost' or orbit_n ~= nbpms then
305
+ log_failure('final_track', mflw[1].status .. ' n=' .. orbit_n)
306
+ return nil
307
+ end
308
+ local sok, S, dx = pcall(fixed_point_shift, start, x0)
309
+ if not sok then log_failure('solve_error', S) return nil end
310
+ local step = 0
311
+ for r = 1, 4 do step = math.max(step, math.abs(dx[r])) end
312
+ for _, coord in ipairs(orbit_coords) do
313
+ local val, R = orbit_val[coord], orbit_R[coord]
314
+ for i = 1, nbpms do
315
+ local shift, row = 0, (i - 1) * 4
316
+ for c = 1, 4 do shift = shift + R[row + c] * dx[c] end
317
+ val:seti(i, val[i] + shift)
318
+ end
319
+ orbit_jac[coord] = R * S + orbit_jac[coord]
320
+ end
321
+ local corrected = vector(6)
322
+ for i = 1, 6 do corrected:seti(i, x0[i] + (i <= 4 and dx[i] or 0)) end
323
+ return S, step, corrected
324
+ end
325
+
326
+ function compute_closed_orbit()
327
+ knobs_to_plain()
328
+ local x0, found = predicted_start(), false
329
+ for attempt = 1, 3 do
330
+ if not x0 then
331
+ local ok, plain = pcall(plain_cofind)
332
+ if not ok or plain.status ~= 'stable' then
333
+ knobs_to_param()
334
+ log_failure('cofind_error', ok and plain.status or plain)
335
+ python:send(false, true)
336
+ return
337
+ end
338
+ x0, found = plain:get0(), true
339
+ end
340
+ knobs_to_param() -- whatever happened: the worker's knob updates need the parameters
341
+ local S, step, corrected = parametric_pass(x0)
342
+ if S and (found or step <= fast_tol) then
343
+ knobs_to_plain()
344
+ remember(corrected, S)
345
+ co_by_key[co_key] = corrected
346
+ knobs_to_param()
347
+ python:send(true, true)
348
+ return
349
+ end
350
+ -- Prediction too far off, or the particle was lost from it: converge with cofind, from the corrected start if any.
351
+ if found then python:send(false, true) return end
352
+ knobs_to_plain()
353
+ co_by_key[co_key] = corrected
354
+ co_base[co_key] = nil
355
+ x0 = nil
356
+ end
357
+ python:send(false, true)
358
+ end
359
+
360
+ ! Orbit-only results: BPM names, then per coordinate its values and Jacobian.
361
+ function send_orbit_only()
362
+ python:send(orbit_names, true)
363
+ for _, coord in ipairs(orbit_coords) do
364
+ python:send(orbit_val[coord], true)
365
+ python:send(orbit_jac[coord], true)
366
+ end
367
+ end
368
+
369
+ function knobs_to_plain()
370
+ for _, p in ipairs(knob_names) do loaded_sequence[p] = loaded_sequence[p]:get0() end
371
+ end
372
+
373
+ function knobs_to_param()
374
+ for _, p in ipairs(knob_names) do loaded_sequence[p] = loaded_sequence[p] + x0map[p] end
375
+ end
376
+
377
+ ! After knobs_to_plain(): sends success, then each orbit coordinate's values in BPM order.
378
+ function compute_orbit_plain_core()
379
+ reset_orbit()
380
+ local start = plain_cofind(save_orbit)
381
+ -- orbit_n must be exactly nbpms: fewer means one of the passes stopped early
382
+ if start.status ~= 'stable' or orbit_n ~= nbpms then
383
+ python:send(false, true)
384
+ return
385
+ end
386
+ python:send(true, true)
387
+ for _, coord in ipairs(orbit_coords) do python:send(orbit_val[coord], true) end
388
+ end
389
+
390
+ function compute_orbit_plain()
391
+ local ok = pcall(compute_orbit_plain_core)
392
+ if not ok then python:send(false, true) end
393
+ end
394
+
395
+ ! Send d(orbit coordinate)/d(knob) as one (nbpm x nknob) matrix per coordinate, as recorded by ``save_orbit_jac`` during
396
+ ! the twiss track (no per-BPM maps are saved). The scalar orbit is already a plain twiss column.
397
+ function send_orbit_jacobian()
398
+ for _, coord in ipairs(orbit_coords) do python:send(orbit_jac[coord], true) end
399
+ end