|
19 | 19 | from absl.testing import parameterized |
20 | 20 | import imas |
21 | 21 | import numpy as np |
| 22 | +import pydantic |
22 | 23 | from torax._src.geometry import geometry_loader |
23 | 24 | from torax._src.geometry import imas as imas_geometry |
24 | 25 | from torax._src.geometry import pydantic_model as geometry_pydantic_model |
@@ -98,6 +99,48 @@ def test_IMAS_input_with_equilibrium_object(self): |
98 | 99 | ) |
99 | 100 | config.build_geometry() |
100 | 101 |
|
| 102 | + def test_IMAS_loading_specific_slice(self): |
| 103 | + filename = 'ITERhybrid_COCOS17_IDS_ddv4.nc' |
| 104 | + SLICE_TIME = 5 |
| 105 | + SLICE_INDEX = 1 |
| 106 | + config_at_0 = geometry_pydantic_model.IMASConfig(imas_filepath=filename) |
| 107 | + config_at_slice_from_time = geometry_pydantic_model.IMASConfig( |
| 108 | + imas_filepath=filename, slice_time=SLICE_TIME |
| 109 | + ) |
| 110 | + config_at_slice_from_index = geometry_pydantic_model.IMASConfig( |
| 111 | + imas_filepath=filename, slice_index=SLICE_INDEX |
| 112 | + ) |
| 113 | + geo_at_0 = config_at_0.build_geometry() |
| 114 | + geo_at_slice_from_time = config_at_slice_from_time.build_geometry() |
| 115 | + geo_at_slice_from_index = config_at_slice_from_index.build_geometry() |
| 116 | + |
| 117 | + for key in geo_at_0.__dict__.keys(): |
| 118 | + np.testing.assert_allclose( |
| 119 | + geo_at_slice_from_time[key], |
| 120 | + geo_at_slice_from_index[key], |
| 121 | + err_msg=f'Value {key} mismatched between slice_time and slice_index', |
| 122 | + ) |
| 123 | + with self.assertRaises(AssertionError): |
| 124 | + np.testing.assert_allclose( |
| 125 | + geo_at_0[key], |
| 126 | + geo_at_slice_from_time[key], |
| 127 | + ) |
| 128 | + with self.assertRaises(AssertionError): |
| 129 | + np.testing.assert_allclose( |
| 130 | + geo_at_0[key], |
| 131 | + geo_at_slice_from_index[key], |
| 132 | + ) |
| 133 | + |
| 134 | + def test_IMAS_raises_if_slice_specified_twice(self): |
| 135 | + filename = 'ITERhybrid_COCOS17_IDS_ddv4.nc' |
| 136 | + with self.assertRaisesRegex( |
| 137 | + pydantic.ValidationError, |
| 138 | + 'exactly one of `slice_time` and `slice_index`', |
| 139 | + ): |
| 140 | + geometry_pydantic_model.IMASConfig( |
| 141 | + imas_filepath=filename, slice_index=1, slice_time=1 |
| 142 | + ) |
| 143 | + |
101 | 144 |
|
102 | 145 | if __name__ == '__main__': |
103 | 146 | absltest.main() |
0 commit comments