|
| 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 | +import copy |
| 15 | +import importlib |
| 16 | +from typing import Literal |
| 17 | + |
| 18 | +from absl.testing import parameterized |
| 19 | +import chex |
| 20 | +from torax import array_typing |
| 21 | +from torax import state |
| 22 | +from torax.config import runtime_params_slice |
| 23 | +from torax.geometry import geometry |
| 24 | +from torax.sources import base as source_base_pydantic_model |
| 25 | +from torax.sources import gas_puff_source as gas_puff_source_lib |
| 26 | +from torax.sources import runtime_params |
| 27 | +from torax.sources import source as source_lib |
| 28 | +from torax.sources import source_profiles |
| 29 | +from torax.torax_pydantic import model_config |
| 30 | +from torax.torax_pydantic import register_config |
| 31 | +from torax.torax_pydantic import torax_pydantic |
| 32 | + |
| 33 | + |
| 34 | +@chex.dataclass(frozen=True) |
| 35 | +class DynamicRuntimeParams(runtime_params.DynamicRuntimeParams): |
| 36 | + a: array_typing.ScalarFloat |
| 37 | + b: bool |
| 38 | + |
| 39 | + |
| 40 | +def double_gas_puff_source( |
| 41 | + unused_static_runtime_params_slice: runtime_params_slice.StaticRuntimeParamsSlice, |
| 42 | + dynamic_runtime_params_slice: runtime_params_slice.DynamicRuntimeParamsSlice, |
| 43 | + geo: geometry.Geometry, |
| 44 | + source_name: str, |
| 45 | + unused_state: state.CoreProfiles, |
| 46 | + unused_calculated_source_profiles: source_profiles.SourceProfiles | None, |
| 47 | +) -> tuple[chex.Array, ...]: |
| 48 | + """Calculates external source term for n from puffs.""" |
| 49 | + output = gas_puff_source_lib.calc_puff_source( |
| 50 | + unused_static_runtime_params_slice, |
| 51 | + dynamic_runtime_params_slice, |
| 52 | + geo, |
| 53 | + source_name, |
| 54 | + unused_state, |
| 55 | + unused_calculated_source_profiles, |
| 56 | + ) |
| 57 | + return 2 * output |
| 58 | + |
| 59 | + |
| 60 | +class NewGasPuffSourceModelConfig(source_base_pydantic_model.SourceModelBase): |
| 61 | + """New source model config.""" |
| 62 | + model_function_name: Literal['test_model_function'] = 'test_model_function' |
| 63 | + a: torax_pydantic.TimeVaryingScalar = torax_pydantic.ValidatedDefault(1.0) |
| 64 | + b: bool = False |
| 65 | + |
| 66 | + @property |
| 67 | + def model_func(self) -> source_lib.SourceProfileFunction: |
| 68 | + return double_gas_puff_source |
| 69 | + |
| 70 | + def build_source(self) -> source_lib.Source: |
| 71 | + return gas_puff_source_lib.GasPuffSource(model_func=self.model_func) |
| 72 | + |
| 73 | + def build_dynamic_params( |
| 74 | + self, |
| 75 | + t: chex.Numeric, |
| 76 | + ) -> DynamicRuntimeParams: |
| 77 | + return DynamicRuntimeParams( |
| 78 | + a=self.a.get_value(t), |
| 79 | + b=self.b, |
| 80 | + prescribed_values=self.prescribed_values.get_value(t), |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +class DuplicateGasPuffSourceModelConfig( |
| 85 | + source_base_pydantic_model.SourceModelBase |
| 86 | +): |
| 87 | + # Name that is already registered. |
| 88 | + model_function_name: Literal['calc_puff_source'] = 'calc_puff_source' |
| 89 | + a: torax_pydantic.TimeVaryingScalar = torax_pydantic.ValidatedDefault(1.0) |
| 90 | + b: bool = False |
| 91 | + |
| 92 | + @property |
| 93 | + def model_func(self) -> source_lib.SourceProfileFunction: |
| 94 | + return double_gas_puff_source |
| 95 | + |
| 96 | + def build_source(self) -> source_lib.Source: |
| 97 | + return gas_puff_source_lib.GasPuffSource(model_func=self.model_func) |
| 98 | + |
| 99 | + def build_dynamic_params( |
| 100 | + self, |
| 101 | + t: chex.Numeric, |
| 102 | + ) -> DynamicRuntimeParams: |
| 103 | + return DynamicRuntimeParams( |
| 104 | + a=self.a.get_value(t), |
| 105 | + b=self.b, |
| 106 | + prescribed_values=self.prescribed_values.get_value(t), |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +class RegisterConfigTest(parameterized.TestCase): |
| 111 | + |
| 112 | + def test_register_source_model_config(self): |
| 113 | + config_name = 'test_iterhybrid_rampup' |
| 114 | + test_config_path = '.tests.test_data.' + config_name |
| 115 | + config_module = importlib.import_module(test_config_path, 'torax') |
| 116 | + config = copy.deepcopy(config_module.CONFIG) |
| 117 | + # Register the new source model config against the gas puff source. |
| 118 | + register_config.register_source_model_config( |
| 119 | + NewGasPuffSourceModelConfig, 'gas_puff_source' |
| 120 | + ) |
| 121 | + |
| 122 | + # Load the original config and check the gas puff source is expected type. |
| 123 | + config_pydantic = model_config.ToraxConfig.from_dict(config) |
| 124 | + gas_puff_source_config = config_pydantic.sources.gas_puff_source |
| 125 | + self.assertIsInstance( |
| 126 | + gas_puff_source_config, gas_puff_source_lib.GasPuffSourceConfig |
| 127 | + ) |
| 128 | + gas_puff_source = gas_puff_source_config.build_source() |
| 129 | + self.assertIsInstance(gas_puff_source, gas_puff_source_lib.GasPuffSource) |
| 130 | + dynamic_params = gas_puff_source_config.build_dynamic_params(t=0.0) |
| 131 | + self.assertIsInstance( |
| 132 | + dynamic_params, gas_puff_source_lib.DynamicGasPuffRuntimeParams |
| 133 | + ) |
| 134 | + |
| 135 | + # Now modify the original config to use the new config. |
| 136 | + del config['sources']['gas_puff_source'] |
| 137 | + config['sources']['gas_puff_source'] = { |
| 138 | + 'model_function_name': 'test_model_function', # new registered name. |
| 139 | + 'a': 2.0, |
| 140 | + } |
| 141 | + config_pydantic = model_config.ToraxConfig.from_dict(config) |
| 142 | + # Check we build the correct config. |
| 143 | + new_gas_puff_config = config_pydantic.sources.gas_puff_source |
| 144 | + self.assertIsInstance(new_gas_puff_config, NewGasPuffSourceModelConfig) |
| 145 | + # Check the dynamic params are built correctly. |
| 146 | + new_dynamic_params = new_gas_puff_config.build_dynamic_params(t=0.0) |
| 147 | + self.assertIsInstance(new_dynamic_params, DynamicRuntimeParams) |
| 148 | + self.assertEqual(new_dynamic_params.a, 2.0) |
| 149 | + self.assertEqual(new_dynamic_params.b, False) |
| 150 | + |
| 151 | + def test_error_thrown_if_model_function_name_is_already_registered(self): |
| 152 | + with self.assertRaises(ValueError): |
| 153 | + register_config.register_source_model_config( |
| 154 | + DuplicateGasPuffSourceModelConfig, 'gas_puff_source' |
| 155 | + ) |
| 156 | + |
| 157 | + @parameterized.parameters('qei', 'j_bootstrap') |
| 158 | + def test_error_thrown_if_using_special_source(self, special_source): |
| 159 | + with self.assertRaisesRegex( |
| 160 | + ValueError, |
| 161 | + 'Cannot register a new source model config for the qei or j_bootstrap' |
| 162 | + ' sources.', |
| 163 | + ): |
| 164 | + register_config.register_source_model_config( |
| 165 | + NewGasPuffSourceModelConfig, special_source |
| 166 | + ) |
| 167 | + |
| 168 | + def test_error_thrown_if_source_not_supported(self): |
| 169 | + with self.assertRaisesRegex( |
| 170 | + ValueError, |
| 171 | + 'The source name foo_source is not supported.', |
| 172 | + ): |
| 173 | + register_config.register_source_model_config( |
| 174 | + NewGasPuffSourceModelConfig, 'foo_source' |
| 175 | + ) |
0 commit comments