freegsnke.control_loop.shape_category module

Module to implement shape 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.shape_category.ShapeController(data: dict[str, dict[str, dict[str, Any]]], ctrl_targets: list[str], mode: str | None = None)[source]

Bases: object

A controller class for managing shape control waveforms.

Parameters:
  • data (dict) –

    A nested dictionary containing waveforms for each target to be controlled. Each target’s dictionary must include keys for both spline-based and step-based parameters:

    • Spline keys: “ff”, “ref”, “blend”

    • Step keys: “k_prop”, “k_int”, “damping”

    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_targets (list of str) – A list of shape target names (keys in data) that the controller will manage.

  • mode (str) – Choose the type of controller to use, here the default is an “PI_with_P_damping” controller, see “run_control” method for more information.

ctrl_targets

The list of shape targets being managed.

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 for each shape target. Structure: {target: {spline/step key: interpolant_function}}

Type:

dict

interpolant_derivatives

A nested dictionary storing derivatives of the spline interpolants. These are constructed with interpolants so control steps only evaluate them.

Type:

dict

__init__(data: dict[str, dict[str, dict[str, Any]]], ctrl_targets: list[str], mode: str | None = None) → None[source]

Initialise the shape controller.

Selects a control algorithm based on mode, determines which data keys that algorithm requires, validates that data contains those keys (per control target) in the expected format, and builds the spline/step interpolants used to evaluate them at arbitrary times.

Parameters:
  • data (dict) – Dictionary keyed by control target (each entry of ctrl_targets must be a key). Each value is itself a dictionary of time-series entries, containing whichever of “ff”, “ref”, “blend”, “k_prop”, “k_int”, “k_deriv”, and “damping” are required by the selected mode (see below), each in the format expected by check_data_entry.

  • ctrl_targets (list of str) – Names of the shape control targets. Each must be a top-level key in data.

  • mode (str, optional) –

    Control algorithm to use. One of:

    • ”PI_with_P_damping” (default): PI control with proportional

    damping. Requires spline keys “ff”, “ref”, “blend” and step keys “k_prop”, “k_int”, “k_deriv”, “damping”. - “PID_with_scaled_out_damping”: PID control with damping scaled out. Requires spline keys “ff”, “ref”, “blend” and step keys “k_prop”, “damping”. - “PID”: standard PID control. Requires spline keys “ff”, “ref”, “blend” and step keys “k_prop”, “k_int”, “k_deriv”.

    If None, defaults to “PI_with_P_damping”.

ctrl_targets

Stored copy of ctrl_targets.

Type:

list of str

data

Internal reference to the input data.

Type:

dict

run_control

Bound method implementing the selected control algorithm (run_control_PI_with_P_damping, run_control_PID_with_scaled_out_damping, or run_control_PID).

Type:

callable

keys_to_spline

Data keys (per target) that will be spline-interpolated, as determined by mode.

Type:

list of str

keys_to_step

Data keys (per target) that will be step-interpolated, as determined by mode.

Type:

list of str

Raises:

ValueError – If a required key is missing from data[targ] for any target in ctrl_targets, 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], key: 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 shape target names. Each must correspond to a key in self.interpolants.

  • key (str) – The waveform name (e.g., ‘ff’, ‘ref’, ‘blend’, ‘k_prop’, etc.) used to select the interpolant.

  • 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][key] is a valid scipy.interpolate object.

  • Spline derivatives are constructed by update_interpolants and reused here.

plot_data(targ: str, tmin: float = -1.0, tmax: float = 1.0, nt: int = 1001) → None[source]

Visualizes interpolated control waveforms and corresponding raw inputs for a specified shape target.

This method generates subplots for each control waveform (both spline and step types), showing the interpolated time series alongside the original data points. It helps verify the quality and behavior of the interpolation.

Parameters:
  • targ (str) – The name of the shape target waveforms to plot. Must be a key in self.interpolants and self.data.

  • 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 10001).

Notes

  • Each subplot corresponds to a control parameter (e.g., ‘ff’, ‘ref’, ‘blend’, ‘k_prop’, etc.).

  • 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_PID(t: float, dt: float, T_meas: ndarray, T_err_prev: ndarray, T_hist_prev: ndarray) → Tuple[ndarray, ndarray, ndarray][source]

Computes the time derivative of shape target requests based on measured values, reference trajectories, and control gains. It blends feedforward and feedback contributions using a time-varying blend factor.

Parameters:
  • t (float) – Current time [s].

  • dt (float) – Time step [s].

  • T_meas (np.ndarray) – Measured values of the shape targets at the current time [m].

  • T_err_prev (np.ndarray) – Previously filtered error signal [m].

  • T_hist_prev (np.ndarray) – Previous integral term (used for PI control) [m.s].

Returns:

  • dT_dt (np.ndarray) – Time derivative of the shape target requests [m/s].

  • T_err (np.ndarray) – Filtered error signal at the current time [m].

  • T_hist (np.ndarray) – Updated integral term for use in the next control step [m.s].

Notes

  • The integral term is updated using trapezoidal integration.

  • The final output blends feedforward and feedback derivatives based on a dynamic blend factor.

run_control_PID_with_scaled_out_damping(t: float, dt: float, T_meas: ndarray, T_err_prev: ndarray, T_hist_prev: ndarray) → Tuple[ndarray, ndarray, ndarray][source]

Computes the time derivative of shape target requests based on measured values, reference trajectories, and control gains. It blends feedforward and feedback contributions using a time-varying blend factor, and applies damping to the error signal.

This function re-formulates “run_control_PI_with_scaled_out_damping” to not include a damping term.

Parameters:
  • t (float) – Current time [s].

  • dt (float) – Time step [s].

  • T_meas (np.ndarray) – Measured values of the shape targets at the current time [m].

  • T_err_prev (np.ndarray) – Previously filtered error signal [m].

  • T_hist_prev (np.ndarray) – Previous integral term (used for PI control) [m.s].

Returns:

  • dT_dt (np.ndarray) – Time derivative of the shape target requests [m/s].

  • T_err (np.ndarray) – Filtered error signal at the current time [m].

  • T_hist (np.ndarray) – Updated integral term for use in the next control step [m.s].

run_control_PI_with_P_damping(t: float, dt: float, T_meas: ndarray, T_err_prev: ndarray, T_hist_prev: ndarray) → Tuple[ndarray, ndarray, ndarray][source]

Computes the time derivative of shape target requests based on measured values, reference trajectories, and control gains. It blends feedforward and feedback contributions using a time-varying blend factor, and applies damping to the error signal.

Parameters:
  • t (float) – Current time [s].

  • dt (float) – Time step [s].

  • T_meas (np.ndarray) – Measured values of the shape targets at the current time [m].

  • T_err_prev (np.ndarray) – Previously filtered error signal (used for damping) [m].

  • T_hist_prev (np.ndarray) – Previous integral term (used for PI control) [m.s].

Returns:

  • dT_dt (np.ndarray) – Time derivative of the shape target requests [m/s].

  • T_err (np.ndarray) – Filtered error signal at the current time [m].

  • T_hist (np.ndarray) – Updated integral term for use in the next control step [m.s].

Notes

  • The error signal is filtered using a damping factor to smooth transitions.

  • The integral term is updated using trapezoidal integration.

  • The final output blends feedforward and feedback derivatives based on a dynamic blend factor.

update_interpolants() → None[source]

Recompute all interpolant functions from the current self.data.

This method rebuilds self.interpolants by applying either interpolate_spline or interpolate_step, and rebuilds the cached derivatives of every spline interpolant.