Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Critical gradient #1318

Draft
wants to merge 26 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b3c50ad
Creating critical_gradient optimization
kfrybes Oct 7, 2024
aeb2ff8
Changes to turbulence optimization, inluding jnp instead of np
kfrybes Oct 9, 2024
0069348
Jax adaptation and zonal flow notebook
kfrybes Oct 14, 2024
7a62626
Making the effective radius computation jit compatible
kfrybes Oct 22, 2024
5b2de89
Merge branch 'master' into kf/critical_gradient
kfrybes Oct 23, 2024
afc54a3
fixing change I made on objectives/_stability for testing
kfrybes Oct 23, 2024
5eeec7b
updating the effective radius objective to return array of effective…
kfrybes Oct 23, 2024
5000336
Cleaning up the critical_gradient2 notebook to be understandable
kfrybes Oct 23, 2024
08680ab
updating R_eff compute function to take n_wells as kwargs
kfrybes Oct 23, 2024
df9260e
Remove flake8 errors, code formatting
kfrybes Oct 24, 2024
56fdce4
add mask sorting to extract_Kd_wells function
kfrybes Oct 25, 2024
32019fc
fix print name L_par
kfrybes Oct 25, 2024
2ad65de
add absolute value to number of toroidal turns + update notebook
kfrybes Oct 29, 2024
aa6a6c5
Merge branch 'master' into kf/critical_gradient
daniel-dudt Oct 30, 2024
99a6ab4
remove NFP from get_rtz_grid and update notebook
kfrybes Oct 31, 2024
f6a1068
update notebook for testing different boundary modes
kfrybes Nov 1, 2024
b1f772e
move old files to local and update optimization notebook
kfrybes Nov 8, 2024
eb8d2a1
Merge branch 'master' into kf/critical_gradient
dpanici Nov 20, 2024
719e318
Update critical gradient notebook to make it easy to run and undrstand
kfrybes Nov 20, 2024
58ac2f6
Add possibility of accessing length of good curvature regions,
kfrybes Dec 6, 2024
884d659
Add curvature parameter to L_par objective to be able to target both …
kfrybes Dec 9, 2024
d30065a
bug fix
kfrybes Dec 9, 2024
7edb28b
Use arc length coordinate for calculating R_eff
kfrybes Dec 17, 2024
96b63c6
Adapt code to target also good curvature regions
kfrybes Dec 17, 2024
96a42bc
Add xi compute quantity for flux surface spacing turbulence optimization
kfrybes Dec 17, 2024
6e4cf29
Add GradS objective for optimizing turbulence
kfrybes Dec 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions desc/compute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
_profiles,
_stability,
_surface,
_turbulence,
)
from .data_index import all_kwargs, allowed_kwargs, data_index
from .geom_utils import rpz2xyz, rpz2xyz_vec, xyz2rpz, xyz2rpz_vec
Expand Down
127 changes: 127 additions & 0 deletions desc/compute/_turbulence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"""Compute functions for turbulent transport.

Notes
-----
Some quantities require additional work to compute at the magnetic axis.
A Python lambda function is used to lazily compute the magnetic axis limits
of these quantities. These lambda functions are evaluated only when the
computational grid has a node on the magnetic axis to avoid potentially
expensive computations.
"""

from ..backend import jnp, trapezoid
from ..integrals.critical_gradient import (
extract_Kd_wells,
extract_Kd_wells_and_peaks,
fit_Kd_wells,
)
from ..utils import cumtrapz
from .data_index import register_compute_fun

_doc = {
"n_wells": (
"int : Number of wells to detect for each pitch and field line. "
"Default is 10 wells,"
),
"curvature": (
"str : good or bad curvature regions for the calculation of R_eff and L_par "
"Default is bad curvature regions"
),
}


@register_compute_fun(
name="Kd",
label="\\mathrm{cvdrift} = a^2\\nabla\\alpha\\cdot\\mathbf{b}\\times\\kappa",
units="",
units_long="",
description="Dimensionless drift curvature",
dim=1,
params=[],
transforms={},
profiles=[],
coordinates="rtz",
data=["cvdrift", "|B|", "a"],
)
def _Kd(params, transforms, profiles, data, **kwargs):
# Exact definition of the dimenstionless drift curvature can be found
# in https://journals.aps.org/prresearch/pdf/10.1103/PhysRevResearch.4.L032028
data["Kd"] = data["a"] ** 2 * jnp.multiply(data["|B|"], data["cvdrift"])
return data


@register_compute_fun(
name="R_eff",
label="R_eff",
units="",
units_long="",
description="Effective radius of the drift curvature along the field line",
dim=1,
params=[],
transforms={"grid": []},
profiles=[],
coordinates="rtz",
data=["Kd", "|e_zeta|r,a|"],
**_doc,
)
def _R_eff(params, transforms, profiles, data, **kwargs):
# Exact definition of the effective radius of curvature can be found
# in https://journals.aps.org/prresearch/pdf/10.1103/PhysRevResearch.4.L032028
grid = transforms["grid"].source_grid
n_wells = kwargs.get("n_wells", 5)
Kd_wells, _, masks = extract_Kd_wells(data["Kd"], n_wells=n_wells)
l = cumtrapz(data["|e_zeta|r,a|"], x=grid.nodes[:, 2], initial=0)
_, _, R_eff = fit_Kd_wells(l, Kd_wells, masks, n_wells=n_wells)
data["R_eff"] = R_eff
return data


@register_compute_fun(
name="L_par",
label="L_par",
units="",
units_long="",
description="Width of Kd wells along the field line",
dim=1,
params=[],
transforms={"grid": []},
profiles=[],
coordinates="rtz",
data=["Kd", "|e_zeta|r,a|"],
**_doc,
)
def _L_par(params, transforms, profiles, data, **kwargs):
# Parallel connection length defined as width of Kd wells
grid = transforms["grid"].source_grid
n_wells = kwargs.get("n_wells", 5)
curvature = kwargs.get("curvature", "bad")
_, masks, _ = extract_Kd_wells_and_peaks(data["Kd"], n_wells=n_wells)
if curvature == "good":
L_par = trapezoid(data["|e_zeta|r,a|"] * masks["masks_peaks"], grid.nodes[:, 2])
else:
L_par = trapezoid(data["|e_zeta|r,a|"] * masks["masks_wells"], grid.nodes[:, 2])
data["L_par"] = L_par
return data


@register_compute_fun(
name="xi",
label="ξ",
units="",
units_long="",
description="Target for spacing of flux surfaces",
dim=1,
params=[],
transforms={"grid": []},
profiles=[],
coordinates="rtz",
data=["a", "|grad(rho)|", "Kd"],
**_doc,
)
def _xi(params, transforms, profiles, data, **kwargs):
# Parallel connection length defined as width of Kd wells
grid = transforms["grid"]
mask = jnp.where(data["Kd"] < 0, 1.0, 0.0)
xi = (2 * data["a"] * mask * data["|grad(rho)|"] * grid.nodes[:, 0][0]) ** 2
data["xi"] = xi
return data
Loading
Loading