|
| 1 | +# Copyright 2024 DeepMind Technologies Limited |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Unit tests for torax.transport_model.tglf_based_transport_model.""" |
| 16 | +from absl.testing import absltest |
| 17 | +from absl.testing import parameterized |
| 18 | +import chex |
| 19 | +import jax.numpy as jnp |
| 20 | +from torax import core_profile_setters |
| 21 | +from torax import state |
| 22 | +from torax.config import runtime_params as general_runtime_params |
| 23 | +from torax.config import runtime_params_slice |
| 24 | +from torax.geometry import circular_geometry |
| 25 | +from torax.geometry import geometry |
| 26 | +from torax.pedestal_model import pedestal_model as pedestal_model_lib |
| 27 | +from torax.pedestal_model import set_tped_nped |
| 28 | +from torax.sources import source_models as source_models_lib |
| 29 | +from torax.transport_model import tglf_based_transport_model |
| 30 | +from torax.transport_model import quasilinear_transport_model |
| 31 | +from torax.transport_model import runtime_params as runtime_params_lib |
| 32 | + |
| 33 | + |
| 34 | +def _get_model_inputs(transport: tglf_based_transport_model.RuntimeParams): |
| 35 | + """Returns the model inputs for testing.""" |
| 36 | + runtime_params = general_runtime_params.GeneralRuntimeParams() |
| 37 | + geo = circular_geometry.build_circular_geometry() |
| 38 | + source_models_builder = source_models_lib.SourceModelsBuilder() |
| 39 | + source_models = source_models_builder() |
| 40 | + pedestal_model_builder = ( |
| 41 | + set_tped_nped.SetTemperatureDensityPedestalModelBuilder() |
| 42 | + ) |
| 43 | + dynamic_runtime_params_slice = ( |
| 44 | + runtime_params_slice.DynamicRuntimeParamsSliceProvider( |
| 45 | + runtime_params=runtime_params, |
| 46 | + transport=transport, |
| 47 | + sources=source_models_builder.runtime_params, |
| 48 | + pedestal=pedestal_model_builder.runtime_params, |
| 49 | + torax_mesh=geo.torax_mesh, |
| 50 | + )( |
| 51 | + t=runtime_params.numerics.t_initial, |
| 52 | + ) |
| 53 | + ) |
| 54 | + static_slice = runtime_params_slice.build_static_runtime_params_slice( |
| 55 | + runtime_params=runtime_params, |
| 56 | + source_runtime_params=source_models_builder.runtime_params, |
| 57 | + torax_mesh=geo.torax_mesh, |
| 58 | + ) |
| 59 | + core_profiles = core_profile_setters.initial_core_profiles( |
| 60 | + dynamic_runtime_params_slice=dynamic_runtime_params_slice, |
| 61 | + static_runtime_params_slice=static_slice, |
| 62 | + geo=geo, |
| 63 | + source_models=source_models, |
| 64 | + ) |
| 65 | + return dynamic_runtime_params_slice, geo, core_profiles |
| 66 | + |
| 67 | + |
| 68 | +class TGLFBasedTransportModelTest(parameterized.TestCase): |
| 69 | + """Unit tests for the `torax.transport_model.tglf_based_transport_model` module.""" |
| 70 | + |
| 71 | + def test_tglf_based_transport_model_output_shapes(self): |
| 72 | + """Tests that the core transport output has the right shapes.""" |
| 73 | + transport = tglf_based_transport_model.RuntimeParams( |
| 74 | + **runtime_params_lib.RuntimeParams() |
| 75 | + ) |
| 76 | + transport_model = FakeTGLFBasedTransportModel() |
| 77 | + dynamic_runtime_params_slice, geo, core_profiles = _get_model_inputs( |
| 78 | + transport |
| 79 | + ) |
| 80 | + pedestal_model = set_tped_nped.SetTemperatureDensityPedestalModel() |
| 81 | + pedestal_model_outputs = pedestal_model( |
| 82 | + dynamic_runtime_params_slice, geo, core_profiles |
| 83 | + ) |
| 84 | + |
| 85 | + core_transport = transport_model( |
| 86 | + dynamic_runtime_params_slice, geo, core_profiles, pedestal_model_outputs |
| 87 | + ) |
| 88 | + expected_shape = geo.rho_face_norm.shape |
| 89 | + self.assertEqual(core_transport.chi_face_ion.shape, expected_shape) |
| 90 | + self.assertEqual(core_transport.chi_face_el.shape, expected_shape) |
| 91 | + self.assertEqual(core_transport.d_face_el.shape, expected_shape) |
| 92 | + self.assertEqual(core_transport.v_face_el.shape, expected_shape) |
| 93 | + |
| 94 | + def test_tglf_based_transport_model_prepare_tglf_inputs_shapes(self): |
| 95 | + """Tests that the tglf inputs have the expected shapes.""" |
| 96 | + transport = tglf_based_transport_model.RuntimeParams( |
| 97 | + **runtime_params_lib.RuntimeParams() |
| 98 | + ) |
| 99 | + dynamic_runtime_params_slice, geo, core_profiles = _get_model_inputs( |
| 100 | + transport |
| 101 | + ) |
| 102 | + transport_model = FakeTGLFBasedTransportModel() |
| 103 | + tglf_inputs = transport_model._prepare_tglf_inputs( |
| 104 | + Zeff_face=dynamic_runtime_params_slice.plasma_composition.Zeff_face, |
| 105 | + q_correction_factor=dynamic_runtime_params_slice.numerics.q_correction_factor, |
| 106 | + geo=geo, |
| 107 | + core_profiles=core_profiles, |
| 108 | + ) |
| 109 | + |
| 110 | + # Inputs that are 1D |
| 111 | + vector_keys = [ |
| 112 | + 'chiGB', |
| 113 | + 'lref_over_lti', |
| 114 | + 'lref_over_lte', |
| 115 | + 'lref_over_lne', |
| 116 | + 'lref_over_lni0', |
| 117 | + 'lref_over_lni1', |
| 118 | + 'Ti_over_Te', |
| 119 | + 'drmaj', |
| 120 | + 'q', |
| 121 | + 's_hat', |
| 122 | + 'nu_ee', |
| 123 | + 'kappa', |
| 124 | + 'kappa_shear', |
| 125 | + 'delta', |
| 126 | + 'delta_shear', |
| 127 | + 'beta_e', |
| 128 | + 'Zeff', |
| 129 | + ] |
| 130 | + # Inputs that are 0D |
| 131 | + scalar_keys = ['Rmaj', 'Rmin'] |
| 132 | + |
| 133 | + expected_vector_length = geo.rho_face_norm.shape[0] |
| 134 | + for key in vector_keys: |
| 135 | + try: |
| 136 | + self.assertEqual( |
| 137 | + getattr(tglf_inputs, key).shape, (expected_vector_length,) |
| 138 | + ) |
| 139 | + except Exception as e: |
| 140 | + print(key, getattr(tglf_inputs, key)) |
| 141 | + raise e |
| 142 | + for key in scalar_keys: |
| 143 | + self.assertEqual(getattr(tglf_inputs, key).shape, ()) |
| 144 | + |
| 145 | + |
| 146 | +class FakeTGLFBasedTransportModel( |
| 147 | + tglf_based_transport_model.TGLFBasedTransportModel |
| 148 | +): |
| 149 | + """Fake TGLFBasedTransportModel for testing purposes.""" |
| 150 | + |
| 151 | + def __init__(self): |
| 152 | + super().__init__() |
| 153 | + self._frozen = True |
| 154 | + |
| 155 | + # pylint: disable=invalid-name |
| 156 | + def prepare_tglf_inputs( |
| 157 | + self, |
| 158 | + Zeff_face: chex.Array, |
| 159 | + q_correction_factor: chex.Numeric, |
| 160 | + geo: geometry.Geometry, |
| 161 | + core_profiles: state.CoreProfiles, |
| 162 | + ) -> tglf_based_transport_model.TGLFInputs: |
| 163 | + """Exposing prepare_tglf_inputs for testing.""" |
| 164 | + return self._prepare_tglf_inputs( |
| 165 | + Zeff_face=Zeff_face, |
| 166 | + q_correction_factor=q_correction_factor, |
| 167 | + geo=geo, |
| 168 | + core_profiles=core_profiles, |
| 169 | + ) |
| 170 | + |
| 171 | + # pylint: enable=invalid-name |
| 172 | + |
| 173 | + def _call_implementation( |
| 174 | + self, |
| 175 | + dynamic_runtime_params_slice: runtime_params_slice.DynamicRuntimeParamsSlice, |
| 176 | + geo: geometry.Geometry, |
| 177 | + core_profiles: state.CoreProfiles, |
| 178 | + pedestal_model_output: pedestal_model_lib.PedestalModelOutput, |
| 179 | + ) -> state.CoreTransport: |
| 180 | + tglf_inputs = self._prepare_tglf_inputs( |
| 181 | + Zeff_face=dynamic_runtime_params_slice.plasma_composition.Zeff_face, |
| 182 | + q_correction_factor=dynamic_runtime_params_slice.numerics.q_correction_factor, |
| 183 | + geo=geo, |
| 184 | + core_profiles=core_profiles, |
| 185 | + ) |
| 186 | + |
| 187 | + transport = dynamic_runtime_params_slice.transport |
| 188 | + # Assert required for pytype. |
| 189 | + assert isinstance( |
| 190 | + transport, |
| 191 | + tglf_based_transport_model.DynamicRuntimeParams, |
| 192 | + ) |
| 193 | + |
| 194 | + return self._make_core_transport( |
| 195 | + qi=jnp.ones(geo.rho_face_norm.shape) * 0.4, |
| 196 | + qe=jnp.ones(geo.rho_face_norm.shape) * 0.5, |
| 197 | + pfe=jnp.ones(geo.rho_face_norm.shape) * 1.6, |
| 198 | + quasilinear_inputs=tglf_inputs, |
| 199 | + transport=transport, |
| 200 | + geo=geo, |
| 201 | + core_profiles=core_profiles, |
| 202 | + gradient_reference_length=geo.Rmaj, # TODO |
| 203 | + gyrobohm_flux_reference_length=geo.Rmin, # TODO |
| 204 | + ) |
| 205 | + |
| 206 | + |
| 207 | +if __name__ == '__main__': |
| 208 | + absltest.main() |
0 commit comments