forge.machine

Defines the Machine class representing a tokamak’s PF coil set.

Inspired by the Machine class in machine.py from FreeGS (https://github.com/freegs-plasma/freegs), but substantially rewritten for FORGE.

Copyright 2016-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.machine.Machine(magnets_data=None, wall_R=None, wall_Z=None, circuits=None, other_structures=None)[source]

Bases: object

Object representing a Tokamak.

Represents the machine (Tokamak) including the PF coils, wall, and additional structures. Coils may be powered independently, or wired in Circuits with one another.

Parameters:
  • magnets_data (dict) – A dictionary of data containing information on the PF coils.

  • wall_R (list) – R coordinates of the machine wall.

  • wall_Z (list) – Z coordinates of the machine wall.

  • circuits (dict) –

    Dictionary of list of names of coils in Circuits and associated current multipliers - each entry represents a Circuit. Can be None if there are no Circuits.

    Example:

    {
        "P1": {
            "coil_names": ['P1U', 'P1L'],
            "current_multipliers": [1.0, 1.0]
        },
        "P3": {
            "coil_names": ['P3U', 'P3L'],
            "current_multipliers": [1.0, 1.0]
        },
    }
    

  • other_structures (dict) –

    A dictionary of other structures (e.g. a TF coil) that can be used in plotting of the device. Either lines or filled polygons can be plotted.

    Example:

    {
        "TF": {
            "exterior": {
                "R": [R_1, R_2, ... R_N],
                "Z": [Z_1, Z_2, ... Z_N],
            },
            "interior": {
                "R": [R_1, R_2, ... R_M],
                "Z": [Z_1, Z_2, ... Z_M],
            },
            "colour": "k",
            "fill_colour": "orange"
        }
    }
    

    The exterior and interior of a shape are specified, along with a colour for the outline and the fill colour. If a filled shape without an interior is desired, interior can be None. Likewise, if a simple line is desired, then both interior and fill_colour can be None.

create_coilset()[source]

Creates the PF coil set.

Creates PF coil objects from the input magnets data. If Circuits are present, coils will be wired in Circuits accordingly.

to_dict()[source]

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

Reads current values from the live coilset objects, so the output reflects any post-optimisation current changes.

Returns:

dict{"coils": {...}, "circuits": {...}} suitable for forge.io.save_fancy_json().

psiRZ(R, Z)[source]

Calculates the poloidal magnetic flux at (R,Z) from the coilset (not including the plasma).

BrRZ(R, Z)[source]

Calculates the radial magnetic field at (R,Z) from the coilset (not including the plasma).

BzRZ(R, Z)[source]

Calculates the vertical magnetic field at (R,Z) from the coilset (not including the plasma).

control_psi(R, Z)[source]

Calculates the poloidal magnetic flux at (R,Z) from a unit current through each of the PF coils.

This will return result of size N_coils x shape(R,Z).

control_Br(R, Z)[source]

Calculates the radial magnetic field at (R,Z) from a unit current through each of the PF coils.

This will return result of size N_coils x shape(R,Z).

control_Bz(R, Z)[source]

Calculates the vertical magnetic field at (R,Z) from a unit current through each of the PF coils.

This will return result of size N_coils x shape(R,Z).

control_Bp_jacobians(R, Z)[source]

Calculates the poloidal magnetic field Jacobians at (R,Z) for a unit current through each of the PF coils.

This will return result of size N_coils x shape(R,Z).

get_coil_names()[source]

Returns a list of names of the coils in the PF coilset.

get_currents()[source]

Returns a list of currents in the PF coilset.

update_currents(new_currents)[source]

Updates the currents in the PF coilset, taking a list of new currents as input.

plot(ax=None, plot_wall=True, plot_other_structures=True, show=False)[source]

Plots the tokamak.

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

  • plot_wall (bool) – Flag for plotting the wall.

  • plot_other_structures (bool) – Flag for plotting additional structures that may exist.

  • show (bool) – Flag for whether the plot should be displayed or not. If False, the axes will be returned.

Returns:

ax – matplotlib axes containing the plot of the machine. Only returned if show is False.