freegsnke.machine module

Classes and routines to represent coils and circuits in the tokamak machine.

Copyright 2024 Nicola C. Amorisco, Adriano Agnello, George K. Holt, Ben Dudson.

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.machine.Circuit(coils: list[tuple[str, object, float]], current: float = 0.0, control: bool = True)[source]

Bases: object

Represents a collection of coils connected to the same circuit.

Br(R: ndarray | float, Z: ndarray | float) → ndarray | float[source]

Calculate radial magnetic field Br at (R,Z).

Parameters:
  • R (float or np.array) – Radial position(s) to evaluate at.

  • Z (float or np.array) – Vertical position(s) to evaluate at.

Returns:

Radial magnetic field at (R,Z) locations [T].

Return type:

np.array

Bz(R: ndarray | float, Z: ndarray | float) → ndarray | float[source]

Calculate vertical magnetic field Bz at (R,Z).

Parameters:
  • R (float or np.array) – Radial position(s) to evaluate at.

  • Z (float or np.array) – Vertical position(s) to evaluate at.

Returns:

Vertical magnetic field at (R,Z) locations [T].

Return type:

np.array

__init__(coils: list[tuple[str, object, float]], current: float = 0.0, control: bool = True) → None[source]

Initialise a circuit of coils sharing a single current.

Parameters:
  • coils (list of tuple) – The coils in the circuit as [(label, coil, multiplier), ...], where coil is a Coil-like object and multiplier scales the circuit current in that coil (e.g. -1.0 for opposite polarity).

  • current (float, optional) – Current in the circuit [A]. Default is 0.0.

  • control (bool, optional) – Whether the circuit current is to be inferred during an inverse solve (True) or not (False). Default is True.

calcPsiFromGreens(pgreen: dict[str, ndarray]) → ndarray | float[source]

Calculate the poloidal flux from pre-computed Greens functions.

The current of each coil is first set to the circuit current times its multiplier.

Parameters:

pgreen (dict[str, np.ndarray]) – Greens functions for each coil in the circuit, keyed by coil label.

Returns:

Poloidal flux from the Greens functions [Webers/2pi].

Return type:

np.ndarray or float

