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
VirtualCircuitHandlingfunctionality, 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:
VirtualCircuitProviderVirtual Circuit (VC) generator based on FreeGSNKE’s
VirtualCircuitHandlinginfrastructure 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, andgenerate_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
VirtualCircuitHandlinginstance and registers the solver object required for VC computations.- Parameters:
solver (object) – A FreeGSNKE solver instance used internally by
VirtualCircuitHandlingto 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 viaget_vc/get_inputs_from_eq(e.g. throughVirtualCircuitsController), where the full coil list is instead supplied per-call by the controller.vc_update_rate (float, optional) – How often, in seconds,
VirtualCircuitsController.run_controlshould recompute the emulated VC viaget_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_dataargument ofVirtualCircuitsController/PlasmaControlSystem.For each timestamp in
times, a VC matrix is computed from the corresponding equilibrium/profile pair ineq_list/profile_list, usingself.targets_calcandself.coils_calc(set when thisVCGeneratorwas initialised) for the underlying sensitivity calculation and inversion. The resulting per-coil coefficients for each target inself.target_names(the full set of targets this generator was initialised with) are stored over time, with targets not inself.targets_ctrlleft 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 ascircuits_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 toVirtualCircuitHandling.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_ctrlare left with all-zero “vals”. Plus a"coil_order"entry givingself.coils.- One entry per target in
- Return type:
dict
- Raises:
ValueError – If
self.coilswas not provided when thisVCGeneratorwas initialised; ifself.targets_ctrlis not a subset ofself.target_namesor ofself.targets_calc; ifself.targets_calcis not a subset ofself.target_names; ifself.coils_calcis not a subset ofself.coils; ifeq_list/profile_listdo not matchtimesin 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 asoutputs.- 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 matchcoils/targetslike the returnedvc_matrixis.- Raises:
ValueError – If
targets_calccontains a target with no corresponding entry inself.target_calculator_dict; ifinput_datais 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:
ABCDefines 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 totargets_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_controlshould recompute the emulated VC viaget_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