Example: Virtual circuit calculations


This example notebook will demonstrate how to construct virtual circuits (VCs) for a MAST-U-like tokamak equilibrium.

What are Virtual Circuits and why are they useful?

VCs can be used to identify which (active) poloidal field (PF) coils have the most significant impact on a set of specified plasma shape parameters (which we will refer to henceforth as targets). The targets are equilibrium-related quantities such as the inner or outer midplane radii \(R_{in}\) or \(R_{out}\) (more will be listed later on).

More formally, the virtual circuit (VC) matrix \(V\) for a given equilibrium (and chosen set of shape targets and PF coils) is defined as

\[ V = (S^T S)^{-1} S^T, \]

where \(S\) is the shape (Jacobian) matrix:

\[ S_{i,j} = \frac{\partial T_i}{\partial I_j}. \]

Here, \(T_i\) is the \(i\) th target and \(I_j\) is the current in the \(j\) th PF coil. We note that \(V\) is simply the Moore-Penrose pseudo-inverse of \(S\). In FreeGSNKE, \(S\) will be calculated using finite difference (see below).

How can these VCs be used?

Once we know the VC matrix \(V\) for a given equilibrium (and its associated target values \(\vec{T}\)), we can specify a perturbation in the targets \(\vec{\Delta T}\) and calculate the change in coil currents required to achieve the new targets. The shifts in coil currents can be found via:

\[ \vec{\Delta I} = V \vec{\Delta T}. \]

Using \(\vec{\Delta I}\) we can perturb the coil currents in our equilibrium, re-solve the static forward Grad-Shafranov (GS) problem, and observe how the targets (call them \(\vec{T}_{new}\)) have changed in the new equilibrium vs. the old targets \(\vec{T}\).


Generate a starting equilibrium

Firstly, we need an equilibrium to test the VCs on.

import numpy as np
import matplotlib.pyplot as plt

# build machine
from freegsnke import build_machine
tokamak = build_machine.tokamak(
    active_coils_path=f"../machine_configs/MAST-U/MAST-U_like_active_coils.pickle",
    passive_coils_path=f"../machine_configs/MAST-U/MAST-U_like_passive_coils.pickle",
    limiter_path=f"../machine_configs/MAST-U/MAST-U_like_limiter.pickle",
    wall_path=f"../machine_configs/MAST-U/MAST-U_like_wall.pickle",
    magnetic_probe_path=f"../machine_configs/MAST-U/MAST-U_like_magnetic_probes.pickle",
)

# initialise the equilibrium
from freegsnke import equilibrium_update
eq = equilibrium_update.Equilibrium(
    tokamak=tokamak,
    Rmin=0.1, Rmax=2.0,   # Radial range
    Zmin=-2.2, Zmax=2.2,  # Vertical range
    nx=65,                # Number of grid points in the radial direction (needs to be of the form (2**n + 1) with n being an integer)
    ny=129,               # Number of grid points in the vertical direction (needs to be of the form (2**n + 1) with n being an integer)
    # psi=plasma_psi
)  

# initialise the profiles
from freegsnke.jtor_update import ConstrainPaxisIp
profiles = ConstrainPaxisIp(
    eq=eq,        # equilibrium object
    paxis=8e3,    # profile object
    Ip=6e5,       # plasma current
    fvac=0.5,     # fvac = rB_{tor}
    alpha_m=1.8,  # profile function parameter
    alpha_n=1.2   # profile function parameter
)

# load the nonlinear solver
from freegsnke import GSstaticsolver
GSStaticSolver = GSstaticsolver.NKGSsolver(eq)    

# set the coil currents
import pickle
with open('data/simple_diverted_currents_PaxisIp.pk', 'rb') as f:
    current_values = pickle.load(f)
for key in current_values.keys():
    eq.tokamak.set_coil_current(key, current_values[key])

# carry out the forward solve to find the equilibrium
GSStaticSolver.solve(eq=eq, 
                     profiles=profiles, 
                     constrain=None, 
                     target_relative_tolerance=1e-4)

# plot the resulting equilibria 
fig1, ax1 = plt.subplots(1, 1, figsize=(4, 8), dpi=80)
ax1.grid(True, which='both')
eq.plot(axis=ax1, show=False)
eq.tokamak.plot(axis=ax1, show=False)
ax1.set_xlim(0.1, 2.15)
ax1.set_ylim(-2.25, 2.25)
plt.tight_layout()
Active coils --> built from pickle file.
Passive structures --> built from pickle file.
Limiter --> built from pickle file.
Wall --> built from pickle file.
Magnetic probes --> built from pickle file.
Resistance (R) and inductance (M) matrices --> built using actives (and passives if present).
Tokamak built.
Forward static solve SUCCESS. Tolerance 5.98e-05 (vs. requested 1.00e-04) reached in 19/100 iterations.
../_images/9dad88bc7ad97783d3eea170fe1eb6d2252550665a50fb9a79fb838976402498.png

