"""Contains circular equilibrium."""
import numpy as np
import xarray as xr
from numba import njit, cfunc
from numbalsoda import lsoda_sig
from .equilibrium_m import EquilibriumBaseClass
from torx.units import Quantity
from torx.units import check_units
from torx.geometry import Polygon2D
from torx.decorators import autogrid_method
from torx.decorators import autodoc_class
[docs]
@autodoc_class
class CircularEquilibrium(EquilibriumBaseClass):
"""
Represents a Circular equilibrium as defined in PARALLAX.
Has concentric circles as flux surfaces and is located at R=0, the
toroidal field is defined as 1.
"""
[docs]
def __init__(
self,
rhomin: float=0.2412,
rhomax: float=0.3685,
qtype: float=1.0,
rhoq_ref: float=0.305,
q_ref: float=4.0,
shear: float=3.71,
theta_limiter: float=3.1415,
dtheta_limiter: float=0.4,
rho_limiter: float=0.3235,
pen_chi_radial_w: float=4e-3,
B0: Quantity=Quantity("1T"),
L_ref: float=1.0,
):
"""
Initialize the CircularEquilibrium.
Arguments to the initializer match the parameters supplied in PARALLAX.
You can either manually specify the arguments,
or pass a **kwargs_dictionary generated from a parameter file.
The B0 parameter sets the value of the toroidal magnetic field at the
normalized axis location (x0, y0).
The L_ref parameter sets the length normalization in meters.
"""
self.B0 = B0
self._norm_length = Quantity(L_ref, "m")
# Box-fraction
boxfac = 0.4 / 0.3685
self.x0 = 0.0
self.y0 = 0.0
self.rhomin = rhomin
self.rhomax = rhomax
self.xmin = -boxfac * rhomax
self.xmax = boxfac * rhomax
self.ymin = -boxfac * rhomax
self.ymax = boxfac * rhomax
self.qtype = qtype
self.rhoq_ref = rhoq_ref
self.q_ref = q_ref
self.shear = shear
self.theta_limiter = theta_limiter
self.dtheta_limiter = dtheta_limiter
self.rho_limiter = rho_limiter
self.pen_chi_radial_w = pen_chi_radial_w
# compute position of limiter plates
self.thlim1 = theta_limiter + dtheta_limiter
self.thlim1 = np.mod(self.thlim1, 2.0 * np.pi)
self.thlim2 = theta_limiter - dtheta_limiter
self.thlim2 = np.mod(self.thlim2, 2.0 * np.pi)
# if no limiter set, set rho_limiter large
if dtheta_limiter <= 0.0:
self.rho_limiter = 10.0 * rhomax
assert(dtheta_limiter < np.pi), \
"Error: dtheta_limiter cannot be larger than pi"
@property
def axis_r_norm(self):
"""Magnetic axis radial position in normalized units."""
return xr.DataArray(self.x0, attrs={"norm":self.R0})
@property
def axis_z_norm(self):
"""Magnetic axis vertical position in normalized units."""
return xr.DataArray(self.y0, attrs={"norm":self.R0})
@property
def B0(self) -> Quantity:
"""Magnetic field normalization."""
return self._norm_magfield
@B0.setter
def B0(self, value: Quantity):
"""Set the magnetic field normalization."""
check_units(value, {"[mass]":1, "[time]":-2, "[current]":-1}, "B0")
self._norm_magfield = value
@property
def R0(self) -> Quantity:
"""Length normalization."""
return self._norm_length
@R0.setter
def R0(self, value: Quantity):
"""Set the length normalization."""
check_units(value, {"[length]":1}, "R0")
self._norm_length = value
[docs]
@autogrid_method
def normalized_flux_surface_label(self, r_norm, z_norm, **kwargs) \
-> xr.DataArray:
"""Return the normalized flux surface label (rho_pol)."""
r_norm = self.convert_length_to_normalized(r_norm)
z_norm = self.convert_length_to_normalized(z_norm)
if kwargs["is_structured"]:
r_mesh, z_mesh = np.meshgrid(r_norm, z_norm)
else:
r_mesh = r_norm
z_mesh = z_norm
rho = type(self)._rho_base(np.asarray(r_mesh), np.asarray(z_mesh))
return xr.DataArray(rho,
attrs={"norm":Quantity(1.0)},
dims=kwargs["dims"],
coords=kwargs["coords"])
[docs]
@autogrid_method
def magfield_component_r(self, r_norm, z_norm, **kwargs) -> xr.DataArray:
"""Return the radial magnetic field component."""
r_norm = self.convert_length_to_normalized(r_norm)
z_norm = self.convert_length_to_normalized(z_norm)
if kwargs["is_structured"]:
r_mesh, z_mesh = np.meshgrid(r_norm, z_norm)
else:
r_mesh, z_mesh = r_norm, z_norm
Br = type(self)._br_base(np.asarray(r_mesh), np.asarray(z_mesh),
self.qtype, self.rhoq_ref, self.q_ref, self.shear)
return xr.DataArray(Br,
attrs={"norm":self.B0},
dims=kwargs["dims"],
coords=kwargs["coords"])
[docs]
@autogrid_method
def magfield_component_z(self, r_norm, z_norm, **kwargs) -> xr.DataArray:
"""Return the vertical magnetic field component."""
r_norm = self.convert_length_to_normalized(r_norm)
z_norm = self.convert_length_to_normalized(z_norm)
if kwargs["is_structured"]:
r_mesh, z_mesh = np.meshgrid(r_norm, z_norm)
else:
r_mesh, z_mesh = r_norm, z_norm
Bz = type(self)._bz_base(np.asarray(r_mesh), np.asarray(z_mesh),
self.qtype, self.rhoq_ref, self.q_ref, self.shear)
return xr.DataArray(Bz,
attrs={"norm":self.B0},
dims=kwargs["dims"],
coords=kwargs["coords"])
[docs]
@autogrid_method
def magfield_component_toroidal(self, r_norm, z_norm, **kwargs) \
-> xr.DataArray:
"""Return the toroidal magnetic field component."""
r_norm = self.convert_length_to_normalized(r_norm)
z_norm = self.convert_length_to_normalized(z_norm)
rho = self.normalized_flux_surface_label(r_norm, z_norm)
return xr.DataArray(np.ones_like(rho),
attrs={"norm":self.B0},
dims=kwargs["dims"],
coords=kwargs["coords"])
[docs]
@autogrid_method
def jacobian(self, r_norm, z_norm, **kwargs) -> xr.DataArray:
"""Return the Jacobian of the circular geometry."""
r_norm = self.convert_length_to_normalized(r_norm)
z_norm = self.convert_length_to_normalized(z_norm)
rho = self.normalized_flux_surface_label(r_norm, z_norm)
return xr.DataArray(np.ones_like(rho),
attrs={"norm":Quantity(1.0)},
dims=kwargs["dims"],
coords=kwargs["coords"])
[docs]
@autogrid_method
def theta(self, r_norm, z_norm, **kwargs) -> xr.DataArray:
"""Return the geometric poloidal angle in the range [0, 2*pi]."""
r_norm = self.convert_length_to_normalized(r_norm)
z_norm = self.convert_length_to_normalized(z_norm)
if kwargs["is_structured"]:
r_mesh, z_mesh = np.meshgrid(r_norm, z_norm)
else:
r_mesh = r_norm
z_mesh = z_norm
theta = type(self)._theta_base(np.asarray(r_mesh), np.asarray(z_mesh))
return xr.DataArray(theta,
attrs={"norm":Quantity(1.0)},
dims=kwargs["dims"],
coords=kwargs["coords"])
[docs]
def qval(self, rho):
"""Return the safety factor for a given flux-surface label."""
return type(self)._qval_base(rho, self.qtype,
self.rhoq_ref, self.q_ref,
self.shear)
[docs]
def theta_centered(self, theta, limiter_edge):
"""Return the signed poloidal distance to limiter edge."""
theta_centered = np.mod(theta - limiter_edge, 2.0 * np.pi)
if theta_centered.ndim > 0:
LL = np.where(theta_centered > np.pi - self.dtheta_limiter)
theta_centered[LL] -= 2.0 * np.pi
elif theta_centered > np.pi - self.dtheta_limiter:
theta_centered -= 2.0 * np.pi
return theta_centered
def _magfield_scalars(self, r: float, z: float, phi: float) -> tuple:
"""Return (b_r, b_z, b_t, jacobian) as plain floats for ODE hot path."""
rho = np.sqrt(r * r + z * z)
theta = np.arctan2(z, r) % (2.0 * np.pi)
q = self.qval(rho)
b_r = -rho * np.sin(theta) / q
b_z = rho * np.cos(theta) / q
return b_r, b_z, 1.0, 1.0
[docs]
def get_boundary_polygon(self, angular_resolution=100, rhomax_factor=1.1):
"""
Return a polygon which approximates the position of the limiter.
Can be used for plotting and to control fieldline tracing.
"""
theta_points = np.linspace(0, 2 * np.pi, num=angular_resolution)
rho_points = np.ones_like(theta_points) * self.rhomax * rhomax_factor
in_limiter = np.logical_and(
self.theta_centered(theta_points, self.thlim1) < 0.0,
self.theta_centered(theta_points, self.thlim2) > 0.0,
)
rho_points[in_limiter] = min(self.rho_limiter,
self.rhomax * rhomax_factor)
x_points = rho_points * np.cos(theta_points)
y_points = rho_points * np.sin(theta_points)
return Polygon2D(x_points=x_points, y_points=y_points)
@staticmethod
@njit
def _rho_base(r, z):
return np.sqrt(r**2 + z**2)
@staticmethod
@njit
def _theta_base(r, z):
th = np.arctan2(z, r)
return np.mod(th, 2.0 * np.pi)
@staticmethod
@njit
def _qval_base(rho, qtype, rhoq_ref, q_ref, shear):
if qtype == 0:
return q_ref + shear * (rho - rhoq_ref)
return q_ref / (1.0 - shear * (rho - rhoq_ref))
@staticmethod
@njit
def _br_base(r, z, qtype, rhoq_ref, q_ref, shear):
rho = np.sqrt(r**2 + z**2)
theta = np.mod(np.arctan2(z, r), 2.0 * np.pi)
if qtype == 0:
qval = q_ref + shear * (rho - rhoq_ref)
else:
qval = q_ref / (1.0 - shear * (rho - rhoq_ref))
return -rho * np.sin(theta) / qval
@staticmethod
@njit
def _bz_base(r, z, qtype, rhoq_ref, q_ref, shear):
rho = np.sqrt(r**2 + z**2)
theta = np.mod(np.arctan2(z, r), 2.0 * np.pi)
if qtype == 0:
qval = q_ref + shear * (rho - rhoq_ref)
else:
qval = q_ref / (1.0 - shear * (rho - rhoq_ref))
return rho * np.cos(theta) / qval
[docs]
def make_trace_eq(self):
"""Return cfunc address for numbalsoda field-line tracing."""
qtype = self.qtype
rhoq_ref = self.rhoq_ref
q_ref = self.q_ref
shear = self.shear
@cfunc(lsoda_sig)
def trace_eq(t, y, du, p): # pragma: no cover
r_norm = y[0]
z_norm = y[1]
rho = np.sqrt(r_norm**2 + z_norm**2)
theta = np.mod(np.arctan2(z_norm, r_norm), 2.0 * np.pi)
if qtype == 0:
qval = q_ref + shear * (rho - rhoq_ref)
else:
qval = q_ref / (1.0 - shear * (rho - rhoq_ref))
factor = rho / qval
du[0] = -factor * np.sin(theta)
du[1] = factor * np.cos(theta)
du[2] = np.sqrt(du[0] * du[0] + du[1] * du[1] + 1.0)
return trace_eq.address