freegsnke.control_loop.coil_activation_category module

Module implementing time‑dependent coil activation scheduling for 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.coil_activation_category.CoilActivationController(data: dict[str, dict[str, Any]], active_coils: list[str])[source]

Bases: object

A controller class for managing time-dependent coil activation times.

Parameters:
  • data (dict) –

    A nested dictionary containing coil activation waveforms for the controller. The required keys for both spline-based and step-based waveforms are:

    • Spline keys: “<coil>_activation”

    • Step keys:

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

  • active_coils (list of str) – The list of active coils being used.

active_coils

The list of active coils being used.

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

__init__(data: dict[str, dict[str, Any]], active_coils: list[str]) → None[source]

Initialise the coil activation controller.

Validates that the required time-series entries are present in data for every coil in active_coils, stores a reference to the data, and builds the spline/step interpolants used to evaluate coil activation at arbitrary times.

Parameters:
  • data (dict) – Dictionary of time-series entries. For each key required by this controller (see keys_to_spline and keys_to_step), the corresponding entry must be present and in the format expected by check_data_entry (i.e. containing ‘times’ and ‘vals’ arrays of matching length).

  • active_coils (list of str) – Names of the coils this controller manages. For each coil, a corresponding “<coil>_activation” entry must exist in data.

active_coils

Stored copy of active_coils.

Type:

list of str

keys_to_spline

Data keys that will be spline-interpolated. Currently unused (empty) for this controller.

Type:

list of str

keys_to_step

Data keys that will be step-interpolated: one “<coil>_activation” entry per coil in active_coils.

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.

  • If deriv=True, the method calls .derivative() on the interpolant before evaluation.

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 (step 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.

  • 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, active_coil_resists: ndarray) → ndarray[source]

Compute effective coil resistances at a given time step.

This function extracts coil activation values at time t and scales the base resistances accordingly. Coils that are inactive (activation = 0) are assigned a very large resistance to effectively disable them in the control model.

Parameters:
  • t (float) – Current time at which to evaluate coil activations.

  • dt (float) – Time step size (currently unused, but kept for interface consistency).

  • active_coil_resists (numpy.ndarray) – Array of active coil resistances when coils are switched on [Ohms].

Returns:

Array of effective coil resistances, where inactive coils are set to a large resistance value (1e12).

Return type:

numpy.ndarray

update_interpolants() → None[source]

Recompute all interpolant functions from the current self.data.

This method clears the existing self.interpolants dictionary and rebuilds it by applying either interpolate_spline or interpolate_step depending on whether each key belongs to self.keys_to_spline or self.keys_to_step.