Example: Advanced constraints in static inverse free-boundary equilibrium calculations
In this example notebook, we demonstrate some of the more advanced types of constraints and techniques that can be employed in the inverse solver.
Instantiate the objects
As before, we start by instantiating the machine, equilibrium, profiles, and solver objects.
# build machine
from freegsnke import build_machine
tokamak = build_machine.tokamak(
active_coils_path="../machine_configs/MAST-U/MAST-U_like_active_coils.pickle",
passive_coils_path="../machine_configs/MAST-U/MAST-U_like_passive_coils.pickle",
limiter_path="../machine_configs/MAST-U/MAST-U_like_limiter.pickle",
wall_path="../machine_configs/MAST-U/MAST-U_like_wall.pickle",
)
from freegsnke import equilibrium_update
eq = equilibrium_update.Equilibrium(
tokamak=tokamak, # provide tokamak object
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
)
from freegsnke import GSstaticsolver
GSStaticSolver = GSstaticsolver.NKGSsolver(eq)
Active coils --> built from pickle file.
Passive structures --> built from pickle file.
Limiter --> built from pickle file.
Wall --> built from pickle file.
Magnetic probes --> none provided.
Resistance (R) and inductance (M) matrices --> built using actives (and passives if present).
Tokamak built.
# as before, let's fix the Solenoid current
eq.tokamak.set_coil_current('Solenoid', 5000)
eq.tokamak['Solenoid'].control = False # ensures the current in the Solenoid is fixed
Feature 1: (Normalised) poloidal flux constraints at specific locations
In addition to the null points, isoflux set, and coil_current_limits constraints introduced in the previous notebook, additional methods are available for more advanced control of the inverse problem.
We can also constrain:
Flux values (
psi_vals)
If the flux is known at a location \((R_j, Z_j)\), we can impose $\( \psi(R_j, Z_j) = \psi_j^{\text{target}}. \)\( More generally, one could prescribe \)\psi(R,Z)$ over a region (e.g. a full flux map). Flux values are typically difficult to estimate a priori to simulation and so the following constraint is often more useful.Normalised flux values (
psi_norm_limits)
At a location \((R_j, Z_j)\), we can impose upper (or lower) bound constraints on the normalised flux $\( \hat{\psi}(R,Z) = \frac{\psi(R,Z) - \psi_{axis}}{\psi_{boundary} - \psi_{axis}}, \)\( such that \)\( \hat{\psi}(R_j, Z_j) \leq \hat{\psi}_j^{\text{target}}. \)\( or \)\( \hat{\psi}(R_j, Z_j) \geq \hat{\psi}_j^{\text{target}}. \)\( Note that \)\psi_{axis}\( and \)\psi_{boundary}$ are the values of the flux on the magnetic axis and plasma boundary, respectively. As we’ll see, this can be useful for setting explicit constraints on flux behaviour near the wall if there are certain safety limits to adhere to.
Once again, we stress that users should be mindful of specifying too many constraints that may conflict with one another during an inverse solve. For example, a given normalised \(\psi\) constraint may be impossible/difficult to achieve given other isoflux constraints or coil current limits. More generally, specifying too many constraints could make the problem ill-posed and reduce the overall quality of the final solution.
Recall that if you find that the solver is violating the limit (inequality) constraints (coil limits and normalised \(\psi\)), you should look to reduce the number of constraints or try modifying the mu_* parameters. These parameters control how much a violation of the constraint is penalised in the solver, and increasing this penalty (by increasing mu_*) will increase the likelihood the constraint is adequately satisfied. Again, it may also be that the constraint is impossible to satisfy given your other constraints.
In the following, we will show how to use the normalised flux constraints to control the normalised flux on the divertor nose.
import numpy as np
from freegsnke.inverse import Inverse_optimizer
Rx = 0.6 # X-point radius
Zx = 1.1 # X-point height
Rout = 1.4 # outboard midplane radius
Rin = 0.34 # inboard midplane radius
# set desired null_points locations (this can include X-point and O-point locations)
null_points = [[Rx, Rx], [Zx, -Zx]]
# Set desired isoflux constraints with format
# isoflux_set = [isoflux_0, isoflux_1 ... ]
# with each isoflux_i = [R_coords, Z_coords, weights]
isoflux_set = np.array([
[
[Rx, Rx, Rin, Rout],
[Zx, -Zx, 0., 0.],
]
])
# set the coil current limits (upper and lower)
# coil ordering in this case: PX, D1, D2, D3, Dp, D5, D6, D7, P4, P5, P6
coil_current_limits = [
[5e3, 9e3, 9e3, 7e3, 7e3, 5e3, 4e3, 5e3, 0.0, 0.0, None],
[-5e3, -9e3, -9e3, -7e3, -7e3, -5e3, -4e3, -5e3, -10e3, -10e3, None]
]
# normalised psi constraints set with format:
# [R, Z, psiN, 1] --> ψ̂(R,Z) ≥ psiN (the +1 or -1 defines ≥ or ≤)
# [R, Z, psiN, -1] --> ψ̂(R,Z) ≤ psiN
psi_norm_limits = [
[0.82, -1.55, 1.2, 1],
[0.82, -1.55, 1.3, -1],
]
# instantiate the freegsnke constrain object
constrain = Inverse_optimizer(
null_points=null_points,
isoflux_set=isoflux_set,
coil_current_limits=coil_current_limits,
psi_norm_limits=psi_norm_limits,
mu_psi_norm = 1e7, # penalise how much the normalised psi constraint is violated (default 1e6)
)
We solve, as in the previous example, by passing the equilibrium, profiles, and constraints to the static solver. Again, we apply regularisation to encourage a solution with low coil currents.
GSStaticSolver.solve(eq=eq,
profiles=profiles,
constrain=constrain,
target_relative_tolerance=1e-6,
target_relative_psit_update=1e-3,
verbose=True, # print output
l2_reg=np.array([1e-12]*10+[1e-6]),
)
-----
Inverse static solve starting...
Successfully computed GS residual using initial plasma_psi and tokamak_psi guesses.
Initial relative error = 1.00e+00
-----
Iteration: 0
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.24e+04', '-4.61e+03', '1.74e+04', '1.70e+04', '2.56e+04', '-8.39e+03', '-1.87e+04', '-1.78e+04', '-3.36e+04', '1.34e+04', '-1.16e+00']
Constraint losses = 6.01e-01
Relative update of tokamak psi (in plasma core): 4.21e+01
Handing off to forward solve (with updated currents).
Relative error = 9.31e-01
-----
Iteration: 1
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-9.71e+03', '2.51e+04', '-3.58e+03', '-9.25e+03', '-2.71e+04', '2.06e+03', '1.23e+04', '1.00e+04', '1.80e+04', '-8.54e+03', '6.62e-01']
Constraint losses = 1.45e-01
Relative update of tokamak psi (in plasma core): 2.38e-02
Handing off to forward solve (with updated currents).
Relative error = 8.68e-01
-----
Iteration: 2
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['8.96e+02', '-4.69e+03', '-2.21e+03', '-1.43e+03', '-1.07e+03', '1.33e+03', '3.29e+03', '2.43e+03', '4.27e+03', '-2.61e+03', '4.00e-01']
Constraint losses = 1.00e-01
Relative update of tokamak psi (in plasma core): 2.04e-02
Handing off to forward solve (with updated currents).
Relative error = 7.78e-01
-----
Iteration: 3
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['8.63e+02', '-3.70e+03', '-1.41e+03', '-4.44e+03', '-5.53e+02', '1.89e+03', '5.21e+03', '1.35e+03', '8.77e+02', '-1.67e+03', '1.74e-01']
Constraint losses = 7.16e-02
Relative update of tokamak psi (in plasma core): 1.80e-02
Handing off to forward solve (with updated currents).
Relative error = 6.52e-01
-----
Iteration: 4
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['3.24e+02', '-2.35e+03', '-8.83e+02', '-2.27e+03', '-6.45e+02', '2.02e+03', '5.75e+02', '3.55e+03', '1.20e+03', '-1.69e+03', '4.76e-02']
Constraint losses = 5.11e-02
Relative update of tokamak psi (in plasma core): 1.94e-02
Handing off to forward solve (with updated currents).
Relative error = 4.80e-01
-----
Iteration: 5
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-1.81e+02', '-8.03e+02', '-1.52e+03', '-2.92e+02', '1.38e+01', '7.23e+02', '-1.10e+03', '7.14e+02', '3.52e+03', '-1.43e+03', '2.47e-04']
Constraint losses = 3.62e-02
Relative update of tokamak psi (in plasma core): 2.29e-02
Handing off to forward solve (with updated currents).
Relative error = 2.48e-01
-----
Iteration: 6
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-3.86e+02', '1.47e+01', '-2.54e+03', '-2.65e+02', '3.30e+02', '4.32e+02', '-4.05e+02', '5.33e+02', '1.96e+03', '-9.76e+02', '-9.35e-03']
Constraint losses = 2.45e-02
Relative update of tokamak psi (in plasma core): 1.63e-02
Handing off to forward solve (with updated currents).
Relative error = 4.11e-02
-----
Iteration: 7
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-5.07e+02', '-5.56e+02', '-2.60e+02', '-9.07e+01', '-1.16e+01', '8.51e+01', '2.77e+02', '2.39e+02', '4.05e+02', '-4.72e+02', '-3.69e-06']
Constraint losses = 1.19e-02
Relative update of tokamak psi (in plasma core): 1.14e-02
Handing off to forward solve (with updated currents).
Relative error = 3.46e-05
-----
Iteration: 8
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-2.42e+02', '1.56e+01', '3.10e+01', '1.70e+01', '6.43e+01', '-3.83e+01', '-3.76e+01', '-5.61e+01', '-1.17e+02', '-1.00e+02', '2.33e-06']
Constraint losses = 3.45e-02
Relative update of tokamak psi (in plasma core): 1.74e-02
Handing off to forward solve (with updated currents).
Relative error = 6.60e-05
-----
Iteration: 9
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-4.70e+02', '1.41e+02', '1.20e+02', '5.79e+01', '1.44e+02', '-7.42e+01', '-1.29e+02', '-1.51e+02', '-3.07e+02', '4.47e+01', '-4.19e-05']
Constraint losses = 7.16e-03
Relative update of tokamak psi (in plasma core): 1.65e-02
Handing off to forward solve (with updated currents).
Relative error = 2.30e-05
-----
Iteration: 10
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['2.39e+02', '6.63e-01', '-2.67e+01', '-1.87e+01', '-7.49e+01', '4.22e+01', '3.35e+01', '5.60e+01', '1.19e+02', '1.58e+02', '3.66e-06']
Constraint losses = 2.02e-02
Relative update of tokamak psi (in plasma core): 1.60e-02
Handing off to forward solve (with updated currents).
Relative error = 5.31e-05
-----
Iteration: 11
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-1.87e+02', '-3.18e+01', '6.80e-01', '4.50e+00', '4.58e+01', '-2.46e+01', '-1.28e+00', '-2.08e+01', '-5.12e+01', '-1.58e+02', '-2.94e-06']
Constraint losses = 1.59e-02
Relative update of tokamak psi (in plasma core): 1.51e-02
Handing off to forward solve (with updated currents).
Relative error = 7.63e-05
-----
Iteration: 12
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.81e+02', '2.02e+01', '-7.05e+00', '-7.05e+00', '-4.76e+01', '2.92e+01', '1.06e+01', '2.99e+01', '6.66e+01', '1.55e+02', '-5.83e-05']
Constraint losses = 1.27e-02
Relative update of tokamak psi (in plasma core): 1.37e-02
Handing off to forward solve (with updated currents).
Relative error = 3.23e-05
-----
Iteration: 13
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-1.56e+02', '-2.00e+01', '4.06e+00', '4.99e+00', '3.91e+01', '-2.25e+01', '-5.62e+00', '-2.18e+01', '-5.04e+01', '-1.28e+02', '7.19e-07']
Constraint losses = 1.74e-02
Relative update of tokamak psi (in plasma core): 1.30e-02
Handing off to forward solve (with updated currents).
Relative error = 9.74e-06
-----
Iteration: 14
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.55e+02', '2.92e+01', '7.77e-01', '-3.02e+00', '-3.76e+01', '2.40e+01', '2.53e+00', '2.01e+01', '4.66e+01', '1.53e+02', '1.04e-05']
Constraint losses = 7.05e-03
Relative update of tokamak psi (in plasma core): 1.29e-02
Handing off to forward solve (with updated currents).
Relative error = 5.43e-05
-----
Iteration: 15
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-1.36e+02', '-2.20e+01', '1.01e+00', '3.25e+00', '3.31e+01', '-1.98e+01', '-2.75e+00', '-1.74e+01', '-4.08e+01', '-1.21e+02', '-1.09e-05']
Constraint losses = 2.12e-02
Relative update of tokamak psi (in plasma core): 1.21e-02
Handing off to forward solve (with updated currents).
Relative error = 2.32e-05
-----
Iteration: 16
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['2.73e+01', '1.03e+01', '2.92e+00', '6.07e-01', '-5.44e+00', '4.16e+00', '-1.68e+00', '1.83e+00', '4.78e+00', '3.57e+01', '7.73e-06']
Constraint losses = 1.66e-03
Relative update of tokamak psi (in plasma core): 2.76e-03
Handing off to forward solve (with updated currents).
Relative error = 2.50e-05
-----
Iteration: 17
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-9.95e+01', '-1.28e+01', '2.46e+00', '3.06e+00', '2.48e+01', '-1.45e+01', '-3.38e+00', '-1.38e+01', '-3.20e+01', '-8.29e+01', '-6.12e-05']
Constraint losses = 4.24e-03
Relative update of tokamak psi (in plasma core): 7.80e-03
Handing off to forward solve (with updated currents).
Relative error = 3.03e-05
-----
Iteration: 18
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.15e+02', '1.23e+01', '-4.76e+00', '-4.70e+00', '-3.04e+01', '1.72e+01', '6.09e+00', '1.79e+01', '4.06e+01', '9.14e+01', '1.21e-06']
Constraint losses = 1.12e-02
Relative update of tokamak psi (in plasma core): 8.15e-03
Handing off to forward solve (with updated currents).
Relative error = 6.11e-05
-----
Iteration: 19
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-9.26e+01', '-9.76e+00', '3.53e+00', '3.37e+00', '2.36e+01', '-1.38e+01', '-4.48e+00', '-1.40e+01', '-3.19e+01', '-7.43e+01', '-3.77e-05']
Constraint losses = 6.39e-03
Relative update of tokamak psi (in plasma core): 7.28e-03
Handing off to forward solve (with updated currents).
Relative error = 3.11e-05
-----
Iteration: 20
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['8.65e+01', '1.08e+01', '-2.65e+00', '-3.07e+00', '-2.23e+01', '1.29e+01', '3.71e+00', '1.27e+01', '2.91e+01', '7.18e+01', '3.76e-06']
Constraint losses = 7.96e-03
Relative update of tokamak psi (in plasma core): 6.41e-03
Handing off to forward solve (with updated currents).
Relative error = 1.69e-05
-----
Iteration: 21
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-6.96e+01', '-7.56e+00', '2.52e+00', '2.48e+00', '1.77e+01', '-1.04e+01', '-3.20e+00', '-1.04e+01', '-2.37e+01', '-5.63e+01', '-2.01e-05']
Constraint losses = 5.73e-03
Relative update of tokamak psi (in plasma core): 5.48e-03
Handing off to forward solve (with updated currents).
Relative error = 1.89e-05
-----
Iteration: 22
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['6.02e+01', '7.84e+00', '-1.64e+00', '-2.03e+00', '-1.54e+01', '9.00e+00', '2.47e+00', '8.78e+00', '2.01e+01', '5.07e+01', '3.84e-06']
Constraint losses = 5.15e-03
Relative update of tokamak psi (in plasma core): 4.57e-03
Handing off to forward solve (with updated currents).
Relative error = 2.05e-05
-----
Iteration: 23
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-5.99e+01', '-6.65e+00', '2.08e+00', '2.08e+00', '1.52e+01', '-8.92e+00', '-2.67e+00', '-8.88e+00', '-2.03e+01', '-4.88e+01', '-2.66e-05']
Constraint losses = 4.49e-03
Relative update of tokamak psi (in plasma core): 4.69e-03
Handing off to forward solve (with updated currents).
Relative error = 1.25e-05
-----
Iteration: 24
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['5.62e+01', '6.93e+00', '-1.75e+00', '-2.00e+00', '-1.45e+01', '8.43e+00', '2.50e+00', '8.36e+00', '1.91e+01', '4.66e+01', '5.48e-06']
Constraint losses = 4.89e-03
Relative update of tokamak psi (in plasma core): 4.24e-03
Handing off to forward solve (with updated currents).
Relative error = 6.95e-06
-----
Iteration: 25
Using full Jacobian (of constraints wrt coil currents) to optimsise currents.
- calculating derivatives for coil 1/11
- calculating derivatives for coil 2/11
- calculating derivatives for coil 3/11
- calculating derivatives for coil 4/11
- calculating derivatives for coil 5/11
- calculating derivatives for coil 6/11
- calculating derivatives for coil 7/11
- calculating derivatives for coil 8/11
- calculating derivatives for coil 9/11
- calculating derivatives for coil 10/11
- calculating derivatives for coil 11/11
Change in coil currents (being controlled): ['-2.80e-01', '-4.83e-01', '-5.16e-01', '-7.17e-01', '-1.70e+00', '-2.70e+00', '-2.52e+00', '-2.96e+00', '-4.19e+00', '-8.98e+00', '-5.97e-01']
Constraint losses = 4.05e-03
Relative update of tokamak psi (in plasma core): 1.03e-03
Handing off to forward solve (with updated currents).
Relative error = 9.33e-06
-----
Iteration: 26
Using full Jacobian (of constraints wrt coil currents) to optimsise currents.
- calculating derivatives for coil 1/11
- calculating derivatives for coil 2/11
- calculating derivatives for coil 3/11
- calculating derivatives for coil 4/11
- calculating derivatives for coil 5/11
- calculating derivatives for coil 6/11
- calculating derivatives for coil 7/11
- calculating derivatives for coil 8/11
- calculating derivatives for coil 9/11
- calculating derivatives for coil 10/11
- calculating derivatives for coil 11/11
Change in coil currents (being controlled): ['-1.82e-01', '-1.98e-01', '-1.93e-01', '-2.79e-01', '-5.96e-01', '-1.18e+00', '-1.02e+00', '-1.26e+00', '-1.86e+00', '-4.44e+00', '4.27e-01']
Constraint losses = 2.04e-03
Relative update of tokamak psi (in plasma core): 4.75e-04
Handing off to forward solve (with updated currents).
Relative error = 9.90e-07
-----
Inverse static solve SUCCESS. Tolerance 9.90e-07 (vs. requested 1e-06) reached in 27/100 iterations.
import matplotlib.pyplot as plt
# 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)
constrain.plot(axis=ax1,show=True)
ax1.set_xlim(0.1, 2.15)
ax1.set_ylim(-2.25, 2.25)
plt.tight_layout()
<Figure size 640x480 with 0 Axes>
We can now check that the normalised \(\psi\) constraint has indeed been satisfied!
for psi_con in psi_norm_limits:
sign = ">=" if psi_con[3] >= 0 else "<="
print(f"Psi norm at ({psi_con[0]}, {psi_con[1]}) = {eq.psiNRZ(psi_con[0], psi_con[1]):.3f} ({sign} {psi_con[2]})")
Psi norm at (0.82, -1.55) = 1.213 (>= 1.2)
Psi norm at (0.82, -1.55) = 1.213 (<= 1.3)
Feature 2: Weighting constraints within the solver
It is possible to specify weights on the different types of constraints within the inverse solver, enabling the solver to prioritise certain constraints over others.
There are two ways of doing this:
Weighting different types of constraints relative to one another:
Different classes of constraint can be weighted relative to each other (e.g. null points, isoflux, and psi constraints). This allows the solver to be instructed that one type of constraint is more important than others. For example, a user may want null point constraints to be strictly satisfied, but may be less concerned about the isoflux set being met with the same strictness.
Weighting individual isoflux constraints within a set:
Individual isoflux constraints within a set can optionally be weighted relative to one another, instructing the solver to prioritise satisfying some subset over others.
For example, consider a set of three isoflux constraints: one at the X-point, one at the outer midplane, and one at a desired strike point. The strike point constraint could be assigned a lower weight to allow the solver to find a solution with a strike point close to — but not necessarily exactly on — the target location.
Option 1: weighting different types of constraints
Here, we’ll show how to weight the null point constraints more than the isoflux constraints. This solver then prioritises fitting the null points exactly and the isoflux points less so.
# core constraints
Rx = 0.55 # X-point radius
Zx = 1.2 # X-point height
Rout = 1.4 # outboard midplane radius
Rin = 0.34 # inboard midplane radius
# set desired null_points locations (this can include X-point and O-point locations)
null_points = [[Rx, Rx], [Zx, -Zx]]
# set desired isoflux constraints with format
# isoflux_set = [isoflux_0, isoflux_1 ... ]
# with each isoflux_i = [R_coords, Z_coords]
isoflux_set = np.array([
[
[Rx, Rx, Rin, Rout, 0.75, 1.0, 0.75, 1.0],
[Zx, -Zx, 0.0, 0.0, -1.6, -2.0, 1.6, 2.0],
]
])
# instantiate the freegsnke constrain object
constrain = Inverse_optimizer(
null_points=null_points,
isoflux_set=isoflux_set,
coil_current_limits=coil_current_limits,
weight_isoflux=0.2, # <-- weight the isofluxes less than the null points (default 1.0)
weight_nulls=1.0, # <-- weight the null points more than the null points (default 1.0)
# weight_psi=1.0 # <-- weight for psi values not used here
)
# solve!
GSStaticSolver.solve(eq=eq,
profiles=profiles,
constrain=constrain,
target_relative_tolerance=1e-6,
target_relative_psit_update=1e-3,
verbose=True,
l2_reg=np.array([1e-12]*10+[1e-6]),
)
-----
Inverse static solve starting...
Successfully computed GS residual using initial plasma_psi and tokamak_psi guesses.
Initial relative error = 9.90e-07
-----
Iteration: 0
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['3.37e+02', '-3.28e+03', '-5.49e+02', '1.21e+03', '-3.69e+02', '7.78e+02', '4.93e+02', '5.78e+02', '4.47e+02', '-6.33e+02', '-3.84e-06']
Constraint losses = 8.90e-02
Relative update of tokamak psi (in plasma core): 9.80e-03
Handing off to forward solve (with updated currents).
Relative error = 3.52e-05
-----
Iteration: 1
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['3.67e+02', '-1.37e+03', '1.81e+02', '8.51e+02', '-2.17e+02', '3.21e+02', '-6.24e+01', '5.09e+01', '-1.53e+02', '1.36e+02', '-2.74e-05']
Constraint losses = 2.99e-02
Relative update of tokamak psi (in plasma core): 9.51e-03
Handing off to forward solve (with updated currents).
Relative error = 2.28e-05
-----
Iteration: 2
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['2.29e+01', '-1.60e+02', '1.02e+02', '1.10e+02', '2.94e+01', '-4.31e+00', '-4.43e+01', '-3.91e+01', '-8.62e+01', '-4.11e+01', '-5.12e-06']
Constraint losses = 1.71e-02
Relative update of tokamak psi (in plasma core): 9.19e-03
Handing off to forward solve (with updated currents).
Relative error = 2.19e-05
-----
Iteration: 3
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['2.17e+02', '-4.16e+02', '2.75e+02', '2.16e+02', '1.13e+02', '2.61e+00', '-7.78e+01', '-5.59e+01', '-1.17e+02', '1.57e+02', '-1.25e-06']
Constraint losses = 8.71e-03
Relative update of tokamak psi (in plasma core): 7.13e-03
Handing off to forward solve (with updated currents).
Relative error = 3.47e-05
-----
Iteration: 4
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['3.48e-01', '-8.58e+01', '5.13e+01', '2.56e+01', '4.66e+01', '-2.06e+01', '-1.30e+01', '-2.07e+01', '-3.77e+01', '-7.05e+01', '-7.50e-07']
Constraint losses = 1.44e-02
Relative update of tokamak psi (in plasma core): 8.25e-03
Handing off to forward solve (with updated currents).
Relative error = 3.63e-05
-----
Iteration: 5
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.38e+02', '-1.76e+02', '1.27e+02', '3.03e+01', '9.84e+01', '-2.15e+01', '-1.62e+01', '-1.57e+01', '-1.37e+01', '1.01e+02', '-3.27e-06']
Constraint losses = 6.56e-03
Relative update of tokamak psi (in plasma core): 7.96e-03
Handing off to forward solve (with updated currents).
Relative error = 2.03e-05
-----
Iteration: 6
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-1.51e+01', '-3.56e+01', '2.18e+01', '1.34e+01', '2.11e+01', '-1.38e+01', '-8.69e+00', '-1.45e+01', '-2.74e+01', '-6.66e+01', '-2.17e-06']
Constraint losses = 1.45e-02
Relative update of tokamak psi (in plasma core): 7.50e-03
Handing off to forward solve (with updated currents).
Relative error = 1.66e-05
-----
Iteration: 7
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['8.24e+01', '-8.57e+01', '6.85e+01', '1.23e+01', '5.16e+01', '-1.35e+01', '-1.00e+01', '-9.65e+00', '-7.39e+00', '5.69e+01', '-9.50e-06']
Constraint losses = 3.60e-03
Relative update of tokamak psi (in plasma core): 4.42e-03
Handing off to forward solve (with updated currents).
Relative error = 4.03e-05
-----
Iteration: 8
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-1.03e+01', '-2.96e+01', '1.74e+01', '1.02e+01', '1.79e+01', '-1.18e+01', '-6.55e+00', '-1.18e+01', '-2.21e+01', '-5.80e+01', '-1.09e-05']
Constraint losses = 7.93e-03
Relative update of tokamak psi (in plasma core): 6.18e-03
Handing off to forward solve (with updated currents).
Relative error = 2.22e-05
-----
Iteration: 9
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['5.06e+01', '-2.33e+01', '2.05e+01', '-2.10e+00', '1.38e+01', '7.02e-01', '5.45e-01', '3.57e+00', '1.20e+01', '6.10e+01', '-3.75e-06']
Constraint losses = 6.95e-03
Relative update of tokamak psi (in plasma core): 5.64e-03
Handing off to forward solve (with updated currents).
Relative error = 2.38e-05
-----
Iteration: 10
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-1.42e+01', '-1.64e+01', '8.92e+00', '7.57e+00', '1.02e+01', '-8.41e+00', '-4.71e+00', '-8.86e+00', '-1.76e+01', '-5.10e+01', '-2.51e-05']
Constraint losses = 7.35e-03
Relative update of tokamak psi (in plasma core): 5.33e-03
Handing off to forward solve (with updated currents).
Relative error = 7.74e-06
-----
Iteration: 11
Using full Jacobian (of constraints wrt coil currents) to optimsise currents.
- calculating derivatives for coil 1/11
- calculating derivatives for coil 2/11
- calculating derivatives for coil 3/11
- calculating derivatives for coil 4/11
- calculating derivatives for coil 5/11
- calculating derivatives for coil 6/11
- calculating derivatives for coil 7/11
- calculating derivatives for coil 8/11
- calculating derivatives for coil 9/11
- calculating derivatives for coil 10/11
- calculating derivatives for coil 11/11
Change in coil currents (being controlled): ['-2.38e-01', '2.48e-01', '5.29e-01', '7.14e-01', '2.17e+00', '2.16e+00', '2.51e+00', '2.60e+00', '3.56e+00', '5.05e+00', '1.30e-01']
Constraint losses = 5.41e-03
Relative update of tokamak psi (in plasma core): 7.90e-04
Handing off to forward solve (with updated currents).
Relative error = 5.91e-07
-----
Inverse static solve SUCCESS. Tolerance 5.91e-07 (vs. requested 1e-06) reached in 12/100 iterations.
Notice how the null points constraints are satisfied perfectly while the isoflux constraints are “less so” (especially in the divertor region).
# 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)
constrain.plot(axis=ax1,show=True)
ax1.set_xlim(0.1, 2.15)
ax1.set_ylim(-2.25, 2.25)
plt.tight_layout()
<Figure size 640x480 with 0 Axes>
Option 2: weighting different isoflux constraints
Here, we’ll show how to weight individual isoflux constraints differently from one another. For example, here we want to prioritise the core shape over the exact location of the strikepoint.
# core constraints
Rx = 0.55 # X-point radius
Zx = 1.2 # X-point height
Rout = 1.4 # outboard midplane radius
Rin = 0.34 # inboard midplane radius
# set desired null_points locations (this can include X-point and O-point locations)
null_points = [[Rx, Rx], [Zx, -Zx]]
# set desired isoflux constraints with format
# isoflux_set = [isoflux_0, isoflux_1 ... ]
# with each isoflux_i = [R_coords, Z_coords, weights]
isoflux_set = np.array([
[
[Rx, Rx, Rin, Rout, 1.0, 1.0, 0.75, 1.0, 0.75, 1.0],
[Zx, -Zx, 0.0, 0.0, -0.9, 0.9, -1.6, -2.0, 1.6, 2.0],
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.05, 0.05, 0.05],
]
])
# instantiate the freegsnke constrain object
constrain = Inverse_optimizer(
null_points=null_points,
isoflux_set=isoflux_set,
coil_current_limits=coil_current_limits,
)
# solve!
GSStaticSolver.solve(eq=eq,
profiles=profiles,
constrain=constrain,
target_relative_tolerance=1e-6,
target_relative_psit_update=1e-3,
verbose=True, # print output
l2_reg=np.array([1e-12]*10+[1e-6]),
)
-----
Inverse static solve starting...
Successfully computed GS residual using initial plasma_psi and tokamak_psi guesses.
Initial relative error = 5.91e-07
-----
Iteration: 0
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.10e+02', '-1.62e+02', '-5.44e+02', '-1.50e+02', '7.79e+02', '-5.53e+01', '1.08e+02', '-1.68e+02', '-8.41e+02', '3.12e+02', '4.85e-04']
Constraint losses = 2.07e-02
Relative update of tokamak psi (in plasma core): 9.80e-03
Handing off to forward solve (with updated currents).
Relative error = 1.34e-05
-----
Iteration: 1
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['7.93e+02', '-3.73e+02', '-1.23e+03', '-3.21e+02', '1.36e+03', '3.24e+01', '4.27e+02', '-9.03e+01', '-1.21e+03', '4.58e+02', '9.87e-04']
Constraint losses = 1.20e-02
Relative update of tokamak psi (in plasma core): 9.51e-03
Handing off to forward solve (with updated currents).
Relative error = 5.92e-05
-----
Iteration: 2
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['5.49e-01', '-1.10e+01', '-1.54e+02', '-5.32e+00', '4.60e+01', '3.01e+01', '6.70e+01', '2.74e+01', '-4.34e+01', '-1.37e+02', '-1.85e-05']
Constraint losses = 2.34e-02
Relative update of tokamak psi (in plasma core): 9.19e-03
Handing off to forward solve (with updated currents).
Relative error = 2.41e-05
-----
Iteration: 3
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['3.95e+02', '-1.48e+01', '-4.97e+02', '2.43e+01', '1.25e+02', '1.64e+02', '2.27e+02', '1.22e+02', '-1.18e+02', '-5.73e+01', '3.44e-04']
Constraint losses = 4.72e-03
Relative update of tokamak psi (in plasma core): 6.00e-03
Handing off to forward solve (with updated currents).
Relative error = 2.21e-05
-----
Iteration: 4
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-9.39e+01', '4.61e+01', '-1.37e+02', '6.50e+01', '1.06e+01', '4.95e+01', '2.33e+01', '-3.95e+00', '-1.27e+02', '-6.08e+01', '1.38e-04']
Constraint losses = 7.76e-03
Relative update of tokamak psi (in plasma core): 8.05e-03
Handing off to forward solve (with updated currents).
Relative error = 5.48e-05
-----
Iteration: 5
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.33e+02', '1.19e+01', '-8.11e+01', '3.92e+01', '-4.04e+00', '5.49e+01', '3.45e+01', '2.76e+01', '-2.24e+01', '8.45e+01', '-1.17e-05']
Constraint losses = 1.10e-02
Relative update of tokamak psi (in plasma core): 7.76e-03
Handing off to forward solve (with updated currents).
Relative error = 6.22e-05
-----
Iteration: 6
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-8.72e+01', '2.12e+01', '-7.07e+01', '3.54e+01', '-1.47e+00', '2.41e+01', '1.38e+01', '-1.75e-01', '-5.80e+01', '-7.62e+01', '8.80e-05']
Constraint losses = 9.47e-03
Relative update of tokamak psi (in plasma core): 7.28e-03
Handing off to forward solve (with updated currents).
Relative error = 3.86e-05
-----
Iteration: 7
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.09e+02', '1.06e+01', '-6.51e+01', '3.57e+01', '-7.43e+00', '4.72e+01', '2.79e+01', '2.37e+01', '-1.62e+01', '7.19e+01', '1.53e-04']
Constraint losses = 8.21e-03
Relative update of tokamak psi (in plasma core): 6.78e-03
Handing off to forward solve (with updated currents).
Relative error = 4.22e-05
-----
Iteration: 8
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-8.05e+01', '1.45e+01', '-4.10e+01', '2.32e+01', '-2.79e+00', '1.20e+01', '5.65e+00', '-2.99e+00', '-3.87e+01', '-6.56e+01', '4.19e-05']
Constraint losses = 9.43e-03
Relative update of tokamak psi (in plasma core): 6.26e-03
Handing off to forward solve (with updated currents).
Relative error = 3.20e-05
-----
Iteration: 9
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['8.78e+01', '1.07e+01', '-5.31e+01', '3.30e+01', '-9.71e+00', '4.10e+01', '2.26e+01', '2.02e+01', '-1.29e+01', '6.03e+01', '3.85e-06']
Constraint losses = 5.81e-03
Relative update of tokamak psi (in plasma core): 5.74e-03
Handing off to forward solve (with updated currents).
Relative error = 3.20e-05
-----
Iteration: 10
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-7.42e+01', '1.03e+01', '-2.57e+01', '1.54e+01', '-2.10e+00', '5.21e+00', '1.67e+00', '-4.54e+00', '-2.88e+01', '-5.84e+01', '3.93e-05']
Constraint losses = 9.05e-03
Relative update of tokamak psi (in plasma core): 5.59e-03
Handing off to forward solve (with updated currents).
Relative error = 2.72e-05
-----
Iteration: 11
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['7.66e+01', '8.15e+00', '-4.07e+01', '2.66e+01', '-8.87e+00', '3.34e+01', '1.81e+01', '1.70e+01', '-7.88e+00', '5.41e+01', '1.60e-04']
Constraint losses = 4.62e-03
Relative update of tokamak psi (in plasma core): 5.15e-03
Handing off to forward solve (with updated currents).
Relative error = 3.19e-05
-----
Iteration: 12
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-6.36e+01', '6.97e+00', '-1.50e+01', '9.34e+00', '-1.06e+00', '7.31e-01', '-6.21e-01', '-5.07e+00', '-2.08e+01', '-4.92e+01', '1.96e-05']
Constraint losses = 8.69e-03
Relative update of tokamak psi (in plasma core): 4.72e-03
Handing off to forward solve (with updated currents).
Relative error = 2.79e-05
-----
Iteration: 13
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['5.32e+01', '6.54e+00', '-3.04e+01', '2.11e+01', '-7.22e+00', '2.51e+01', '1.32e+01', '1.23e+01', '-7.14e+00', '3.84e+01', '-2.16e-05']
Constraint losses = 2.96e-03
Relative update of tokamak psi (in plasma core): 3.66e-03
Handing off to forward solve (with updated currents).
Relative error = 3.18e-05
-----
Iteration: 14
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-5.22e+01', '5.31e+00', '-1.07e+01', '6.66e+00', '-5.53e-01', '-3.62e-01', '-1.03e+00', '-4.53e+00', '-1.64e+01', '-3.99e+01', '1.44e-05']
Constraint losses = 6.51e-03
Relative update of tokamak psi (in plasma core): 3.83e-03
Handing off to forward solve (with updated currents).
Relative error = 1.98e-05
-----
Iteration: 15
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['5.01e+01', '2.74e+00', '-1.73e+01', '1.23e+01', '-5.08e+00', '1.69e+01', '9.34e+00', '9.73e+00', '3.70e-01', '3.61e+01', '-9.56e-06']
Constraint losses = 3.02e-03
Relative update of tokamak psi (in plasma core): 3.46e-03
Handing off to forward solve (with updated currents).
Relative error = 2.27e-05
-----
Iteration: 16
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-4.30e+01', '3.81e+00', '-6.42e+00', '4.01e+00', '-2.86e-02', '-1.69e+00', '-1.60e+00', '-4.24e+00', '-1.24e+01', '-3.23e+01', '1.24e-05']
Constraint losses = 5.93e-03
Relative update of tokamak psi (in plasma core): 3.12e-03
Handing off to forward solve (with updated currents).
Relative error = 1.69e-05
-----
Iteration: 17
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['3.01e+01', '1.97e+00', '-1.14e+01', '8.15e+00', '-3.35e+00', '1.08e+01', '6.04e+00', '6.11e+00', '-3.80e-01', '2.16e+01', '-1.02e-05']
Constraint losses = 1.91e-03
Relative update of tokamak psi (in plasma core): 2.08e-03
Handing off to forward solve (with updated currents).
Relative error = 1.71e-05
-----
Iteration: 18
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-3.34e+01', '3.18e+00', '-5.40e+00', '3.47e+00', '-8.76e-02', '-1.06e+00', '-1.15e+00', '-3.27e+00', '-1.01e+01', '-2.48e+01', '1.06e-05']
Constraint losses = 3.62e-03
Relative update of tokamak psi (in plasma core): 2.37e-03
Handing off to forward solve (with updated currents).
Relative error = 3.99e-06
-----
Iteration: 19
Using full Jacobian (of constraints wrt coil currents) to optimsise currents.
- calculating derivatives for coil 1/11
- calculating derivatives for coil 2/11
- calculating derivatives for coil 3/11
- calculating derivatives for coil 4/11
- calculating derivatives for coil 5/11
- calculating derivatives for coil 6/11
- calculating derivatives for coil 7/11
- calculating derivatives for coil 8/11
- calculating derivatives for coil 9/11
- calculating derivatives for coil 10/11
- calculating derivatives for coil 11/11
Change in coil currents (being controlled): ['-1.54e-01', '-1.41e-02', '2.10e-01', '3.00e-01', '6.78e-01', '1.30e+00', '1.14e+00', '1.39e+00', '2.05e+00', '4.75e+00', '2.86e-01']
Constraint losses = 4.28e-03
Relative update of tokamak psi (in plasma core): 5.10e-04
Handing off to forward solve (with updated currents).
Relative error = 2.39e-07
-----
Inverse static solve SUCCESS. Tolerance 2.39e-07 (vs. requested 1e-06) reached in 20/100 iterations.
Notice how the strikepoint isoflux constraint is no longer satisfied.
# 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)
constrain.plot(axis=ax1,show=True)
ax1.set_xlim(0.1, 2.15)
ax1.set_ylim(-2.25, 2.25)
plt.tight_layout()
<Figure size 640x480 with 0 Axes>
Feature 3: second-order magnetic null constraints (i.e. generating snowflake divertors)
Now we’ll try to use the more complex constraints to form second order nulls (using the null_points_2nd_order parameter) and a snowflake divertor geometry.
In some situations, it may be desirable to control the local structure of the magnetic field around a null point. This is achieved by enforcing second-order null point constraints, which additionally constrain the spatial derivatives of the magnetic field.
Recall that the poloidal magnetic field is related to the poloidal flux \(\psi\) via
A standard null point \((R_X, Z_X)\) satisfies
Second-order null point constraints extend this by additionally imposing conditions on the Jacobian of the magnetic field, i.e. its first spatial derivatives:
In FreeGSNKE, the following constraints are enforced at each second-order null point:
This results in a total of six constraints per null point so be aware that:
imposing too many (second-order) constraints can easily overconstrain the system.
in practice, they are most useful when combined with a limited number of other constraints (e.g. isoflux sets).
Here, we’ll use one second-order null constraint in combination with a few isoflux constraints.
Also note how we increase constrain.mu_coils as snowflake geometries tend to demand a lot from the PF coils. We need to make sure we don’t violate their limits.
But first let’s reset the equilibrium and profile objects (cranking up the resolution slightly).
# we are modifying the control, build a new tokamak
tokamak = build_machine.tokamak(
active_coils_path="../machine_configs/MAST-U/MAST-U_like_active_coils.pickle",
passive_coils_path="../machine_configs/MAST-U/MAST-U_like_passive_coils.pickle",
limiter_path="../machine_configs/MAST-U/MAST-U_like_limiter.pickle",
wall_path="../machine_configs/MAST-U/MAST-U_like_wall.pickle",
)
# a new eq object resets the coil currents and equilibrium
eq = equilibrium_update.Equilibrium(
tokamak=tokamak, # provide tokamak object
Rmin=0.1, Rmax=2.0, # radial range
Zmin=-2.2, Zmax=2.2, # vertical range
nx=129, # 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)
)
# reset the profiles
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
)
# re-initialise the solver object (due to new grid resolution)
GSStaticSolver = GSstaticsolver.NKGSsolver(eq)
# first we specify some alternative constraints
Rout = 1.32 # outboard midplane radius
Rin = 0.3 # inboard midplane radius
# locations of X-points (this time they will be second order nulls!)
Rx = 0.65
Zx = 1.2
# second order null constraints to form the snowflake
null_points_2nd_order = [[Rx], [Zx]]
# isoflux constraints (no divertor constraints this time)
isoflux_set = np.array([[[Rx, Rin, Rout], [Zx, 0.,0.]]])
# starting guess for the Solenoid current
# eq.tokamak.set_coil_current('Solenoid', 5000)
# eq.tokamak['Solenoid'].control = True
# also make it perfectly up/down symmetric
eq.tokamak.set_coil_current('P6', 0)
eq.tokamak['P6'].control = False
coil_current_limits = [
# upper limits...
# P1, PX, D1, D2, D3, Dp, D5, D6, D7, P4, P5
[1e4, 6e3, 9e3, 9e3, 7e3, 7e3, 5e3, 3.5e3, 5e3, 0, 0],
# lower limits...
# P1, PX, D1, D2, D3, Dp, D5, D6, D7, P4, P5
[-1e4, -6e3, -9e3, -9e3, -7e3, -7e3, -5e3, -3.5e3, -5e3, -1.2e4, -1.2e4]
]
# pass the magnetic constraints to a new constrain object
constrain = Inverse_optimizer(
null_points_2nd_order=null_points_2nd_order,
isoflux_set=isoflux_set,
coil_current_limits=coil_current_limits,
)
# increase this to ensure coil currents stay with limits
constrain.mu_coils = 5e7
# carry out the solve
GSStaticSolver.solve(eq=eq,
profiles=profiles,
constrain=constrain,
target_relative_tolerance=1e-6,
target_relative_psit_update=1e-3,
verbose=True,
l2_reg=np.array([1e-12]*11),
)
Active coils --> built from pickle file.
Passive structures --> built from pickle file.
Limiter --> built from pickle file.
Wall --> built from pickle file.
Magnetic probes --> none provided.
Resistance (R) and inductance (M) matrices --> built using actives (and passives if present).
Tokamak built.
-----
Inverse static solve starting...
Successfully computed GS residual using initial plasma_psi and tokamak_psi guesses.
Initial relative error = 1.00e+00
-----
Iteration: 0
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-1.01e+04', '6.18e+03', '9.77e+02', '-9.06e+03', '-7.03e+03', '-4.56e+03', '-5.00e+03', '3.54e+03', '5.02e+03', '-4.37e+01', '-1.20e+04']
Constraint losses = 1.30e+00
Relative update of tokamak psi (in plasma core): 1.88e+07
Handing off to forward solve (with updated currents).
Relative error = 9.41e-01
-----
Iteration: 1
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['2.37e+01', '-5.95e+01', '1.46e+03', '1.99e+01', '1.03e+01', '1.49e+03', '1.00e+04', '-1.32e+01', '-5.51e+00', '-8.30e+03', '2.98e+03']
Constraint losses = 7.36e-01
Relative update of tokamak psi (in plasma core): 3.20e-02
Handing off to forward solve (with updated currents).
Relative error = 8.89e-01
-----
Iteration: 2
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.56e+01', '-3.90e+01', '1.17e+03', '1.28e+01', '6.63e+00', '2.31e+03', '-1.10e-01', '-8.08e+00', '-3.49e+00', '-3.38e+03', '3.00e+03']
Constraint losses = 5.02e-01
Relative update of tokamak psi (in plasma core): 3.32e-02
Handing off to forward solve (with updated currents).
Relative error = 8.13e-01
-----
Iteration: 3
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.14e+01', '-2.86e+01', '1.30e+03', '9.45e+00', '4.86e+00', '2.36e+03', '-4.62e+03', '-6.27e+00', '-2.86e+00', '-2.76e+02', '2.35e+03']
Constraint losses = 3.43e-01
Relative update of tokamak psi (in plasma core): 3.08e-02
Handing off to forward solve (with updated currents).
Relative error = 7.13e-01
-----
Iteration: 4
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['8.52e+00', '-2.13e+01', '1.11e+03', '6.98e+00', '3.62e+00', '1.59e+03', '-3.32e+03', '-4.30e+00', '-1.80e+00', '1.53e-01', '1.56e+03']
Constraint losses = 2.32e-01
Relative update of tokamak psi (in plasma core): 2.29e-02
Handing off to forward solve (with updated currents).
Relative error = 5.79e-01
-----
Iteration: 5
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['7.21e+00', '-1.80e+01', '9.78e+02', '5.90e+00', '3.06e+00', '9.78e+02', '-1.81e+03', '-3.64e+00', '-1.52e+00', '1.29e-01', '8.96e+02']
Constraint losses = 1.43e-01
Relative update of tokamak psi (in plasma core): 1.54e-02
Handing off to forward solve (with updated currents).
Relative error = 4.02e-01
-----
Iteration: 6
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['6.13e+00', '-5.28e+02', '8.26e+02', '5.03e+00', '2.60e+00', '3.50e+02', '-2.56e+02', '-3.15e+00', '-3.63e+01', '3.25e-02', '2.40e+02']
Constraint losses = 7.82e-02
Relative update of tokamak psi (in plasma core): 6.76e-03
Handing off to forward solve (with updated currents).
Relative error = 1.68e-01
-----
Iteration: 7
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['8.76e+02', '-3.46e+03', '-3.86e+02', '2.19e+00', '1.25e+01', '-7.97e+02', '5.92e+02', '-5.87e-02', '3.43e+01', '2.07e+03', '-1.37e+03']
Constraint losses = 6.43e-02
Relative update of tokamak psi (in plasma core): 9.37e-03
Handing off to forward solve (with updated currents).
Relative error = 5.83e-05
-----
Iteration: 8
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['2.16e+03', '-8.15e+02', '-6.03e+02', '9.08e+02', '5.24e+02', '-5.04e+02', '3.12e+02', '-2.46e+02', '4.27e-01', '1.54e+03', '-1.02e+03']
Constraint losses = 4.83e-02
Relative update of tokamak psi (in plasma core): 1.90e-02
Handing off to forward solve (with updated currents).
Relative error = 1.51e-04
-----
Iteration: 9
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['6.44e+02', '2.38e+02', '-1.71e+02', '3.45e+02', '2.04e+02', '-2.32e+01', '8.15e+01', '-2.64e+02', '-1.81e+01', '3.00e+02', '2.17e+02']
Constraint losses = 3.69e-02
Relative update of tokamak psi (in plasma core): 1.63e-02
Handing off to forward solve (with updated currents).
Relative error = 5.44e-04
-----
Iteration: 10
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['7.69e+02', '5.37e+02', '-2.32e+02', '4.02e+02', '2.49e+02', '7.08e+01', '6.03e+01', '-3.11e+02', '-4.76e+01', '2.98e+02', '-4.09e+01']
Constraint losses = 3.56e-03
Relative update of tokamak psi (in plasma core): 1.27e-02
Handing off to forward solve (with updated currents).
Relative error = 9.14e-05
-----
Iteration: 11
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['3.71e+02', '2.81e+02', '-1.25e+02', '1.79e+02', '1.15e+02', '6.28e+01', '4.86e+00', '-1.30e+02', '-2.89e+01', '1.18e+02', '-2.51e+02']
Constraint losses = 8.74e-03
Relative update of tokamak psi (in plasma core): 5.12e-03
Handing off to forward solve (with updated currents).
Relative error = 3.52e-05
-----
Iteration: 12
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.94e+02', '1.65e+02', '-2.90e+01', '1.22e+02', '7.01e+01', '-3.31e+00', '3.47e+01', '-1.08e+02', '-1.52e+01', '8.67e+01', '2.21e+02']
Constraint losses = 2.19e-02
Relative update of tokamak psi (in plasma core): 1.02e-02
Handing off to forward solve (with updated currents).
Relative error = 3.80e-05
-----
Iteration: 13
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.70e+02', '5.20e+01', '-7.33e+01', '5.78e+01', '4.03e+01', '2.96e+01', '-1.45e+01', '-2.14e+01', '-2.99e+00', '5.29e+01', '-3.39e+02']
Constraint losses = 1.24e-02
Relative update of tokamak psi (in plasma core): 1.05e-02
Handing off to forward solve (with updated currents).
Relative error = 1.65e-05
-----
Iteration: 14
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.63e+02', '1.42e+02', '-1.83e+01', '1.08e+02', '6.09e+01', '-8.73e+00', '3.54e+01', '-9.91e+01', '-1.32e+01', '7.71e+01', '2.56e+02']
Constraint losses = 3.05e-02
Relative update of tokamak psi (in plasma core): 1.05e-02
Handing off to forward solve (with updated currents).
Relative error = 6.17e-05
-----
Iteration: 15
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['7.55e+01', '7.81e+01', '-3.44e+01', '2.37e+01', '1.87e+01', '3.41e+01', '-1.22e+01', '-1.51e+01', '-9.92e+00', '6.66e+00', '-1.67e+02']
Constraint losses = 6.80e-03
Relative update of tokamak psi (in plasma core): 5.49e-03
Handing off to forward solve (with updated currents).
Relative error = 4.68e-03
-----
Iteration: 16
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.33e+02', '9.08e+01', '-1.62e+01', '8.06e+01', '4.56e+01', '-1.09e+01', '2.52e+01', '-6.80e+01', '-5.81e+00', '6.60e+01', '1.47e+02']
Constraint losses = 1.43e-02
Relative update of tokamak psi (in plasma core): 7.43e-03
Handing off to forward solve (with updated currents).
Relative error = 3.94e-05
-----
Iteration: 17
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['2.25e+01', '1.07e+01', '-1.99e+01', '-7.43e+00', '-6.87e-01', '2.30e+01', '-1.56e+01', '1.18e+01', '-2.96e+00', '-9.42e+00', '-1.71e+02']
Constraint losses = 6.90e-03
Relative update of tokamak psi (in plasma core): 6.43e-03
Handing off to forward solve (with updated currents).
Relative error = 6.37e-04
-----
Iteration: 18
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['7.88e+01', '7.55e+01', '-3.83e+00', '5.39e+01', '3.00e+01', '-5.70e+00', '1.96e+01', '-5.20e+01', '-7.31e+00', '3.74e+01', '1.58e+02']
Constraint losses = 1.71e-02
Relative update of tokamak psi (in plasma core): 6.61e-03
Handing off to forward solve (with updated currents).
Relative error = 3.02e-04
-----
Iteration: 19
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['6.64e+00', '-1.35e+01', '-8.81e+00', '-1.51e+01', '-6.81e+00', '8.71e+00', '-1.18e+01', '2.09e+01', '3.64e+00', '-4.06e+00', '-1.44e+02']
Constraint losses = 5.54e-03
Relative update of tokamak psi (in plasma core): 5.34e-03
Handing off to forward solve (with updated currents).
Relative error = 1.26e-05
-----
Iteration: 20
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['5.76e+01', '5.62e+01', '-1.88e+00', '3.95e+01', '2.20e+01', '-4.04e+00', '1.49e+01', '-3.93e+01', '-5.85e+00', '2.67e+01', '1.26e+02']
Constraint losses = 1.37e-02
Relative update of tokamak psi (in plasma core): 5.25e-03
Handing off to forward solve (with updated currents).
Relative error = 7.58e-05
-----
Iteration: 21
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-4.44e+00', '-1.53e+01', '-5.32e+00', '-1.71e+01', '-8.14e+00', '1.13e+01', '-1.10e+01', '1.71e+01', '2.84e-01', '-1.29e+01', '-1.03e+02']
Constraint losses = 4.53e-03
Relative update of tokamak psi (in plasma core): 4.14e-03
Handing off to forward solve (with updated currents).
Relative error = 3.17e-05
-----
Iteration: 22
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['4.43e+01', '4.34e+01', '-2.08e-01', '2.99e+01', '1.65e+01', '-3.82e+00', '1.18e+01', '-2.99e+01', '-4.18e+00', '2.10e+01', '1.00e+02']
Constraint losses = 1.05e-02
Relative update of tokamak psi (in plasma core): 4.25e-03
Handing off to forward solve (with updated currents).
Relative error = 1.30e-03
-----
Iteration: 23
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['-1.90e+01', '-1.93e+01', '1.03e+00', '-2.36e+01', '-1.24e+01', '7.72e+00', '-1.03e+01', '2.25e+01', '2.34e+00', '-1.53e+01', '-8.99e+01']
Constraint losses = 4.22e-03
Relative update of tokamak psi (in plasma core): 3.79e-03
Handing off to forward solve (with updated currents).
Relative error = 1.47e-05
-----
Iteration: 24
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['3.50e+01', '3.22e+01', '1.05e-01', '2.32e+01', '1.28e+01', '-3.27e+00', '9.32e+00', '-2.34e+01', '-3.27e+00', '1.64e+01', '7.98e+01']
Constraint losses = 8.93e-03
Relative update of tokamak psi (in plasma core): 3.40e-03
Handing off to forward solve (with updated currents).
Relative error = 8.72e-06
-----
Iteration: 25
Using full Jacobian (of constraints wrt coil currents) to optimsise currents.
- calculating derivatives for coil 1/11
- calculating derivatives for coil 2/11
- calculating derivatives for coil 3/11
- calculating derivatives for coil 4/11
- calculating derivatives for coil 5/11
- calculating derivatives for coil 6/11
- calculating derivatives for coil 7/11
- calculating derivatives for coil 8/11
- calculating derivatives for coil 9/11
- calculating derivatives for coil 10/11
- calculating derivatives for coil 11/11
Change in coil currents (being controlled): ['2.62e-01', '-4.39e-01', '-1.28e+00', '-5.45e-01', '-4.17e-01', '3.63e-01', '-1.90e+00', '-1.54e+00', '-2.14e+00', '-3.45e+00', '-6.13e+00']
Constraint losses = 9.20e-03
Relative update of tokamak psi (in plasma core): 3.58e-04
Handing off to forward solve (with updated currents).
Relative error = 8.49e-04
-----
Iteration: 26
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['1.52e+00', '-1.47e+01', '-1.75e+00', '-1.01e+01', '-4.90e+00', '4.53e+00', '-4.53e+00', '1.17e+01', '2.56e+00', '-2.82e+00', '-5.43e+01']
Constraint losses = 2.58e-03
Relative update of tokamak psi (in plasma core): 2.00e-03
Handing off to forward solve (with updated currents).
Relative error = 1.52e-03
-----
Iteration: 27
Using simplified Green's Jacobian (of constraints wrt coil currents) to optimise the currents.
Change in coil currents (being controlled): ['2.76e+01', '1.77e+01', '-1.44e+00', '1.59e+01', '8.84e+00', '-3.05e+00', '5.98e+00', '-1.40e+01', '-9.17e-01', '1.39e+01', '3.97e+01']
Constraint losses = 5.77e-03
Relative update of tokamak psi (in plasma core): 1.96e-03
Handing off to forward solve (with updated currents).
Relative error = 5.55e-06
-----
Iteration: 28
Using full Jacobian (of constraints wrt coil currents) to optimsise currents.
- calculating derivatives for coil 1/11
- calculating derivatives for coil 2/11
- calculating derivatives for coil 3/11
- calculating derivatives for coil 4/11
- calculating derivatives for coil 5/11
- calculating derivatives for coil 6/11
- calculating derivatives for coil 7/11
- calculating derivatives for coil 8/11
- calculating derivatives for coil 9/11
- calculating derivatives for coil 10/11
- calculating derivatives for coil 11/11
Change in coil currents (being controlled): ['-9.20e-02', '2.58e-01', '-2.46e-01', '-1.84e-01', '-7.24e-02', '2.16e-01', '-6.35e-02', '1.35e-01', '-2.95e-02', '-1.55e-01', '-8.51e-02']
Constraint losses = 5.98e-04
Relative update of tokamak psi (in plasma core): 5.75e-06
Handing off to forward solve (with updated currents).
Relative error = 4.72e-07
-----
Inverse static solve SUCCESS. Tolerance 4.72e-07 (vs. requested 1e-06) reached in 29/100 iterations.
# plot the resulting equilibrium
fig1, ax1 = plt.subplots(1, 1, figsize=(4, 8), dpi=100)
ax1.grid(True, which='both')
eq.plot(axis=ax1, show=False)
eq.tokamak.plot(axis=ax1, show=False)
constrain.plot(axis=ax1,show=True)
ax1.set_xlim(0.1, 2.15)
ax1.set_ylim(-2.25, 2.25)
plt.tight_layout()
<Figure size 640x480 with 0 Axes>
Zoom in to see the snowflake divertor geometry!
# upper X-point
fig1, ax1 = plt.subplots(1, 1, figsize=(8, 8), dpi=80)
ax1.grid(True, which='both')
eq.plot(axis=ax1, show=False)
eq.tokamak.plot(axis=ax1, show=False)
constrain.plot(axis=ax1,show=False)
ax1.set_xlim(0.15, 1.15)
ax1.set_ylim(0.75, 1.75)
plt.tight_layout()
# lower X-point
fig1, ax1 = plt.subplots(1, 1, figsize=(8, 8), dpi=80)
ax1.grid(True, which='both')
eq.plot(axis=ax1, show=False)
eq.tokamak.plot(axis=ax1, show=False)
constrain.plot(axis=ax1,show=False)
ax1.set_xlim(0.15, 1.15)
ax1.set_ylim(-1.75, -0.75)
plt.tight_layout()
Just double check that the coil currents stayed within the limits!
# storage
coil_names = []
active_currents = []
min_limits = []
max_limits = []
for coil_name, coil_current, ul, ll in zip(
eq.tokamak.coils_dict,
eq.tokamak.getCurrentsVec()[:12],
coil_current_limits[0] + [None], # upper limits
coil_current_limits[1] + [None], # lower limits
):
coil_names.append(coil_name)
active_currents.append(coil_current)
min_limits.append(ll)
max_limits.append(ul)
# skip coils without limits
if ul is not None or ll is not None:
if (coil_current >= ll) & (coil_current <= ul):
within_limit = True
else:
within_limit = False
else:
within_limit = None
# print
print(f"{coil_name}: {coil_current:.2f} --> [{ll},{ul}] [A] --> limits met = {within_limit}")
# convert to numpy
active_currents = np.array(active_currents)
min_limits = np.array(min_limits)
max_limits = np.array(max_limits)
Solenoid: -4189.45 --> [-10000.0,10000.0] [A] --> limits met = True
PX: 2970.82 --> [-6000.0,6000.0] [A] --> limits met = True
D1: 6092.99 --> [-9000.0,9000.0] [A] --> limits met = True
D2: -6683.42 --> [-9000.0,9000.0] [A] --> limits met = True
D3: -5604.57 --> [-7000.0,7000.0] [A] --> limits met = True
Dp: 3405.97 --> [-7000.0,7000.0] [A] --> limits met = True
D5: -3874.58 --> [-5000.0,5000.0] [A] --> limits met = True
D6: 2161.94 --> [-3500.0,3500.0] [A] --> limits met = True
D7: 4840.29 --> [-5000.0,5000.0] [A] --> limits met = True
P4: -7322.61 --> [-12000.0,0] [A] --> limits met = True
P5: -3380.38 --> [-12000.0,0] [A] --> limits met = True
P6: 0.00 --> [None,None] [A] --> limits met = None
# VISUALISE HOW CLOSE THEY ARE TO THE LIMITS (normalised)
# normalized to [-1, 1] scale (relative to min/max)
min_limits[min_limits==None] = np.inf
max_limits[max_limits==None] = np.inf
norm_current = 2 * (active_currents - min_limits) / (max_limits - min_limits) - 1
# plot
plt.figure(figsize=(12, 5), dpi=70)
x = np.arange(len(coil_names))
# line plot
plt.plot(x, norm_current, marker='x', linestyle='-', color='red', label="Current")
plt.axhline(-1, color='k', linestyle='-', linewidth=1, label="Min. limit")
plt.axhline(1, color='k', linestyle='-', linewidth=1, label="Max. limit")
# formatting
plt.xticks(x, coil_names, rotation=45, ha='right')
plt.ylabel('Coil name [Amps]')
plt.ylabel('Normalised current')
plt.title('Coil current proximity to limits (normalised)')
plt.grid(True, linestyle='--', alpha=0.6)
plt.legend()
plt.tight_layout()
plt.show()
/tmp/ipykernel_2505/2518307275.py:6: RuntimeWarning: invalid value encountered in subtract
norm_current = 2 * (active_currents - min_limits) / (max_limits - min_limits) - 1