Initialise the VC class

We initialise the VC class and tell it to use the static solver we already initialised above. This will be used to repeatedly (and rapidly) to solve the static GS problem when calculating the finite differences for the shape matrix.

from freegsnke import virtual_circuits

VCs = virtual_circuits.VirtualCircuitHandling()
VCs.define_solver(GSStaticSolver)

Define shape targets

Next we need to define the shape targets (i.e. our quantities of interest) that we wish to monitor.

The way we do this is exactly the same as was seen in Example05b with the “plasma_descriptors” function. The user just needs to define a function that takes in the eq as a (single) input and returns an array of the shape targets (sometimes called parameters) of interest. See the function defined in the next cell.

# define the descriptors function (it should return an array of values and take in an eq object)
def plasma_descriptors(eq):

    # inboard/outboard midplane radii
    RinRout = eq.innerOuterSeparatrix()

    # inboard midplane radius
    Rout = eq.innerOuterSeparatrix()[0]

    # find lower X-point
    # define a "box" in which to search for the lower X-point
    XPT_BOX = [[0.33, -0.88], [0.95, -1.38]]

    # mask those points
    xpt_mask = (
        (eq.xpt[:, 0] >= XPT_BOX[0][0])
        & (eq.xpt[:, 0] <= XPT_BOX[1][0])
        & (eq.xpt[:, 1] <= XPT_BOX[0][1])
        & (eq.xpt[:, 1] >= XPT_BOX[1][1])
    )
    xpts = eq.xpt[xpt_mask, 0:2].squeeze()
    if xpts.ndim > 1 and xpts.shape[0] > 1:
        opt = eq.opt[0, 0:2]
        dists = np.linalg.norm(xpts - opt, axis=1)
        idx = np.argmin(dists)  # index of closest point
        Rx, Zx = xpts[idx, :]
    else:
        Rx, Zx = xpts

    return np.array([RinRout[0], RinRout[1], Rx, Zx])

Assign these shape targets some descriptive names (and ensure the names are in the correct order!). You can then call the function to test it works correctly.

target_names = ["Rin", "Rout", "Rx", "Zx"]
target_values = plasma_descriptors(eq)

Let us visualise the targets on our equilibrium.

# plot the resulting equilibria 
fig1, ax1 = plt.subplots(1, 1, figsize=(4, 8), dpi=80)
ax1.grid(True, which='both')
ax1.contour(eq.R, eq.Z, eq.psi(), levels=[eq.psi_bndry], colors='r')
eq.tokamak.plot(axis=ax1, show=False)
ax1.plot(eq.tokamak.wall.R, eq.tokamak.wall.Z, color='k', linewidth=1.2, linestyle="-")

ax1.scatter(target_values[0], 0.0, s=100, color='green', marker='x', zorder=20, label=target_names[0])
ax1.scatter(target_values[1], 0.0, s=100, color='blue', marker='x', zorder=20, label=target_names[1])
ax1.scatter(target_values[2], target_values[3], s=100, color='m', marker='*', zorder=20, label=f"({target_names[2]},{target_names[3]})")

ax1.set_xlim(0.1, 2.15)
ax1.set_ylim(-2.25, 2.25)
ax1.legend(loc="upper right")
plt.tight_layout()
../_images/30de3aba108006bec5071771182011f452ede6f5a1d27138c2723d61a88dd6ee.png

Calculating the VCs

Now we’ve defined the targets we’re interested in, we can begin calculating the shape and virtual circuit matrices. The following is a brief outline of how they’re calculated:

1. Initialise and solve the base equilibrium

Before computing derivatives, the static forward GS solver is run with some initial coil currents. Following this we store the:

  • equilibrium state eq and plasma profiles profiles.

  • plasma current vector \(I_y\) (which defines the amount of plasma current at each computational grid point, restricted to the limiter region to save computational resources).

  • target values \(\vec{T} = [T_1,\ldots,T_{N_T}]\) are evaluated.

This establishes a reference state before we perturb the coil currents - we have already carried out these steps above (the \(I_y\) vector is stored within profiles).

2. Find the appropriate coil current perturbations

Next, we need to identify the appropriate coil current perturbations \(\vec{\delta I}\) to use in the finite differences. To do this, we aim to scale the starting guess starting_dI (denoted \(\vec{\delta I}^{start}\)) according to the target tolerance on the plasma current vector (target_dIy = \(\delta I_y^{target}\)). While the user can choose starting_dI, the default is given by

