Source code for torx.equilibrium.circular_m

"""Contains circular equilibrium."""
import numpy as np
import xarray as xr
from torx import Quantity
from torx.units_handling_m import check_units
from torx.equilibrium.equilibrium_m import EquilibriumBaseClass
from torx.geometry.polygon_2d_m import Polygon2D
from torx.autogrid_decorators_m import autogrid_method
from torx.autodoc_decorators_m 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 = np.sqrt(r_mesh**2 + z_mesh**2) 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) return xr.DataArray(-rho * np.sin(theta) / self.qval(rho), 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) return xr.DataArray(rho * np.cos(theta) / self.qval(rho), 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 = np.arctan2(z_mesh, r_mesh) 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.""" if self.qtype == 0: return self.q_ref + self.shear * (rho - self.rhoq_ref) elif self.qtype == 1: return self.q_ref / (1.0 - self.shear * (rho - self.rhoq_ref)) else: raise NotImplementedError( f"Error: No implementation for qtype={self.qtype} " \ "in circular equilibrium" )
[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. """ 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)