Source code for freegsnke.circuit_eq_metal
"""
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/>.
"""
from __future__ import annotations
import logging
import numpy as np
from .gradshafranov import Greens, GreensBr, GreensBz
from .implicit_euler import implicit_euler_solver
from .normal_modes import mode_decomposition
logger = logging.getLogger(__name__)
[docs]
class metal_currents:
"""
Time 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
"""
[docs]
def __init__(
self,
eq: object,
flag_vessel_eig: bool,
flag_plasma: bool,
max_mode_frequency: float,
max_internal_timestep: float,
full_timestep: float,
plasma_pts: np.ndarray | None = None,
coil_resist: np.ndarray | None = None,
coil_self_ind: np.ndarray | None = None,
verbose: bool = True,
passive_reflection_operator: np.ndarray | None = None,
remove_odd_passive_modes: bool = True,
symmetry_tolerance: float = 1e-8,
) -> None:
"""
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.
"""
self.n_coils = eq.tokamak.n_coils
self.n_active_coils = eq.tokamak.n_active_coils
self.verbose = verbose
self.passive_reflection_operator = passive_reflection_operator
# prepare resistance data
if coil_resist is not None:
if len(coil_resist) != self.n_coils:
raise ValueError(
f"Resistance vector provided (size: {len(coil_resist)}) is not compatible with machine description (size: {self.n_coils})."
)
self.coil_resist = coil_resist
else:
self.coil_resist = eq.tokamak.coil_resist
self.Rm1 = 1.0 / self.coil_resist
# self.R = np.copy(self.coil_resist)
self.active_coil_resistances = np.copy(self.coil_resist[: self.n_active_coils])
# prepare inductance data
if coil_self_ind is not None:
if np.size(coil_self_ind) != self.n_coils**2:
raise ValueError(
f"Mutual inductance matrix provided (size: {np.size(coil_self_ind)}) is not compatible with machine description (size: {self.n_coils**2})."
)
self.coil_self_ind = coil_self_ind
else:
self.coil_self_ind = eq.tokamak.coil_self_ind
self.build_rm1l()
self.flag_vessel_eig = flag_vessel_eig
self.flag_plasma = flag_plasma
self.max_internal_timestep = max_internal_timestep
self.full_timestep = full_timestep
if flag_vessel_eig:
# builds mode decomposition
self.normal_modes = mode_decomposition(
coil_resist=self.coil_resist,
coil_self_ind=self.coil_self_ind,
n_coils=self.n_coils,
n_active_coils=self.n_active_coils,
passive_reflection_operator=passive_reflection_operator,
symmetry_tolerance=symmetry_tolerance,
)
passive_modes = self.normal_modes.passive_mode_parity
if passive_modes is None or not remove_odd_passive_modes:
self.available_modes_mask = np.ones(self.n_coils, dtype=bool)
else:
self.available_modes_mask = np.concatenate(
(
np.ones(self.n_active_coils, dtype=bool),
passive_modes > 0,
)
)
self.max_mode_frequency = max_mode_frequency
self.initialize_for_eig(selected_modes_mask=False)
else:
self.max_mode_frequency = 0
self.initialize_for_no_eig()
if flag_plasma:
self.plasma_pts = plasma_pts
self.Mey_matrix = self.Mey(eq)
# Dummy voltage vector
self.empty_U = np.zeros(self.n_coils)
[docs]
def build_rm1l(
self,
) -> None:
"""
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`.
"""
self.rm1l_non_symm = np.diag(self.coil_resist**-1.0) @ self.coil_self_ind
[docs]
def make_selected_mode_mask(
self,
mode_coupling_masks: tuple[np.ndarray, np.ndarray] | None,
verbose: bool,
fixed_n_passive_modes: int | None = None,
) -> np.ndarray:
"""
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
-------
ndarray of bool
Selection mask expressed in the parity-available mode basis. The
full-basis selection remains available as
``self.selected_modes_mask``.
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`.
"""
available_passive = self.available_modes_mask[self.n_active_coils :]
n_passive_modes = int(np.sum(available_passive))
if fixed_n_passive_modes is not None:
if mode_coupling_masks is not None:
raise ValueError(
"fixed_n_passive_modes cannot be combined with coupling masks."
)
if not 0 <= fixed_n_passive_modes <= n_passive_modes:
raise ValueError(
"fixed_n_passive_modes must be between zero and the number "
f"of available passive modes ({n_passive_modes}); received "
f"{fixed_n_passive_modes}."
)
selected_modes_mask = np.zeros(n_passive_modes, dtype=bool)
selected_modes_mask[:fixed_n_passive_modes] = True
else:
selected_modes_mask = (
self.normal_modes.w_passive[available_passive] < self.max_mode_frequency
)
freq_only_number = np.sum(selected_modes_mask)
local_selected_modes_mask = np.concatenate(
(np.ones(self.n_active_coils).astype(bool), selected_modes_mask)
)
log_level = logging.INFO if verbose else logging.DEBUG
if logger.isEnabledFor(log_level):
logger.log(log_level, " Active coils")
logger.log(
log_level,
" total selected = %d (out of %d)",
self.n_active_coils,
self.n_active_coils,
)
logger.log(log_level, " Passive structures")
if fixed_n_passive_modes is None:
logger.log(
log_level,
" %d selected below 'max_mode_frequency'",
freq_only_number,
)
else:
logger.log(
log_level,
" %d lowest-frequency (longest-timescale) modes selected",
freq_only_number,
)
if mode_coupling_masks is not None:
# reintroduce modes that couple strongly
local_selected_modes_mask = (
local_selected_modes_mask + mode_coupling_masks[0]
).astype(bool)
freq_and_thresh_number = np.sum(local_selected_modes_mask)
if logger.isEnabledFor(log_level):
logger.log(
log_level,
" %d recovered that couple with the plasma more than 'threshold_dIy_dI'",
freq_and_thresh_number - (freq_only_number + self.n_active_coils),
)
# exclude modes that do not couple enough
local_selected_modes_mask = (
local_selected_modes_mask * mode_coupling_masks[1]
).astype(bool)
final_number = np.sum(local_selected_modes_mask)
if logger.isEnabledFor(log_level):
logger.log(
log_level,
" %d removed that couple with the plasma less than 'min_dIy_dI'",
freq_and_thresh_number - final_number,
)
logger.log(
log_level,
" total selected = %d (out of %d)",
final_number - self.n_active_coils,
np.sum(available_passive),
)
logger.log(
log_level,
" Total number of modes = %d (%d active coils + %d passive structures)",
final_number,
self.n_active_coils,
final_number - self.n_active_coils,
)
logger.log(
log_level,
" (Note: some additional modes may be removed after Jacobian calculation)",
)
available_indices = np.flatnonzero(self.available_modes_mask)
self.selected_modes_mask = np.zeros(self.n_coils, dtype=bool)
self.selected_modes_mask[available_indices[local_selected_modes_mask]] = True
self.n_independent_vars = np.sum(local_selected_modes_mask)
return local_selected_modes_mask
[docs]
def initialize_for_eig(
self,
selected_modes_mask: np.ndarray | bool | None = None,
mode_coupling_masks: tuple[np.ndarray, np.ndarray] | None = None,
verbose: bool = True,
fixed_n_passive_modes: int | None = None,
) -> np.ndarray:
"""
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
-------
ndarray of bool
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.
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.
"""
if selected_modes_mask is None:
# this is the case when mode_coupling_masks are used to build self.selected_modes_mask
selection_mask = self.make_selected_mode_mask(
mode_coupling_masks,
verbose,
fixed_n_passive_modes=fixed_n_passive_modes,
)
# Pmatrix is the full matrix that changes the basis in the current space
# from the normal modes Id (for diagonal) to the metal currents I:
# I = Pmatrix Id
# And also
# Id = Pmatrixm1 I
# Therefore, taking the truncation into account:
self.P = self.normal_modes.Pmatrix[:, self.selected_modes_mask]
self.Pm1 = self.normal_modes.Pmatrix_inverse[self.selected_modes_mask]
elif selected_modes_mask is False:
# Include all modes available to this solver. In symmetric operation,
# odd passive modes have already been made unavailable.
self.selected_modes_mask = self.available_modes_mask.copy()
selection_mask = np.ones(np.sum(self.available_modes_mask), dtype=bool)
self.n_independent_vars = np.sum(self.selected_modes_mask)
self.P = self.normal_modes.Pmatrix[:, self.selected_modes_mask]
self.Pm1 = self.normal_modes.Pmatrix_inverse[self.selected_modes_mask]
else:
# this is the case used by nonlinear_solver.remove_modes
self.selected_modes_mask_partial = selected_modes_mask
logger.info("Further mode reduction:")
logger.info(
" %d previously included modes couple with the plasma less than 'min_dIy_dI' (following Jacobian calculation)",
len(selected_modes_mask) - np.sum(selected_modes_mask),
)
self.n_independent_vars = np.sum(self.selected_modes_mask_partial)
logger.info(
" Final number of modes = %d (%d active coils + %d passive structures)",
self.n_independent_vars,
self.n_active_coils,
self.n_independent_vars - self.n_active_coils,
)
self.P = self.P[:, self.selected_modes_mask_partial]
self.Pm1 = self.Pm1[self.selected_modes_mask_partial]
retained_full_indices = np.flatnonzero(self.selected_modes_mask)[
self.selected_modes_mask_partial
]
self.selected_modes_mask[:] = False
self.selected_modes_mask[retained_full_indices] = True
selection_mask = self.selected_modes_mask_partial
# this is not needed any longer and now incorrect, the eigenvectors in P are independent but NOT orthogonal
# self.Pm1 = (self.P).T
# Note Lambda is not actually diagonal because the passive structures has been
# diagonalised separately from the active coils. The modes of used for the passive structures
# diagonalise the isolated dynamics of the walls.
# Equation is Lambda**(-1)Iddot + I = F
self.Lambdam1 = self.Pm1 @ (self.rm1l_non_symm @ self.P)
# self.RP = np.diag(self.coil_resist) @ self.P
# self.RP_inv = np.linalg.solve(self.RP.T @ self.RP, self.RP.T)
# self.Lambdam1 = (self.RP_inv @ self.coil_self_ind) @ self.P
self.solver = implicit_euler_solver(
Mmatrix=self.Lambdam1,
Rmatrix=np.eye(self.n_independent_vars),
max_internal_timestep=self.max_internal_timestep,
full_timestep=self.full_timestep,
)
if self.flag_plasma:
self.forcing_term = self.forcing_term_eig_plasma
else:
self.forcing_term = self.forcing_term_eig_no_plasma
return selection_mask
[docs]
def reset_active_coil_resistances(
self, active_coil_resistances: np.ndarray
) -> None:
"""
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).
"""
self.coil_resist = np.concatenate(
(active_coil_resistances, self.coil_resist[self.n_active_coils :])
)
self.active_coil_resistances = np.copy(self.coil_resist[: self.n_active_coils])
self.Rm1 = 1 / self.coil_resist
self.build_rm1l()
self.Lambdam1 = self.Pm1 @ (self.rm1l_non_symm @ self.P)
[docs]
def initialize_for_no_eig(self) -> None:
"""
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.
"""
# Equation is Mmatrix Idot + Rmatrix I = F
self.solver = implicit_euler_solver(
Mmatrix=self.coil_self_ind,
Rmatrix=np.diag(self.coil_resist),
max_internal_timestep=self.max_internal_timestep,
full_timestep=self.full_timestep,
)
if self.flag_plasma:
self.forcing_term = self.forcing_term_no_eig_plasma
else:
self.forcing_term = self.forcing_term_no_eig_no_plasma
[docs]
def reset_timesteps(
self, max_internal_timestep: float, full_timestep: float
) -> None:
"""
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.
"""
self.solver.set_timesteps(
full_timestep=full_timestep, max_internal_timestep=max_internal_timestep
)
[docs]
def forcing_term_eig_plasma(
self, active_voltage_vec: np.ndarray, Iydot: np.ndarray
) -> np.ndarray:
"""
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
-------
np.ndarray
Forcing term expressed in the eigenmode basis.
"""
all_Us = np.zeros_like(self.empty_U)
all_Us[: self.n_active_coils] = active_voltage_vec
all_Us -= self.Mey @ Iydot
all_Us = np.dot(self.Pm1, self.Rm1 * all_Us)
return all_Us
[docs]
def forcing_term_eig_no_plasma(
self, active_voltage_vec: np.ndarray, Iydot: np.ndarray | float = 0
) -> np.ndarray:
"""
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
-------
np.ndarray
Forcing term expressed in the eigenmode basis.
"""
all_Us = self.empty_U.copy()
all_Us[: self.n_active_coils] = active_voltage_vec
all_Us = np.dot(self.Pm1, self.Rm1 * all_Us)
return all_Us
[docs]
def forcing_term_no_eig_plasma(
self, active_voltage_vec: np.ndarray, Iydot: np.ndarray
) -> np.ndarray:
"""
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
-------
np.ndarray
Forcing term in the physical coil basis.
"""
all_Us = self.empty_U.copy()
all_Us[: self.n_active_coils] = active_voltage_vec
all_Us -= np.dot(self.Mey, Iydot)
return all_Us
[docs]
def forcing_term_no_eig_no_plasma(
self, active_voltage_vec: np.ndarray, Iydot: np.ndarray | float = 0
) -> np.ndarray:
"""
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
-------
np.ndarray
Forcing term in the physical coil basis.
"""
all_Us = self.empty_U.copy()
all_Us[: self.n_active_coils] = active_voltage_vec
return all_Us
[docs]
def IvesseltoId(self, Ivessel: np.ndarray) -> np.ndarray:
"""
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
-------
np.ndarray
Currents in the eigenmode basis.
"""
return self.Pm1 @ Ivessel
[docs]
def IdtoIvessel(self, Id: np.ndarray) -> np.ndarray:
"""
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
-------
np.ndarray
Vessel currents (all metals).
"""
return self.P @ Id
[docs]
def stepper(
self,
It: np.ndarray,
active_voltage_vec: np.ndarray,
Iydot: np.ndarray | float = 0,
) -> np.ndarray:
"""
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
-------
np.ndarray
Currents at time t+dt.
"""
forcing = self.forcing_term(active_voltage_vec, Iydot)
It = self.solver.full_stepper(It, forcing)
return It
[docs]
def Mey(
self,
eq: object,
) -> np.ndarray:
"""
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
-------
np.ndarray
Array of shape (n_coils, n_plasma_pts) of mutual inductances
between all coils and the plasma grid points.
"""
coils_dict = eq.tokamak.coils_dict
mey = np.zeros((eq.tokamak.n_coils, len(self.plasma_pts)))
for j, labelj in enumerate(eq.tokamak.coils_list):
greenm = Greens(
self.plasma_pts[:, 0, np.newaxis],
self.plasma_pts[:, 1, np.newaxis],
coils_dict[labelj]["coords"][0][np.newaxis, :],
coils_dict[labelj]["coords"][1][np.newaxis, :],
)
greenm *= coils_dict[labelj]["polarity"][np.newaxis, :]
greenm *= coils_dict[labelj]["multiplier"][np.newaxis, :]
mey[j] = np.sum(greenm, axis=-1)
if self.passive_reflection_operator is not None:
reflection = np.eye(self.n_coils)
reflection[self.n_active_coils :, self.n_active_coils :] = (
self.passive_reflection_operator
)
grid_indices = -np.ones((eq.nx, eq.ny), dtype=int)
mask = eq.limiter_handler.mask_inside_limiter
grid_indices[mask] = np.arange(len(self.plasma_pts))
reflected_indices = grid_indices[:, ::-1][mask]
if np.any(reflected_indices < 0):
raise ValueError(
"The limiter mask must be up-down symmetric when a passive "
"reflection operator is supplied."
)
# Enforce M_ey = S_m M_ey S_y. The column permutation applies
# plasma-grid reflection S_y; the matrix product applies metal
# reflection S_m.
mey = 0.5 * (
mey + np.einsum("ij,jp->ip", reflection, mey[:, reflected_indices])
)
return 2 * np.pi * mey