\[ \vec{\delta I}^{start} := | \vec{I} | \times \delta I_y^{target}. \]

For each coil current \(j \in \{1,\ldots,N_c \}\), this starting guess is scaled as follows:

  1. Perturb coil current \(j\):

\[ I_j^{new} := I_j + \delta I_j^{start}.\]
  1. Solve the equilibrium with the updated \(j\) th coil current (others are left unchanged) and store the plasma current vector \(\vec{I}_y^{new}\).

  2. The starting guess for the \(j\) th coil current perturbation is then scaled by the relative norm of the plasma current change (and the predefined target tolerance target_dIy):

\[ \delta I_j^{final} = \delta I_j^{start} \times \left( \delta I_y^{target} \frac{\| \vec{I}_y^{start} \|}{\| \vec{I}_y^{new} - \vec{I}_y^{start} \|} \right).\]

If this relative norm is larger than \(\delta I_y^{target}\), then the starting guess \(\delta I_j^{start}\) needs to be made smaller (and vice versa).

After this, we have our scaled perturbations \(\vec{\delta I}^{final}\) ready to use in the finite differences.

3. Find the finite differences (i.e. the shape matrix)

For each coil current \(j \in \{1,\ldots,N_c \}\):

  1. Perturb coil current \(j\):

\[ I_j^{new} := I_j + \delta I_j^{final}.\]
  1. Solve the equilibrium with the updated \(j\) th coil current (others are left unchanged) and store the plasma current vector \(\vec{I}_y^{new}\).

  2. Using the new target values \(\vec{T}^{new}\) from the equilibrium, calculate the \(j\) th column of the shape matrix:

\[ S_{:,j} = \frac{\vec{T}^{new} - \vec{T}}{\delta I_j^{final}}. \]

This gives the sensitivity of each of the targets \(T_i\) with respect to a change in the \(j\) th coil current.

Note that we can also obtain the Jacobian matrix of the plasma current vector (using \(\vec{I}_y^{new}\)) with respect to the coil currents: \(\frac{\partial \vec{I}_y}{\partial \vec{I}}\).

4. Find the virtual circuit matrix

Once the full shape matrix \(S \in \Reals^{N_T \times N_c} \) is known, the virtual circuit matrix is computed as:

\[ V = (S^T S)^{-1} S^T \in \Reals^{N_c \times N_T}.\]

This matrix provides a mapping from requested shifts in the targets to the shifts in the coil currents required.

# next we need to define which coils we wish to calculate the shape (finite difference) derivatives (and therefore VCs) for
print("All active coils :", eq.tokamak.coils_list[0:12])

# here we'll look at a subset of the coils and the targets
coils = ['PX', 'D1', 'D2', 'D3', 'Dp', 'D5', 'D6', 'D7', 'P4', 'P5']
print("Coils to calculate VCs for :", coils)
All active coils : ['Solenoid', 'PX', 'D1', 'D2', 'D3', 'Dp', 'D5', 'D6', 'D7', 'P4', 'P5', 'P6']
Coils to calculate VCs for : ['PX', 'D1', 'D2', 'D3', 'Dp', 'D5', 'D6', 'D7', 'P4', 'P5']
# calculate the shape and VC matrices using the in-built method
VCs.calculate_VC(
    eq=eq,
    profiles=profiles,
    coils=coils,
    target_names=target_names,
    target_calculator=plasma_descriptors,
    starting_dI=None,    
    min_starting_dI=50,
    verbose=True,
    name="VC_for_lower_targets", # name for the VC
    n_vc_workers=2,              # use more than one thread to build VCs
    )
--- Stage one ---
Re-sizing each initial coil current shift so that it produces a 0.1% change in plasma current density from the input equilibrium.
Building shape matrix columns with 2 worker processes.
Forward static solve SUCCESS. Tolerance 3.88e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 3.92e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 4.67e-08 (vs. requested 1.00e-07) reached in 4/100 iterations.

Forward static solve SUCCESS. Tolerance 7.44e-08 (vs. requested 1.00e-07) reached in 4/100 iterations.

Forward static solve SUCCESS. Tolerance 2.97e-08 (vs. requested 1.00e-07) reached in 4/100 iterations.

Forward static solve SUCCESS. Tolerance 1.77e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 2.23e-08 (vs. requested 1.00e-07) reached in 4/100 iterations.

Forward static solve SUCCESS. Tolerance 2.11e-08 (vs. requested 1.00e-07) reached in 4/100 iterations.

Forward static solve SUCCESS. Tolerance 1.53e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 5.64e-09 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 1.24e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 3.27e-08 (vs. requested 1.00e-07) reached in 6/100 iterations.

Forward static solve SUCCESS. Tolerance 6.83e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 5.35e-08 (vs. requested 1.00e-07) reached in 6/100 iterations.

