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