forge.magnets

Description of the magnets (PF coils) that make up the tokamak.

Based on the Coil, ShapedCoil, Solenoid and Circuit classes from FreeGS (https://github.com/freegs-plasma/freegs), with modifications for FORGE including the addition of the FilamentPointCoil class and JSON serialisation.

Copyright 2016-2022 FreeGS contributors Copyright 2019 Ben Dudson, University of York. Email: benjamin.dudson@york.ac.uk Copyright 2025-2026 Chris Marsden

This file is part of FORGE.

FORGE 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.

FORGE 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 FORGE. If not, see <http://www.gnu.org/licenses/>.

class forge.magnets.Coil(R, Z, name=None, current=0.0, turns=1)[source]

Bases: object

Represents a poloidal field coil as a point source of current.

Parameters:
  • R (float) – R coordinate of the coil.

  • Z (float) – Z coordinate of the coil.

  • name (str) – Name of the coil.

  • current (float) – Per-turn current in the coil (A).

  • turns (int) – Number of turns. Total coil current is current * turns.

control_psi(R, Z)[source]

Calculates the poloidal magnetic flux at (R,Z) from a unit current through the coil.

control_Br(R, Z)[source]

Calculates the radial magnetic field at (R,Z) from a unit current through the coil.

control_Bz(R, Z)[source]

Calculates the vertical magnetic field at (R,Z) from a unit current through the coil.

control_dBp(R, Z, deriv=None)[source]

Calculates the selected poloidal field derivative at (R,Z) from a unit current through the coil.

Options for deriv:

“dBr_dZ” - d(Br)/dZ - vertical derivative of the radial field “dBr_dR” - d(Br)/dR - radial derivative of the radial field “dBz_dZ” - d(Bz)/dZ - vertical derivative of the vertical field “dBz_dR” - d(Bz)/dR - radial derivative of the vertical field

control_Bp_jacobian(R, Z)[source]

Computes the 2x2 Jacobian matrix of poloidal field about the point (R,Z) from a unit current through the coil.

J = [[dBr/dR,dBr/dZ],

[dBz/dR,dBz/dZ]]

psiRZ(R, Z)[source]

Calculates the poloidal magnetic flux at (R,Z).

BrRZ(R, Z)[source]

Calculates the radial magnetic field at (R,Z).

BzRZ(R, Z)[source]

Calculates the vertical magnetic field at (R,Z).

dBpRZ(R, Z, deriv=None)[source]

Calculates the selected poloidal field derivative at (R,Z).

Options for deriv:

“dBr_dZ” - d(Br)/dZ - vertical derivative of the radial field “dBr_dR” - d(Br)/dR - radial derivative of the radial field “dBz_dZ” - d(Bz)/dZ - vertical derivative of the vertical field “dBz_dR” - d(Bz)/dR - radial derivative of the vertical field

BpRZ_jacobian(R, Z)[source]

Computes the 2x2 Jacobian matrix of the poloidal field about the point (R,Z).

J = [[dBr/dR,dBr/dZ],

[dBz/dR,dBz/dZ]]

set_fill_colour(fill_colour)[source]

Changes the colour used to fill the coil when plotted.

set_edge_colour(edge_colour)[source]

Changes the colour of the edge of the coil when plotted.

plot(ax=None)[source]

Plots the coil.

Parameters:

ax (matplotlib axes) – Axes to plot onto. If None, these will be created.

Returns:

ax – matplotlib axes containing the plot of the machine.

to_dict()[source]

Return a JSON-serialisable dictionary matching the magnets JSON schema.

class forge.magnets.ShapedCoil(shape, name=None, current=0.0, turns=1, npoints=6)[source]

Bases: Coil

Represents a poloidal field coil with a polygonal shape.

The coil’s shape is represented as a triangular mesh, with Gaussian qadrature used to represent the distribution of current throughout the cross section of the coil.

Parameters:
  • shape (list) – Outline of the coil shape as a list of points [(r1,z1), (r2,z2), …]. Must have more than two points.

  • name (str) – Name of the coil.

  • current (float) – Per-turn current in the coil (A).

  • turns (int) – Number of turns. Total coil current is current * turns.

  • npoints (int) – Number of quadrature points to use.

control_psi(R, Z)[source]

Calculates the poloidal magnetic flux at (R,Z) from a unit current through the coil.

control_Br(R, Z)[source]

Calculates the radial magnetic field at (R,Z) from a unit current through the coil.

control_Bz(R, Z)[source]

Calculates the vertical magnetic field at (R,Z) from a unit current through the coil.

control_dBp(R, Z, deriv=None)[source]

Calculates the selected poloidal field derivative at (R,Z) from a unit current through the coil.

Options for deriv:

“dBr_dZ” - d(Br)/dZ - vertical derivative of the radial field “dBr_dR” - d(Br)/dR - radial derivative of the radial field “dBz_dZ” - d(Bz)/dZ - vertical derivative of the vertical field “dBz_dR” - d(Bz)/dR - radial derivative of the vertical field

control_Bp_jacobian(R, Z)[source]

Computes the 2x2 Jacobian matrix of poloidal field about the point (R,Z) from a unit current through the coil.

J = [[dBr/dR,dBr/dZ],

[dBz/dR,dBz/dZ]]

psiRZ(R, Z)[source]

Calculates the poloidal magnetic flux at (R,Z).

BrRZ(R, Z)[source]

Calculates the radial magnetic field at (R,Z).

BzRZ(R, Z)[source]

Calculates the vertical magnetic field at (R,Z).

dBpRZ(R, Z, deriv=None)[source]

Calculates the selected poloidal field derivative at (R,Z).

Options for deriv:

“dBr_dZ” - d(Br)/dZ - vertical derivative of the radial field “dBr_dR” - d(Br)/dR - radial derivative of the radial field “dBz_dZ” - d(Bz)/dZ - vertical derivative of the vertical field “dBz_dR” - d(Bz)/dR - radial derivative of the vertical field

BpRZ_jacobian(R, Z)[source]

Computes the 2x2 Jacobian matrix of the poloidal field about the point (R,Z).

J = [[dBr/dR,dBr/dZ],

[dBz/dR,dBz/dZ]]

set_fill_colour(fill_colour)[source]

Changes the colour used to fill the coil when plotted.

set_edge_colour(edge_colour)[source]

Changes the colour of the edge of the coil when plotted.

plot(ax=None)[source]

Plots the coil.

Parameters:

ax (matplotlib axes) – Axes to plot onto. If None, these will be created.

Returns:

ax – matplotlib axes containing the plot of the machine.

to_dict()[source]

Return a JSON-serialisable dictionary matching the magnets JSON schema.

class forge.magnets.Solenoid(R, Z_min, Z_max, name=None, current=0.0, turns=1, npoints=51)[source]

Bases: object

Represents a central solenoid.

The solenoid has no radial thickness, and is represented by a series of point sources spread across the vertical extent of the solenoid.

Parameters:
  • R (float) – R coordinate of the solenoid.

  • Z_min (float) – Minimum Z coordinate of the solenoid.

  • Z_max (float) – Minimum Z coordinate of the solenoid.

  • name (str) – Name of the coil.

  • current (float) – Per-turn current in the coil (A).

  • turns (int) – Number of turns. Total coil current is current * turns.

  • npoints (int) – Number of point sources of current to be spread evenly along the vertical extent of the solenoid.

control_psi(R, Z)[source]

Calculates the poloidal magnetic flux at (R,Z) from a unit current through the coil.

control_Br(R, Z)[source]

Calculates the radial magnetic field at (R,Z) from a unit current through the coil.

control_Bz(R, Z)[source]

Calculates the vertical magnetic field at (R,Z) from a unit current through the coil.

control_dBp(R, Z, deriv=None)[source]

Calculates the selected poloidal field derivative at (R,Z) from a unit current through the coil.

Options for deriv:

“dBr_dZ” - d(Br)/dZ - vertical derivative of the radial field “dBr_dR” - d(Br)/dR - radial derivative of the radial field “dBz_dZ” - d(Bz)/dZ - vertical derivative of the vertical field “dBz_dR” - d(Bz)/dR - radial derivative of the vertical field

control_Bp_jacobian(R, Z)[source]

Computes the 2x2 Jacobian matrix of poloidal field about the point (R,Z) from a unit current through the coil.

J = [[dBr/dR,dBr/dZ],

[dBz/dR,dBz/dZ]]

psiRZ(R, Z)[source]

Calculates the poloidal magnetic flux at (R,Z).

BrRZ(R, Z)[source]

Calculates the radial magnetic field at (R,Z).

BzRZ(R, Z)[source]

Calculates the vertical magnetic field at (R,Z).

dBpRZ(R, Z, deriv=None)[source]

Calculates the selected poloidal field derivative at (R,Z).

Options for deriv:

“dBr_dZ” - d(Br)/dZ - vertical derivative of the radial field “dBr_dR” - d(Br)/dR - radial derivative of the radial field “dBz_dZ” - d(Bz)/dZ - vertical derivative of the vertical field “dBz_dR” - d(Bz)/dR - radial derivative of the vertical field

BpRZ_jacobian(R, Z)[source]

Computes the 2x2 Jacobian matrix of the poloidal field about the point (R,Z).

J = [[dBr/dR,dBr/dZ],

[dBz/dR,dBz/dZ]]

set_fill_colour(fill_colour)[source]

Changes the colour used to fill the coil when plotted.

set_edge_colour(edge_colour)[source]

Changes the colour of the edge of the coil when plotted.

plot(ax=None)[source]

Plots the coil.

Parameters:

ax (matplotlib axes) – Axes to plot onto. If None, these will be created.

Returns:

ax – matplotlib axes containing the plot of the machine.

to_dict()[source]

Return a JSON-serialisable dictionary matching the magnets JSON schema.

class forge.magnets.FilamentPointCoil(name=None, current=0.0, turns=1, R_filaments=None, Z_filaments=None, dR=None, dZ=None)[source]

Bases: Coil

Represents a coil containing a set of current filaments.

Each filament acts as a point source of current. The current sources here are prescribed by given R,Z points. Each filament carries a factor of 1/N_filamanets of the total coil current, where N_filaments are the number of filaments. A useful case for this coil is where filaments are placed at the centre of the real physical turns of the coil.

Parameters:
  • name (str) – Name of the coil.

  • current (float) – Per-turn current in the coil (A).

  • turns (int) – Number of turns. Total coil current is current * turns.

  • R_filaments (list) – R coordinates of the filaments.

  • Z_filaments (list) – Z coordinates of the filaments.

  • dR (float) – Full radial width of the filaments. If None, they will be plotted as points.

  • dZ (float) – Full vertical height of the filaments. If None, they will be plotted as points.

control_psi(R, Z)[source]

Calculates the poloidal magnetic flux at (R,Z) from a unit current through the coil.

control_Br(R, Z)[source]

Calculates the radial magnetic field at (R,Z) from a unit current through the coil.

control_Bz(R, Z)[source]

Calculates the vertical magnetic field at (R,Z) from a unit current through the coil.

control_dBp(R, Z, deriv=None)[source]

Calculates the selected poloidal field derivative at (R,Z) from a unit current through the coil.

Options for deriv:

“dBr_dZ” - d(Br)/dZ - vertical derivative of the radial field “dBr_dR” - d(Br)/dR - radial derivative of the radial field “dBz_dZ” - d(Bz)/dZ - vertical derivative of the vertical field “dBz_dR” - d(Bz)/dR - radial derivative of the vertical field

control_Bp_jacobian(R, Z)[source]

Computes the 2x2 Jacobian matrix of poloidal field about the point (R,Z) from a unit current through the coil.

J = [[dBr/dR,dBr/dZ],

[dBz/dR,dBz/dZ]]

psiRZ(R, Z)[source]

Calculates the poloidal magnetic flux at (R,Z).

BrRZ(R, Z)[source]

Calculates the radial magnetic field at (R,Z).

BzRZ(R, Z)[source]

Calculates the vertical magnetic field at (R,Z).

dBpRZ(R, Z, deriv=None)[source]

Calculates the selected poloidal field derivative at (R,Z).

Options for deriv:

“dBr_dZ” - d(Br)/dZ - vertical derivative of the radial field “dBr_dR” - d(Br)/dR - radial derivative of the radial field “dBz_dZ” - d(Bz)/dZ - vertical derivative of the vertical field “dBz_dR” - d(Bz)/dR - radial derivative of the vertical field

BpRZ_jacobian(R, Z)[source]

Computes the 2x2 Jacobian matrix of the poloidal field about the point (R,Z).

J = [[dBr/dR,dBr/dZ],

[dBz/dR,dBz/dZ]]

set_fill_colour(fill_colour)[source]

Changes the colour used to fill the coil when plotted.

set_edge_colour(edge_colour)[source]

Changes the colour of the edge of the coil when plotted.

plot(ax=None)[source]

Plots the coil.

Parameters:

ax (matplotlib axes) – Axes to plot onto. If None, these will be created.

Returns:

ax – matplotlib axes containing the plot of the machine.

to_dict()[source]

Return a JSON-serialisable dictionary matching the magnets JSON schema.

class forge.magnets.Circuit(magnets=None, multipliers=None, name=None, circuit_current=None)[source]

Bases: object

Represents a collection of coils connected together in the same circuit.

Parameters:
  • magnets (list) – List of PF coil objects - [Coils, ShapedCoils, Solenoids] etc.

  • multipliers (list) – List of circuit current multipliers for the current in each coil. E.g, if a circuit of 2 coils, [“PF1U”,”PF1L”], a set of multipliers [1, -1] would correspond to “PF1U” recieving the circuit current and “PF1L” recieving the negative of the circuit current.

  • name (str) – Name of the circuit.

  • circuit_current (float) – Current in the circuit (A).

control_psi(R, Z)[source]

Calculates the poloidal magnetic flux at (R,Z) from a unit current through the circuit.

control_Br(R, Z)[source]

Calculates the radial magnetic field at (R,Z) from a unit current through the circuit.

control_Bz(R, Z)[source]

Calculates the vertical magnetic field at (R,Z) from a unit current through the circuit.

control_dBp(R, Z, deriv=None)[source]

Calculates the selected poloidal field derivative at (R,Z) from a unit current through the circuit.

Options for deriv:

“dBr_dZ” - d(Br)/dZ - vertical derivative of the radial field “dBr_dR” - d(Br)/dR - radial derivative of the radial field “dBz_dZ” - d(Bz)/dZ - vertical derivative of the vertical field “dBz_dR” - d(Bz)/dR - radial derivative of the vertical field

control_Bp_jacobian(R, Z)[source]

Computes the 2x2 Jacobian matrix of poloidal field about the point (R,Z) from a unit circuit current.

J = [[dBr/dR,dBr/dZ],

[dBz/dR,dBz/dZ]]

psiRZ(R, Z)[source]

Calculates the poloidal magnetic flux at (R,Z).

BrRZ(R, Z)[source]

Calculates the radial magnetic field at (R,Z).

BzRZ(R, Z)[source]

Calculates the vertical magnetic field at (R,Z).

dBpRZ(R, Z, deriv=None)[source]

Calculates the selected poloidal field derivative at (R,Z).

Options for deriv:

“dBr_dZ” - d(Br)/dZ - vertical derivative of the radial field “dBr_dR” - d(Br)/dR - radial derivative of the radial field “dBz_dZ” - d(Bz)/dZ - vertical derivative of the vertical field “dBz_dR” - d(Bz)/dR - radial derivative of the vertical field

BpRZ_jacobian(R, Z)[source]

Computes the 2x2 Jacobian matrix of the poloidal field about the point (R,Z).

J = [[dBr/dR,dBr/dZ],

[dBz/dR,dBz/dZ]]

set_fill_colour(fill_colour)[source]

Changes the colour used to fill the coils in the Circuit when plotted.

set_edge_colour(edge_colour)[source]

Changes the colour of the edge of the coils in the Circuit when plotted.

plot(ax=None)[source]

Plots the coils in the circuit.

Parameters:

ax (matplotlib axes) – Axes to plot onto. If None, these will be created.

Returns:

ax – matplotlib axes containing the plot of the machine.