freegsnke.circuit_eq_metal module
Defines the metal_currents object, which handles the circuit equations of all metal structures in the tokamak - both active PF coils and passive structures.
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.circuit_eq_metal.metal_currents(eq: object, flag_vessel_eig: bool, flag_plasma: bool, max_mode_frequency: float, max_internal_timestep: float, full_timestep: float, plasma_pts: ndarray | None = None, coil_resist: ndarray | None = None, coil_self_ind: ndarray | None = None, verbose: bool = True, passive_reflection_operator: ndarray | None = None, remove_odd_passive_modes: bool = True, symmetry_tolerance: float = 1e-08)[source]
Bases:
objectTime evolution model for electrical currents in conducting structures within a tokamak (active coils, passive vessel, and optional plasma coupling).
This class solves the dynamical circuit equations for metallic components using an implicit-Euler time integrator. It can operate in multiple modes:
Coil-only (vacuum vessel response)
Vessel eigenmode representation of passive structures
Fully coupled plasma–metal system (optional)
The system evolves currents in a reduced basis determined by: - physical active coil currents - vessel eigenmodes (if enabled) - optional plasma current coupling
Key features: - Construction of resistance and inductance operators - Optional diagonalisation of passive vessel dynamics - Optional coupling to plasma current evolution - Support for multi-step implicit time integration
- IdtoIvessel(Id: ndarray) ndarray[source]
Given the vector of currents in the eigenmode basis, this returns Ivessel, the vector of currents in all the metals.
- Parameters:
Id (np.ndarray) – Currents in the eigenmode basis.
- Returns:
Vessel currents (all metals).
- Return type:
np.ndarray
- IvesseltoId(Ivessel: ndarray) ndarray[source]
Given the vector of currents in the metals, this returns Id, the vector of currents in the eigenmodes basis.
- Parameters:
Ivessel (np.ndarray) – Vessel currents (all metals).
- Returns:
Currents in the eigenmode basis.
- Return type:
np.ndarray
- Mey(eq: object) ndarray[source]
Calculate the mutual inductances between plasma grid points and all coils.
Uses the plasma grid points (self.plasma_pts) included in the dynamics calculations and all vessel coils.
- Parameters:
eq (object) – FreeGSNKE equilibrium object.
- Returns:
Array of shape (n_coils, n_plasma_pts) of mutual inductances between all coils and the plasma grid points.
- Return type:
np.ndarray
- __init__(eq: object, flag_vessel_eig: bool, flag_plasma: bool, max_mode_frequency: float, max_internal_timestep: float, full_timestep: float, plasma_pts: ndarray | None = None, coil_resist: ndarray | None = None, coil_self_ind: ndarray | None = None, verbose: bool = True, passive_reflection_operator: ndarray | None = None, remove_odd_passive_modes: bool = True, symmetry_tolerance: float = 1e-08) None[source]
Initialise the dynamical evolution model for metallic currents.
This class solves the time evolution of currents in conducting structures (coils, vessel, and passive structures). It can also be used independently to solve vacuum vessel circuit equations in the absence of plasma.
- Parameters:
eq (object) – Initial equilibrium used to define grid geometry, machine configuration, and linearisation for the evolution solver. Changing machine geometry requires re-instantiation.
flag_vessel_eig (bool) – If True, include vessel eigenmodes in the circuit model.
flag_plasma (bool) – If True, include plasma coupling in the circuit equations. In this case, plasma_pts must be provided.
max_mode_frequency (float) – Maximum vessel eigenmode frequency to include (s⁻¹).
max_internal_timestep (float) – Internal timestep used by the implicit Euler solver.
full_timestep (float) – Physical timestep for advancing the system. May be split into multiple internal steps if larger than max_internal_timestep.
plasma_pts (np.ndarray or None, optional) – Array of shape (n_points, 2) with the (R, Z) coordinates of the grid points included in plasma evolution coupling (typically points inside the limiter).
coil_resist (np.ndarray or None, optional) – Resistances of all conducting elements (coils + passive structures). If None, default machine values are used.
coil_self_ind (np.ndarray or None, optional) – Mutual inductance matrix for all conducting elements. If None, default machine values are used.
passive_reflection_operator (ndarray, optional) – Reflection map used to classify passive-current parity and reflection-average the sampled metal-plasma mutual inductance. Odd-in-Z modes are excluded by default; see
remove_odd_passive_modes.remove_odd_passive_modes (bool, default=True) – If False, retain both passive-mode parities while still using the reflection operator to classify modes and symmetrise metal-plasma coupling. This is useful as a full-mode reference for an even-plasma evolution.
symmetry_tolerance (float, optional) – Maximum relative commutator error permitted between passive dynamics and reflection.
verbose (bool) – If True, enable diagnostic output during setup.
- Raises:
ValueError – If coil_resist or coil_self_ind are not compatible with the machine description.
- build_rm1l() None[source]
Construct the R⁻¹L coupling matrix for the circuit model.
This method builds the product of the inverse resistance matrix with the self-inductance (mutual inductance) matrix for all conducting elements (active coils and passive structures).
The resulting matrix is used in the formulation of the linear circuit evolution equations. The result is stored in self.rm1l_non_symm.
- forcing_term_eig_no_plasma(active_voltage_vec: ndarray, Iydot: ndarray | float = 0) ndarray[source]
Compute forcing term in eigenmode basis without plasma coupling.
This method constructs the right-hand side of the circuit equations in eigenmode coordinates when plasma effects are neglected. Only coil voltages are included.
- Parameters:
active_voltage_vec (np.ndarray) – Voltages applied to the active coils.
Iydot (np.ndarray or float, optional) – Unused placeholder for interface compatibility.
- Returns:
Forcing term expressed in the eigenmode basis.
- Return type:
np.ndarray
- forcing_term_eig_plasma(active_voltage_vec: ndarray, Iydot: ndarray) ndarray[source]
Compute forcing term in eigenmode basis including plasma coupling.
This method constructs the effective right-hand side of the circuit equations in eigenmode coordinates when plasma dynamics are included.
The forcing is built from: - applied coil voltages - inductive coupling to plasma current evolution
- Parameters:
active_voltage_vec (np.ndarray) – Voltages applied to the active coils.
Iydot (np.ndarray) – Time derivative of plasma current degrees of freedom.
- Returns:
Forcing term expressed in the eigenmode basis.
- Return type:
np.ndarray
- forcing_term_no_eig_no_plasma(active_voltage_vec: ndarray, Iydot: ndarray | float = 0) ndarray[source]
Compute forcing term in coil basis without plasma coupling.
This method constructs the right-hand side of the circuit equations in the physical (non-eigenmode) coil basis when plasma effects are neglected. Only applied coil voltages are included.
- Parameters:
active_voltage_vec (np.ndarray) – Voltages applied to the active coils.
Iydot (np.ndarray or float, optional) – Unused placeholder for interface consistency.
- Returns:
Forcing term in the physical coil basis.
- Return type:
np.ndarray
- forcing_term_no_eig_plasma(active_voltage_vec: ndarray, Iydot: ndarray) ndarray[source]
Compute forcing term in coil basis including plasma coupling.
This method builds the right-hand side of the circuit equations in the physical (non-eigenmode) coil basis, including inductive coupling to the plasma evolution.
- Parameters:
active_voltage_vec (np.ndarray) – Voltages applied to the active coils.
Iydot (np.ndarray) – Time derivative of plasma current degrees of freedom.
- Returns:
Forcing term in the physical coil basis.
- Return type:
np.ndarray
- initialize_for_eig(selected_modes_mask: ndarray | bool | None = None, mode_coupling_masks: tuple[ndarray, ndarray] | None = None, verbose: bool = True, fixed_n_passive_modes: int | None = None) ndarray[source]
Initialise the metal current system in eigenmode representation.
This method prepares the reduced-order circuit model when vessel eigenmodes are included. It constructs the mode transformation matrices, applies optional mode selection/reduction, and builds the system matrices required for the implicit time integration solver.
The system is transformed between physical currents and eigenmodes via: - P: transformation from eigenmodes to physical currents - Pm1: inverse transformation (restricted to selected modes)
- Parameters:
selected_modes_mask (np.ndarray or bool or None, optional) – If None, the mask is constructed using mode_coupling_masks. If False, all modes are used. If a boolean array, it is applied to the currently retained modes to reduce them further.
mode_coupling_masks (tuple[np.ndarray, np.ndarray] or None, optional) –
Pair of boolean masks used to:
include strongly coupled modes
exclude weakly coupled modes
Only used when selected_modes_mask is None.
verbose (bool, optional) – If True, print diagnostic information about mode reduction.
fixed_n_passive_modes (int or None, optional) – Retain exactly this many lowest-frequency passive modes. This is a timescale-only selection and cannot be combined with coupling masks.
- Returns:
Mask selecting the retained modes relative to the mode basis that was available on entry. Internal mode transformations and the time integrator are updated as side effects.
- Return type:
ndarray of bool
Notes
Updates the internal solver state, including the mode selection masks, the transformation matrices (P, Pm1), the system matrix (Lambdam1), the time integrator solver and the forcing term selection.
- initialize_for_no_eig() None[source]
Initialise the metal current system without eigenmode decomposition.
This method constructs and solves the full circuit equations in the physical current basis, without projecting onto vessel eigenmodes.
The governing equation is: Mmatrix · dI/dt + Rmatrix · I = F
where: - Mmatrix is the full mutual inductance matrix - Rmatrix is the diagonal resistance matrix
Updates the internal solver and forcing term configuration.
- make_selected_mode_mask(mode_coupling_masks: tuple[ndarray, ndarray] | None, verbose: bool, fixed_n_passive_modes: int | None = None) ndarray[source]
Build selection mask for vessel normal modes used in circuit equations.
This method selects which passive structure eigenmodes are included in the reduced-order circuit model based on a frequency cutoff and optional plasma-coupling criteria.
Active coil variables are always included. Passive vessel modes are selected in one of two ways:
if fixed_n_passive_modes is provided, retain exactly that many modes, starting with the lowest-frequency (longest-timescale) modes;
otherwise, apply self.max_mode_frequency and then, if provided, the plasma-coupling inclusion and exclusion masks.
Coupling masks describe the plasma response at the equilibrium where they were calculated. If the plasma is expected to move or change shape substantially, the retained modes should be validated at representative equilibria or selected using equilibrium-independent timescale criteria.
- Parameters:
mode_coupling_masks (tuple[np.ndarray, np.ndarray] or None) –
Optional pair of boolean masks used to:
reintroduce strongly coupled modes
remove weakly coupled modes
These masks are local to the equilibrium used to calculate them.
verbose (bool) – If True, print diagnostic information about mode selection.
fixed_n_passive_modes (int or None, optional) – If provided, retain exactly this many lowest-frequency passive modes. mode_coupling_masks must be None in this mode.
- Returns:
Selection mask expressed in the parity-available mode basis. The full-basis selection remains available as
self.selected_modes_mask.- Return type:
ndarray of bool
- Raises:
ValueError – If fixed_n_passive_modes is combined with mode_coupling_masks, or lies outside the range zero to the number of available passive modes.
Notes
Updates the attributes self.selected_modes_mask and self.n_independent_vars.
- reset_active_coil_resistances(active_coil_resistances: ndarray) None[source]
Update the resistances of the active coils and rebuild derived system matrices.
This method replaces the active-coil portion of the full resistance vector while keeping passive/vessel resistances unchanged. It then updates all dependent operators used in the circuit evolution model.
The update triggers a rebuild of: - the full resistance vector - the inverse resistance matrix - the resistance–inductance coupling operator - the reduced system matrix used in vessel mode dynamics
- Parameters:
active_coil_resistances (np.ndarray) – Updated resistances for the active coils only (length = n_active_coils).
- reset_timesteps(max_internal_timestep: float, full_timestep: float) None[source]
Update solver time-stepping parameters.
This method resets both the internal solver timestep and the external evolution timestep used to advance the circuit equations. If the full timestep exceeds the internal timestep, multiple substeps are performed automatically by the solver.
- Parameters:
max_internal_timestep (float) – Maximum timestep used internally by the implicit Euler solver.
full_timestep (float) – External timestep used to advance the system in time.
- stepper(It: ndarray, active_voltage_vec: ndarray, Iydot: ndarray | float = 0) ndarray[source]
Step the circuit equation forward in time.
- Parameters:
It (np.ndarray) – Currents at time t.
active_voltage_vec (np.ndarray) – Vector of active coil voltages.
Iydot (np.ndarray or float, optional) – Vector of rate of change of plasma currents. Defaults to 0.
- Returns:
Currents at time t+dt.
- Return type:
np.ndarray