Correction
Constants
Specific constants to be used in correction, to help with consistency.
Filters
Filters for different kind of measurement data, and to filter the entries
in the response matrix based on (the presumably then filtered) measurement data.
Measurement filters extract valid (or trustworthy) data, e.g. to be used in
corrections.
The main function is omc3.correction.filters.filter_measurement()
which decides on which filter to use for the given keys.
In earlier implementations there was a split between all kinds of measures, i.e. beta, phase etc. In this implementation most of it is handled by the _get_filtered_generic function.
- omc3.correction.filters.filter_measurement(keys: Sequence[str], meas: dict[str, pd.DataFrame], model: pd.DataFrame, opt: DotDict) dict [source]
Filters measurements in keys based on the dict-entries (keys as in keys) in opt.errorcut, opt.modelcut and opt.weights and unifies the data-column names to VALUE, ERROR, WEIGHT. If opt.use_errorbars is True the weights will be also based on the errors.
- omc3.correction.filters.filter_response_index(response: dict, measurement: dict, keys: Sequence[str])[source]
Filters the index of the response matrices response by the respective entries in measurement.
Handler
This module contains high-level functions to manage most functionality of the corrections calculations.
- omc3.correction.handler.correct(accel_inst: Accelerator, opt: DotDict) None [source]
Perform global correction as described in
omc3.global_correction
.- Parameters:
accel_inst (Accelerator) -- Accelerator Instance
opt (DotDict) -- Correction options, see
omc3.global_correction
for details.
- omc3.correction.handler.get_measurement_data(keys: Sequence[str], meas_dir: Path, beta_filename: str, w_dict: dict[str, float] = None) tuple[list[str], dict[str, tfs.TfsDataFrame]] [source]
Loads all measurements defined by keys into a dictionary.
Model Appenders
Utilities to append new columns to measurement and model dataframes. E.g. get differences between measurement and model and append those to the measurement data (for corrections).
- omc3.correction.model_appenders.add_coupling_to_model(model: DataFrame) DataFrame [source]
Computes the coupling RDTs from the input model TfsDataFrame and returns a copy of said TfsDataFrame with columns for the real and imaginary parts of the computed coupling RDTs.
- Parameters:
model (tfs.TfsDataFrame) -- Twiss dataframe.
- Returns:
A TfsDataFrame with the added columns.
- omc3.correction.model_appenders.add_differences_to_model_to_measurements(model: DataFrame, measurement: Dict[str, DataFrame], keys: Sequence[str] = None) Dict[str, DataFrame] [source]
Provided with DataFrames from a model and a measurement, and a number of keys to be found in both, returns a dictionary with the variation from measurement to model for each key.
- Parameters:
model (pd.DataFrame) -- DataFrame of the model.
measurement (Dict[str, pd.DataFrame]) -- DataFrames of the measurement.
keys (Sequence[str]) -- Parameters to get variation to model for. Optional. If omitted, all entries in measurement are used.
- Returns:
A dictionary of optics parameters and the resulting DataFrames.
Model Diff
Calculate the differences in optics parameters between twiss-models.
Similar to omc3.correction.model_appenders.add_differences_to_model_to_measurements()
,
yet operates on two twiss files instead.
- omc3.correction.model_diff.diff_twiss_parameters(model_a: TfsDataFrame, model_b: TfsDataFrame, parameters: Sequence[str] = None) TfsDataFrame [source]
Create a TfsDataFrame containing of the given parameters between model_a and model_b.
Response MAD-X
Provides a function to create the responses of beta, phase, dispersion, tune and coupling via iterative madx calls.
The variables under investigation need to be provided as a list (which can be obtained from the accelerator class).
For now, the response matrix is stored in a hdf5 file.
- author:
Lukas Malina, Joschua Dilly, Jaime (…) Coello de Portugal
- omc3.correction.response_madx.create_fullresponse(accel_inst: Accelerator, variable_categories: Sequence[str], delta_k: float = 2e-05, num_proc: int = 4, temp_dir: Path = None) dict[str, pd.DataFrame] [source]
Generate a dictionary containing response matrices for beta, phase, dispersion, tune and coupling and saves it to a file.
- Parameters:
accel_inst -- Accelerator Instance.
variable_categories (list) -- Categories of the variables/knobs to use. (from .json)
delta_k (float) -- delta K1L to be applied to quads for sensitivity matrix
num_proc (int) -- Number of processes to use in parallel.
temp_dir (str) -- temporary directory. If
None
, uses folder of original_jobfile.
Response TWISS
Provides a class to get response matrices from Twiss parameters.
Warning
The responses are only valid for MAD-X Beam 1 and Beam 2 twiss-files, not for Beam 4 !! Also, it only works properly for on-orbit twiss files.
The calculation is based on formulas in [1], [2] and is summarized in [3], where the following equations can be found in Eq. 10 - Eq. 15.
Beta Response:
Dispersion Response:
Norm. Dispersion Response: similar as above but with \(\frac{1}{\sqrt{\beta}}\) linearized
Phase Advance Response:
Tune Response:
Coupling Response:
For people reading the code, the response matrices are first calculated like:
| Elements of interest (j) --> ... |
|Magnets (m) |
| | |
| v |
| . |
| . |
| . |
| |
This avoids transposing all vectors individually in the beginning. At the end (of the calculation) the matrix is then transposed to fit the \(M \cdot \delta K\) orientation.
Also \(\Delta \Phi_{z,wj}\) needs to be multiplied by \(2\pi\) to be consistent.
References
- class omc3.correction.response_twiss.TwissResponse(accel_inst, variable_categories, varmap_or_path, at_elements='bpms')[source]
Provides Response Matrices calculated from sequence, model and given variables.
- Parameters:
accel_inst (accelerator) -- Accelerator Instance (needs to contain elements model).
variable_categories (list) -- List of variable categories to get from the accelerator class.
varmap_or_path (dict, string) -- mapping of the variables, either as dict-structure of Series or path to a pickled-file.
at_elements (str) -- Get response matrix for these elements. Can be: ‘bpms’: All BPMS (Default) ‘bpms+’: BPMS+ used magnets (== magnets defined by variables in varfile) ‘all’: All BPMS and Magnets given in the model (Markers are removed)
- get_beta_beat(mapped=True)[source]
Returns Response Matrix for Beta Beating
- get_coupling(mapped=True)[source]
Returns Response Matrix for the coupling
- get_dispersion(mapped=True)[source]
Returns Response Matrix for Dispersion
- get_norm_dispersion(mapped=True)[source]
Returns Response Matrix for Normalized Dispersion
- get_phase(mapped=True)[source]
Returns Response Matrix for Total Phase
- get_phase_adv(mapped=True)[source]
Returns Response Matrix for Phase Advance
- get_response_for(observables=None) dict [source]
Calculates and returns only desired response matrices
- get_tune(mapped=True)[source]
Returns Response Matrix for the Tunes
- omc3.correction.response_twiss.create_response(accel_inst: Accelerator, vars_categories: Sequence[str], optics_params: list[str]) dict [source]
Wrapper to create response via TwissResponse
- omc3.correction.response_twiss.dphi(data, q)[source]
Return dphi from phase advances in data, see Eq. 7 in [3]
- omc3.correction.response_twiss.get_phase_advances(twiss_df: DataFrame) dict[str, DataFrame] [source]
Calculate phase advances between all elements
- Returns:
Matrices similar to DPhi(i,j) = Phi(j) - Phi(i)
- omc3.correction.response_twiss.response_add(*args) DataFrame [source]
Merges two or more Response Matrix DataFrames
- omc3.correction.response_twiss.tau(data, q)[source]
Return tau from phase advances in data, see Eq. 8 in [3]
- omc3.correction.response_twiss.upper(list_of_strings: Sequence[str]) Sequence[str] [source]
Set all items of list to uppercase
Sequence Evaluation
Evaluates the variable responses from a sequence in MAD-X.
First: Set all variables to 0 Then: Set one variable at a time to 1
Compare results with case all==0.
- omc3.correction.sequence_evaluation.check_varmap_file(accel_inst: Accelerator, vars_categories)[source]
Checks on varmap file and creates it if not in model folder.
- omc3.correction.sequence_evaluation.evaluate_for_variables(accel_inst: Accelerator, variable_categories, order: int = 4, num_proc: int = 4, temp_dir: Path = None) dict [source]
Generate a dictionary containing response matrices for beta, phase, dispersion, tune and coupling and saves it to a file.
- Parameters:
accel_inst (Accelerator) -- Accelerator Instance.
variable_categories (list) -- Categories of the variables/knobs to use. (from .json)
order (int or tuple) -- Max or [min, max] of K-value order to use.
num_proc (int) -- Number of processes to use in parallel.
temp_dir (Path) -- temporary directory. If
None
, uses model_dir.
Response Matrix IO
Input and output functions for response matrices.
- omc3.correction.response_io.ignore_natural_name_warning()[source]
This context manager catches and ignores the ‘NaturalNameWarning’ emitted within, which is our case comes from pytables. It warns about table entries such as ‘kq4.r8b2’ which we can’t access with syntax such as some_table.kq4.r8b2 but we don’t care about this. We let pandas handle the access with getattr (which works).
If encountering issues, comment out the context manager and debug.
- omc3.correction.response_io.read_fullresponse(path: Path, optics_parameters: Sequence[str] = None) dict[str, DataFrame] [source]
Load the response matrices from disk. Beware: As empty DataFrames are skipped on write, default for not found entries are empty DataFrames.
- omc3.correction.response_io.read_varmap(path: Path, k_values: Sequence[str] = None) dict[str, dict[str, Series]] [source]
Load the variable mapping file from disk. Beware: As empty DataFrames are skipped on write, default for not found entries are empty Series.
- omc3.correction.response_io.write_fullresponse(path: Path, fullresponse: dict[str, DataFrame])[source]
Write the full response matrices to disk. Beware: Empty Dataframes are skipped! (HDF creates gigantic files otherwise)
- omc3.correction.response_io.write_varmap(path: Path, varmap: dict[str, dict[str, Series]])[source]
Write the variable mapping file to disk. Beware: Empty Dataframes are skipped! (HDF creates gigantic files otherwise)