Forward static solve SUCCESS. Tolerance 3.38e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 1.45e-08 (vs. requested 1.00e-07) reached in 6/100 iterations.

Forward static solve SUCCESS. Tolerance 2.03e-08 (vs. requested 1.00e-07) reached in 4/100 iterations.

Forward static solve SUCCESS. Tolerance 4.06e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 1.15e-08 (vs. requested 1.00e-07) reached in 6/100 iterations.

Forward static solve SUCCESS. Tolerance 2.07e-08 (vs. requested 1.00e-07) reached in 6/100 iterations.

Coil PX (original current shift = 50.0 [A] --> scaled current shift 27.05 [A]).
Coil D1 (original current shift = 50.0 [A] --> scaled current shift 19.79 [A]).
Coil D2 (original current shift = 50.0 [A] --> scaled current shift 20.14 [A]).
Coil D3 (original current shift = 50.0 [A] --> scaled current shift 15.24 [A]).
Coil Dp (original current shift = 50.0 [A] --> scaled current shift 6.41 [A]).
Coil D5 (original current shift = 50.0 [A] --> scaled current shift 4.27 [A]).
Coil D6 (original current shift = 50.0 [A] --> scaled current shift 4.4 [A]).
Coil D7 (original current shift = 50.0 [A] --> scaled current shift 3.8 [A]).
Coil P4 (original current shift = 50.0 [A] --> scaled current shift 2.6 [A]).
Coil P5 (original current shift = 50.0 [A] --> scaled current shift 1.32 [A]).
--- Stage three ---
Inverting the shape matrix to get the virtual circuit matrix.
VC object stored under name: 'VC_for_lower_targets'.
VC computing using Moore-Penrose pseudoinverse.
# these are the finite difference derivatives of the targets wrt the coil currents
shape_matrix = 1.0*VCs.VC_for_lower_targets.shape_matrix
print(f"Dimension of the shape matrix is {shape_matrix.shape} [no. of targets x no. of coils]. Has units [m/A]")

# these are the VCs corresponding to the above shape matrix
VCs_matrix = 1.0*VCs.VC_for_lower_targets.VCs_matrix
print(f"Dimension of the virtual circuit matrix is {VCs_matrix.shape} [no. of coils x no. of targets]. Has units [A/m].")
Dimension of the shape matrix is (4, 10) [no. of targets x no. of coils]. Has units [m/A]
Dimension of the virtual circuit matrix is (10, 4) [no. of coils x no. of targets]. Has units [A/m].

How do we make use of the VCs?

Now that we have the VCs, we can use them to identify the coil current shifts required to change the targets by a certain amount.

For example, we will ask for shifts in a few shape targets and observe what happens to the equilibrium once we apply the new currents from the VCs.

# let's remind ourselves of the targets
print(target_names)
['Rin', 'Rout', 'Rx', 'Zx']
# let's set the requested shifts (units are metres) for the full set of targets
requested_target_shifts = [0.05, -0.05, 0.0, 0.0]

We specify a perturbation in the targets \(\vec{\Delta T}\) (the shifts above) and calculate the change in coil currents required to achieve this by calculating:

\[ \vec{\Delta I} = V \vec{\Delta T}. \]

Using \(\vec{\Delta I}\) we then perturb the coil currents in our original equilibrium, re-solve the static forward Grad-Shafranov (GS) problem, and return. This is all done in the following cell.

# we apply the VCs to the desired shifts, using the VC_object we just created
eq_new, profiles_new, new_target_values, old_target_values = VCs.apply_VC(
    eq=eq,
    profiles=profiles,
    VC_object=VCs.VC_for_lower_targets,
    requested_target_shifts=requested_target_shifts,
    verbose=True,
)
Currents shifts from VCs:
['PX', 'D1', 'D2', 'D3', 'Dp', 'D5', 'D6', 'D7', 'P4', 'P5'] = [ 3201.77621652    85.77243566  -302.44582425  -231.29569331
 -1078.88448917   231.4244428    297.50853903   552.24426612
  1553.20963776 -1033.17763969].
Targets shifts from VCs:
['Rin', 'Rout', 'Rx', 'Zx'] = [ 0.05272224 -0.0465142   0.00103746 -0.00359457].
# plot the resulting equilibria 
fig1, ax1 = plt.subplots(1, 1, figsize=(4, 8), dpi=80)
ax1.grid(True, which='both')
ax1.plot(eq.tokamak.wall.R, eq.tokamak.wall.Z, color='k', linewidth=1.2, linestyle="-")
eq.tokamak.plot(axis=ax1, show=False)

