Example: Static forward free-boundary equilibrium calculations (in OpenSTEP)
Here we set up the OpenSTEP tokamak, plot its coil and limiter geometry, and solve a static forward free-boundary Grad-Shafranov equilibrium using the packaged coil currents and profile data.
Refer back to Example02c where we analyse convergence of this case using higher resolution spatial meshes - this is required for STEP-like plasmas that have quite complex internal current density profiles!
Import packages
import pickle
import matplotlib.pyplot as plt
import numpy as np
Create the machine object
from freegsnke import build_machine
tokamak = build_machine.tokamak(
active_coils_path="../machine_configs/OpenSTEP/OpenSTEP_simple_active_coils.pickle",
limiter_path="../machine_configs/OpenSTEP/OpenSTEP_simple_limiter.pickle",
wall_path="../machine_configs/OpenSTEP/OpenSTEP_simple_limiter.pickle",
)
Active coils --> built from pickle file.
Passive structures --> none provided.
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.
# plot the machine
fig1, ax1 = plt.subplots(1, 1, figsize=(4, 8), dpi=80)
plt.tight_layout()
tokamak.plot(axis=ax1, show=False)
ax1.fill(tokamak.wall.R, tokamak.wall.Z, color='k', linewidth=1.2, facecolor='w', zorder=0)
ax1.grid(zorder=0, alpha=0.5)
ax1.set_aspect('equal')
ax1.set_xlabel(r'Major radius, $R$ [m]')
ax1.set_ylabel(r'Height, $Z$ [m]')
Text(10.027777777777777, 0.5, 'Height, $Z$ [m]')
Instantiate an equilibrium
Let’s set up the equilibrium and profiles objects using the packaged data. We’ll then design an initial plasma flux guess using some approximate information about the plasma shape.
# load the p' and FF' profile data from file
with open('../machine_configs/OpenSTEP/OpenSTEP_profiles.pickle', 'rb') as f:
profiles_data = pickle.load(f)
from freegsnke import equilibrium_update
from freegsnke.jtor_update import GeneralPprimeFFprime
from freegsnke.equilibrium import PsiGuessParabolicJtor
# equilibrium
eq = equilibrium_update.Equilibrium(
tokamak=tokamak, # provide tokamak object
Rmin=0.5, Rmax=9.0, # radial range
Zmin=-10.0, Zmax=10.0, # vertical range
nx=65, # number of grid points in the radial direction
ny=129, # number of grid points in the vertical direction
)
# set active coil currents
currentvec = np.array([
-4790870.39240653,
5155466.64491291,
2254704.04332795,
5458947.31403196,
-7854367.88994826,
5100000.0,
-2274646.53005716,
])
eq.tokamak.set_all_coil_currents(currentvec)
# profiles
profiles = GeneralPprimeFFprime(
eq=eq,
Ip=2.2767e7,
fvac=0.5,
psi_n=np.linspace(0,1,256),
pprime_data=profiles_data['pprime'],
ffprime_data=profiles_data['ffprime']
)
# create the initial guess using alternate method
parabolic_plasma_psi = PsiGuessParabolicJtor(
Ip = 2.2767e7, # plasma current (to scale the guess)
h = (max(tokamak.wall.R) + min(tokamak.wall.R))/2, # centre of ellipse (R)
k = 0.0, # centre of ellipse (Z)
a = 1.0, # roughly the minor radius of the plasma
b = 1.0*5, # roughly the minor radius times the elongation of expected plasma
psi_scale = 1.0 # might need to vary this default
)
# set plasma psi guess
_plasma_psi = parabolic_plasma_psi(eq=eq)
eq.plasma_psi = _plasma_psi
Load the static nonlinear solver
from freegsnke import GSstaticsolver
GSStaticSolver = GSstaticsolver.NKGSsolver(
eq=eq,
solver_type='LUsparse',
gs_operator_order=4,
)
The forward solve
Note that we solve to a high tolerance and the force_up_down_symmetric flag has been enabled. This is because of the complexity of the internal plasma current density profile of this equilibrium.
# solver
GSStaticSolver.solve(
eq=eq,
profiles=profiles,
constrain=None,
target_relative_tolerance=1e-10,
force_up_down_symmetric=True,
verbose=True,
)
-----
Forward static solve starting...
Initial guess for plasma_psi successful, residual found.
Initial relative error = 7.75e-01
-----
Forward static solve starting...
Initial guess for plasma_psi successful, residual found.
Initial relative error = 7.75e-01
-----
Picard iteration: 0
Forcing up-dpwn symmetry of the plasma.
Update too large, resized.
...relative error = 6.53e-01
-----
Picard iteration: 1
Forcing up-dpwn symmetry of the plasma.
Update too large, resized.
...relative error = 5.28e-01
-----
Picard iteration: 2
Forcing up-dpwn symmetry of the plasma.
Update too large, resized.
...relative error = 3.92e-01
-----
Picard iteration: 3
Forcing up-dpwn symmetry of the plasma.
Update too large, resized.
...relative error = 2.61e-01
-----
Picard iteration: 4
Forcing up-dpwn symmetry of the plasma.
Update too large, resized.
...relative error = 1.26e-01
-----
Picard iteration: 5
Forcing up-dpwn symmetry of the plasma.
...relative error = 4.93e-02
-----
-----
Newton-Krylov iteration: 6
...number of Krylov vectors used = 1
Forcing up-dpwn symmetry of the plasma.
Increase in residual, update reduction triggered.
...relative error = 4.21e-02
-----
-----
Newton-Krylov iteration: 7
...number of Krylov vectors used = 3
Forcing up-dpwn symmetry of the plasma.
Update too large, resized.
New starting_direction used due to collinear residuals.
...relative error = 4.18e-02
-----
-----
Newton-Krylov iteration: 8
...number of Krylov vectors used = 8
Forcing up-dpwn symmetry of the plasma.
New starting_direction used due to collinear residuals.
...relative error = 2.29e-02
-----
-----
Newton-Krylov iteration: 9
...number of Krylov vectors used = 13
Forcing up-dpwn symmetry of the plasma.
...relative error = 3.63e-02
-----
-----
Newton-Krylov iteration: 10
...number of Krylov vectors used = 2
Forcing up-dpwn symmetry of the plasma.
...relative error = 1.12e-02
-----
-----
Newton-Krylov iteration: 11
...number of Krylov vectors used = 5
Forcing up-dpwn symmetry of the plasma.
...relative error = 7.13e-03
-----
-----
Newton-Krylov iteration: 12
...number of Krylov vectors used = 4
Forcing up-dpwn symmetry of the plasma.
...relative error = 2.10e-02
-----
-----
Newton-Krylov iteration: 13
...number of Krylov vectors used = 4
Forcing up-dpwn symmetry of the plasma.
...relative error = 6.63e-03
-----
-----
Newton-Krylov iteration: 14
...number of Krylov vectors used = 1
Forcing up-dpwn symmetry of the plasma.
Increase in residual, update reduction triggered.
...relative error = 3.48e-03
-----
-----
Newton-Krylov iteration: 15
...number of Krylov vectors used = 2
Forcing up-dpwn symmetry of the plasma.
Increase in residual, update reduction triggered.
...relative error = 6.89e-03
-----
-----
Newton-Krylov iteration: 16
...number of Krylov vectors used = 1
Forcing up-dpwn symmetry of the plasma.
...relative error = 5.17e-03
-----
-----
Newton-Krylov iteration: 17
...number of Krylov vectors used = 2
Forcing up-dpwn symmetry of the plasma.
...relative error = 2.26e-03
-----
-----
Newton-Krylov iteration: 18
...number of Krylov vectors used = 1
Forcing up-dpwn symmetry of the plasma.
...relative error = 8.58e-04
-----
-----
Newton-Krylov iteration: 19
...number of Krylov vectors used = 1
Forcing up-dpwn symmetry of the plasma.
...relative error = 3.38e-04
-----
-----
Newton-Krylov iteration: 20
...number of Krylov vectors used = 2
Forcing up-dpwn symmetry of the plasma.
...relative error = 1.10e-05
-----
-----
Newton-Krylov iteration: 21
...number of Krylov vectors used = 2
Forcing up-dpwn symmetry of the plasma.
...relative error = 2.09e-06
-----
-----
Newton-Krylov iteration: 22
...number of Krylov vectors used = 2
Forcing up-dpwn symmetry of the plasma.
...relative error = 2.05e-07
-----
-----
Newton-Krylov iteration: 23
...number of Krylov vectors used = 2
Forcing up-dpwn symmetry of the plasma.
...relative error = 2.59e-08
-----
-----
Newton-Krylov iteration: 24
...number of Krylov vectors used = 3
Forcing up-dpwn symmetry of the plasma.
...relative error = 6.46e-09
-----
-----
Newton-Krylov iteration: 25
...number of Krylov vectors used = 2
Forcing up-dpwn symmetry of the plasma.
...relative error = 8.81e-10
-----
-----
Newton-Krylov iteration: 26
...number of Krylov vectors used = 2
Forcing up-dpwn symmetry of the plasma.
...relative error = 1.39e-10
-----
-----
Newton-Krylov iteration: 27
...number of Krylov vectors used = 3
Forcing up-dpwn symmetry of the plasma.
...relative error = 1.12e-11
-----
Forward static solve SUCCESS. Tolerance 1.12e-11 (vs. requested 1.00e-10) reached in 28/100 iterations.
# plot the converged equilibrium
fig2, ax2 = plt.subplots(1, 1, figsize=(5, 10), dpi=80)
plt.tight_layout()
ax2.grid(zorder=0, alpha=0.5)
ax2.set_aspect('equal')
eq.tokamak.plot(axis=ax2, show=False)
ax2.fill(tokamak.wall.R, tokamak.wall.Z, color='k', linewidth=1.2, facecolor='w', zorder=0)
eq.plot(axis=ax2, show=False)
ax2.set_xlabel(r'Major radius, $R$ [m]')
ax2.set_ylabel(r'Height, $Z$ [m]')
Text(20.02777777777778, 0.5, 'Height, $Z$ [m]')
eq.tokamak.getCurrents()
{'p3': np.float64(-4790870.39240653),
'p4': np.float64(5155466.64491291),
'p5': np.float64(2254704.04332795),
'p6': np.float64(5458947.31403196),
'p9': np.float64(-7854367.88994826),
's1': np.float64(5100000.0),
's2': np.float64(-2274646.53005716)}