Skip to content

Commit

Permalink
Merge branch 'yge/qr' of github.com:PlasmaControl/DESC into yge/qr
Browse files Browse the repository at this point in the history
  • Loading branch information
YigitElma committed Aug 15, 2024
2 parents dc1c6aa + 0c0430e commit 7975cc3
Show file tree
Hide file tree
Showing 33 changed files with 1,879 additions and 952 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

New Features

- Add ``use_signed_distance`` flag to ``PlasmaVesselDistance`` which will use a signed distance as the target, which is positive when the plasma is inside of the vessel surface and negative if the plasma is outside of the vessel surface, to allow optimizer to distinguish if the equilbrium surface exits the vessel surface and guard against it by targeting a positive signed distance.

v0.12.1
-------

Expand Down
27 changes: 27 additions & 0 deletions desc/compute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,30 @@ def _build_data_index():


_build_data_index()


def set_tier(name, p):
"""Determine how deep in the dependency tree a given name is.
tier of 0 means no dependencies on other data,
tier of 1 means it depends on only tier 0 stuff,
tier of 2 means it depends on tier 0 and tier 1, etc etc.
Designed such that if you compute things in the order determined by tiers,
all dependencies will always be computed in the correct order.
"""
if "tier" in data_index[p][name]:
return
if len(data_index[p][name]["full_with_axis_dependencies"]["data"]) == 0:
data_index[p][name]["tier"] = 0
else:
thistier = 0
for name1 in data_index[p][name]["full_with_axis_dependencies"]["data"]:
set_tier(name1, p)
thistier = max(thistier, data_index[p][name1]["tier"])
data_index[p][name]["tier"] = thistier + 1


for par in data_index.keys():
for name in data_index[par]:
set_tier(name, par)
241 changes: 235 additions & 6 deletions desc/compute/_basis_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from desc.backend import jnp

from .data_index import register_compute_fun
from .utils import cross, safediv
from .utils import cross, dot, safediv


