|
| 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 | +from absl.testing import absltest |
| 16 | +from absl.testing import parameterized |
| 17 | +from torax.config import build_runtime_params |
| 18 | +from torax.config import runtime_params as general_runtime_params |
| 19 | +from torax.geometry import pydantic_model as geometry_pydantic_model |
| 20 | +from torax.mhd import pydantic_model as mhd_pydantic_model |
| 21 | +from torax.mhd import runtime_params as mhd_runtime_params |
| 22 | +from torax.mhd.sawtooth import pydantic_model as sawtooth_pydantic_model |
| 23 | +from torax.mhd.sawtooth import runtime_params as sawtooth_runtime_params |
| 24 | +from torax.mhd.sawtooth import sawtooth_model |
| 25 | +from torax.stepper import pydantic_model as stepper_pydantic_model |
| 26 | +from torax.tests.test_lib import default_sources |
| 27 | +from torax.torax_pydantic import model_config |
| 28 | + |
| 29 | + |
| 30 | +class MHDPydanticModelTest(parameterized.TestCase): |
| 31 | + """Tests for the MHD Pydantic model and dynamic params construction.""" |
| 32 | + |
| 33 | + def setUp(self): |
| 34 | + super().setUp() |
| 35 | + self.geo = geometry_pydantic_model.CircularConfig().build_geometry() |
| 36 | + self.runtime_params = general_runtime_params.GeneralRuntimeParams() |
| 37 | + self.sources = default_sources.get_default_sources() |
| 38 | + self.stepper = stepper_pydantic_model.Stepper() |
| 39 | + |
| 40 | + def test_no_mhd_config(self): |
| 41 | + """Tests the case where the 'mhd' key is entirely absent.""" |
| 42 | + config_dict = { |
| 43 | + 'runtime_params': {}, |
| 44 | + 'geometry': {'geometry_type': 'circular'}, |
| 45 | + 'pedestal': {}, |
| 46 | + 'sources': {}, |
| 47 | + 'stepper': {}, |
| 48 | + 'time_step_calculator': {}, |
| 49 | + 'transport': {}, |
| 50 | + } |
| 51 | + torax_config = model_config.ToraxConfig.from_dict(config_dict) |
| 52 | + |
| 53 | + self.assertIs(torax_config.mhd, None) |
| 54 | + provider = build_runtime_params.DynamicRuntimeParamsSliceProvider( |
| 55 | + runtime_params=self.runtime_params, |
| 56 | + sources=self.sources, |
| 57 | + stepper=self.stepper, |
| 58 | + torax_mesh=self.geo.torax_mesh, |
| 59 | + mhd=torax_config.mhd, |
| 60 | + ) |
| 61 | + dynamic_slice = provider(t=0.0) |
| 62 | + self.assertIsInstance( |
| 63 | + dynamic_slice.mhd, mhd_runtime_params.DynamicMHDParams |
| 64 | + ) |
| 65 | + self.assertIs(dynamic_slice.mhd.sawtooth, None) |
| 66 | + |
| 67 | + def test_empty_mhd_config(self): |
| 68 | + """Tests the case where 'mhd' key exists but is an empty dict.""" |
| 69 | + config_dict = { |
| 70 | + 'runtime_params': {}, |
| 71 | + 'geometry': {'geometry_type': 'circular'}, |
| 72 | + 'pedestal': {}, |
| 73 | + 'sources': {}, |
| 74 | + 'stepper': {}, |
| 75 | + 'time_step_calculator': {}, |
| 76 | + 'transport': {}, |
| 77 | + 'mhd': {}, |
| 78 | + } |
| 79 | + torax_config = model_config.ToraxConfig.from_dict(config_dict) |
| 80 | + |
| 81 | + self.assertIsInstance(torax_config.mhd, mhd_pydantic_model.MHD) |
| 82 | + assert isinstance(torax_config.mhd, mhd_pydantic_model.MHD) |
| 83 | + mhd_models = torax_config.mhd.build_mhd_models() |
| 84 | + self.assertIs(mhd_models.sawtooth, None) |
| 85 | + provider = build_runtime_params.DynamicRuntimeParamsSliceProvider( |
| 86 | + runtime_params=self.runtime_params, |
| 87 | + sources=self.sources, |
| 88 | + stepper=self.stepper, |
| 89 | + torax_mesh=self.geo.torax_mesh, |
| 90 | + mhd=torax_config.mhd, |
| 91 | + ) |
| 92 | + dynamic_slice = provider(t=0.0) |
| 93 | + self.assertIsInstance( |
| 94 | + dynamic_slice.mhd, mhd_runtime_params.DynamicMHDParams |
| 95 | + ) |
| 96 | + self.assertIs(dynamic_slice.mhd.sawtooth, None) |
| 97 | + |
| 98 | + def test_mhd_config_with_sawtooth(self): |
| 99 | + """Tests the case with a valid sawtooth configuration.""" |
| 100 | + config_dict = { |
| 101 | + 'runtime_params': {}, |
| 102 | + 'geometry': {'geometry_type': 'circular'}, |
| 103 | + 'pedestal': {}, |
| 104 | + 'sources': {}, |
| 105 | + 'stepper': {}, |
| 106 | + 'time_step_calculator': {}, |
| 107 | + 'transport': {}, |
| 108 | + 'mhd': { |
| 109 | + 'sawtooth': { |
| 110 | + 'trigger_model_config': {'trigger_model_type': 'simple'}, |
| 111 | + 'redistribution_model_config': { |
| 112 | + 'redistribution_model_type': 'simple' |
| 113 | + }, |
| 114 | + 'minimum_radius': 0.06, |
| 115 | + } |
| 116 | + }, |
| 117 | + } |
| 118 | + torax_config = model_config.ToraxConfig.from_dict(config_dict) |
| 119 | + |
| 120 | + self.assertIsInstance(torax_config.mhd, mhd_pydantic_model.MHD) |
| 121 | + assert torax_config.mhd is not None |
| 122 | + self.assertIsInstance( |
| 123 | + torax_config.mhd.sawtooth, sawtooth_pydantic_model.SawtoothConfig |
| 124 | + ) |
| 125 | + |
| 126 | + mhd_models = torax_config.mhd.build_mhd_models() |
| 127 | + self.assertIn('sawtooth', mhd_models) |
| 128 | + self.assertIsInstance(mhd_models['sawtooth'], sawtooth_model.SawtoothModel) |
| 129 | + |
| 130 | + provider = build_runtime_params.DynamicRuntimeParamsSliceProvider( |
| 131 | + runtime_params=self.runtime_params, |
| 132 | + sources=self.sources, |
| 133 | + stepper=self.stepper, |
| 134 | + torax_mesh=self.geo.torax_mesh, |
| 135 | + mhd=torax_config.mhd, |
| 136 | + ) |
| 137 | + dynamic_slice = provider(t=0.0) |
| 138 | + self.assertIn('sawtooth', dynamic_slice.mhd) |
| 139 | + sawtooth_dynamic_params = dynamic_slice.mhd.sawtooth |
| 140 | + self.assertIsInstance( |
| 141 | + sawtooth_dynamic_params, sawtooth_runtime_params.DynamicRuntimeParams |
| 142 | + ) |
| 143 | + self.assertEqual(sawtooth_dynamic_params.minimum_radius, 0.06) |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == '__main__': |
| 147 | + absltest.main() |
0 commit comments