freegsnke.control_loop.vc_provider module

Module to compute virtual circuits (VCs) and interface with the PCS class in a simulation.

Provides two ways to obtain VCs: - Compute a VC on demand at a given simulation time, using FreeGSNKE’s built-in

VirtualCircuitHandling functionality, for “real time updates”.

  • Generate a fixed schedule of VCs up front, to pass directly to the PCS class.

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.vc_provider.VCGenerator(solver: object, target_calculator: Callable[[object], ndarray], target_names: list[str], targets_ctrl: list[str], targets_calc: list[str], coils_calc: list[str], coils: list[str] | None = None, vc_update_rate: float = 0.0, n_vc_workers: int = 1, verbose: bool = False)[source]

Bases: VirtualCircuitProvider

Virtual Circuit (VC) generator based on FreeGSNKE’s VirtualCircuitHandling infrastructure to interface with PCS class in the vc_generator argument.

See each method’s own docstring for details: get_targets, get_vc, get_inputs_from_eq, and generate_fixed_schedule.

__init__(solver: object, target_calculator: Callable[[object], ndarray], target_names: list[str], targets_ctrl: list[str], targets_calc: list[str], coils_calc: list[str], coils: list[str] | None = None, vc_update_rate: float = 0.0, n_vc_workers: int = 1, verbose: bool = False) → None[source]

Initialise the VC generator and bind it to a solver.

This sets up a VirtualCircuitHandling instance and registers the solver object required for VC computations.

Parameters:
  • solver (object) – A FreeGSNKE solver instance used internally by VirtualCircuitHandling to compute virtual circuits.

  • target_calculator (Callable[[object], np.ndarray]) – Function to compute array of shape targets from a given equilibrium. Same as the target_calculator used by VirtualCircuitHandling.calculate_VC.

  • target_names (list[str]) – list of target names associated with the outputs of target_calculator.

  • targets_ctrl (list[str]) – List of targets to be controlled using the emulated VC’s. Must be subset of ctrl_targets, and subset/equal to targets_calc. Those not defined in this list will be taken from waveform-defined VCs.

  • targets_calc (list[str]) – List of targets to be used when performing pseudoinverse of jacobian when calculating the emulated VC.

  • coils_calc (list[str]) – List of coils to use in emulated VC compuation. These are coils to use in computing shape sensitivity matrix.

  • coils (list[str], optional) – Full list of coils defining the output matrix column ordering, used only by generate_fixed_schedule. Not needed if this generator is only used via get_vc/get_inputs_from_eq (e.g. through VirtualCircuitsController), where the full coil list is instead supplied per-call by the controller.

  • vc_update_rate (float, optional) – How often, in seconds, VirtualCircuitsController.run_control should recompute the emulated VC via get_vc. Default is 0.0, i.e. a new VC is computed at every control step.

  • n_vc_workers (int, optional) – Number of worker processes used to build independent virtual-circuit shape-matrix columns. Default is 1, retaining serial calculation.

  • verbose (bool, optional) – If True, print the configuration used for VC computations. Default is False.

generate_fixed_schedule(times: list[float], eq_list: list[object], profile_list: list[object], tikhonov_lambda: ndarray | None = None, verbose: bool = False) → dict[source]

Generate the shape-target virtual circuit (VC) entries for a fixed schedule, in the format expected by the circuits_data argument of VirtualCircuitsController/PlasmaControlSystem.

For each timestamp in times, a VC matrix is computed from the corresponding equilibrium/profile pair in eq_list/profile_list, using self.targets_calc and self.coils_calc (set when this VCGenerator was initialised) for the underlying sensitivity calculation and inversion. The resulting per-coil coefficients for each target in self.target_names (the full set of targets this generator was initialised with) are stored over time, with targets not in self.targets_ctrl left as all-zero arrays (i.e. reported to PCS but not actively controlled).

Note that this method only builds the shape-target entries. The plasma-current VC and any feedforward coil drives ("<coil>_ref") are not shape targets computed here and must be added separately before the result is used as circuits_data.

Parameters:
  • times (list[float]) – Timestamps for the start of each VC phase.

  • eq_list (list[object]) – Equilibria used to compute the VC for each phase (one entry per timestamp in times).

  • profile_list (list[object]) – Equilibrium profiles used to compute the VC for each phase (one entry per timestamp in times).

  • tikhonov_lambda (np.ndarray, optional) – Regularisation parameter(s) passed through to get_vc (and in turn to VirtualCircuitHandling.calculate_VC) for every phase in the schedule.

  • verbose (bool, optional) – If True, print progress messages as each phase is computed. Default is False.

Returns:

schedule –

One entry per target in self.target_names, each a dict with:
”times”np.ndarray, shape (len(times),)

the schedule timestamps for this target

”vals”np.ndarray, shape (len(times), len(self.coils))

that target’s coil coefficients at each scheduled time

Targets not in self.targets_ctrl are left with all-zero “vals”. Plus a "coil_order" entry giving self.coils.

Return type:

dict

Raises:

ValueError – If self.coils was not provided when this VCGenerator was initialised; if self.targets_ctrl is not a subset of self.target_names or of self.targets_calc; if self.targets_calc is not a subset of self.target_names; if self.coils_calc is not a subset of self.coils; if eq_list/profile_list do not match times in length.

get_inputs_from_eq(eq: object, profiles: object) → tuple[object, object][source]

