Source code for freegsnke.inverse

"""
Implements the optimiser for the inverse Grad-Shafranov problem.

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/>.
"""

from __future__ import annotations

import itertools
from typing import TYPE_CHECKING

import cvxpy
import numpy as np
from scipy import interpolate

if TYPE_CHECKING:
    from matplotlib.axes import Axes


def _solve_regularized_lstsq(
    A: np.ndarray, b: np.ndarray, regularization: np.ndarray
) -> np.ndarray:
    """
    Solve ``min ||A x - b||² + xᵀ diag(regularization) x`` in augmented form.

    Parameters
    ----------
    A : np.ndarray
        Least-squares matrix of shape (n_constraints, n_variables).
    b : np.ndarray
        Right-hand side vector of length n_constraints.
    regularization : np.ndarray
        Non-negative Tikhonov weights, one per variable (length n_variables).

    Returns
    -------
    np.ndarray
        Least-squares solution x of length n_variables.

    Raises
    ------
    ValueError
        If `regularization` does not have one value per variable, or contains
        negative or non-finite values.
    """
    regularization = np.asarray(regularization)
    if regularization.shape != (A.shape[1],):
        raise ValueError(
            "Regularization must contain one value per least-squares variable."
        )
    if not np.all(np.isfinite(regularization)) or np.any(regularization < 0):
        raise ValueError("Regularization values must be finite and non-negative.")

    augmented_A = np.vstack((A, np.diag(np.sqrt(regularization))))
    augmented_b = np.concatenate((b, np.zeros(A.shape[1])))
    return np.linalg.lstsq(augmented_A, augmented_b, rcond=None)[0]


