Skip to content

Commit

Permalink
Merge pull request #658 from google-deepmind:add-ne-greenwald-fraction
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 726452906
  • Loading branch information
Torax team committed Feb 13, 2025
2 parents 6890048 + 0ca5aa2 commit af447f2
Show file tree
Hide file tree
Showing 57 changed files with 70 additions and 0 deletions.
30 changes: 30 additions & 0 deletions torax/post_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,32 @@ def _calculate_q95(
return q95


def _calculate_greenwald_fraction_from_ne_vol_avg(
ne_volume_avg: array_typing.ScalarFloat,
core_profiles: state.CoreProfiles,
geo: geometry.Geometry,
) -> array_typing.ScalarFloat:
"""Calculates the Greenwald fraction from the volume-average electron density.
Args:
ne_volume_avg: Volume-averaged electron density [nref m^-3]
core_profiles: CoreProfiles object containing information on currents
and densities.
geo: Geometry object
Returns:
fgw_ne_vol_avg: Volume-averaged electron density Greenwald fraction
"""
# gw_limit is in units of 10^20 m^-3 when Ip is in MA and rmid is in m.
gw_limit = (
core_profiles.currents.Ip_profile_face[-1]
* 1e-6
/ (jnp.pi * geo.Rmin ** 2)
)
fgw_ne_vol_avg = ne_volume_avg * core_profiles.nref / (gw_limit * 1e20)
return fgw_ne_vol_avg


@jax_utils.jit
def make_outputs(
sim_state: state.ToraxSimState,
Expand Down Expand Up @@ -465,6 +491,9 @@ def make_outputs(
/ geo.volume[-1]
)

fgw_ne_volume_avg = _calculate_greenwald_fraction_from_ne_vol_avg(
ne_volume_avg, sim_state.core_profiles, geo
)
Wpol = physics.calc_Wpol(geo, sim_state.core_profiles.psi)
li3 = physics.calc_li3(
geo.Rmaj, Wpol, sim_state.core_profiles.currents.Ip_profile_face[-1]
Expand Down Expand Up @@ -499,6 +528,7 @@ def make_outputs(
ti_volume_avg=ti_volume_avg,
ne_volume_avg=ne_volume_avg,
ni_volume_avg=ni_volume_avg,
fgw_ne_volume_avg=fgw_ne_volume_avg,
q95=q95,
Wpol=Wpol,
li3=li3,
Expand Down
4 changes: 4 additions & 0 deletions torax/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ class PostProcessedOutputs:
ti_volume_avg: Volume average ion temperature [keV]
ne_volume_avg: Volume average electron density [nref m^-3]
ni_volume_avg: Volume average main ion density [nref m^-3]
fgw_ne_volume_avg: Greenwald fraction from volume-averaged electron density
[dimensionless]
q95: q at 95% of the normalized poloidal flux
Wpol: Total magnetic energy [J]
li3: Normalized plasma internal inductance, ITER convention [dimensionless]
Expand Down Expand Up @@ -385,6 +387,7 @@ class PostProcessedOutputs:
ti_volume_avg: array_typing.ScalarFloat
ne_volume_avg: array_typing.ScalarFloat
ni_volume_avg: array_typing.ScalarFloat
fgw_ne_volume_avg: array_typing.ScalarFloat
q95: array_typing.ScalarFloat
Wpol: array_typing.ScalarFloat
li3: array_typing.ScalarFloat
Expand Down Expand Up @@ -443,6 +446,7 @@ def zeros(cls, geo: geometry.Geometry) -> PostProcessedOutputs:
ti_volume_avg=jnp.array(0.0),
ne_volume_avg=jnp.array(0.0),
ni_volume_avg=jnp.array(0.0),
fgw_ne_volume_avg=jnp.array(0.0),
q95=jnp.array(0.0),
Wpol=jnp.array(0.0),
li3=jnp.array(0.0),
Expand Down
36 changes: 36 additions & 0 deletions torax/tests/post_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Tests for post_processing.py."""

import dataclasses
from unittest import mock
from absl.testing import absltest
from absl.testing import parameterized
import jax
Expand All @@ -29,6 +30,7 @@
from torax.config import runtime_params_slice
from torax.fvm import cell_variable
from torax.geometry import circular_geometry
from torax.geometry import geometry
from torax.geometry import geometry_provider
from torax.sources import source_profiles as source_profiles_lib
from torax.tests.test_lib import default_sources
Expand Down Expand Up @@ -184,6 +186,40 @@ def test_compute_stored_thermal_energy(self):
np.testing.assert_allclose(wth_ion, 1.5 * p_ion[0] * volume)
np.testing.assert_allclose(wth_tot, 1.5 * p_tot[0] * volume)

def test_calculate_greenwald_fraction_from_ne_vol_avg(self):
"""Test that Greenwald fraction is calculated correctly."""
ne_volume_avg = 1.0

core_profiles = mock.create_autospec(
state.CoreProfiles,
instance=True,
nref=1e20,
currents=mock.create_autospec(
state.Currents,
instance=True,
Ip_profile_face=np.array([0, np.pi * 1e6]),
),
)
geo = mock.create_autospec(
geometry.Geometry,
instance=True,
Rmin=1.0,
)

# pylint: disable=protected-access
fgw_ne_volume_avg_calculated = (
post_processing._calculate_greenwald_fraction_from_ne_vol_avg(
ne_volume_avg, core_profiles, geo
)
)

fgw_ne_volume_avg_expected = 1.0

np.testing.assert_allclose(
fgw_ne_volume_avg_calculated, fgw_ne_volume_avg_expected
)
# pylint: enable=protected-access

def test_calculate_integrated_sources(self):
"""Checks integrated quantities match expectations."""
# pylint: disable=protected-access
Expand Down
Binary file modified torax/tests/test_data/test_all_transport_crank_nicolson.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_all_transport_fusion_qlknn.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_bohmgyrobohm_all.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_bootstrap.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_bremsstrahlung.nc
Binary file not shown.
Binary file not shown.
Binary file modified torax/tests/test_data/test_cgmheat.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_changing_config_after.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_changing_config_before.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_chease.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_eqdsk.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_explicit.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_fixed_dt.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_fusion_power.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_implicit.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_implicit_short_optimizer.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_iterbaseline_mockup.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_iterhybrid_mockup.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_iterhybrid_newton.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_iterhybrid_predictor_corrector.nc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified torax/tests/test_data/test_iterhybrid_predictor_corrector_zi2.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_iterhybrid_rampup.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_ne_qlknn_deff_veff.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_ne_qlknn_defromchie.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_ohmic_power.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_particle_sources_cgm.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_particle_sources_constant.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_pc_method_ne.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_pedestal.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_prescribed_generic_current_source.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_prescribed_timedependent_ne.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_psi_and_heat.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_psi_heat_dens.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_psichease_ip_chease.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_psichease_ip_parameters.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_psichease_prescribed_johm.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_psichease_prescribed_jtot.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_psiequation.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_qei.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_qei_chease_highdens.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_qlknnheat.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_semiimplicit_convection.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_time_dependent_circular_geo.nc
Binary file not shown.
Binary file modified torax/tests/test_data/test_timedependence.nc
Binary file not shown.

0 comments on commit af447f2

Please sign in to comment.