freegsnke.control_loop.systems_category module
Module to implement systems control in FreeGSNKE control loops.
Copyright 2025 UKAEA, UKRI-STFC, and The Authors, as per the COPYRIGHT and README files.
This file is part of FreeGSNKE.
FreeGSNKE is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
FreeGSNKE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with FreeGSNKE. If not, see <http://www.gnu.org/licenses/>.
- class freegsnke.control_loop.systems_category.SystemsController(data: dict[str, dict[str, Any]], ctrl_coils: list[str])[source]
Bases:
objectA controller class for managing coil current perturbations, coil current limits, and coil current ramp rate limits.
- Parameters:
data (dict) –
A nested dictionary containing control waveforms for the systems controller. The required keys for both spline-based and step-based waveforms are:
Spline keys: “<coil>_pert”
Step keys: “min_coil_curr_lims”, “max_coil_curr_lims”, “max_coil_curr_ramp_lims”
- Each key should map to a waveform dictionary suitable for interpolation with keys:
’times’: 1D array of time points
’vals’: 1D array of values at those time points (same length).
ctrl_coils (list of str) – The list of active coils being controlled.
- ctrl_coils
The list of active coils being controlled.
- Type:
list of str
- keys_to_spline
Keys corresponding to waveforms that will be interpolated using splines.
- Type:
list of str
- keys_to_step
Keys corresponding to waveforms that will be interpolated using step functions.
- Type:
list of str
- data
Internal copy of the input control waveforms.
- Type:
dict
- interpolants
A nested dictionary storing interpolation functions of each input waveform. Structure: {spline/step key: interpolant_function}
- Type:
dict
- interpolant_derivatives
Derivatives of the coil-perturbation spline interpolants, rebuilt whenever update_interpolants is called and reused during control steps.
- Type:
dict
- __init__(data: dict[str, dict[str, Any]], ctrl_coils: list[str]) None[source]
Initialise the systems controller.
Validates that per-coil perturbation data and coil current/ramp limits are present in data, stores a reference to the data, and builds the spline/step interpolants used to evaluate them at arbitrary times.
- Parameters:
data (dict) –
Dictionary of time-series entries. Must contain the following keys, each in the format expected by check_data_entry (i.e. containing ‘times’ and ‘vals’ arrays of matching length):
”<coil>_pert” for each coil in ctrl_coils : perturbation
signal for that coil. - “min_coil_curr_lims” : minimum coil current limits. - “max_coil_curr_lims” : maximum coil current limits. - “max_coil_curr_ramp_lims” : maximum coil current ramp-rate limits.
ctrl_coils (list of str) – Names of the coils controlled by this controller. Determines which “<coil>_pert” keys are required in data.
- ctrl_coils
Stored copy of ctrl_coils.
- Type:
list of str
- keys_to_spline
Data keys that will be spline-interpolated: one “<coil>_pert” entry per coil in ctrl_coils.
- Type:
list of str
- keys_to_step
Data keys that will be step-interpolated: “min_coil_curr_lims”, “max_coil_curr_lims”, and “max_coil_curr_ramp_lims”.
- Type:
list of str
- data
Internal reference to the input data.
- Type:
dict
- Raises:
ValueError – If a required key is missing from data or is not in the expected format, as enforced by check_data_entry.
Notes
Calls update_interpolants at the end of initialisation to build the interpolating functions from data.
- extract_values(t: float, targets: list[str], deriv: bool = False) ndarray[source]
Extracts interpolated values or their derivatives for specified shape targets at a given time.
This method queries the stored interpolation functions for each target and key, returning either the interpolated value or its first derivative depending on the deriv flag.
- Parameters:
t (float) – Time at which to evaluate the interpolants [s].
targets (list of str) – List of keys. Each must correspond to a key in self.interpolants.
deriv (bool, optional) – If True, returns the first derivative of the interpolant at time t. Default is False.
- Returns:
Array of interpolated values (or derivatives) for each target at time t.
- Return type:
np.ndarray
Notes
Assumes that self.interpolants[target] is a valid scipy.interpolate object.
Spline derivatives are constructed by update_interpolants and reused here.
- plot_data(tmin: float = -1.0, tmax: float = 1.0, nt: int = 1001) None[source]
Visualizes interpolated control waveforms and corresponding raw inputs.
This method generates subplots for each control waveform (spline types), showing the interpolated time series alongside the original data points. It helps verify the quality and behavior of the interpolation.
- Parameters:
tmin (float, optional) – Start time for the evaluation grid (default is -1.0 seconds).
tmax (float, optional) – End time for the evaluation grid (default is 1.0 seconds).
nt (int, optional) – Number of time points to evaluate the interpolants over the interval [tmin, tmax] (default is 1001).
Notes
Each subplot corresponds to a control waveform (e.g., ‘<coil>_pert’).
Interpolated curves are plotted in navy; raw data points are shown in red.
Axis labels include units where applicable.
Useful for debugging or validating the interpolation quality.
- run_control(t: float, dt: float, I_unapproved: ndarray, dI_dt_unapproved: ndarray, verbose: bool = False) Tuple[ndarray, ndarray][source]
Applies coil current perturbations to unapproved coil currents and enforce coil current constraints to produce approved control signals.
This method adjusts the unapproved coil currents and their rates of change by applying time-dependent perturbations, then clips the results according to current and ramp rate limits. It returns the final approved coil currents and their derivatives.
- Parameters:
t (float) – Current time [s].
dt (float) – Time step [s].
I_unapproved (numpy.ndarray) – Coil currents (not yet approved), computed via Euler integration [A].
dI_dt_unapproved (numpy.ndarray) – Rate of change of coil currents (not yet approved) [A/s].
verbose (bool, optional) – If True, prints diagnostic messages about clipping and approved values.
- Returns:
I_approved (numpy.ndarray) – Coil currents (approved) [A].
dI_dt_approved (numpy.ndarray) – Rate of change of coil currents (approved) [A/s].