[docs] class Inverse_optimizer: """This class implements a gradient based optimiser for the coil currents, used to perform (static) inverse Grad-Shafranov solves. """
[docs] def __init__( self, isoflux_set: list | np.ndarray | None = None, null_points: list | np.ndarray | None = None, null_points_2nd_order: list | np.ndarray | None = None, psi_vals: list | np.ndarray | None = None, coil_current_limits: list | None = None, psi_norm_limits: list | np.ndarray | None = None, *, weight_isoflux: float = 1.0, weight_nulls: float = 1.0, weight_psi: float = 1.0, mu_coils: float = 1e5, mu_psi_norm: float = 1e6, ) -> None: """ Initialise magnetic constraint definitions for inverse equilibrium optimisation. This object stores magnetic configuration constraints that are enforced during inverse Grad-Shafranov optimisation. Constraints supported include: • Isoflux constraints • Magnetic axis (O-point) and X-point constraints • Direct flux value constraints • Coil current bound constraints • Normalised flux inequality constraints These constraints are used to construct objective and penalty terms during nonlinear current optimisation. Parameters ---------- isoflux_set : list or np.ndarray, optional Isoflux constraints. Each constraint set is ``[Rcoords, Zcoords, weights]`` (or a list of such sets), where `Rcoords` and `Zcoords` are 1D arrays of coordinates and `weights` is an optional 1D array (ones if not provided) used to increase/decrease the influence of each point on the solution. All points within a set are required to share the same poloidal flux. null_points : list or np.ndarray, optional Magnetic null point (X-point and O-point) constraints, specified as ``[Rcoords, Zcoords]``. null_points_2nd_order : list or np.ndarray, optional Second-order magnetic null point constraints (typically used for snowflake divertor configurations), specified as ``[Rcoords, Zcoords]``. Do not repeat coordinates already provided in `null_points`. psi_vals : list or np.ndarray, optional Direct flux value constraints ``[Rcoords, Zcoords, psi_values]``, where the three entries have identical flattened shapes. Enforces ψ(R,Z) = ψ_target at the specified locations. coil_current_limits : list, optional Hard inequality bounds on coil currents, ``[upper_limits, lower_limits]``, where each entry is a list with length equal to the number of controllable coils, e.g. ``[[Imax1, Imax2, ...], [Imin1, Imin2, ...]]``. Use None within a list to indicate no bound. psi_norm_limits : list or np.ndarray, optional Normalised flux inequality constraints, with one row per constraint ``[Rcoord, Zcoord, normalised_psi_value, constraint_sign]``. A `constraint_sign` of 1 enforces ψ_norm ≥ ψ_target and -1 enforces ψ_norm ≤ ψ_target, where ψ_norm = (ψ - ψ_axis) / (ψ_boundary - ψ_axis). weight_isoflux : float, optional Weight of the isoflux constraints in the least-squares optimisation problem (default = 1.0). weight_nulls : float, optional Weight of the null point (X-point) constraints in the least-squares optimisation problem (default = 1.0). weight_psi : float, optional Weight of the psi value constraints in the least-squares optimisation problem (default = 1.0). mu_coils : float, optional Penalty factor applied to violation of the coil current limits (default = 1e5). mu_psi_norm : float, optional Penalty factor applied to violation of the normalised psi limits (default = 1e6). Notes ----- Increasing the weights/penalty factors causes the least-squares optimisation to prioritise satisfying the higher-weighted/penalised constraints. Constraint weights multiply residual rows before squaring, so their contribution to the objective scales with the square of the supplied value. """ # ------------------------------------------------------------ # Isoflux constraint processing # ------------------------------------------------------------ self.isoflux_set = isoflux_set self.isoflux_weight = [] if isoflux_set is not None: # Test if structure is already nested numeric arrays # If indexing succeeds, we assume isoflux_set contains # structured coordinate arrays. try: type(self.isoflux_set[0][0][0]) self.isoflux_set = [] for isoflux in isoflux_set: iso_set, weights = self._extract_isoflux_constraints_weights( np.array(isoflux) ) self.isoflux_set.append(iso_set) self.isoflux_weight.append(weights) # rebuild as list of numpy arrays for numerical stability except (TypeError, IndexError): self.isoflux_set = np.array(self.isoflux_set)[np.newaxis] self.isoflux_weight = np.ones(self.isoflux_set.shape[1])[np.newaxis] # number of isoflux points per constraint set self.isoflux_set_n = [len(isoflux[0]) for isoflux in self.isoflux_set] # number of isoflux points per constraint set self.isoflux_set_n = [len(isoflux[0]) for isoflux in self.isoflux_set] # ------------------------------------------------------------ # Null point constraints (X-points, O-points) # ------------------------------------------------------------ self.null_points = null_points if self.null_points is not None: self.null_points = np.array(self.null_points) # ------------------------------------------------------------ # Second-order null point constraints (snowflakes) # ------------------------------------------------------------ self.null_points_2nd_order = null_points_2nd_order if self.null_points_2nd_order is not None: self.null_points_2nd_order = np.array(self.null_points_2nd_order) # ------------------------------------------------------------ # Direct flux value constraints # These impose ψ(R,Z) = ψ_target at specified locations # ------------------------------------------------------------ self.psi_vals = psi_vals if self.psi_vals is not None: Rcoords, Zcoords, psi_values = self.psi_vals Rcoords = np.asarray(Rcoords).reshape(-1) Zcoords = np.asarray(Zcoords).reshape(-1) psi_values = np.asarray(psi_values).reshape(-1) self.psi_vals = [Rcoords, Zcoords, psi_values.reshape(-1)] # Store magnitude scale of flux constraints # Used for normalisation in optimisation loss self.norm_psi_vals = np.linalg.norm(self.psi_vals[2]) # ------------------------------------------------------------ # Coil current bounds and penalty regularisation weights # ------------------------------------------------------------ self.coil_current_limits = coil_current_limits self.mu_coils = mu_coils # ------------------------------------------------------------ # Normalised psi bounds and penalty regularisation weights # ------------------------------------------------------------ self.psi_norm_limits = ( None if psi_norm_limits is None else np.array(psi_norm_limits) ) self.mu_psi_norm = mu_psi_norm # ------------------------------------------------------------ # Weighting equality constraint classes # ------------------------------------------------------------ self.weight_isoflux = weight_isoflux self.weight_nulls = weight_nulls self.weight_psi = weight_psi
@staticmethod def _extract_isoflux_constraints_weights( isoflux_set: np.ndarray, ) -> tuple[np.ndarray, np.ndarray]: """ Extract isoflux constraint locations and associated weights. The input array encodes a set of isoflux constraints in one of two formats: - (3, N): first two rows are constraint coordinates, third row contains weights - (2, N): constraint coordinates only, with unit weights assumed Parameters ---------- isoflux_set : np.ndarray Array of isoflux constraints with shape (2, N) or (3, N), where N is the number of constraint points. Returns ------- constraints : np.ndarray Constraint coordinates with shape (2, N). weights : np.ndarray Weights associated with each constraint point, shape (N,). Defaults to an array of ones if not provided in input. Raises ------ ValueError If input does not have shape (2, N) or (3, N). """ if isoflux_set.shape[0] == 3: return isoflux_set[0:2, :], isoflux_set[2, :] elif isoflux_set.shape[0] == 2: return isoflux_set, np.ones(isoflux_set.shape[1]) raise ValueError( f"Expected isoflux set to be of shape (2, N) or (3, N) not {isoflux_set.shape}" )
[docs] def prepare_for_solve(self, eq: object) -> None: """ Prepare constraint solver quantities after object instantiation. This method must be called before performing optimisation or nonlinear solve steps. It builds: • Control coil selection masks • Green's function response operators These quantities are required for computing: - Constraint residuals - Gradient/Jacobian approximations - Loss function evaluations during optimisation Parameters ---------- eq : object Equilibrium object providing the coil geometry, coil current state and magnetic Green's function operators. """ self.build_control_coils(eq) self.build_greens(eq)
[docs] def source_domain_properties(self, eq: object) -> None: """ Source and cache computational domain geometry from the equilibrium object. This method extracts and stores the spatial grid coordinates used for solving the Grad–Shafranov equation. Parameters ---------- eq : object Equilibrium object providing the computational domain grids ``eq.R`` and ``eq.Z`` (2D radial and vertical coordinates). Stored as `eqR` and `eqZ`. """ self.eqR = eq.R self.eqZ = eq.Z
[docs] def build_control_coils(self, eq: object) -> None: """ Identify and cache coil systems available for active control. This method constructs internal indexing and masking structures that separate: • Control coils (actively optimised) • Passive coils (fixed currents or structures) These mappings are required for: - Current optimisation - Jacobian construction - Constraint enforcement - Vectorised current updates Parameters ---------- eq : object Equilibrium providing the tokamak coil system. Each coil in ``eq.tokamak.coils`` must have a boolean ``control`` attribute indicating controllability. """ # extract coils that are actively controllable # each coil is stored as (label, coil_object) self.control_coils = [ (label, coil) for label, coil in eq.tokamak.coils if coil.control ] # Boolean mask indicating which coils are controllable self.control_mask = np.array( [coil.control for label, coil in eq.tokamak.coils] ).astype(bool) # complement mask (passive / fixed coils) self.no_control_mask = np.logical_not(self.control_mask) # number of actively optimised coils self.n_control_coils = np.sum(self.control_mask) # preserve physical coil ordering from equilibrium object self.coil_order = eq.tokamak.coil_order # total number of coils in system self.n_coils = len(eq.tokamak.coils) # dummy vector used for building perturbation current vectors (for Jacobians) self.full_current_dummy = np.zeros(self.n_coils) # cache domain geometry (PDE solver grid) self.source_domain_properties(eq)
[docs] def build_control_currents(self, eq: object) -> None: """ Extract coil current values for controllable coils from the equilibrium object. This method builds a reduced current vector containing only the currents of actively optimised control coils. Parameters ---------- eq : object Equilibrium object providing the coil current state via ``eq.tokamak.getCurrentsVec``. The result is stored in `control_currents`. """ self.control_currents = eq.tokamak.getCurrentsVec(coils=self.control_coils)
[docs] def build_control_currents_Vec(self, full_currents_vec: np.ndarray) -> None: """ Extract controllable coil currents from a full coil current vector. This function performs projection: I_control = M_control I_full where M_control is a boolean masking operator selecting controllable coils. Parameters ---------- full_currents_vec : np.ndarray Full vector of coil currents, e.g. as returned by ``eq.tokamak.getCurrentsVec()``. The result is stored in `control_currents`. """ self.control_currents = full_currents_vec[self.control_mask]
[docs] def build_full_current_vec(self, eq: object) -> None: """ Build full coil current vector from equilibrium object. This retrieves all coil currents, including: • Control coils • Passive structure coils Parameters ---------- eq : object Equilibrium providing the coil current state via ``eq.tokamak.getCurrentsVec``. The result is stored in `full_currents_vec`. """ self.full_currents_vec = eq.tokamak.getCurrentsVec()
[docs] def rebuild_full_current_vec( self, control_currents: np.ndarray, filling: float = 0 ) -> np.ndarray: """ Reconstruct a full coil current vector from control coil values. This performs the inverse mapping: I_full = P I_control + I_background where: P = projection operator embedding control currents into full coil system ordering. Non-controlled coils are assigned the value specified by: filling Parameters ---------- control_currents : np.ndarray Current values for actively controlled coils. filling : float, optional Default value used to fill non-controlled coil entries. Returns ------- np.ndarray Complete coil current vector in system ordering. """ full_current_vec = filling * np.ones_like(self.full_current_dummy) for i, current in enumerate(control_currents): full_current_vec[self.coil_order[self.control_coils[i][0]]] = current return full_current_vec
[docs] def build_greens(self, eq: object) -> None: """ Construct and cache magnetic Green's function response operators. This method precomputes magnetic response matrices used for: • Flux matching constraints • Null point field constraints • Isoflux surface optimisation • Flux inequality constraints Green's functions represent linear mappings between: Coil current perturbations → Magnetic field / flux perturbations These operators are used during: - Constraint optimisation - Jacobian approximation - Inverse equilibrium solving Parameters ---------- eq : object Equilibrium providing access to tokamak geometry and magnetic response calculation routines. """ # ------------------------------------------------------------ # Isoflux constraint Green's functions # # For each isoflux contour: # # G = ψ response matrix # # Compute pairwise flux differences: # # dG_ij = G_i - G_j # # Used to enforce equal-flux constraints across contours. # ------------------------------------------------------------ if self.isoflux_set is not None: self.dG_set = [] self.mask_set = [] for i, isoflux in enumerate(self.isoflux_set): # compute flux response to unit coil currents G = eq.tokamak.createPsiGreensVec(R=isoflux[0], Z=isoflux[1]) # Upper triangular mask selects unique pairwise constraints # Avoids redundant symmetric entries. mask = np.triu( np.ones((self.isoflux_set_n[i], self.isoflux_set_n[i])), k=1 ).astype(bool) self.mask_set.append(mask) # Ccmpute pairwise flux differences dG = G[:, :, np.newaxis] - G[:, np.newaxis, :] # flatten masked pairwise responses self.dG_set.append(dG[:, mask]) # ------------------------------------------------------------ # Magnetic null point Green's functions # # Used to constrain: # • X-point locations # • O-point locations # ------------------------------------------------------------ if self.null_points is not None: # compute radial magnetic field response to unit coil currents self.Gbr = eq.tokamak.createBrGreensVec( R=self.null_points[0], Z=self.null_points[1] ) # compute vertical magnetic field response to unit coil currents self.Gbz = eq.tokamak.createBzGreensVec( R=self.null_points[0], Z=self.null_points[1] ) # ------------------------------------------------------------ # Magnetic null point Green's functions (for second-order nulls) # ------------------------------------------------------------ if self.null_points_2nd_order is not None: # for first order null self.Gbr_2nd_order = eq.tokamak.createBrGreensVec( R=self.null_points_2nd_order[0], Z=self.null_points_2nd_order[1] ) self.Gbz_2nd_order = eq.tokamak.createBzGreensVec( R=self.null_points_2nd_order[0], Z=self.null_points_2nd_order[1] ) # for second order null self.Gdbrdr_2nd_order = eq.tokamak.createdBrdrGreensVec( R=self.null_points_2nd_order[0], Z=self.null_points_2nd_order[1] ) self.Gdbzdz_2nd_order = eq.tokamak.createdBzdzGreensVec( R=self.null_points_2nd_order[0], Z=self.null_points_2nd_order[1] ) self.Gdbrdz_2nd_order = eq.tokamak.createdBrdzGreensVec( R=self.null_points_2nd_order[0], Z=self.null_points_2nd_order[1] ) self.Gdbzdr_2nd_order = eq.tokamak.createdBzdrGreensVec( R=self.null_points_2nd_order[0], Z=self.null_points_2nd_order[1] ) # ------------------------------------------------------------ # Flux value constraint Green's functions # ------------------------------------------------------------ if self.psi_vals is not None: # detect if the constraint was built directly on this equilibrium's # own (R, Z) grid, rather than an arbitrary/sparse set of points. self.full_grid = np.array_equal( self.psi_vals[0], eq.R.reshape(-1) ) and np.array_equal(self.psi_vals[1], eq.Z.reshape(-1)) if self.full_grid: # constraint was built directly from this equilibrium's own grid # axes - reuse its cached Greens functions instead # of recomputing them pointwise self.G = np.copy(eq._vgreen).reshape((self.n_coils, -1)) # sparse or pointwise constraint case else: self.G = eq.tokamak.createPsiGreensVec( R=self.psi_vals[0], Z=self.psi_vals[1] ) # ------------------------------------------------------------ # Normalised flux inequality constraint Green's functions # ------------------------------------------------------------ if self.psi_norm_limits is not None: # compute flux response to unit coil currents self.G_psi_norm = eq.tokamak.createPsiGreensVec( R=self.psi_norm_limits[:, 0], Z=self.psi_norm_limits[:, 1] )
[docs] def build_plasma_vals(self, trial_plasma_psi: np.ndarray) -> None: """ Compute and cache plasma-dependent magnetic quantities from a candidate flux solution. This method evaluates the plasma flux field and derives quantities required for constraint optimisation and nonlinear solve diagnostics, including: • Magnetic field components at null points • Isoflux surface flux differences • Pointwise flux constraint values The plasma flux field is interpolated using bicubic spline interpolation. Parameters ---------- trial_plasma_psi : np.ndarray Candidate plasma poloidal flux solution, with the shape of the equilibrium grid (``len(self.eqR) × len(self.eqZ)``). Notes ----- Updates the internal state variables `brp`, `bzp`, the 2nd-order null point field quantities, `d_psi_plasma_vals_iso` and `psi_plasma_vals`, depending on which constraints are configured. """ # interpolate current plasma flux map psi_func = interpolate.RectBivariateSpline( self.eqR[:, 0], self.eqZ[0, :], trial_plasma_psi ) # ------------------------------------------------------------ # Magnetic field evaluation at null points # # GS magnetic field relations: # # B_R = - (1/R) ∂ψ/∂Z # B_Z = (1/R) ∂ψ/∂R # # dx=1 requests derivative with respect to R # dy=1 requests derivative with respect to Z # ------------------------------------------------------------ if self.null_points is not None: # radial magnetic field component self.brp = ( -psi_func(self.null_points[0], self.null_points[1], dy=1, grid=False) / self.null_points[0] ) # vertical magnetic field component self.bzp = ( psi_func(self.null_points[0], self.null_points[1], dx=1, grid=False) / self.null_points[0] ) # ------------------------------------------------------------ # Magnetic field evaluation (and derivatives) at null points # # We evaluate the poloidal magnetic field components (B_R, B_Z) # and their spatial derivatives at the identified null points # using the Grad–Shafranov relations: # # B_R(R,Z) = - (1/R) ∂ψ/∂Z # B_Z(R,Z) = (1/R) ∂ψ/∂R # # where ψ(R,Z) is the poloidal flux function. # # The callable `psi_func` provides ψ and its derivatives: # dx = order of derivative with respect to R # dy = order of derivative with respect to Z # # e.g. # dx=1, dy=0 → ∂ψ/∂R # dx=0, dy=1 → ∂ψ/∂Z # dx=1, dy=1 → ∂²ψ/(∂R∂Z) # # These are then used to construct: # - Magnetic field components (B_R, B_Z) # - First spatial derivatives of the field: # ∂B_R/∂R, ∂B_R/∂Z, ∂B_Z/∂R, ∂B_Z/∂Z # # All quantities are evaluated at the null point locations # stored in `self.null_points_2nd_order = (R, Z)`. # ------------------------------------------------------------ if self.null_points_2nd_order is not None: dpsidr = psi_func( self.null_points_2nd_order[0], self.null_points_2nd_order[1], dx=1, dy=0, grid=False, ) dpsidz = psi_func( self.null_points_2nd_order[0], self.null_points_2nd_order[1], dx=0, dy=1, grid=False, ) d2psidrdz = psi_func( self.null_points_2nd_order[0], self.null_points_2nd_order[1], dx=1, dy=1, grid=False, ) d2psidr2 = psi_func( self.null_points_2nd_order[0], self.null_points_2nd_order[1], dx=2, dy=0, grid=False, ) d2psidz2 = psi_func( self.null_points_2nd_order[0], self.null_points_2nd_order[1], dx=0, dy=2, grid=False, ) # Br(R,Z) self.Brp_2nd_order = -dpsidz / self.null_points_2nd_order[0] # Bz(R,Z) self.Bzp_2nd_order = dpsidr / self.null_points_2nd_order[0] # dBrdr(R,Z) self.dBrdrp_2nd_order = ( (dpsidz / self.null_points_2nd_order[0]) - d2psidrdz ) / self.null_points_2nd_order[0] # dBzdz(R,Z) self.dBzdzp_2nd_order = d2psidrdz / self.null_points_2nd_order[0] # dBrdz(R,Z) self.dBrdzp_2nd_order = -d2psidz2 / self.null_points_2nd_order[0] # dBzdr(R,Z) self.dBzdrp_2nd_order = ( -(dpsidr / self.null_points_2nd_order[0]) + d2psidr2 ) / self.null_points_2nd_order[0] # ------------------------------------------------------------ # Isoflux contour constraint evaluation # # For each contour: # # Δψ_ij = ψ_i - ψ_j # # Used to enforce equal-flux topology constraints. # ------------------------------------------------------------ if self.isoflux_set is not None: # loop over each set of isoflux constraints self.d_psi_plasma_vals_iso = [] for i, isoflux in enumerate(self.isoflux_set): # evaluate flux along contour coordinates plasma_vals = psi_func(isoflux[0], isoflux[1], grid=False) # compute pairwise flux differences d_plasma_vals = plasma_vals[:, np.newaxis] - plasma_vals[np.newaxis, :] # apply triangular mask to remove redundant symmetric pairs self.d_psi_plasma_vals_iso.append(d_plasma_vals[self.mask_set[i]]) # ------------------------------------------------------------ # Direct flux value constraints # # These constraints enforce: # # ψ(R_i, Z_i) ≈ ψ_target_i # # at specified spatial locations. # ------------------------------------------------------------ if self.psi_vals is not None: # evaluate flux on each point if self.full_grid: self.psi_plasma_vals = trial_plasma_psi.reshape(-1) else: self.psi_plasma_vals = psi_func( self.psi_vals[0], self.psi_vals[1], grid=False )
[docs] def build_isoflux_lsq( self, full_currents_vec: np.ndarray ) -> tuple[list[np.ndarray], list[np.ndarray], list[float]]: """ Construct linear least-squares system for enforcing isoflux magnetic constraints. This method assembles the linear optimisation problem: A I_control ≈ b where: A = Green's function response matrix I_control = vector of controllable coil currents b = plasma + vacuum flux vector The least-squares system is derived from: ψ_total = ψ_tokamak + ψ_plasma and enforcing: ψ_i − ψ_j ≈ constant (equal-flux constraints along magnetic surfaces) Parameters ---------- full_currents_vec : np.ndarray Full vector of all coil currents, e.g. as returned by ``eq.tokamak.getCurrentsVec()``. Returns ------- A : list of np.ndarray List of arrays for each isoflux constraint set. b : list of np.ndarray Right-hand side residual vectors. loss : list of float Constraint violation magnitudes (L2 norms). Notes ----- For each isoflux constraint set: A_i = (dG_i)ᵀ P_control b_i = − ( Σ_k dG_i I_k + Δψ_plasma ) where: dG_i = pairwise Green's function flux differences P_control = coil control projection operator """ loss = [] A = [] b = [] # loop over each isoflux set for i, isoflux in enumerate(self.isoflux_set): # Weight the complete linearised pair residual, including both its # Green response and its present flux mismatch. pair_weights = np.array( list(itertools.combinations(self.isoflux_weight[i], 2)) ).min(axis=1) # pairwise Greens' flux differences (only in control coils) A.append(self.dG_set[i][self.control_mask].T * pair_weights[:, np.newaxis]) # tokamak flux contribution b_val = np.sum(self.dG_set[i] * full_currents_vec[:, np.newaxis], axis=0) # add the plasma flux contribution b_val += self.d_psi_plasma_vals_iso[i] # isoflux constraint violation are for pairs of constraints within the isoflux set # e.g. 8 isoflux constraints means b has 28 elements (8 choose 2). # Use the smaller point weight for each pairwise residual row. b_val *= pair_weights # total b.append(-b_val) # constraint violation magnitude loss.append(np.linalg.norm(b_val)) return A, b, loss
[docs] def build_null_points_lsq( self, full_currents_vec: np.ndarray ) -> tuple[np.ndarray, np.ndarray, list[float]]: """ Construct a least-squares system enforcing magnetic null-point constraints. This method builds the linear optimisation system: A I_control ≈ b for magnetic field cancellation at prescribed null points. Null point constraints enforce: B_R(R_i, Z_i) ≈ 0 B_Z(R_i, Z_i) ≈ 0 where: B_R = radial magnetic field component B_Z = vertical magnetic field component These fields are expressed using Green's function response matrices: B(R,Z) = Σ_k G_k(R,Z) I_k + B_plasma Parameters ---------- full_currents_vec : np.ndarray Full vector of coil currents, e.g. as returned by ``eq.tokamak.getCurrentsVec()``. Returns ------- A : np.ndarray Combined Jacobian matrix for null-point field constraints. b : np.ndarray Residual field mismatch vector (negated for optimisation solving). loss : list of float Constraint violation magnitudes ``[ ||B_R||₂ , ||B_Z||₂ ]``. Notes ----- This formulation is used to stabilise: • Magnetic axis positioning • X-point control • Divertor topology shaping The optimisation problem solved is: min_I || G I + B_plasma ||² where: G = magnetic Green's response operator I = coil current vector """ # radial field constraint A_r = self.Gbr[self.control_mask].T b_r = np.sum(self.Gbr * full_currents_vec[:, np.newaxis], axis=0) b_r += self.brp loss = [np.linalg.norm(b_r)] # vertical field constraint A_z = self.Gbz[self.control_mask].T b_z = np.sum(self.Gbz * full_currents_vec[:, np.newaxis], axis=0) b_z += self.bzp loss.append(np.linalg.norm(b_z)) # stack contraints A = np.concatenate((A_r, A_z), axis=0) b = -np.concatenate((b_r, b_z), axis=0) return A, b, loss
[docs] def build_psi_vals_lsq( self, full_currents_vec: np.ndarray ) -> tuple[np.ndarray, np.ndarray, list[float]]: """ Construct a least-squares system enforcing direct flux value constraints. This method builds the optimisation system: A I_control ≈ b where: ψ_model(R_i, Z_i) = ψ_target(R_i, Z_i) is enforced by matching magnetic flux values at specified locations. The optimisation residual is defined as: b = ψ_tokamak(I) + ψ_plasma - ψ_target - ⟨b⟩ Mean flux removal is applied to remove arbitrary vertical flux offsets, since the Grad–Shafranov equation is invariant under constant flux shifts. The same mean removal is applied to each coil-response column in ``A``. Parameters ---------- full_currents_vec : np.ndarray Full coil current vector, e.g. as returned by ``eq.tokamak.getCurrentsVec()``. Returns ------- A : np.ndarray Jacobian matrix mapping coil current perturbations → flux changes. b : np.ndarray Flux mismatch residual vector. list of float Single-element list with the normalised constraint violation magnitude. Notes ----- This constraint formulation is commonly used for: • Magnetic axis pinning • Boundary flux matching • Profile shape control Solve: min_I || G I + ψ_plasma − ψ_target ||² """ # Flux response wrt coil currents, with the same offset removal as b. A = self.G[self.control_mask].T A -= np.mean(A, axis=0) # tokamak coil flux b = np.sum(self.G * full_currents_vec[:, np.newaxis], axis=0) # add plasma flux b += self.psi_plasma_vals # Remove the arbitrary flux offset consistently with the centred # response columns above. b -= np.mean(b) b -= self.psi_vals[2] b *= -1 # normalised loss normalised_loss = np.linalg.norm(b) / self.norm_psi_vals return A, b, [normalised_loss]
[docs] def build_lsq(self, full_currents_vec: np.ndarray) -> None: """ Assemble the global least-squares optimisation system combining all active magnetic and control constraints. This method aggregates all available constraint types into a single linear optimisation problem of the form: A I_control ≈ b where: A = stacked Jacobian / response matrices b = stacked residual constraint vectors The constraint types that can be combined include: • Isoflux surface constraints • Magnetic null-point field constraints • Direct flux value constraints • Direct coil current constraints Parameters ---------- full_currents_vec : np.ndarray Full vector of coil currents, e.g. as returned by ``eq.tokamak.getCurrentsVec()``. Notes ----- The assembled system is stored as `A`, `b` and `loss`. This formulation solves the global constrained optimisation problem: min_I || A I − b ||² where each constraint type contributes a block to the system. Constraint dimensional bookkeeping is stored as: self.isoflux_dim self.nullp_dim self.psiv_dim self.curr_dim """ # storage loss = 0 A = np.empty(shape=(0, self.n_control_coils)) b = np.empty(shape=0) loss = [] # isfolux constrains if self.isoflux_set is not None: A_i, b_i, l = self.build_isoflux_lsq(full_currents_vec) A = np.concatenate(A_i, axis=0) * self.weight_isoflux b = np.concatenate(b_i, axis=0) * self.weight_isoflux self.isoflux_dim = len(b) loss = loss + l # null point constraints if self.null_points is not None: A_np, b_np, l = self.build_null_points_lsq(full_currents_vec) A = np.concatenate((A, A_np * self.weight_nulls), axis=0) b = np.concatenate((b, b_np * self.weight_nulls), axis=0) self.nullp_dim = len(b) loss = loss + l # direct flux value constraints if self.psi_vals is not None: A_pv, b_pv, l = self.build_psi_vals_lsq(full_currents_vec) A = np.concatenate((A, A_pv * self.weight_psi), axis=0) b = np.concatenate((b, b_pv * self.weight_psi), axis=0) self.psiv_dim = len(b) loss = loss + l # second order null point constraints if self.null_points_2nd_order is not None: A_np_2nd_order, b_np_2nd_order, l = self.build_null_points_2nd_order_lsq( full_currents_vec ) A = np.concatenate((A, A_np_2nd_order), axis=0) b = np.concatenate((b, b_np_2nd_order), axis=0) self.nullp_2nd_order_dim = len(b) loss = loss + l # assemble the full system self.A = np.copy(A) self.b = np.copy(b) self.loss = np.array(loss)
[docs] def optimize_currents( self, eq: object, profiles: object, full_currents_vec: np.ndarray, trial_plasma_psi: np.ndarray, l2_reg: float | np.ndarray, ) -> tuple[np.ndarray, float]: """ Solve the constrained least-squares optimisation problem for coil current updates. This method computes optimal coil current corrections by solving: min_I || A I − b ||² + Iᵀ R I where: A = combined constraint Jacobian matrix b = combined constraint residual vector R = diagonal Tikhonov regularisation matrix The optimisation accounts for: • Magnetic topology constraints • Flux surface matching • Null point field cancellation • Hardware current constraints Parameters ---------- eq : object Equilibrium providing geometry and tokamak configuration. profiles : object Plasma profile properties used for constraint evaluation. full_currents_vec : np.ndarray Full vector of all coil currents, e.g. as returned by ``eq.tokamak.getCurrentsVec()``. trial_plasma_psi : np.ndarray Candidate plasma flux solution, with the same shape as ``eq.R``. l2_reg : float or np.ndarray Tikhonov regularisation parameter. If a float, R is the scalar value times the identity matrix; if an array (one entry per controlled coil), R contains the supplied coil-wise values on its diagonal. Returns ------- delta_current : np.ndarray Optimal coil current update vector. loss : float Residual loss norm. Raises ------ ValueError If `l2_reg` is an array whose length differs from the number of controlled coils. """ # prepare the plasma-related values self.build_plasma_vals(trial_plasma_psi=trial_plasma_psi) # build the matrices that define the optimization self.build_lsq(full_currents_vec) # build Tikhonov matrix if isinstance(l2_reg, float): reg_matrix = l2_reg * np.eye(self.n_control_coils) else: if len(l2_reg) != self.n_control_coils: raise ValueError( f"Expected l2_reg to have length equal to number of coils being controlled ({self.n_control_coils}), but got {len(l2_reg)}." ) reg_matrix = np.diag(l2_reg) # ------------------------------------------------------------ # solve least-squares optimisation problem # # If inequality constraints (for coil limits or normalised psi bounds) are present: # Use quadratic programming solver. # # Otherwise: # Solve the augmented regularised least-squares system directly. # ------------------------------------------------------------ if self.coil_current_limits is not None or self.psi_norm_limits is not None: delta_current, loss = self.optimize_currents_quadratic( eq, profiles, full_currents_vec, reg_matrix ) else: # TODO: should we just use the quadratic solver all the time, regardless # of whether coil limits are specified? delta_current = _solve_regularized_lstsq( self.A, self.b, np.diag(reg_matrix) ) loss = np.linalg.norm(self.loss) return delta_current, loss
[docs] def optimize_currents_quadratic( self, eq: object, profiles: object, full_currents_vec: np.ndarray, reg_matrix: np.ndarray, *, mu_coils: float | None = None, mu_psi_norm: float | None = None, A: np.ndarray | None = None, b: np.ndarray | None = None, ) -> tuple[np.ndarray, float]: """ Solve the regularised constrained least-squares problem using convex optimisation. This method computes coil current updates by solving the quadratic program: minimise_ΔI || A ΔI − b ||² + ΔIᵀ R ΔI + penalty(slack variables) subject to optional inequality constraints: • Coil current upper/lower bounds • Normalised flux (ψ_norm) inequality constraints Slack variables are introduced to allow temporary constraint violation, penalised in the objective function. This improves optimisation robustness and prevents infeasibility during nonlinear solve iterations. Parameters ---------- eq : object Equilibrium object used to evaluate plasma quantities and ψ_norm. profiles : object Provides boundary flux (psi_bndry) and related quantities. full_currents_vec : np.ndarray Full vector of coil currents. reg_matrix : np.ndarray Regularisation matrix (n_control_coils × n_control_coils), typically diagonal, encoding Tikhonov L2 regularisation. mu_coils : float or None, optional Currently unused: the coil limit slack penalty always uses ``self.mu_coils``. mu_psi_norm : float or None, optional Scaling factor for ψ_norm slack penalties. If None (or zero), defaults to ``self.mu_psi_norm``. A : np.ndarray or None, optional Sensitivity matrix. If None, uses ``self.A``. b : np.ndarray or None, optional Target residual vector. If None, uses ``self.b``. Returns ------- delta : np.ndarray Optimal coil current update vector ΔI. loss : float Combined loss: the magnetic constraint residual norm plus the sum of the slack variable values. Raises ------ ValueError If a ψ_norm constraint sign in `psi_norm_limits` is not 1 or -1. Notes ----- The optimisation problem solved is a convex quadratic program (QP). Base objective: min_ΔI ||A ΔI − b||² + ΔIᵀ R ΔI With optional inequality constraints: I_min ≤ I_current + ΔI ≤ I_max ψ_norm constraints (≥ or ≤ type) Slack variables s ≥ 0 allow: constraint_violation ≤ s with large quadratic penalties to discourage violation. """ # use stored least-squares system unless inputs are provided A = self.A if A is None else A b = self.b if b is None else b # optimsiation variable: coil currents delta = cvxpy.Variable(self.n_control_coils) slack_variables = [] constraints = [] # Setup the coil limits slack variables and constraints # Slack variables allow the coil limit to be violated by the solver # however it is penalised in the objective function; this is useful # to allow coil limits be violated 'on the path' to a solution. if self.coil_current_limits is not None: coil_limits_upper_slack = cvxpy.Variable(self.n_control_coils, nonneg=True) coil_limits_lower_slack = cvxpy.Variable(self.n_control_coils, nonneg=True) coil_limit_slack_scale = self.mu_coils * np.diag(A.T @ A).max() coil_upper_limits, coil_lower_limits = self.coil_current_limits # upper bound constraints for coil_index, ul in enumerate(coil_upper_limits): if ul is not None: constraints.append( (full_currents_vec[self.control_mask][coil_index] / 1000) + (delta[coil_index] / 1000) <= (ul / 1000) + coil_limits_upper_slack[coil_index] ) # lower bound constraints for coil_index, ll in enumerate(coil_lower_limits): if ll is not None: constraints.append( (full_currents_vec[self.control_mask][coil_index] / 1000) + (delta[coil_index] / 1000) >= (ll / 1000) - coil_limits_lower_slack[coil_index] ) # penalise slack variables heavily to discourage violations slack_variables.append(coil_limit_slack_scale * coil_limits_upper_slack) slack_variables.append(coil_limit_slack_scale * coil_limits_lower_slack) # normalised flux cosntraints if self.psi_norm_limits is not None: # ensure eq object is up-to-date eq._updatePlasmaPsi(eq.plasma_psi) eq.psi_bndry = profiles.psi_bndry psi_norm_slack = cvxpy.Variable(self.psi_norm_limits.shape[0], nonneg=True) psi_norm_slack_scale = (mu_psi_norm or self.mu_psi_norm) * np.diag( A.T @ A ).max() # sensitivity of ψ_norm w.r.t coil currents psi_norm_A = self.G_psi_norm[self.control_mask].T # chain rule: G_psi_norm is derivative wrt ψ, not ψ_norm psi_norm_A /= eq.psi_bndry - eq.psi_axis # residual between target ψ_norm and current ψ_norm psi_norm_b = self.psi_norm_limits[:, 2] - eq.psiNRZ( self.psi_norm_limits[:, 0], self.psi_norm_limits[:, 1] ) for psin_limit_idx in range(self.psi_norm_limits.shape[0]): psin_con_sign = self.psi_norm_limits[psin_limit_idx, 3] lhs = psi_norm_A[psin_limit_idx, :] @ delta # enforce lower bound if psin_con_sign == 1: constraints.append( lhs >= psi_norm_b[psin_limit_idx] - psi_norm_slack[psin_limit_idx] ) # enforce upper bound elif psin_con_sign == -1: constraints.append( lhs <= psi_norm_b[psin_limit_idx] + psi_norm_slack[psin_limit_idx] ) else: raise ValueError( f"Unexpected psi norm constraint sign {psin_con_sign}. Expected 1 or -1." ) # penalise ψ_norm slack slack_variables.append(psi_norm_slack_scale * psi_norm_slack) # minimise the objectives (the least squares objective + regularisation + slack variables) minimisation_expression = cvxpy.sum_squares(A @ delta - b) + cvxpy.quad_form( delta, reg_matrix ) for expr in slack_variables: minimisation_expression += cvxpy.sum_squares(expr) # solve problem problem = cvxpy.Problem( cvxpy.Minimize(minimisation_expression), constraints or None ) problem.solve(solver=cvxpy.CLARABEL, tol_infeas_abs=1e-12, tol_infeas_rel=1e-10) # combine magnetic residual loss with slack penalties slack_loss = sum([i.value.sum() for i in slack_variables]) return ( delta.value, np.linalg.norm(self.loss) + slack_loss, )
[docs] def optimize_currents_grad( self, full_currents_vec: np.ndarray, trial_plasma_psi: np.ndarray, isoflux_weight: float = 1.0, null_points_weight: float = 1.0, null_points_2nd_order_weight: float = 1.0, psi_vals_weight: float = 1.0, ) -> tuple[np.ndarray, float]: """ Compute the gradient of the magnetic least-squares objective with respect to control coil currents. This method evaluates the gradient of the unconstrained objective: J(ΔI) = ½ || A ΔI − b ||² evaluated at ΔI = 0, i.e. ∇J = Aᵀ b Optional weighting factors allow different constraint classes (isoflux, null points, direct flux values) to be scaled prior to gradient evaluation. Parameters ---------- full_currents_vec : np.ndarray Full vector of all coil current values, e.g. as returned by ``eq.tokamak.getCurrentsVec()``. trial_plasma_psi : np.ndarray Plasma flux contribution. Must have same shape as eq.R. isoflux_weight : float, optional Weight applied to isoflux constraint residuals. null_points_weight : float, optional Weight applied to null-point field constraint residuals. null_points_2nd_order_weight : float, optional Weight applied to second-order null-point constraint residuals. psi_vals_weight : float, optional Weight applied to direct flux value constraint residuals. Returns ------- grad : np.ndarray Gradient of the weighted least-squares objective with respect to control coil current updates. loss : float Combined magnetic constraint loss (unweighted). """ # prepare the plasma-related values self.build_plasma_vals(trial_plasma_psi=trial_plasma_psi) # build the matrices that define the optimization self.build_lsq(full_currents_vec) # weight the different terms in the loss b_weighted = np.copy(self.b) idx = 0 if self.isoflux_set is not None: b_weighted[idx : idx + self.isoflux_dim] *= isoflux_weight idx += self.isoflux_dim if self.null_points is not None: b_weighted[idx : idx + self.nullp_dim] *= null_points_weight idx += self.nullp_dim if self.null_points_2nd_order is not None: b_weighted[ idx : idx + self.nullp_2nd_order_dim ] *= null_points_2nd_order_weight idx += self.nullp_dim_2nd_order if self.psi_vals is not None: b_weighted[idx : idx + self.psiv_dim] *= psi_vals_weight idx += self.psiv_dim grad = np.dot(self.A.T, b_weighted) return grad, np.linalg.norm(self.loss)
[docs] def plot(self, axis: Axes | None = None, show: bool = True) -> Axes: """ Visualise the active coil control constraints. This method provides a graphical representation of the magnetic and operational constraints currently configured in the object. Parameters ---------- axis : matplotlib.axes.Axes | None, optional Axis object on which to draw the plot. If None, a new figure and axis are created internally. show : bool, optional If True, calls matplotlib.pyplot.show() before returning. Returns ------- matplotlib.axes.Axes Axis containing the generated plot. Notes ----- This is a thin wrapper around: freegsnke.plotting.plotIOConstraints and exists primarily for convenience and API consistency. """ from .plotting import plotIOConstraints return plotIOConstraints(self, axis=axis, show=show)
[docs] def prepare_plasma_psi(self, trial_plasma_psi: np.ndarray) -> None: """ Preprocess plasma flux values for normalisation and constraint evaluation. This method computes shifted plasma flux extrema used for normalised flux calculations and ψ_norm-based constraints. Parameters ---------- trial_plasma_psi : np.ndarray Plasma flux array. Notes ----- Sets the attributes `min_psi` and `psi0`. """ self.min_psi = np.amin(trial_plasma_psi) self.psi0 = np.amax(trial_plasma_psi) self.min_psi -= 0.001 * (self.psi0 - self.min_psi) self.psi0 -= self.min_psi
[docs] def prepare_plasma_vals_for_plasma(self, trial_plasma_psi: np.ndarray) -> None: """ Precompute plasma-dependent quantities required for plasma optimisation. This method evaluates the plasma flux at constraint locations and constructs pairwise flux-difference terms used in isoflux constraints. In addition to raw flux differences, a nonlinear transformed version of the normalised flux is computed: ψ̂ = (ψ − ψ_min) / ψ0 f(ψ̂) = ψ̂ log(ψ̂) which is then used to build weighted pairwise differences. Parameters ---------- trial_plasma_psi : np.ndarray Plasma flux array defined on the equilibrium grid (eqR, eqZ). Notes ----- Sets `d_psi_plasma_vals_iso` and `d_psi_for_plasma_iso`. For each isoflux constraint set: 1. Interpolate ψ at constraint coordinates. 2. Construct pairwise differences: ψ_i − ψ_j 3. Construct transformed differences: ψ0 [ f(ψ̂_i) − f(ψ̂_j) ] These quantities are cached for use in plasma optimisation routines. The logarithmic transform enhances sensitivity near ψ̂ → 0 and improves conditioning when enforcing plasma-based constraints. """ # prepare plasma flux guess self.prepare_plasma_psi(trial_plasma_psi=trial_plasma_psi) # interpolate psi_func = interpolate.RectBivariateSpline( self.eqR[:, 0], self.eqZ[0, :], trial_plasma_psi ) # prepare isoflux constraints (contributions from plasma flux) if self.isoflux_set is not None: self.d_psi_plasma_vals_iso = [] self.d_psi_for_plasma_iso = [] for i, isoflux in enumerate(self.isoflux_set): # interpolate plasma_vals = psi_func(isoflux[0], isoflux[1], grid=False) # pairwise differences d_plasma_vals = plasma_vals[:, np.newaxis] - plasma_vals[np.newaxis, :] self.d_psi_plasma_vals_iso.append(d_plasma_vals[self.mask_set[i]]) # normalised flux hat_plasma_vals = (plasma_vals - self.min_psi) / self.psi0 # log to avoid errors near zero(?) hat_plasma_vals *= np.log(hat_plasma_vals) # pairwise diffs d_hat_plasma_vals = ( hat_plasma_vals[:, np.newaxis] - hat_plasma_vals[np.newaxis, :] ) # rescale self.d_psi_for_plasma_iso.append( self.psi0 * d_hat_plasma_vals[self.mask_set[i]] )
[docs] def prepare_for_plasma_optimization(self, eq: object) -> None: """ Prepare geometry- and Green's-function-dependent quantities required for plasma optimisation. This method ensures that: • Source-domain geometric properties are updated • Magnetic Green's operators are constructed Parameters ---------- eq : object Equilibrium providing geometry, coil configuration, and grid data. Notes ----- This is typically called prior to plasma-only optimisation steps to ensure response operators and geometric mappings are consistent with the current equilibrium configuration. """ self.source_domain_properties(eq) self.build_greens(eq=eq)
[docs] def build_plasma_isoflux_lsq( self, full_currents_vec: np.ndarray, trial_plasma_psi: np.ndarray ) -> None: """ Assemble the least-squares system for plasma-only isoflux optimisation. This method constructs a linearised least-squares problem of the form: A_plasma x ≈ b_plasma where x ∈ ℝ² represents plasma transformation parameters (e.g. normalisation and nonlinear shaping terms). The residual enforces pairwise isoflux constraints: Δψ_total = Δψ_coils + Δψ_plasma ≈ 0 using both raw flux differences and nonlinear transformed (ψ̂ log ψ̂) contributions. Parameters ---------- full_currents_vec : np.ndarray Full vector of coil currents. trial_plasma_psi : np.ndarray Plasma flux array defined on the equilibrium grid. Notes ----- The constructed system solves for two plasma parameters: x[0] → linear normalisation scaling x[1] → nonlinear shaping contribution Results are stored internally: self.A_plasma self.b_plasma self.loss_plasma """ # pre-compute pairwise flux differences self.prepare_plasma_vals_for_plasma(trial_plasma_psi) loss = [] A = [] b = [] # loop over each constraint for i, isoflux in enumerate(self.isoflux_set): # tokamak flux difference b_val = np.sum(self.dG_set[i] * full_currents_vec[:, np.newaxis], axis=0) # add plasma contribution b_val += self.d_psi_plasma_vals_iso[i] b.append(-b_val) # residual loss.append(np.linalg.norm(b_val)) # build the jacobian Amat = np.zeros((len(b_val), 2)) # gradient with respect to the normalization of psi Amat[:, 0] = self.d_psi_plasma_vals_iso[i] # gradient with respect to the exponent of psi Amat[:, 1] = self.d_psi_for_plasma_iso[i] A.append(Amat) # stack everything self.A_plasma = np.concatenate(A, axis=0) self.b_plasma = np.concatenate(b, axis=0) self.loss_plasma = np.linalg.norm(loss)
[docs] def build_null_points_2nd_order_lsq( self, full_currents_vec: np.ndarray ) -> tuple[np.ndarray, np.ndarray, list[float]]: """ Construct the linear least-squares system enforcing second-order null point constraints. This method assembles a linear system of the form: A ΔI = b where ΔI represents corrections to the *control coil currents*, such that the magnetic field and its first spatial derivatives vanish (or match desired values) at the specified second-order null points. The constraints enforced at each null point are: - B_R = 0 - B_Z = 0 - ∂B_R/∂R = 0 - ∂B_Z/∂Z = 0 - ∂B_R/∂Z = 0 - ∂B_Z/∂R = 0 Each constraint is written as a linear combination of coil currents using precomputed Green's function matrices (G* terms), with an additional contribution from the plasma. The right-hand side vector `b` represents the *negative residual* of the total field (coil + plasma) at the null points, so that solving the system drives the residuals toward zero. Parameters ---------- full_currents_vec : np.ndarray Array of shape (n_coils,) containing the full set of coil currents. This includes both controlled and fixed coils, e.g. as returned by `eq.tokamak.getCurrentsVec()`. Returns ------- A : np.ndarray Least-squares matrix of shape (n_constraints, n_control_coils), mapping control coil current corrections to changes in the magnetic field quantities at the null points. b : np.ndarray Right-hand side vector of shape (n_constraints,), representing the negative residuals of the current field configuration (coil + plasma). loss : list of float L2 norms of each individual constraint residual before solving, in the order ``[||B_R||, ||B_Z||, ||∂B_R/∂R||, ||∂B_Z/∂Z||, ||∂B_R/∂Z||, ||∂B_Z/∂R||]``. Notes ----- - The matrices `G*` encode the linear response of the magnetic field (and its derivatives) to coil currents. - Contributions are split into: * coil contribution: G * I * plasma contribution: precomputed field values at null points - Only coils selected by `self.control_mask` are treated as control variables. - The system is typically overdetermined and intended to be solved in a least-squares sense. """ # Br field constraint A_r = self.Gbr_2nd_order[self.control_mask].T b_r = np.sum( self.Gbr_2nd_order * full_currents_vec[:, np.newaxis], axis=0 ) # coils contribution b_r += self.Brp_2nd_order # plasma contribution loss = [np.linalg.norm(b_r)] # Bz field constraint A_z = self.Gbz_2nd_order[self.control_mask].T b_z = np.sum( self.Gbz_2nd_order * full_currents_vec[:, np.newaxis], axis=0 ) # coils contribution b_z += self.Bzp_2nd_order # plasma contribution loss.append(np.linalg.norm(b_z)) # dBrdr field constraint A_r_deriv = self.Gdbrdr_2nd_order[self.control_mask].T b_r_deriv = np.sum( self.Gdbrdr_2nd_order * full_currents_vec[:, np.newaxis], axis=0 ) # coils contribution b_r_deriv += self.dBrdrp_2nd_order # plasma contribution loss.append(np.linalg.norm(b_r_deriv)) # dBzdz field constraint A_z_deriv = self.Gdbzdz_2nd_order[self.control_mask].T b_z_deriv = np.sum( self.Gdbzdz_2nd_order * full_currents_vec[:, np.newaxis], axis=0 ) # coils contribution b_z_deriv += self.dBzdzp_2nd_order # plasma contribution loss.append(np.linalg.norm(b_z_deriv)) # dBrdz field constraint A_r_deriv_cross = self.Gdbrdz_2nd_order[self.control_mask].T b_r_deriv_cross = np.sum( self.Gdbrdz_2nd_order * full_currents_vec[:, np.newaxis], axis=0 ) # coils contribution b_r_deriv_cross += self.dBrdzp_2nd_order # plasma contribution loss.append(np.linalg.norm(b_r_deriv_cross)) # dBzdr field constraint A_z_deriv_cross = self.Gdbzdr_2nd_order[self.control_mask].T b_z_deriv_cross = np.sum( self.Gdbzdr_2nd_order * full_currents_vec[:, np.newaxis], axis=0 ) # coils contribution b_z_deriv_cross += self.dBzdrp_2nd_order # plasma contribution loss.append(np.linalg.norm(b_z_deriv_cross)) A = np.concatenate( (A_r, A_z, A_r_deriv, A_z_deriv, A_r_deriv_cross, A_z_deriv_cross), axis=0 ) b = -np.concatenate( (b_r, b_z, b_r_deriv, b_z_deriv, b_r_deriv_cross, b_z_deriv_cross), axis=0 ) return A, b, loss
[docs] def optimize_plasma_psi( self, full_currents_vec: np.ndarray, trial_plasma_psi: np.ndarray, l2_reg: float | np.ndarray, ) -> tuple[np.ndarray, float]: """ Solve the regularised least-squares problem for plasma parameters. This solves: min_x || A_plasma x − b_plasma ||² + xᵀ R x where: x ∈ ℝ² R = Tikhonov regularisation matrix Parameters ---------- full_currents_vec : np.ndarray Full vector of coil currents. trial_plasma_psi : np.ndarray Plasma flux array defined on the equilibrium grid. l2_reg : float or np.ndarray Tikhonov regularisation strength. If a float, uniform regularisation is applied; if an array (length 2), its entries are the diagonal regularisation weights. Returns ------- delta_current : np.ndarray Optimal update vector for plasma parameters (length 2). loss_plasma : float Isoflux constraint violation norm (pre-update). Notes ----- The regularisation is appended to the least-squares system before solving, avoiding the condition-number squaring of normal equations. """ # assemble least-squares system self.build_plasma_isoflux_lsq(full_currents_vec, trial_plasma_psi) # assemble regularisatio matrix if type(l2_reg) == float: reg_matrix = l2_reg * np.eye(2) else: reg_matrix = np.diag(l2_reg) delta_current = _solve_regularized_lstsq( self.A_plasma, self.b_plasma, np.diag(reg_matrix) ) return delta_current, self.loss_plasma