ax1.contour(eq.R, eq.Z, eq.psi(), levels=[eq.psi_bndry], colors='r')
ax1.contour(eq.R, eq.Z, eq_new.psi(), levels=[eq_new.psi_bndry], colors='b')

ax1.set_xlim(0.1, 2.15)
ax1.set_ylim(-2.25, 2.25)

plt.tight_layout()
../_images/4abbe37ee24d8b8d283637400832535c26c5cc6288d5a8d452b6faede423efa3.png

We can then measure the accuracy of the VCs by observing the difference between the requested change in shape targets vs. those enacted by the VCs.

for i in range(len(target_names)):
    print(f"Difference in {target_names[i]} = {np.round(new_target_values[i] - old_target_values[i],3)} vs. requested = {(requested_target_shifts)[i]}.")
Difference in Rin = 0.053 vs. requested = 0.05.
Difference in Rout = -0.047 vs. requested = -0.05.
Difference in Rx = 0.001 vs. requested = 0.0.
Difference in Zx = -0.004 vs. requested = 0.0.

In the plots below, we can see the actual shifts by the VCs are almost exactly as those requested (for the targets with actual shifts). Note how the unshifted targets also shift under the VCs due to the nonlinear coupling between targets and coil currents.

# plots
rel_diff = np.abs(np.array(new_target_values) - np.array(old_target_values))/np.abs(old_target_values)

fig1, ((ax1), (ax2)) = plt.subplots(2, 1, figsize=(12,12), dpi=80)

ax1.grid(True, which='both', alpha=0.5)
ax1.scatter(target_names, requested_target_shifts, color='red', marker='o', s=150, label="Requested")
ax1.scatter(target_names, np.array(new_target_values) - np.array(old_target_values), color='royalblue', marker='o', s=75, label="Actual")
ax1.set_xlabel("Target")
ax1.set_ylabel("Shift [m]")
ax1.legend()
# ax1.set_ylim([-max(targets_shift+non_standard_targets_shift)*1.1, max(targets_shift+non_standard_targets_shift)*1.1])

ax2.grid(True, which='both', alpha=0.5)
ax2.scatter(target_names, rel_diff, color='red', marker='o', s=150, edgecolors='black', label="Requested")
ax2.set_xlabel("Target")
ax2.set_ylabel("Relative shift")
labels = ax2.get_xticklabels()
ax2.set_yscale('log')
ax2.set_ylim([1e-6, 1e-0])

plt.tight_layout()
../_images/a94cb955770ede6516978eaddfcd52b0430f18f3341508d665882be79f1d9270.png

Alternative VCs

Note that when the VC_calculate method is called, a name can be provided. This will store the key data in a subclass with name. This useful for recalling from which targets and coils a given shape matrix or VC matrix was calculated.

Note that if no name is provided the default is used (“latest_VC”).

# display the attributes
print(VCs.VC_for_lower_targets.__dict__.keys())
dict_keys(['name', 'eq', 'profiles', 'shape_matrix', 'VCs_matrix', 'target_names', 'coils', 'target_calculator'])

You can then follow up by calculating alternative VCs for different target and coil combinations.

# define the descriptors function (it should return an array of values and take in an eq object)
def plasma_descriptors_new(eq):

    # inboard/outboard midplane radii
    RinRout = eq.innerOuterSeparatrix()

    # find lower X-point
    # define a "box" in which to search for the lower X-point
    XPT_BOX = [[0.33, -0.88], [0.95, -1.38]]

    # mask those points
    xpt_mask = (
        (eq.xpt[:, 0] >= XPT_BOX[0][0])
        & (eq.xpt[:, 0] <= XPT_BOX[1][0])
        & (eq.xpt[:, 1] <= XPT_BOX[0][1])
        & (eq.xpt[:, 1] >= XPT_BOX[1][1])
    )
    xpts = eq.xpt[xpt_mask, 0:2].squeeze()
    if xpts.ndim > 1 and xpts.shape[0] > 1:
        opt = eq.opt[0, 0:2]
        dists = np.linalg.norm(xpts - opt, axis=1)
        idx = np.argmin(dists)  # index of closest point
        Rx, Zx = xpts[idx, :]
    else:
        Rx, Zx = xpts

    return np.array([RinRout[0], Rx, Zx])
# this time we use only the divertor coils and a subset of targets
coils = ["PX", 'D1', 'D2', 'D3', 'Dp', 'D5', 'D6', 'D7']
target_names =['Rin', 'Rx', 'Zx']


# calculate the shape and VC matrices as follows
VCs.calculate_VC(
    eq=eq,
    profiles=profiles,
    coils=coils,
    target_names=target_names,
    target_calculator=plasma_descriptors_new,
    starting_dI=None,
    min_starting_dI=50,
    verbose=True,
    name="VC_for_upper_targets", # name for the VC
    n_vc_workers=2, 
    )