@register_compute_fun(
Expand All @@ -34,7 +34,7 @@ def _b(params, transforms, profiles, data, **kwargs):


@register_compute_fun(
name="e^rho",
name="e^rho", # ∇ρ is the same in (ρ,θ,ζ) and (ρ,α,ζ) coordinates.
label="\\mathbf{e}^{\\rho}",
units="m^{-1}",
units_long="inverse meters",
Expand Down Expand Up @@ -927,7 +927,7 @@ def _e_sup_theta_zz(params, transforms, profiles, data, **kwargs):


@register_compute_fun(
name="e^zeta",
name="e^zeta", # ∇ζ is the same in (ρ,θ,ζ) and (ρ,α,ζ) coordinates.
label="\\mathbf{e}^{\\zeta}",
units="m^{-1}",
units_long="inverse meters",
Expand Down Expand Up @@ -1431,7 +1431,6 @@ def _e_sub_rho(params, transforms, profiles, data, **kwargs):
# At the magnetic axis, this function returns the multivalued map whose
# image is the set { 𝐞ᵨ | ρ=0 }.
data["e_rho"] = jnp.array([data["R_r"], data["R"] * data["omega_r"], data["Z_r"]]).T

return data


Expand Down Expand Up @@ -3396,13 +3395,243 @@ def _n_zeta(params, transforms, profiles, data, **kwargs):
transforms={},
profiles=[],
coordinates="rtz",
data=["e_theta", "e_phi", "phi_t"],
data=["e_theta", "e_phi|r,t", "phi_t"],
parameterization=[
"desc.equilibrium.equilibrium.Equilibrium",
"desc.geometry.surface.FourierRZToroidalSurface",
"desc.geometry.core.Surface",
],
)
def _e_sub_theta_rp(params, transforms, profiles, data, **kwargs):
data["e_theta|r,p"] = data["e_theta"] - (data["e_phi"].T * data["phi_t"]).T
data["e_theta|r,p"] = data["e_theta"] - (data["e_phi|r,t"].T * data["phi_t"]).T
return data


@register_compute_fun(
name="e_rho|a,z",
label="\\mathbf{e}_{\\rho} |_{\\alpha, \\zeta}",
units="m",
units_long="meters",
description="Tangent vector along radial field line label",
dim=3,
params=[],
transforms={},
profiles=[],
coordinates="rtz",
data=["e_rho", "e_alpha", "alpha_r"],
)
def _e_rho_az(params, transforms, profiles, data, **kwargs):
# constant α and ζ
data["e_rho|a,z"] = (
data["e_rho"] - data["e_alpha"] * data["alpha_r"][:, jnp.newaxis]
)
return data


@register_compute_fun(
name="e_alpha",
label="\\mathbf{e}_{\\alpha}",
units="m",
units_long="meters",
description="Tangent vector along poloidal field line label",
dim=3,
params=[],
transforms={},
profiles=[],
coordinates="rtz",
data=["e_theta", "alpha_t"],
)
def _e_alpha(params, transforms, profiles, data, **kwargs):
# constant ρ and ζ
data["e_alpha"] = data["e_theta"] / data["alpha_t"][:, jnp.newaxis]
return data


@register_compute_fun(
name="e_alpha_t",
label="\\partial_{\\theta} \\mathbf{e}_{\\alpha}",
units="m",
units_long="meters",
description="Tangent vector along poloidal field line label, derivative wrt"
" DESC poloidal angle",
dim=3,
params=[],
transforms={},
profiles=[],
coordinates="rtz",
data=["e_theta", "alpha_t", "e_theta_t", "alpha_tt"],
)
def _e_alpha_t(params, transforms, profiles, data, **kwargs):
data["e_alpha_t"] = (
data["e_theta_t"] / data["alpha_t"][:, jnp.newaxis]
- data["e_theta"] * (data["alpha_tt"] / data["alpha_t"] ** 2)[:, jnp.newaxis]
)
return data


@register_compute_fun(
name="e_alpha_z",
label="\\partial_{\\zeta} \\mathbf{e}_{\\alpha}",
units="m",
units_long="meters",
description="Tangent vector along poloidal field line label, "
"derivative wrt DESC toroidal angle",
dim=3,
params=[],
transforms={},
profiles=[],
coordinates="rtz",
data=["e_theta", "alpha_t", "e_theta_z", "alpha_tz"],
)
def _e_alpha_z(params, transforms, profiles, data, **kwargs):
data["e_alpha_z"] = (
data["e_theta_z"] / data["alpha_t"][:, jnp.newaxis]
- data["e_theta"] * (data["alpha_tz"] / data["alpha_t"] ** 2)[:, jnp.newaxis]
)
return data


@register_compute_fun(
name="e_zeta|r,a", # Same as B/(B⋅∇ζ).
label="\\mathbf{e}_{\\zeta} |_{\\rho, \\alpha} "
"= \\frac{\\mathbf{B}}{\\mathbf{B} \\cdot \\nabla \\zeta}",
units="m",
units_long="meters",
description="Tangent vector along (collinear to) field line",
dim=3,
params=[],
transforms={},
profiles=[],
coordinates="rtz",
data=["e_zeta", "e_alpha", "alpha_z"],
)
def _e_zeta_ra(params, transforms, profiles, data, **kwargs):
data["e_zeta|r,a"] = (
data["e_zeta"] - data["e_alpha"] * data["alpha_z"][:, jnp.newaxis]
)
return data


@register_compute_fun(
name="(e_zeta|r,a)_t",
label="\\partial_{\\theta} (\\mathbf{e}_{\\zeta} |_{\\rho, \\alpha})",
units="m",
units_long="meters",
description="Tangent vector along (collinear to) field line, "
"derivative wrt DESC poloidal angle",
dim=3,
params=[],
transforms={},
profiles=[],
coordinates="rtz",
data=["e_zeta_t", "e_alpha", "alpha_z", "e_alpha_t", "alpha_tz"],
)
def _e_zeta_ra_t(params, transforms, profiles, data, **kwargs):
data["(e_zeta|r,a)_t"] = data["e_zeta_t"] - (
data["e_alpha_t"] * data["alpha_z"][:, jnp.newaxis]
+ data["e_alpha"] * data["alpha_tz"][:, jnp.newaxis]
)
return data


@register_compute_fun(
name="(e_zeta|r,a)_a",
label="\\partial_{\\alpha} (\\mathbf{e}_{\\zeta} |_{\\rho, \\alpha})",
units="m",
units_long="meters",
description="Tangent vector along (collinear to) field line, derivative "
"wrt field line poloidal label",
dim=3,
params=[],
transforms={},
profiles=[],
coordinates="rtz",
data=["(e_zeta|r,a)_t", "alpha_t"],
)
def _e_zeta_ra_a(params, transforms, profiles, data, **kwargs):
data["(e_zeta|r,a)_a"] = data["(e_zeta|r,a)_t"] / data["alpha_t"][:, jnp.newaxis]
return data


@register_compute_fun(
name="(e_zeta|r,a)_z",
label="\\partial_{\\zeta} (\\mathbf{e}_{\\zeta} |_{\\rho, \\alpha})",
units="m",
units_long="meters",
description="Tangent vector along (collinear to) field line, "
"derivative wrt DESC toroidal angle",
dim=3,
params=[],
transforms={},
profiles=[],
coordinates="rtz",
data=["e_zeta_z", "e_alpha", "alpha_z", "e_alpha_z", "alpha_zz"],
)
def _e_zeta_ra_z(params, transforms, profiles, data, **kwargs):
data["(e_zeta|r,a)_z"] = data["e_zeta_z"] - (
data["e_alpha_z"] * data["alpha_z"][:, jnp.newaxis]
+ data["e_alpha"] * data["alpha_zz"][:, jnp.newaxis]
)
return data


@register_compute_fun(
name="(e_zeta|r,a)_z|r,a",
label="\\partial_{\\zeta} (\\mathbf{e}_{\\zeta} |_{\\rho, \\alpha}) "
"|_{\\rho, \\alpha}",
units="m",
units_long="meters",
description="Curvature vector along field line",
dim=3,
params=[],
transforms={},
profiles=[],
coordinates="rtz",
data=["(e_zeta|r,a)_z", "(e_zeta|r,a)_a", "alpha_z"],
)
def _e_zeta_z_ra(params, transforms, profiles, data, **kwargs):
data["(e_zeta|r,a)_z|r,a"] = (
data["(e_zeta|r,a)_z"]
- data["(e_zeta|r,a)_a"] * data["alpha_z"][:, jnp.newaxis]
)
return data


@register_compute_fun(
name="|e_zeta|r,a|", # Often written as |dℓ/dζ| = |B/(B⋅∇ζ)|.
label="|\\mathbf{e}_{\\zeta} |_{\\rho, \\alpha}|"
" = \\frac{|\\mathbf{B}|}{|\\mathbf{B} \\cdot \\nabla \\zeta|}",
units="m",
units_long="meters",
description="Differential length along field line",
dim=1,
params=[],
transforms={},
profiles=[],
coordinates="rtz",
data=["e_zeta|r,a"],
)
def _d_ell_d_zeta(params, transforms, profiles, data, **kwargs):
data["|e_zeta|r,a|"] = jnp.linalg.norm(data["e_zeta|r,a"], axis=-1)
return data


@register_compute_fun(
name="|e_zeta|r,a|_z|r,a",
label="\\partial_{\\zeta} |\\mathbf{e}_{\\zeta} |_{\\rho, \\alpha}| "
"|_{\\rho, \\alpha}",
units="m",
units_long="meters",
description="Differential length along field line, derivative along field line",
dim=1,
params=[],
transforms={},
profiles=[],
coordinates="rtz",
data=["|e_zeta|r,a|", "(e_zeta|r,a)_z|r,a", "e_zeta|r,a"],
)
def _d_ell_d_zeta_z(params, transforms, profiles, data, **kwargs):
data["|e_zeta|r,a|_z|r,a"] = (
dot(data["(e_zeta|r,a)_z|r,a"], data["e_zeta|r,a"]) / data["|e_zeta|r,a|"]
)
return data
Loading

0 comments on commit 7975cc3

Please sign in to comment.