"""Contains s-alpha equilibrium."""
import numpy as np
import xarray as xr
from torx import Quantity
from torx.units_handling_m import check_units
from torx import make_xarray
from torx.equilibrium.equilibrium_m import EquilibriumBaseClass
from torx.autogrid_decorators_m import autogrid_method
from torx.autodoc_decorators_m import autodoc_class
[docs]
@autodoc_class
class SalphaEquilibrium(EquilibriumBaseClass):
"""
Represents a Salpha equilibrium as defined in PARALLAX.
Similar to CircularToroidalEquilibrium but mostly used for legacy access
to such simulations. Please consider using CircularToroidalEquilibrium
instead.
"""
[docs]
def __init__(
self,
rhomin: float=0.1,
rhomax: float=0.7,
minor_r: float=0.365,
b_ref: float=1.0,
l_ref: float=1.0,
q_ref: float=0.854,
shear: float=2.184,
theta_limiter: float=3.1415,
dtheta_limiter: float=0.4,
rho_limiter: float=0.3235,
B0: Quantity=Quantity("1T"),
L_ref: float=1.0,
):
"""
Initialize the SalphaEquilibrium.
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. By
definition, the magnetic axis is located at R = L_ref meters.
"""
self.B0 = B0
self._norm_length = Quantity(L_ref, "m")
# Box-fraction
boxfac = 1.25
self.x0 = 1.0
self.y0 = 0.0
self.minor_r = minor_r
self.rhomin = rhomin
self.rhomax = rhomax
self.xmin = 1.0 - minor_r * boxfac * rhomax
self.xmax = 1.0 + minor_r * boxfac * rhomax
self.ymin = -minor_r * boxfac * rhomax
self.ymax = minor_r * boxfac * rhomax
self.L_ref = l_ref
self.B_ref = b_ref
self.q_ref = q_ref
self.shear = shear
self.theta_limiter = theta_limiter
self.dtheta_limiter = dtheta_limiter
self.rho_limiter = rho_limiter
# 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 make_xarray(self.x0)
@property
def axis_z_norm(self):
"""Magnetic axis vertical position in normalized units."""
return make_xarray(self.y0)
@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 = np.sqrt((r_mesh - 1.0)**2 + z_mesh**2) / self.minor_r
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)
rho = self.normalized_flux_surface_label(r_norm, z_norm)
theta = self.theta(r_norm, z_norm)
Br = -(rho * self.minor_r / r_norm) * np.sin(theta) / self.qval(rho)
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)
rho = self.normalized_flux_surface_label(r_norm, z_norm)
theta = self.theta(r_norm, z_norm)
Bz = (rho * self.minor_r / r_norm) * np.cos(theta) / self.qval(rho)
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)
if kwargs["is_structured"]:
r_mesh, _ = np.meshgrid(r_norm, z_norm)
else:
r_mesh = r_norm
return xr.DataArray(1.0 / r_mesh,
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)
if kwargs["is_structured"]:
r_mesh, _ = np.meshgrid(r_norm, z_norm)
else:
r_mesh = r_norm
return xr.DataArray(r_mesh,
attrs={"norm":self.R0},
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 = np.arctan2(z_mesh, r_mesh - 1.0)
theta = np.mod(theta, 2.0 * np.pi)
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 rho poloidal."""
return self.q_ref + self.shear * rho**2
[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
[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.
"""
from torx.geometry.polygon_2d_m import Polygon2D
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)