freegsnke.linear_solve module

Implements the object that advances the linearised dynamics.

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.linear_solve.linear_solver(coil_numbers: tuple[int, int], Lambdam1: ndarray, P: ndarray, Pm1: ndarray, Rm1: ndarray, Mey: ndarray, plasma_norm_factor: float, plasma_resistance_1d: ndarray, max_internal_timestep: float = 0.0001, full_timestep: float = 0.0001)[source]

Bases: object

Linearised plasma–metal circuit solver.

This class provides the interface between the linearised system of coupled plasma and metal circuit equations and the implicit-Euler time integrator. It constructs the matrices required for time integration, computes linear growth rates, and advances the linearised dynamical system in time.

The metal circuit equations are expressed in a reduced basis defined by the mode decomposition matrix P, in which the active coils remain in the physical current basis and the passive structures are represented by vessel eigenmodes.

The solver requires the Jacobian of the plasma current distribution with respect to the independent current variables, dIy/dI, which determines the linear plasma response.

__init__(coil_numbers: tuple[int, int], Lambdam1: ndarray, P: ndarray, Pm1: ndarray, Rm1: ndarray, Mey: ndarray, plasma_norm_factor: float, plasma_resistance_1d: ndarray, max_internal_timestep: float = 0.0001, full_timestep: float = 0.0001) → None[source]

Initialise the linearised circuit solver.

Precomputes matrices describing the coupling between plasma currents and metal circuits in the reduced modal basis, and creates the implicit-Euler integrator used to advance the linearised system.

Parameters:
  • coil_numbers (tuple[int, int]) – Tuple (n_active_coils, n_coils) giving the number of active coils and the total number of conducting elements.

  • Lambdam1 (np.ndarray) – State matrix of the metal circuit equations in the reduced basis. Active coils remain in the physical current basis, while passive structures are represented by vessel eigenmodes.

  • P (np.ndarray) – Change-of-basis matrix mapping currents from the reduced modal basis to the physical conductor basis.

  • Pm1 (np.ndarray) – Inverse of P, mapping currents from the physical conductor basis to the reduced modal basis.

  • Rm1 (np.ndarray) – Inverse resistance matrix of all conducting elements. This is typically diagonal.

  • Mey (np.ndarray) – Mutual inductance matrix between plasma current elements and metal conductors (active coils and passive structures).

  • plasma_norm_factor (float) – Scaling factor applied to the plasma current variable to improve conditioning and keep its magnitude comparable to metal currents.

  • plasma_resistance_1d (np.ndarray) – Effective plasma resistance associated with each plasma current element in the reduced plasma domain.

  • max_internal_timestep (float, optional) – Maximum timestep used internally by the implicit-Euler solver. Longer requested timesteps are subdivided into smaller steps.

  • full_timestep (float, optional) – External timestep associated with a single solver advance.

build_Mmatrix() → None[source]

Initialise the pseudo-inductance matrix of the problem.

The problem is Mdot{x} + Rx = forcing, with M built using the linearisation Jacobian.

Lambda^-1 + P^-1R^-1Mey A P^-1R^-1Mey B

M = M0 + dM = ( )

J(Myy A + MyeP)/Rp J Myy B/Rp

This also builds the forcing:

P^-1R^-1 Voltage P^-1R^-1Mey

forcing = ( ) - ( ) C dot{theta}

0 J Myy/Rp

where A = dIy/dId

B = dIy/dIp C = dIy/plasmapars

The matrices M0matrix, dMmatrix, Mmatrix and (if dIydtheta is not None) forcing_pars_matrix are stored as attributes. All inputs are taken from object attributes.

calculate_linear_growth_rate() → None[source]

Find the unstable modes and growth rates from the eigenvalues of “M”.

Eigenvalues of the Mmatrix attribute with negative real part correspond to unstable modes. Stores all_timescales, all_modes, instability_timescale, growth_rates (plasma + circuits), the *_const_Ip equivalents (circuits only, constant plasma current) and the normalised unstable_modes of the constant-Ip problem.

calculate_pseudo_rigid_projections(dRZdI: ndarray) → ndarray[source]

Project the unstable modes on the current vectors that best isolate R or Z motion.

Parameters:

dRZdI (np.ndarray) – Jacobian of Rcurrent and Zcurrent shifts wrt the modes, as calculated in nonlinear_solve

Returns:

Array of shape (n_unstable_modes, 2). proj[i,0] is the scalar product of the unstable mode i on the vector of modes resulting in an Rcurrent shift proj[i,1] is the scalar product of the unstable mode i on the vector of modes resulting in an Zcurrent shift

Return type:

np.ndarray

calculate_stability_margin() → None[source]

Calculate the stability margin parameter.

See https://iopscience.iop.org/article/10.1088/0029-5515/45/8/021. Uses the M0matrix and dMmatrix attributes and stores all_stability_margins and stability_margin (the positive, i.e. unstable, values).

reset_plasma_resistivity(plasma_resistance_1d: ndarray) → None[source]

Update the plasma resistivity profile used by the linearised solver.

Replaces the vector of effective plasma resistances associated with the reduced plasma domain. This automatically invalidates the current linearisation point so that system matrices will be rebuilt using the updated resistivity.

Parameters:

plasma_resistance_1d (np.ndarray) – Vector of effective plasma resistance coefficients for all grid points in the reduced plasma domain. Each entry represents the geometric resistance factor (2π * resistivity divided by cell area).

reset_timesteps(max_internal_timestep: float, full_timestep: float) → None[source]

Update the timesteps used by the implicit-Euler integrator.

Parameters:
  • max_internal_timestep (float) – Maximum internal timestep used by the implicit-Euler solver. Larger timesteps are subdivided into steps no larger than this value.

  • full_timestep (float) – External timestep associated with a single advance of the circuit equations.

set_linearization_point(dIydI: ndarray | None, dIydtheta: ndarray | None, hatIy0: ndarray | None, Myy_hatIy0: ndarray | None) → None[source]

Set or update the linearisation point for the coupled plasma–metal system.

This method defines the equilibrium state around which the system is linearised and rebuilds the system matrices used by the implicit-Euler solver.

Parameters:
  • dIydI (np.ndarray or None) – Jacobian of plasma cell currents with respect to independent metal currents (active coils, vessel modes, and normalised total plasma current). Typically computed from a Grad–Shafranov solve using finite differences.

  • dIydtheta (np.ndarray or None) – Jacobian of plasma cell currents with respect to plasma profile parameterisation variables.

  • hatIy0 (np.ndarray or None) – Normalised equilibrium plasma current distribution over the reduced plasma domain. This vector is scaled to sum to one.

  • Myy_hatIy0 (np.ndarray or None) – Precomputed product of the plasma–plasma coupling matrix with the equilibrium current distribution, provided by the plasma coupling handler.

Notes

Any argument set to None is left unchanged from the previous linearisation point.

This call rebuilds the system matrix and reinitialises the implicit-Euler solver using the updated linearised dynamics.

stepper(It: ndarray, active_voltage_vec: ndarray, dtheta_dt: ndarray) → ndarray[source]

Execute the time advancement using the implicit_euler instance.

Parameters:
  • It (np.ndarray) – vector of all independent currents that are solved for by the linearised problem, in terms of normal modes: (active currents, vessel normal modes, total plasma current divided by normalisation factor)

  • active_voltage_vec (np.ndarray) – voltages applied to the active coils

  • dtheta_dt (np.ndarray) – Vector of plasma current density profile parameters derivatives with respect to time.

Returns:

Itpdt – Updated state vector after one full implicit-Euler timestep.

Return type:

np.ndarray