--- Stage one ---
Re-sizing each initial coil current shift so that it produces a 0.1% change in plasma current density from the input equilibrium.
Building shape matrix columns with 2 worker processes.
Forward static solve SUCCESS. Tolerance 3.88e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 3.92e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 4.67e-08 (vs. requested 1.00e-07) reached in 4/100 iterations.

Forward static solve SUCCESS. Tolerance 7.44e-08 (vs. requested 1.00e-07) reached in 4/100 iterations.

Forward static solve SUCCESS. Tolerance 2.97e-08 (vs. requested 1.00e-07) reached in 4/100 iterations.

Forward static solve SUCCESS. Tolerance 1.77e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 2.23e-08 (vs. requested 1.00e-07) reached in 4/100 iterations.

Forward static solve SUCCESS. Tolerance 2.11e-08 (vs. requested 1.00e-07) reached in 4/100 iterations.

Forward static solve SUCCESS. Tolerance 1.53e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 5.64e-09 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 1.24e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 3.27e-08 (vs. requested 1.00e-07) reached in 6/100 iterations.

Forward static solve SUCCESS. Tolerance 6.83e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 5.35e-08 (vs. requested 1.00e-07) reached in 6/100 iterations.

Forward static solve SUCCESS. Tolerance 3.38e-08 (vs. requested 1.00e-07) reached in 5/100 iterations.

Forward static solve SUCCESS. Tolerance 1.45e-08 (vs. requested 1.00e-07) reached in 6/100 iterations.

Coil PX (original current shift = 50.0 [A] --> scaled current shift 27.05 [A]).
Coil D1 (original current shift = 50.0 [A] --> scaled current shift 19.79 [A]).
Coil D2 (original current shift = 50.0 [A] --> scaled current shift 20.14 [A]).
Coil D3 (original current shift = 50.0 [A] --> scaled current shift 15.24 [A]).
Coil Dp (original current shift = 50.0 [A] --> scaled current shift 6.41 [A]).
Coil D5 (original current shift = 50.0 [A] --> scaled current shift 4.27 [A]).
Coil D6 (original current shift = 50.0 [A] --> scaled current shift 4.4 [A]).
Coil D7 (original current shift = 50.0 [A] --> scaled current shift 3.8 [A]).
--- Stage three ---
Inverting the shape matrix to get the virtual circuit matrix.
VC object stored under name: 'VC_for_upper_targets'.
VC computing using Moore-Penrose pseudoinverse.
# display the attributes
print(VCs.VC_for_upper_targets.__dict__.keys())
dict_keys(['name', 'eq', 'profiles', 'shape_matrix', 'VCs_matrix', 'target_names', 'coils', 'target_calculator'])

We can now apply this VC using the apply_VC method.

# we'll shift the upper strikepoint (R value) 
requested_target_shifts = [0.0, -0.025, 0.03]

# we apply the VCs to the some desired shifts, using the VC_object we just created
eq_new, profiles_new, new_target_values, old_target_values = VCs.apply_VC(
    eq=eq,
    profiles=profiles,
    VC_object=VCs.VC_for_upper_targets,
    requested_target_shifts=requested_target_shifts,
    verbose=True,
)
Currents shifts from VCs:
['PX', 'D1', 'D2', 'D3', 'Dp', 'D5', 'D6', 'D7'] = [1253.45762757  286.51295434    5.84233795  -58.40679736 -481.71104406
   92.2561056  -152.8056361    30.51447841].
Targets shifts from VCs:
['Rin', 'Rx', 'Zx'] = [ 0.00071954 -0.02149936  0.03151723].
# plot the resulting equilibria 
fig1, ax1 = plt.subplots(1, 1, figsize=(4, 8), dpi=80)
ax1.grid(True, which='both')
ax1.plot(eq.tokamak.wall.R, eq.tokamak.wall.Z, color='k', linewidth=1.2, linestyle="-")
eq.tokamak.plot(axis=ax1, show=False)

ax1.contour(eq.R, eq.Z, eq.psi(), levels=[eq.psi_bndry], colors='r')
ax1.contour(eq.R, eq.Z, eq_new.psi(), levels=[eq_new.psi_bndry], colors='b')

ax1.set_xlim(0.1, 2.15)
ax1.set_ylim(-2.25, 2.25)

plt.tight_layout()
../_images/3072c3093762cb43f2cee2629850b285bac68f3d2ecdae522fb8489934b8ba4d.png
# plots
rel_diff = np.abs(np.array(new_target_values) - np.array(old_target_values))/np.abs(old_target_values)

fig1, ((ax1), (ax2)) = plt.subplots(2, 1, figsize=(12,12), dpi=80)

