freegsnke.control_loop.useful_functions module

Module of functions required by the PCS in FreeGSNKE.

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.useful_functions.ConstantInterpolant(value: Any)[source]

Bases: object

Callable interpolant for a waveform whose value is constant in time.

It preserves the output shapes of the SciPy interpolants used for non-constant waveforms and provides a compatible derivative method.

Parameters:

value (array_like) – The constant value returned for any input time. Stored as a NumPy array.

value

The constant value returned by the interpolant.

Type:

np.ndarray

__init__(value: Any) → None[source]

Initialize the interpolant with a constant value.

Parameters:

value (array_like) – The constant value to store, converted to a NumPy array via np.asarray.

derivative(n: int = 1) → ConstantInterpolant[source]

Return the n-th derivative of this constant interpolant.

Parameters:

n (int, optional) – Order of the derivative. Must be non-negative. Default is 1.

Returns:

self if n == 0 (the value is unchanged), otherwise a new ConstantInterpolant whose value is zero everywhere, since the derivative of a constant is zero for any order greater than zero.

Return type:

ConstantInterpolant

Raises:

ValueError – If n is negative.

freegsnke.control_loop.useful_functions.PID(error_prop: float | ndarray | None = None, error_int: float | ndarray | None = None, error_deriv: float | ndarray | None = None, k_prop: float | ndarray | None = 0.0, k_int: float | ndarray | None = 0.0, k_deriv: float | ndarray | None = 0.0) → float | ndarray[source]

Compute a flexible PID controller output.

Any of the P, I, or D components may be omitted. A component contributes only if both its error term and its gain are provided (i.e. not None); otherwise it contributes zero to the output.

Parameters:
  • error_prop (float or array_like, optional) – Proportional error term. If None, the P contribution is zero.

  • error_int (float or array_like, optional) – Integral error term. If None, the I contribution is zero.

  • error_deriv (float or array_like, optional) – Derivative error term. If None, the D contribution is zero.

  • k_prop (float or array_like, optional) – Proportional gain. Default is 0. If explicitly set to None, the P contribution is zero regardless of error_prop.

  • k_int (float or array_like, optional) – Integral gain. Default is 0. If explicitly set to None, the I contribution is zero regardless of error_int.

  • k_deriv (float or array_like, optional) – Derivative gain. Default is 0. If explicitly set to None, the D contribution is zero regardless of error_deriv.

Returns:

The PID (or PI, PD, P, I, D, or ID) controller output. Arrays must be broadcast-compatible if array inputs are used.

Return type:

float or ndarray

Notes

  • A component contributes only if both its gain and error term are provided.

  • This function performs no time integration or differentiation; the caller

    must compute error_int and error_deriv externally.

freegsnke.control_loop.useful_functions.check_data_entry(data: dict[str, dict[str, Any]], key: str, controller_name: str) → None[source]

Validate that a specified sub-dictionary contains ‘times’ and ‘vals’ keys of equal length.

Parameters:
  • data (dict) – A dictionary where each value is expected to be a sub-dictionary containing at least ‘times’ and ‘vals’.

  • key (str) – The key in data corresponding to the sub-dictionary to validate.

  • controller_name (str) – A string corresponding to which controller is being checked.

Raises:

ValueError – If the specified key is missing from data, if ‘times’ or ‘vals’ is missing from the sub-dictionary, or if ‘times’ and ‘vals’ are not the same length.

freegsnke.control_loop.useful_functions.interpolate_spline(data: dict[str, Any]) → ConstantInterpolant | interp1d | UnivariateSpline[source]

Creates a spline interpolator for time-series data in ‘data’.

Parameters:

data (dict) – Dictionary with keys: - ‘times’: 1D array of time points - ‘vals’: 1D array of values at those time points (same length)

Returns:

f_interp – Callable function f(t) that returns the spline interpolated value at time t. For t < min(times), returns the first value. For t > max(times), returns the last value.

Return type:

function

Notes

Constant waveforms use ConstantInterpolant to avoid repeated SciPy interpolation overhead during control-loop execution.

freegsnke.control_loop.useful_functions.interpolate_step(data: dict[str, Any]) → ConstantInterpolant | interp1d | UnivariateSpline[source]

Creates a step-wise interpolator for time-series data using ‘previous’ value interpolation.

Parameters:

data (dict) – Dictionary with keys: - ‘times’: 1D array of time points - ‘vals’: 1D array of values at those time points (same length)

Returns:

f_interp – Callable function f(t) that returns the step-wise interpolated value at time t. For t < min(times), returns the first value. For t > max(times), returns the last value.

Return type:

function

Notes

Constant waveforms use ConstantInterpolant to avoid repeated SciPy interpolation overhead during control-loop execution.