Package equilibrium and profile data into the input format expected by get_vc.

Parameters:
  • eq (object) – Equilibrium object.

  • profiles (object) – Plasma profile data.

Returns:

(eq, profiles)

Return type:

tuple

get_targets(outputs: list[str], input_data: tuple) → ndarray[source]

Evaluate the current values of a set of targets for a given equilibrium.

Parameters:
  • outputs (list[str]) – Names of the targets to evaluate. Each name must be a key in self.target_calculator_dict.

  • input_data (tuple) – Tuple of inputs as returned by get_inputs_from_eq, i.e. (equilibrium, profiles). Only the equilibrium (first element) is used here.

Returns:

1D array of shape (len(outputs),) containing the evaluated target values, in the same order as outputs.

Return type:

np.ndarray

get_vc(targets: list[str], targets_calc: list[str], coils: list[str], coils_calc: list[str], input_data: tuple, tikhonov_lambda: ndarray | None = None, verbose: bool = False) → ndarray[source]

Compute the virtual circuit (VC) matrix for a given set of targets and coils. Only a subset of coils may be used for the VC computation, but the returned matrix is expanded to include all coils provided in coils.

Parameters:
  • targets (list[str]) – User-facing names of targets to include in the returned matrix (order is preserved). Must be a subset of targets_calc.

  • targets_calc (list[str]) – Targets actually used in the VC calculation (sensitivity calculation and inversion). May be a superset of targets, e.g. if extra targets are needed to condition the inversion.

  • coils (list[str]) – Full list of coils defining the output matrix row ordering.

  • coils_calc (list[str]) – Subset of coils actually used in the VC calculation.

  • input_data (tuple) – Tuple of inputs required for VC computation. Obtained from .get_inputs() method. Expected to be (equilibrium, profiles).

  • tikhonov_lambda (np.ndarray, optional) – Regularisation parameter(s) passed through to VirtualCircuitHandling.calculate_VC.

  • verbose (bool, optional) – If True, print progress messages while computing the VC. Default is False.

Returns:

vc_matrix – Expanded virtual circuit matrix of shape (len(coils), len(targets))

Return type:

np.ndarray

Notes

As a side effect, stores the shape (Jacobian) matrix used to compute this VC as self.latest_shape_matrix, of shape (len(targets_calc), len(coils_calc)) – i.e. not expanded/reordered to match coils/targets like the returned vc_matrix is.

Raises:

ValueError – If targets_calc contains a target with no corresponding entry in self.target_calculator_dict; if input_data is None.

class freegsnke.control_loop.vc_provider.VirtualCircuitProvider(targets_ctrl: list[str], targets_calc: list[str], coils_calc: list[str], vc_update_rate: float = 0.0, verbose: bool = False)[source]

Bases: ABC

Defines the interface for a Virtual Circuit provider.

__init__(targets_ctrl: list[str], targets_calc: list[str], coils_calc: list[str], vc_update_rate: float = 0.0, verbose: bool = False) → None[source]

Store the coil/target configuration used for every VC computation made by this provider.

Parameters:
  • targets_ctrl (list[str]) – List of targets to be controlled using the emulated VCs. Must be a subset of ctrl_targets, and subset/equal to targets_calc. Those not defined in this list will be taken from waveform-defined VCs.

  • targets_calc (list[str]) – List of targets to be used when performing the pseudoinverse of the Jacobian when calculating the emulated VC.

  • coils_calc (list[str]) – List of coils to use in the emulated VC computation. These are the coils used in computing the shape sensitivity matrix.

  • vc_update_rate (float, optional) – How often, in seconds, VirtualCircuitsController.run_control should recompute the emulated VC via get_vc. Default is 0.0, i.e. a new VC is computed at every control step.

  • verbose (bool, optional) – If True, print the configuration used for VC computations. Default is False.

abstract get_inputs_from_eq(eq: object, profiles: object) → tuple | ndarray | None[source]

Method to obtain input_data from equilibrium and profiles

Parameters:
  • eq (object) – Equilibrium object.

  • profiles (object) – Plasma profile data.

Returns:

input_data – data formatted to pass to input_data argument of get_vc()

Return type:

tuple or np.ndarray

abstract get_vc(targets: list[str], targets_calc: list[str], coils: list[str], coils_calc: list[str], input_data: tuple | ndarray, tikhonov_lambda: ndarray | None = None, verbose: bool = False) → ndarray | None[source]

Gets a Virtual Circuit for the given timestamp and observables requested from the registry.

Parameters:
  • targets (list[str]) – User-facing names of targets to include in the returned matrix (order is preserved). Must be a subset of targets_calc.

  • targets_calc (list[str]) – Targets actually used in the VC calculation (sensitivity calculation and inversion). May be a superset of targets, e.g. if extra targets are needed to condition the inversion.

  • coils (list[str]) – Full list of coils defining the output matrix row ordering.

  • coils_calc (list[str]) – Subset of coils actually used in the VC calculation.

  • input_data (tuple) – Tuple of inputs required for VC computation, obtained from .get_inputs() method.

  • tikhonov_lambda (np.ndarray, optional) – Regularisation parameter(s) passed through to VirtualCircuitHandling.calculate_VC.

  • verbose (bool, optional) – If True, print progress messages while computing the VC. Default is False.

Returns:

vc – virtual circuit matrix to be used by the control voltages class or None if no virtual circuit could be obtained or constructed.

Return type:

np.ndarray | None