|
15 | 15 |
|
16 | 16 | from absl.testing import absltest
|
17 | 17 | from absl.testing import parameterized
|
| 18 | +import numpy as np |
18 | 19 | from torax.sources import base
|
19 | 20 | from torax.sources import bootstrap_current_source
|
20 | 21 | from torax.sources import fusion_heat_source
|
|
26 | 27 | from torax.sources import source_models as source_models_lib
|
27 | 28 | from torax.sources.impurity_radiation_heat_sink import impurity_radiation_constant_fraction
|
28 | 29 | from torax.sources.impurity_radiation_heat_sink import impurity_radiation_mavrin_fit
|
| 30 | +from torax.torax_pydantic import torax_pydantic |
29 | 31 |
|
30 | 32 |
|
31 | 33 | class PydanticModelTest(parameterized.TestCase):
|
@@ -150,6 +152,86 @@ def test_empty_source_config_only_has_defaults_turned_off(self):
|
150 | 152 | )
|
151 | 153 | self.assertLen(sources.source_model_config, 3)
|
152 | 154 |
|
| 155 | + def test_adding_a_source_with_prescribed_values(self): |
| 156 | + """Tests that a source can be added with overriding defaults.""" |
| 157 | + sources = pydantic_model.Sources.from_dict({ |
| 158 | + 'generic_current_source': { |
| 159 | + 'mode': 'PRESCRIBED', |
| 160 | + 'prescribed_values': (( |
| 161 | + np.array([0.0, 1.0, 2.0, 3.0]), |
| 162 | + np.array([0., 0.5, 1.0]), |
| 163 | + np.full([4, 3], 42) |
| 164 | + ),), |
| 165 | + }, |
| 166 | + 'electron_cyclotron_source': { |
| 167 | + 'mode': 'PRESCRIBED', |
| 168 | + 'prescribed_values': ( |
| 169 | + 3., |
| 170 | + 4., |
| 171 | + ), |
| 172 | + } |
| 173 | + }) |
| 174 | + mesh = torax_pydantic.Grid1D(nx=4, dx=0.25) |
| 175 | + torax_pydantic.set_grid(sources, mesh) |
| 176 | + source = sources.source_model_config['generic_current_source'] |
| 177 | + self.assertLen(source.prescribed_values, 1) |
| 178 | + self.assertIsInstance( |
| 179 | + source.prescribed_values[0], torax_pydantic.TimeVaryingArray) |
| 180 | + source = sources.source_model_config['electron_cyclotron_source'] |
| 181 | + self.assertLen(source.prescribed_values, 2) |
| 182 | + self.assertIsInstance( |
| 183 | + source.prescribed_values[0], torax_pydantic.TimeVaryingArray) |
| 184 | + self.assertIsInstance( |
| 185 | + source.prescribed_values[1], torax_pydantic.TimeVaryingArray) |
| 186 | + value = source.prescribed_values[0].get_value(0.0) |
| 187 | + np.testing.assert_equal(value, 3.) |
| 188 | + value = source.prescribed_values[1].get_value(0.0) |
| 189 | + np.testing.assert_equal(value, 4.) |
| 190 | + |
| 191 | + def test_bremsstrahlung_and_mavrin_validator_with_bremsstrahlung_zero(self): |
| 192 | + valid_config = { |
| 193 | + 'bremsstrahlung_heat_sink': {'mode': 'ZERO'}, |
| 194 | + 'impurity_radiation_heat_sink': { |
| 195 | + 'mode': 'PRESCRIBED', |
| 196 | + 'model_function_name': 'impurity_radiation_mavrin_fit', |
| 197 | + }, |
| 198 | + } |
| 199 | + pydantic_model.Sources.from_dict(valid_config) |
| 200 | + |
| 201 | + def test_bremsstrahlung_and_mavrin_validator_with_mavrin_zero(self): |
| 202 | + valid_config = { |
| 203 | + 'bremsstrahlung_heat_sink': {'mode': 'PRESCRIBED'}, |
| 204 | + 'impurity_radiation_heat_sink': { |
| 205 | + 'mode': 'ZERO', |
| 206 | + 'model_function_name': 'impurity_radiation_mavrin_fit', |
| 207 | + }, |
| 208 | + } |
| 209 | + pydantic_model.Sources.from_dict(valid_config) |
| 210 | + |
| 211 | + def test_bremsstrahlung_and_mavrin_validator_with_constant_fraction(self): |
| 212 | + valid_config = { |
| 213 | + 'bremsstrahlung_heat_sink': {'mode': 'PRESCRIBED'}, |
| 214 | + 'impurity_radiation_heat_sink': { |
| 215 | + 'mode': 'PRESCRIBED', |
| 216 | + 'model_function_name': 'radially_constant_fraction_of_Pin', |
| 217 | + }, |
| 218 | + } |
| 219 | + pydantic_model.Sources.from_dict(valid_config) |
| 220 | + |
| 221 | + def test_bremsstrahlung_and_mavrin_validator_with_invalid_config(self): |
| 222 | + invalid_config = { |
| 223 | + 'bremsstrahlung_heat_sink': {'mode': 'PRESCRIBED'}, |
| 224 | + 'impurity_radiation_heat_sink': { |
| 225 | + 'mode': 'PRESCRIBED', |
| 226 | + 'model_function_name': 'impurity_radiation_mavrin_fit', |
| 227 | + }, |
| 228 | + } |
| 229 | + with self.assertRaisesRegex( |
| 230 | + ValueError, |
| 231 | + 'Both bremsstrahlung_heat_sink and impurity_radiation_heat_sink', |
| 232 | + ): |
| 233 | + pydantic_model.Sources.from_dict(invalid_config) |
| 234 | + |
153 | 235 |
|
154 | 236 | if __name__ == '__main__':
|
155 | 237 | absltest.main()
|
0 commit comments