controlBr(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the radial magnetic field Br at (R,Z) due to a single unit [Amp] of current in the coils in the circuit.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Radial magnetic field Br at (R,Z) per unit current [T/A].

Return type:

np.ndarray

controlBz(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the vertical magnetic field Bz at (R,Z) due to a single unit [Amp] of current in the coils in the circuit.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Vertical magnetic field Bz at (R,Z) per unit current [T/A].

Return type:

np.ndarray

controlPsi(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the poloidal flux at (R,Z) due to a single unit [Amp] of current in the coils in the circuit.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Poloidal flux at (R,Z) per unit current [Webers/2pi/A].

Return type:

np.ndarray

controldBrdr(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the radial derivative of Br, dBr/dR at (R,Z) due to a single unit [Amp] of current in the coils in the circuit.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Radial derivative of Br, dBr/dR at (R,Z) per unit current [T/m/A].

Return type:

np.ndarray

controldBrdz(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the vertical derivative of Br, dBr/dZ at (R,Z) due to a single unit [Amp] of current in the coils in the circuit.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Vertical derivative of Br, dBr/dZ at (R,Z) per unit current [T/m/A].

Return type:

np.ndarray

controldBzdr(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the radial derivative of Bz, dBz/dR at (R,Z) due to a single unit [Amp] of current in the coils in the circuit.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Radial derivative of Bz, dBz/dR at (R,Z) per unit current [T/m/A].

Return type:

np.ndarray

controldBzdz(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the vertical derivative of Bz, dBz/dZ at (R,Z) due to a single unit [Amp] of current in the coils in the circuit.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Vertical derivative of Bz, dBz/dZ at (R,Z) per unit current [T/m/A].

Return type:

np.ndarray

copy() → Circuit[source]

Create a copy of the circuit by initialising a new Circuit object.

The coils forming the circuit are copied by calling their individual copy methods.

Returns:

New circuit with copies of the coils and the same current and control flag.

Return type:

Circuit

createBrGreensVec(R: ndarray, Z: ndarray) → ndarray[source]

Calculate the Greens function for the radial magnetic field Br of the circuit. The contribution of each coil is weighted by its circuit multiplier.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

Returns:

Radial magnetic field Br at (R,Z) per unit current in the circuit [T/A], same shape as R.

Return type:

np.ndarray

createBzGreensVec(R: ndarray, Z: ndarray) → ndarray[source]

Calculate the Greens function for the vertical magnetic field Bz of the circuit. The contribution of each coil is weighted by its circuit multiplier.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

Returns:

Vertical magnetic field Bz at (R,Z) per unit current in the circuit [T/A], same shape as R.

Return type:

np.ndarray

createPsiGreens(R: ndarray | float, Z: ndarray | float) → dict[str, ndarray][source]

Calculate the flux Greens function for each coil in the circuit at (R,Z).

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Flux Greens function at (R,Z) for each coil, keyed by coil label.

Return type:

dict[str, np.ndarray]

createPsiGreensVec(R: ndarray, Z: ndarray) → ndarray[source]

Calculate the Greens function for the poloidal flux of the circuit. The contribution of each coil is weighted by its circuit multiplier.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

Returns:

Poloidal flux at (R,Z) per unit current in the circuit [Webers/2pi/A], same shape as R.

Return type:

np.ndarray

createdBrdrGreensVec(R: ndarray, Z: ndarray) → ndarray[source]

Calculate the Greens function for the radial derivative of Br, dBr/dR of the circuit. The contribution of each coil is weighted by its circuit multiplier.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

Returns:

Radial derivative of Br, dBr/dR at (R,Z) per unit current in the circuit [T/m/A], same shape as R.

Return type:

np.ndarray

createdBrdzGreensVec(R: ndarray, Z: ndarray) → ndarray[source]

Calculate the Greens function for the vertical derivative of Br, dBr/dZ of the circuit. The contribution of each coil is weighted by its circuit multiplier.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

Returns:

Vertical derivative of Br, dBr/dZ at (R,Z) per unit current in the circuit [T/m/A], same shape as R.

Return type:

np.ndarray

createdBzdrGreensVec(R: ndarray, Z: ndarray) → ndarray[source]

Calculate the Greens function for the radial derivative of Bz, dBz/dR of the circuit. The contribution of each coil is weighted by its circuit multiplier.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

Returns:

Radial derivative of Bz, dBz/dR at (R,Z) per unit current in the circuit [T/m/A], same shape as R.

Return type:

np.ndarray

createdBzdzGreensVec(R: ndarray, Z: ndarray) → ndarray[source]

Calculate the Greens function for the vertical derivative of Bz, dBz/dZ of the circuit. The contribution of each coil is weighted by its circuit multiplier.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

Returns:

Vertical derivative of Bz, dBz/dZ at (R,Z) per unit current in the circuit [T/m/A], same shape as R.

Return type:

np.ndarray

dtype = dtype([('label', 'O'), ('coil', [('R', '<f8'), ('Z', '<f8'), ('current', '<f8'), ('turns', '<i8'), ('control', '?')]), ('multiplier', '<f8')])
classmethod from_numpy_array(value: ndarray) → Circuit[source]

Create a Circuit from a structured NumPy array.

The current and control values are taken from the first coil in the array, which should be consistent with the others.

Parameters:

value (np.ndarray) – Array with dtype Circuit.dtype (see to_numpy_array).

Returns:

The reconstructed circuit.

Return type:

Circuit

Raises:

ValueError – If the dtype of value does not match Circuit.dtype.

getForces(equilibrium: object) → dict[str, ndarray][source]

Calculate the forces on the coils in the circuit.

Parameters:

equilibrium (object) – Equilibrium (or machine) object providing Br and Bz methods.

Returns:

Coil labels mapped to the force on each coil, [Fr, Fz] [N].

Return type:

dict[str, np.ndarray]

plot(axis: Axes | None = None, show: bool = False) → Axes | None[source]

Plot the coils in the circuit.

Parameters:
  • axis (matplotlib.axes.Axes, optional) – Existing axis to plot on. If None, the coils create a new axis.

  • show (bool, optional) – If True, display the plot after plotting the coils. Default is False.

Returns:

The axis used for plotting the coils.

Return type:

matplotlib.axes.Axes or None

psi(R: ndarray | float, Z: ndarray | float) → ndarray | float[source]

Poloidal flux due to coils in the circuit (at chosen R and Z).

Parameters:
  • R (float or np.array) – Radial position(s) to evaluate at.

  • Z (float or np.array) – Vertical position(s) to evaluate at.

Returns:

The poloidal flux due to the coils at chosen (R,Z) locations [Webers/2pi].

Return type:

float or np.array

string_dtype = dtype('O')
to_numpy_array() → ndarray[source]

Convert the Circuit object to a structured NumPy array.

Returns:

Array with dtype Circuit.dtype; each entry holds the coil label, the NumPy representation of the coil, and the coil multiplier.

Return type:

np.ndarray

freegsnke.machine.DIIID() → Machine[source]

Create the DIII-D machine: PF coil set from ef20030203.d3d (Corsica code).

Returns:

Machine with the DIII-D PF coils.

Return type:

Machine

freegsnke.machine.EmptyTokamak() → Machine[source]

Create a tokamak with no coils.

Returns:

Machine with an empty coil list.

Return type:

Machine

freegsnke.machine.MAST() → Machine[source]

Create an approximation of the Mega-Amp Spherical Tokamak (MAST) with independent coils.

Each PF coil is powered separately.

Returns:

Machine with coils P2-P6 (upper and lower) and the solenoid P1.

Return type:

Machine

freegsnke.machine.MASTU() → Machine[source]

Create a more detailed, but not complete, MAST-Upgrade (MAST-U) description with a wall.

Uses MultiCoil to represent coils with different locations for each strand.

Returns:

Machine with MAST-U multi-strand coils and wall.

Return type:

Machine

freegsnke.machine.MASTU_simple() → Machine[source]

Create a simplified MAST-Upgrade (MAST-U) coil set with a wall.

This is an older version of the MAST-U coilset, with one strand per coil. It may be easier to use for initial development of scenarios, but is less detailed than the MultiCoil description (MASTU).

Returns:

Machine with simplified MAST-U coils and wall.

Return type:

Machine

freegsnke.machine.MAST_sym() → Machine[source]

Create an approximation of the Mega-Amp Spherical Tokamak (MAST) with up-down paired coils.

The upper and lower coils are connected to the same circuits P2-P6.

Returns:

Machine with circuits P2-P6 and the solenoid P1.

Return type:

Machine

class freegsnke.machine.Machine(coils: list[tuple[str, object]], wall: Wall | None = None)[source]

Bases: object

Represents the tokamak machine, including the coils and the wall.

coils

A list of tuples containing labeled coil components in the format: [(label, Coil | Circuit | Solenoid)]

Type:

list of tuples

wall

The wall structure of the machine, if defined.

Type:

Wall, optional

Notes

  • A list is used instead of a dictionary to maintain the order of coils, allowing for easy updates by the control system.

  • The __getitem__ method is implemented to allow access to coils by label.

Br(R: ndarray | float, Z: ndarray | float) → ndarray | float[source]

Calculate radial magnetic field Br at (R,Z).

Parameters:
  • R (float or np.array) – Radial position(s) to evaluate at.

  • Z (float or np.array) – Vertical position(s) to evaluate at.

Returns:

Radial magnetic field at (R,Z) locations [T].

Return type:

np.array

Bz(R: ndarray | float, Z: ndarray | float) → ndarray | float[source]

Calculate vertical magnetic field Br at (R,Z).

Parameters:
  • R (float or np.array) – Radial position(s) to evaluate at.

  • Z (float or np.array) – Vertical position(s) to evaluate at.

Returns:

Vertical magnetic field at (R,Z) locations [T].

Return type:

np.array

__init__(coils: list[tuple[str, object]], wall: Wall | None = None) → None[source]

Initialise a Machine from labelled coils and an optional wall.

Also builds the coil name list, the label-to-index map coil_order and the current vector current_vec.

Parameters:
  • coils (list of tuple) – Labelled coil components [(label, Coil | Circuit | Solenoid), ...].

  • wall (Wall, optional) – The wall of the machine. Default is None.

calcPsiFromGreens(pgreen: dict[str, object]) → ndarray | float[source]

Calculate the poloidal flux from pre-computed Greens functions.

Parameters:

pgreen (dict) – Greens functions for the coils, keyed by coil label.

Returns:

Poloidal flux from the Greens functions [Webers/2pi].

Return type:

np.ndarray or float

controlAdjust(current_change: ndarray) → None[source]

Adjust the currents of the coils that have control=True.

Parameters:

current_change (np.ndarray) – Changes in coil currents to apply [A], one per controlled coil and in the same order as the lists returned by controlBr, controlBz, etc. Each element must support .item().

controlBr(R: ndarray | float, Z: ndarray | float) → list[ndarray | float][source]

Calculate the radial magnetic field Br at (R,Z) due to a single unit [Amp] of current in each controlled coil.

Only coils with control=True are included.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

One entry per controlled coil: the radial magnetic field Br at (R,Z) per unit current [T/A].

Return type:

list

controlBz(R: ndarray | float, Z: ndarray | float) → list[ndarray | float][source]

Calculate the vertical magnetic field Bz at (R,Z) due to a single unit [Amp] of current in each controlled coil.

Only coils with control=True are included.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

One entry per controlled coil: the vertical magnetic field Bz at (R,Z) per unit current [T/A].

Return type:

list

controlCurrents() → list[float][source]

Return the currents of the coils being controlled.

Returns:

Currents [A] of the coils with control=True (those solved for in an inverse solve).

Return type:

list of float

controlPsi(R: ndarray | float, Z: ndarray | float) → list[ndarray | float][source]

Calculate the poloidal flux at (R,Z) due to a single unit [Amp] of current in each controlled coil.

Only coils with control=True are included.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

One entry per controlled coil: the poloidal flux at (R,Z) per unit current [Webers/2pi/A].

Return type:

list

controldBrdr(R: ndarray | float, Z: ndarray | float) → list[ndarray | float][source]

Calculate the radial derivative of Br, dBr/dR at (R,Z) due to a single unit [Amp] of current in each controlled coil.

Only coils with control=True are included.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

One entry per controlled coil: the radial derivative of Br, dBr/dR at (R,Z) per unit current [T/m/A].

Return type:

list

controldBrdz(R: ndarray | float, Z: ndarray | float) → list[ndarray | float][source]

Calculate the vertical derivative of Br, dBr/dZ at (R,Z) due to a single unit [Amp] of current in each controlled coil.

Only coils with control=True are included.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

One entry per controlled coil: the vertical derivative of Br, dBr/dZ at (R,Z) per unit current [T/m/A].

Return type:

list

controldBzdr(R: ndarray | float, Z: ndarray | float) → list[ndarray | float][source]

Calculate the radial derivative of Bz, dBz/dR at (R,Z) due to a single unit [Amp] of current in each controlled coil.

Only coils with control=True are included.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

One entry per controlled coil: the radial derivative of Bz, dBz/dR at (R,Z) per unit current [T/m/A].

Return type:

list

controldBzdz(R: ndarray | float, Z: ndarray | float) → list[ndarray | float][source]

Calculate the vertical derivative of Bz, dBz/dZ at (R,Z) due to a single unit [Amp] of current in each controlled coil.

Only coils with control=True are included.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

One entry per controlled coil: the vertical derivative of Bz, dBz/dZ at (R,Z) per unit current [T/m/A].

Return type:

list

copy() → Machine[source]

Create a copy of the machine by initialising a new object.

The coils are copied by calling the copy method of each coil/circuit.

Returns:

New machine with copied coils and the same wall object.

Return type:

Machine

createBrGreensVec(R: ndarray, Z: ndarray, coils: list[tuple[str, object]] | None = None) → ndarray[source]

Pre-compute the Greens functions for the radial magnetic field Br of each coil.

The result can be stored and used later to evaluate the field quickly.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

  • coils (list of tuple, optional) – Labelled coils to use. Default is None, which uses self.coils.

Returns:

Greens functions of shape (len(coils), *R.shape).

Return type:

np.ndarray

createBzGreensVec(R: ndarray, Z: ndarray, coils: list[tuple[str, object]] | None = None) → ndarray[source]

Pre-compute the Greens functions for the vertical magnetic field Bz of each coil.

The result can be stored and used later to evaluate the field quickly.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

  • coils (list of tuple, optional) – Labelled coils to use. Default is None, which uses self.coils.

Returns:

Greens functions of shape (len(coils), *R.shape).

Return type:

np.ndarray

createPsiGreens(R: ndarray | float, Z: ndarray | float) → dict[str, object][source]

Calculate the flux Greens function of each coil at (R,Z).

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Flux Greens functions at (R,Z), keyed by coil label.

Return type:

dict

createPsiGreensVec(R: ndarray, Z: ndarray, coils: list[tuple[str, object]] | None = None) → ndarray[source]

Pre-compute the Greens functions for the poloidal flux of each coil.

The result can be stored and used later to evaluate the field quickly.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

  • coils (list of tuple, optional) – Labelled coils to use. Default is None, which uses self.coils.

Returns:

Greens functions of shape (len(coils), *R.shape).

Return type:

np.ndarray

createdBrdrGreensVec(R: ndarray, Z: ndarray, coils: list[tuple[str, object]] | None = None) → ndarray[source]

Pre-compute the Greens functions for the radial derivative of Br, dBr/dR of each coil.

The result can be stored and used later to evaluate the field quickly.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

  • coils (list of tuple, optional) – Labelled coils to use. Default is None, which uses self.coils.

Returns:

Greens functions of shape (len(coils), *R.shape).

Return type:

np.ndarray

createdBrdzGreensVec(R: ndarray, Z: ndarray, coils: list[tuple[str, object]] | None = None) → ndarray[source]

Pre-compute the Greens functions for the vertical derivative of Br, dBr/dZ of each coil.

The result can be stored and used later to evaluate the field quickly.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

  • coils (list of tuple, optional) – Labelled coils to use. Default is None, which uses self.coils.

Returns:

Greens functions of shape (len(coils), *R.shape).

Return type:

np.ndarray

createdBzdrGreensVec(R: ndarray, Z: ndarray, coils: list[tuple[str, object]] | None = None) → ndarray[source]

Pre-compute the Greens functions for the radial derivative of Bz, dBz/dR of each coil.

The result can be stored and used later to evaluate the field quickly.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

  • coils (list of tuple, optional) – Labelled coils to use. Default is None, which uses self.coils.

Returns:

Greens functions of shape (len(coils), *R.shape).

Return type:

np.ndarray

createdBzdzGreensVec(R: ndarray, Z: ndarray, coils: list[tuple[str, object]] | None = None) → ndarray[source]

Pre-compute the Greens functions for the vertical derivative of Bz, dBz/dZ of each coil.

The result can be stored and used later to evaluate the field quickly.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

  • coils (list of tuple, optional) – Labelled coils to use. Default is None, which uses self.coils.

Returns:

Greens functions of shape (len(coils), *R.shape).

Return type:

np.ndarray

getCurrents() → dict[str, float][source]

Return the current in each coil.

Returns:

Coil labels mapped to their currents [A].

Return type:

dict[str, float]

getCurrentsVec(coils: list[tuple[str, object]] | None = None) → ndarray[source]

Return the coil currents as an array, and store it as current_vec.

Parameters:

coils (list of tuple, optional) – Labelled coils to read. Default is None, which uses self.coils.

Returns:

Coil currents [A], in the order of coils.

Return type:

np.ndarray

getForces(equilibrium: object | None = None) → dict[str, ndarray][source]

Calculate the forces on the coils.

Parameters:

equilibrium (object, optional) – Object providing Br and Bz methods. Default is None, which uses the machine itself.

Returns:

Coil labels mapped to the force on each coil, [Fr, Fz] [N].

Return type:

dict[str, np.ndarray]

getPsitokamak(vgreen: ndarray) → ndarray[source]

Calculate the flux from all active and passive coil currents.

The leading axis of vgreen indexes coils. Flattening the spatial axes expresses the weighted sum as one matrix multiplication, avoiding the full-sized temporary array created by broadcast multiplication.

Parameters:

vgreen (np.ndarray) – Greens functions of shape (n_coils, *spatial_shape), in the order of current_vec.

Returns:

Poloidal flux of shape spatial_shape [Webers/2pi].

Return type:

np.ndarray

plot(axis: Axes | None = None, show: bool = True) → Axes | None[source]

Plot the coils of the machine.

Parameters:
  • axis (matplotlib.axes.Axes, optional) – Existing axis to plot on. If None, the coils create a new axis.

  • show (bool, optional) – If True, display the plot after plotting the coils. Default is True.

Returns:

The axis used for plotting the coils.

Return type:

matplotlib.axes.Axes or None

printCurrents() → None[source]

Print the label and state of each coil.

psi(R: ndarray | float, Z: ndarray | float) → ndarray | float[source]

Poloidal flux due to the coils (at chosen R and Z).

Parameters:
  • R (float or np.array) – Radial position(s) to evaluate at.

  • Z (float or np.array) – Vertical position(s) to evaluate at.

Returns:

The poloidal flux due to the coils at chosen (R,Z) locations [Webers/2pi].

Return type:

float or np.array

setControlCurrents(currents: list[float] | ndarray) → None[source]

Set the currents of the coils being controlled.

Parameters:

currents (list of float or np.ndarray) – Currents [A] for the coils with control=True, in machine coil order.

set_all_coil_currents(current_vec: ndarray) → None[source]

Set the currents of the first len(current_vec) coils, assuming the correct order.

Parameters:

current_vec (np.ndarray) – Current values [A].

set_coil_current(coil_label: str, current_value: float) → None[source]

Set the current of a single coil.

Parameters:
  • coil_label (str) – Coil label, as in self.getCurrents().keys().

  • current_value (float) – Current value [A].

freegsnke.machine.MirroredCoil(R: float, Z: float, current: float = 0.0, turns: int = 1, control: bool = True, area: ~freegsnke.coil.AreaCurrentLimit = <freegsnke.coil.AreaCurrentLimit object>, symmetric: bool = True) → Circuit[source]

Create a circuit of two coils intended to be positioned symmetrically about Z=0.

Parameters:
  • R (float) – Radial position of the coils [m].

  • Z (float) – Vertical position of the coils [m].

  • current (float, optional) – Current flowing through each coil [A]. Default is 0.0.

  • turns (int, optional) – Number of turns in each coil. Default is 1.

  • control (bool, optional) – Whether the coils are actively controlled during an inverse solve. Default is True.

  • area (AreaCurrentLimit, optional) – Current limit constraints for the coils. Default is an AreaCurrentLimit instance.

  • symmetric (bool, optional) – If True, both coils carry the same current direction (multiplier +1). If False, the lower coil has multiplier -1. Default is True.

Returns:

Circuit with coils labelled “U” and “L”.

Return type:

Circuit

Notes

Both coils are currently constructed at the same (R, Z); the lower coil is not reflected to -Z.

class freegsnke.machine.Solenoid(Rs: float, Zsmin: float, Zsmax: float, Ns: int, current: float = 0.0, control: bool = True)[source]

Bases: object

Represents a central solenoid in the tokamak.

Br(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the radial magnetic field Br due to the solenoid at (R,Z).

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Radial magnetic field Br at (R,Z) [T].

Return type:

np.ndarray

Bz(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the vertical magnetic field Bz due to the solenoid at (R,Z).

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Vertical magnetic field Bz at (R,Z) [T].

Return type:

np.ndarray

__init__(Rs: float, Zsmin: float, Zsmax: float, Ns: int, current: float = 0.0, control: bool = True) → None[source]

Initialise a solenoid with the specified parameters.

The solenoid is modelled as Ns filaments at radius Rs, evenly spaced in Z between Zsmin and Zsmax.

Parameters:
  • Rs (float) – Radius of the solenoid [m].

  • Zsmin (float) – Lower vertical limit of the solenoid [m].

  • Zsmax (float) – Upper vertical limit of the solenoid [m].

  • Ns (int) – Number of turns in the solenoid.

  • current (float, optional) – Current flowing through each turn [A]. Default is 0.0.

  • control (bool, optional) – Whether the current is inferred during an inverse solve. Default is True.

calcPsiFromGreens(pgreen: ndarray) → ndarray[source]

Calculate the poloidal flux from a pre-computed Greens function.

Parameters:

pgreen (np.ndarray) – Flux Greens function of the solenoid.

Returns:

Poloidal flux from the Greens function [Webers/2pi].

Return type:

np.ndarray

controlBr(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the radial magnetic field Br at (R,Z) due to a single unit [Amp] of current in the solenoid.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Radial magnetic field Br at (R,Z) per unit current [T/A].

Return type:

np.ndarray

controlBz(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the vertical magnetic field Bz at (R,Z) due to a single unit [Amp] of current in the solenoid.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Vertical magnetic field Bz at (R,Z) per unit current [T/A].

Return type:

np.ndarray

controlPsi(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the poloidal flux at (R,Z) due to a single unit [Amp] of current in the solenoid.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Poloidal flux at (R,Z) per unit current [Webers/2pi/A].

Return type:

np.ndarray

controldBrdr(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the radial derivative of Br, dBr/dR at (R,Z) due to a single unit [Amp] of current in the solenoid.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Radial derivative of Br, dBr/dR at (R,Z) per unit current [T/m/A].

Return type:

np.ndarray

controldBrdz(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the vertical derivative of Br, dBr/dZ at (R,Z) due to a single unit [Amp] of current in the solenoid.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Vertical derivative of Br, dBr/dZ at (R,Z) per unit current [T/m/A].

Return type:

np.ndarray

controldBzdr(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the radial derivative of Bz, dBz/dR at (R,Z) due to a single unit [Amp] of current in the solenoid.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Radial derivative of Bz, dBz/dR at (R,Z) per unit current [T/m/A].

Return type:

np.ndarray

controldBzdz(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the vertical derivative of Bz, dBz/dZ at (R,Z) due to a single unit [Amp] of current in the solenoid.

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Vertical derivative of Bz, dBz/dZ at (R,Z) per unit current [T/m/A].

Return type:

np.ndarray

createBrGreensVec(R: ndarray, Z: ndarray) → ndarray[source]

Calculate the Greens function for the radial magnetic field Br of the solenoid.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

Returns:

Radial magnetic field Br at (R,Z) per unit current in the solenoid [T/A], same shape as R.

Return type:

np.ndarray

createBzGreensVec(R: ndarray, Z: ndarray) → ndarray[source]

Calculate the Greens function for the vertical magnetic field Bz of the solenoid.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

Returns:

Vertical magnetic field Bz at (R,Z) per unit current in the solenoid [T/A], same shape as R.

Return type:

np.ndarray

createPsiGreens(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the flux Greens function for the solenoid at (R,Z).

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Flux per unit current in the solenoid at (R,Z) [Webers/2pi/A].

Return type:

np.ndarray

createPsiGreensVec(R: ndarray, Z: ndarray) → ndarray[source]

Calculate the Greens function for the poloidal flux of the solenoid.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

Returns:

Poloidal flux at (R,Z) per unit current in the solenoid [Webers/2pi/A], same shape as R.

Return type:

np.ndarray

createdBrdrGreensVec(R: ndarray, Z: ndarray) → ndarray[source]

Calculate the Greens function for the radial derivative of Br, dBr/dR of the solenoid.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

Returns:

Radial derivative of Br, dBr/dR at (R,Z) per unit current in the solenoid [T/m/A], same shape as R.

Return type:

np.ndarray

createdBrdzGreensVec(R: ndarray, Z: ndarray) → ndarray[source]

Calculate the Greens function for the vertical derivative of Br, dBr/dZ of the solenoid.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

Returns:

Vertical derivative of Br, dBr/dZ at (R,Z) per unit current in the solenoid [T/m/A], same shape as R.

Return type:

np.ndarray

createdBzdrGreensVec(R: ndarray, Z: ndarray) → ndarray[source]

Calculate the Greens function for the radial derivative of Bz, dBz/dR of the solenoid.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

Returns:

Radial derivative of Bz, dBz/dR at (R,Z) per unit current in the solenoid [T/m/A], same shape as R.

Return type:

np.ndarray

createdBzdzGreensVec(R: ndarray, Z: ndarray) → ndarray[source]

Calculate the Greens function for the vertical derivative of Bz, dBz/dZ of the solenoid.

Parameters:
  • R (np.ndarray) – Radial positions to evaluate at [m].

  • Z (np.ndarray) – Vertical positions to evaluate at [m] (same shape as R).

Returns:

Vertical derivative of Bz, dBz/dZ at (R,Z) per unit current in the solenoid [T/m/A], same shape as R.

Return type:

np.ndarray

dBrdr(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the magnetic field derivative dBr/dR due to the solenoid at (R,Z).

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Magnetic field derivative dBr/dR at (R,Z) [T/m].

Return type:

np.ndarray

dBrdz(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the magnetic field derivative dBr/dZ due to the solenoid at (R,Z).

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Magnetic field derivative dBr/dZ at (R,Z) [T/m].

Return type:

np.ndarray

dBzdr(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the magnetic field derivative dBz/dR due to the solenoid at (R,Z).

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Magnetic field derivative dBz/dR at (R,Z) [T/m].

Return type:

np.ndarray

dBzdz(R: ndarray | float, Z: ndarray | float) → ndarray[source]

Calculate the magnetic field derivative dBz/dZ due to the solenoid at (R,Z).

Parameters:
  • R (np.ndarray or float) – Radial position(s) to evaluate at [m].

  • Z (np.ndarray or float) – Vertical position(s) to evaluate at [m].

Returns:

Magnetic field derivative dBz/dZ at (R,Z) [T/m].

Return type:

np.ndarray

dtype = dtype([('Rs', '<f8'), ('Zsmin', '<f8'), ('Zsmax', '<f8'), ('Ns', '<f8'), ('current', '<f8'), ('control', '?')])
classmethod from_numpy_array(value: ndarray) → Solenoid[source]

Create a Solenoid from a structured NumPy array.

Parameters:

value (np.ndarray) – Zero-dimensional array with dtype Solenoid.dtype (see to_numpy_array).

Returns:

The reconstructed solenoid.

Return type:

Solenoid

Raises:

ValueError – If the dtype of value does not match Solenoid.dtype.

getForces(equilibrium: object) → dict[source]

Calculate the forces on the solenoid (not currently implemented).

Parameters:

equilibrium (object) – Equilibrium object (unused).

Returns:

Always an empty dictionary.

Return type:

dict

plot(axis: Axes | None = None, show: bool = False) → Axes | None[source]

Plot the solenoid (currently a placeholder that draws nothing).

Parameters:
  • axis (matplotlib.axes.Axes, optional) – Existing axis to plot on (returned unchanged).

  • show (bool, optional) – Currently has no effect.

Returns:

The axis argument, unchanged.

Return type:

matplotlib.axes.Axes or None

psi(R: ndarray | float, Z: ndarray | float) → ndarray | float[source]

Poloidal flux due to the solenoid (at chosen R and Z).

Parameters:
  • R (float or np.array) – Radial position(s) to evaluate at.

  • Z (float or np.array) – Vertical position(s) to evaluate at.

Returns:

The poloidal flux due to the solenoid at chosen (R,Z) locations [Webers/2pi].

Return type:

float or np.array

to_numpy_array() → ndarray[source]

Convert the Solenoid object to a structured NumPy array.

Returns:

Array with dtype Solenoid.dtype holding Rs, Zsmin, Zsmax, Ns, current and control.

Return type:

np.ndarray

freegsnke.machine.TCV() → Machine[source]

Create an approximation of the TCV coil set.

Returns:

Machine with the TCV PF coils and an ohmic circuit “OH” (no wall).

Return type:

Machine

freegsnke.machine.TestTokamak() → Machine[source]

Create a simple test tokamak with four coils and a wall.

Returns:

Machine with coils P1L, P1U, P2L, P2U and a simple wall.

Return type:

Machine

class freegsnke.machine.Wall(R: ndarray, Z: ndarray)[source]

Bases: object

Represents the wall of the device. Consists of an ordered list of (R,Z) points.

__init__(R: ndarray, Z: ndarray) → None[source]

Initialise a Wall from ordered radial and vertical coordinates.

Parameters:
  • R (np.ndarray) – Radial coordinates of the wall points [m].

  • Z (np.ndarray) – Vertical coordinates of the wall points [m], same length as R.

Raises:

AssertionError – If the lengths of R and Z do not match.