ax1.grid(True, which='both', alpha=0.5)
ax1.scatter(target_names, requested_target_shifts, color='red', marker='o', s=150, label="Requested")
ax1.scatter(target_names, np.array(new_target_values) - np.array(old_target_values), color='royalblue', marker='o', s=75, label="Actual")
ax1.set_xlabel("Target")
ax1.set_ylabel("Shift [m]")
ax1.legend()
# ax1.set_ylim([-max(targets_shift+non_standard_targets_shift)*1.1, max(targets_shift+non_standard_targets_shift)*1.1])

ax2.grid(True, which='both', alpha=0.5)
ax2.scatter(target_names, rel_diff, color='red', marker='o', s=150, edgecolors='black', label="Requested")
ax2.set_xlabel("Target")
ax2.set_ylabel("Relative shift")
labels = ax2.get_xticklabels()
ax2.set_yscale('log')
ax2.set_ylim([1e-6, 1e-0])

plt.tight_layout()
../_images/bb8c8b9d4770f8cf18d18e974457ef4436ebeff3481212824654d7e525bf4cb6.png

Tikhonov-regularised virtual circuits

So far, calculate_VC has built the virtual circuit matrix \(V\) as the Moore-Penrose pseudo-inverse of the shape matrix \(S\):

\[ V = (S^T S)^{-1} S^T. \]

\(S\) is often “underdetermined” (more coils than independent targets, as above) and/or has coils with very similar effects on the targets. In these cases \(S^T S\) is singular, or close to it, so np.linalg.pinv still returns a solution, but the corresponding current shifts \(\Delta I = V \Delta T\) can be surprisingly large: many different \(\Delta I\) satisfy \(S \Delta I \approx \Delta T\) almost equally well, and the pseudo-inverse has no preference for “gentle” ones over “large, oscillatory, mutually-cancelling” ones.

Tikhonov (ridge) regularisation fixes this by penalising the size of \(\Delta I\) directly. Instead of solving \(\min_{\Delta I} ||S \Delta I - \Delta T||^2\), we solve

\[ \min_{\Delta I} \; ||S \Delta I - \Delta T||^2 + \sum_i \lambda_i \, \Delta I_i^2, \]

whose solution is

\[ V_\lambda = \left[S^T S + \mathrm{diag}(\lambda)\right]^{-1} S^T. \]
  • \(\lambda_i = 0\) for every coil recovers the ordinary pseudo-inverse above (this is the default, tikhonov_lambda=None, used everywhere so far in this notebook).

  • Increasing \(\lambda_i\) makes coil \(i\)’s current shift “more expensive”, so the solver prefers to leave it alone and use other, better-constrained coils instead. This is useful e.g. for protecting a coil with a tight current limit, or for damping the noisy current combinations that arise from near-degenerate coils.

  • The trade-off is accuracy: a larger \(\lambda\) gives smaller, better-behaved \(\Delta I\), but the requested target shift \(\Delta T\) is no longer hit exactly.

This is exactly what VirtualCircuitHandling.calculate_matrix_inverse does when it is passed a tikhonov_lambda array instead of None (as used internally by calculate_VC). Let’s see it in action using the shape matrix already computed above for VC_for_upper_targets.

# reminder of the shape matrix used to build VC_for_upper_targets
S = VCs.VC_for_upper_targets.shape_matrix
print(f"Shape matrix S: {S.shape[0]} targets x {S.shape[1]} coils.")

# S^T @ S is (n_coils x n_coils) but built from only n_targets independent rows,
# so it's rank-deficient/near-singular whenever there are more coils than targets
print(f"Singular values of S:   {np.linalg.svd(S, compute_uv=False)}")
print(f"Eigenvalues of S^T @ S: {np.linalg.eigvalsh(S.T @ S)}")
Shape matrix S: 3 targets x 8 coils.
Singular values of S:   [1.6743933e-04 2.8189978e-05 1.3552435e-05]
Eigenvalues of S^T @ S: [-2.37217346e-24 -6.46546135e-25  6.60566740e-26  4.77666325e-25
  1.27427342e-24  1.83668495e-10  7.94674860e-10  2.80359292e-08]
requested_target_shifts = [0.0, -0.025, 0.03]

# standard (un-regularised) virtual circuit, as already stored in VC_for_upper_targets
V_pinv = VCs.VC_for_upper_targets.VCs_matrix
dI_pinv = V_pinv @ np.array(requested_target_shifts)

print("Coil current shifts from the standard (Moore-Penrose) VC:")
for coil, dI in zip(coils, dI_pinv):
    print(f"  {coil:>3s}: {dI:9.1f} A")
print(f"||dI|| = {np.linalg.norm(dI_pinv):.1f} A")
Coil current shifts from the standard (Moore-Penrose) VC:
   PX:    1253.5 A
   D1:     286.5 A
   D2:       5.8 A
   D3:     -58.4 A
   Dp:    -481.7 A
   D5:      92.3 A
   D6:    -152.8 A
   D7:      30.5 A
||dI|| = 1386.2 A
# sweep the (uniform) regularisation strength lambda and see how the solution
# trades off "hitting the target exactly" against "using small coil currents"
lambdas = np.logspace(-11, -6, 25)
dI_norms = []
residual_norms = []

for lam in lambdas:
    tikhonov_lambda = np.ones(len(coils)) * lam
    V_tik = VCs.calculate_matrix_inverse(S, tikhonov_lambda=tikhonov_lambda)
    dI_tik = V_tik @ np.array(requested_target_shifts)
    dI_norms.append(np.linalg.norm(dI_tik))
    residual_norms.append(np.linalg.norm(S @ dI_tik - requested_target_shifts))

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4.5), dpi=80)

ax1.loglog(lambdas, dI_norms, marker='o')
ax1.axhline(np.linalg.norm(dI_pinv), color='red', linestyle='--', label='Moore-Penrose (no regularisation)')
ax1.set_xlabel(r"$\lambda$")
ax1.set_ylabel(r"$||\Delta I||$ [A]")
ax1.set_title("Coil current shift size vs. regularisation strength")
ax1.legend()
ax1.grid(True, which='both', alpha=0.5)

ax2.loglog(dI_norms, residual_norms, marker='o')
ax2.set_xlabel(r"$||\Delta I||$ [A]")
ax2.set_ylabel(r"$||S \Delta I - \Delta T||$ [m]")
ax2.set_title("L-curve: target-tracking error vs. current shift size")
ax2.grid(True, which='both', alpha=0.5)

plt.tight_layout()
../_images/33d3ad1c89434dc141f74117e47db0c48d70b065255fa16ce89fd5d91cac416f.png

The left-hand plot shows how increasing \(\lambda\) shrinks the coil current shift needed, well below the value required with no regularisation (dashed red line). The right-hand “L-curve” plot shows the classic regularisation trade-off: as \(\lambda\) grows, \(||\Delta I||\) falls but the residual error in hitting the requested target shift grows. The corner of the L is usually a good compromise: enough regularisation to remove the worst of the poorly-constrained, oscillatory currents, without giving up much target-tracking accuracy.

Let’s compare the individual coil current shifts directly, for no regularisation vs. a moderate, uniform \(\lambda = 10^{-9}\).

# compare individual coil current shifts: no regularisation vs. a moderate lambda
tikhonov_lambda = np.ones(len(coils)) * 1e-9
V_tik = VCs.calculate_matrix_inverse(S, tikhonov_lambda=tikhonov_lambda, verbose=True)
dI_tik = V_tik @ np.array(requested_target_shifts)

fig, ax = plt.subplots(figsize=(8, 4.5), dpi=80)
x = np.arange(len(coils))
width = 0.35
ax.bar(x - width / 2, dI_pinv, width, label="Moore-Penrose")
ax.bar(x + width / 2, dI_tik, width, label=r"Tikhonov ($\lambda=10^{-9}$)")
ax.set_xticks(x)
ax.set_xticklabels(coils)
ax.set_ylabel(r"$\Delta I$ [A]")
ax.axhline(0, color='k', linewidth=0.8)
ax.legend()
ax.grid(True, which='both', alpha=0.5, axis='y')
plt.tight_layout()

print(f"||dI|| Moore-Penrose = {np.linalg.norm(dI_pinv):.1f} A")
print(f"||dI|| Tikhonov      = {np.linalg.norm(dI_tik):.1f} A")
VC computed using Tikhonov regularised inverse. 
||dI|| Moore-Penrose = 1386.2 A
||dI|| Tikhonov      = 343.8 A
../_images/b21ba7af1afa7e5b1fa50ec0563074cb90aa73b8845bffef6ecaf789499a9eda.png

Regularisation has reduced the overall size of the current shifts considerably. Notice it doesn’t just uniformly shrink every entry - some coils (e.g. those with the most correlated effect on the X-point targets) even change sign - because the penalty specifically damps the combinations of coils that were only weakly constrained by the requested target shift, while leaving well-constrained directions largely untouched.

In practice, tikhonov_lambda can also be a non-uniform array (one value per coil), passed directly into VirtualCircuitHandling.calculate_VC(..., tikhonov_lambda=...). This is useful, for example, to apply little or no regularisation to well-behaved coils while heavily penalising a coil that has a strict current limit, or one you’d prefer the VC left alone.