Repository navigation
Expand file tree
/
Copy pathtest.cover
More file actions
359 lines (305 loc) · 15.1 KB
/
Copy pathtest.cover
File metadata and controls
359 lines (305 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
1: """TFcoil.py 单元测试:覆盖所有方法与主要分支。"""
1: from __future__ import annotations
1: import logging
1: import sys
1: import unittest
1: import numpy as np
1: import TFcoil
1: from DataStructure import (
BuildDataStructure,
GeometryDataStructure,
InputDataStructure,
ProfileDataStructure,
TFCoilDataStructure,
)
1: try:
1: import importlib.util
1: _HAS_COVERAGE = importlib.util.find_spec("coverage") is not None
except ImportError:
_HAS_COVERAGE = False
2: class Model:
1: def __init__(self) -> None:
20: self.tf_coil_data = TFCoilDataStructure.TFCoilData()
20: self.build_data = BuildDataStructure.BuildData()
20: self.geom_data = GeometryDataStructure.GeometryData()
20: self.prof_data = ProfileDataStructure.ProfileData()
20: self.input_data = InputDataStructure.InputData()
1: def build_model(**overrides) -> Model:
"""构造带合理默认值的 model,可通过 overrides 覆盖字段。"""
20: model = Model()
40: set_fields(
20: model.geom_data,
20: itart=0,
20: rmajor=6.0,
20: rminor=2.0,
)
40: set_fields(
20: model.build_data,
20: r_tf_inboard_in=1.0,
20: r_tf_inboard_out=1.5,
20: dr_tf_inboard=0.5,
20: dr_tf_outboard=0.4,
20: r_tf_outboard_in=6.5,
20: r_cp_top=1.2,
20: z_tf_inside_max=3.0,
20: z_tf_outside_max=4.0,
20: dz_tf_upper=0.2,
20: dz_tf_lower=0.2,
)
440: set_fields(
20: model.tf_coil_data,
20: n_tf_coil=18,
20: n_tf_coil_turn=4.0,
20: f_cool_conductor=0.1,
20: i_tf_inboard_shape=1,
20: i_f_dr_tf_plasma_case_inboard=0,
20: dr_tf_plasma_case_inboard_input=0.05,
20: i_f_dr_side_case_inboard=0,
20: dr_side_case_inboard_input=0.03,
20: dr_side_case_inboard_min=0.02,
20: dr_tf_nose_case_inboard=0.01,
20: r_tf_wp_inboard_in=1.05,
20: r_tf_wp_inboard_out=1.35,
20: dr_tf_wp_insulation_inboard=0.01,
20: dr_tf_turn_insulation_inboard=0.005,
20: dr_tf_wp_no_insulation_inboard=0.25,
20: dx_tf_inboard_out=0.5,
20: area_tf_inboard=0.2,
20: i_tf_shape=1,
20: current_tf_total=5.0e6,
20: current_tf_single=5.0e6 / 18.0,
)
40: set_fields(
20: model.prof_data,
20: b_toroidal_rmajor=5.0,
)
40: set_fields(
20: model.input_data,
20: i_dimension=0,
)
50: for key, value in overrides.items():
30: target, name = key.split(".", 1)
30: setattr(getattr(model, target), name, value)
20: return model
1: def set_fields(obj, **kwargs):
800: for name, value in kwargs.items():
700: setattr(obj, name, value)
100: return obj
2: class TestTFGlobalGeom(unittest.TestCase):
1: def run_geom(self, **overrides):
8: model = build_model(**overrides)
8: TFcoil.TFCoilModule.TFCoilGeom.tf_global_geom(model)
8: return model
1: def test_inboard_shape_arc(self):
1: model = self.run_geom(**{"tf_coil_data.i_tf_inboard_shape": 1})
1: expected = np.pi * (1.5 ** 2 - 1.0 ** 2)
1: self.assertAlmostEqual(model.tf_coil_data.area_tf_inboard, expected)
1: def test_inboard_shape_straight(self):
1: model = self.run_geom(**{"tf_coil_data.i_tf_inboard_shape": 0})
1: n = model.tf_coil_data.n_tf_coil
1: angle = np.pi / n
1: expected = 1.5 ** 2 * np.sin(2 * angle) / 2 * n - np.pi * 1.0 ** 2
1: self.assertAlmostEqual(model.tf_coil_data.area_tf_inboard, expected)
1: def test_plasma_case_from_factor(self):
2: model = self.run_geom(
1: **{
1: "tf_coil_data.i_f_dr_tf_plasma_case_inboard": 1,
1: "tf_coil_data.f_dr_tf_plasma_case_inboard": 0.4,
}
)
1: self.assertAlmostEqual(model.tf_coil_data.dr_tf_plasma_case_inboard, 0.5 * 0.4)
1: def test_plasma_case_from_input(self):
2: model = self.run_geom(
1: **{
1: "tf_coil_data.i_f_dr_tf_plasma_case_inboard": 0,
1: "tf_coil_data.dr_tf_plasma_case_inboard_input": 0.06,
}
)
1: self.assertAlmostEqual(model.tf_coil_data.dr_tf_plasma_case_inboard, 0.06)
1: def test_plasma_case_clamped_to_minimum(self):
2: with self.assertLogs("TFcoil", level="ERROR") as logs:
2: model = self.run_geom(
1: **{
1: "tf_coil_data.i_f_dr_tf_plasma_case_inboard": 0,
1: "tf_coil_data.dr_tf_plasma_case_inboard_input": 1.0e-6,
}
)
1: angle = np.pi / model.tf_coil_data.n_tf_coil
1: expected_min = 1.5 * (1.0 - np.cos(angle))
1: self.assertAlmostEqual(model.tf_coil_data.dr_tf_plasma_case_inboard, expected_min)
2: self.assertTrue(any("dr_tf_plasma_case_inboard is less than" in msg for msg in logs.output))
1: def test_side_case_from_factor(self):
2: model = self.run_geom(
1: **{
1: "tf_coil_data.i_f_dr_side_case_inboard": 1,
1: "tf_coil_data.f_dr_side_case_inboard": 0.5,
}
)
1: angle = model.tf_coil_data.angle_tf_inboard_half
1: expected = (0.01 + 1.0) * np.tan(angle) * 0.5
1: self.assertAlmostEqual(model.tf_coil_data.dr_side_case_inboard, expected)
1: def test_side_case_from_input(self):
2: model = self.run_geom(
1: **{
1: "tf_coil_data.i_f_dr_side_case_inboard": 0,
1: "tf_coil_data.dr_side_case_inboard_input": 0.04,
}
)
1: self.assertAlmostEqual(model.tf_coil_data.dr_side_case_inboard, 0.04)
1: def test_side_case_from_minimum(self):
2: model = build_model(
1: **{
1: "tf_coil_data.i_f_dr_side_case_inboard": 0,
1: "tf_coil_data.dr_side_case_inboard_min": 0.025,
}
)
1: model.tf_coil_data.dr_side_case_inboard_input = None
1: TFcoil.TFCoilModule.TFCoilGeom.tf_global_geom(model)
1: self.assertAlmostEqual(model.tf_coil_data.dr_side_case_inboard, 0.025)
1: def test_outputs_are_written(self):
1: model = self.run_geom()
1: td = model.tf_coil_data
1: self.assertGreater(td.angle_tf_inboard_half, 0.0)
1: self.assertGreater(td.dx_tf_inboard_out, 0.0)
1: self.assertGreater(td.area_tf_outboard, 0.0)
2: class TestTFPoloidalLength(unittest.TestCase):
1: def run_length(self, i_tf_shape: int, itart: int) -> Model:
8: model = build_model(
4: **{
4: "tf_coil_data.i_tf_shape": i_tf_shape,
4: "geom_data.itart": itart,
}
)
4: TFcoil.TFCoilModule.TFCoilGeom.tf_poloidal_length(model)
4: return model
1: def _assert_arc_outputs(self, model: Model):
4: td = model.tf_coil_data
4: self.assertIsInstance(td.length_tf, float)
4: self.assertEqual(td.r_tf_arc.shape, (5,))
4: self.assertEqual(td.z_tf_arc.shape, (5,))
4: self.assertGreater(td.length_tf, 0.0)
1: def test_d_shape_non_tart(self):
1: model = self.run_length(i_tf_shape=1, itart=0)
1: self._assert_arc_outputs(model)
1: self.assertAlmostEqual(model.tf_coil_data.r_tf_arc[0], 1.5)
1: self.assertAlmostEqual(model.tf_coil_data.z_tf_arc[0], model.tf_coil_data.z_tf_arc[1] * 0.6)
1: def test_d_shape_tart(self):
1: model = self.run_length(i_tf_shape=1, itart=1)
1: self._assert_arc_outputs(model)
1: self.assertAlmostEqual(model.tf_coil_data.r_tf_arc[0], 1.2)
1: self.assertAlmostEqual(model.tf_coil_data.z_tf_arc[0], model.tf_coil_data.z_tf_arc[1])
1: def test_rect_non_tart(self):
1: model = self.run_length(i_tf_shape=0, itart=0)
1: self._assert_arc_outputs(model)
1: self.assertAlmostEqual(model.tf_coil_data.r_tf_arc[0], 1.5)
1: def test_rect_tart(self):
1: model = self.run_length(i_tf_shape=0, itart=1)
1: self._assert_arc_outputs(model)
1: self.assertAlmostEqual(model.tf_coil_data.r_tf_arc[0], 1.2)
2: class TestTFResisArea(unittest.TestCase):
1: def run_resis(self, **overrides):
3: model = build_model(**overrides)
3: TFcoil.TFCoilModule.TFCoilGeom.tf_resis_area(model)
3: return model
1: def test_normal_geometry(self):
1: model = self.run_resis()
1: td = model.tf_coil_data
1: self.assertGreater(td.area_tf_wp_with_insulation_inboard, 0.0)
1: self.assertGreater(td.area_tf_wp_no_insulation_inboard, 0.0)
1: self.assertGreaterEqual(td.area_tf_wp_insulation_inboard, 0.0)
1: self.assertGreater(td.area_tf_plasma_case_inboard, 0.0)
1: self.assertGreater(td.area_tf_conductor_coolant_inboard, 0.0)
1: self.assertGreater(td.area_tf_conductor_coolant_outboard, 0.0)
1: self.assertGreater(td.area_tf_conductor_inboard, 0.0)
1: self.assertGreaterEqual(td.area_tf_turn_insulation_inboard, 0.0)
1: self.assertGreater(td.current_tf_single_turn, 0.0)
1: self.assertGreater(td.current_density_tf_outboard_avg, 0.0)
1: def test_n_tf_coil_turn_near_zero_is_clamped(self):
1: model = self.run_resis(**{"tf_coil_data.n_tf_coil_turn": 0.0})
1: self.assertEqual(model.tf_coil_data.n_tf_coil_turn, 1.0)
1: def test_negative_wp_areas_log_errors(self):
2: with self.assertLogs("TFcoil", level="ERROR") as logs:
2: model = self.run_resis(
1: **{
1: "tf_coil_data.r_tf_wp_inboard_in": 1.40,
1: "tf_coil_data.r_tf_wp_inboard_out": 1.10,
1: "tf_coil_data.dr_tf_wp_insulation_inboard": 0.20,
}
)
1: self.assertLess(model.tf_coil_data.area_tf_wp_with_insulation_inboard, 0.0)
1: self.assertLess(model.tf_coil_data.area_tf_wp_no_insulation_inboard, 0.0)
3: self.assertEqual(len([m for m in logs.output if "Winding pack cross-section problem" in m]), 2)
2: class TestTFCurrent(unittest.TestCase):
1: def run_current(self, **overrides):
1: model = build_model(**overrides)
1: TFcoil.TFCoilModule.TFCoilGeom.tf_global_geom(model)
1: TFcoil.TFCoilModule.tf_current(model)
1: return model
1: def test_i_dimension_zero(self):
2: model = self.run_current(
1: **{
1: "tf_coil_data.dr_tf_plasma_case_inboard": 0.05,
1: "tf_coil_data.dr_tf_wp_insulation_inboard": 0.01,
1: "tf_coil_data.dr_tf_wp_insertion_gap_inboard": 0.005,
}
)
1: td = model.tf_coil_data
1: r_bmax = 1.5 - 0.05 - 0.01 - 0.005
1: self.assertAlmostEqual(td.b_tf_inboard_max, 5.0 * 6.0 / r_bmax)
1: self.assertGreater(td.current_tf_total, 0.0)
1: self.assertAlmostEqual(td.current_tf_single, td.current_tf_total / td.n_tf_coil)
1: self.assertAlmostEqual(td.current_density_tf_inboard_avg, td.current_tf_total / td.area_tf_inboard)
1: def test_i_dimension_nonzero_hits_else_branch(self):
"""else 分支目前仅 pass,后续赋值会因局部变量未定义而报错。"""
1: model = build_model(**{"input_data.i_dimension": 1})
1: TFcoil.TFCoilModule.TFCoilGeom.tf_global_geom(model)
2: with self.assertRaises(UnboundLocalError):
1: TFcoil.TFCoilModule.tf_current(model)
2: class TestTFCoilModuleIntegration(unittest.TestCase):
1: def test_run_base(self):
1: model = build_model()
1: TFcoil.TFCoilModule().run_base(model)
1: td = model.tf_coil_data
1: self.assertGreater(td.area_tf_inboard, 0.0)
1: self.assertGreater(td.length_tf, 0.0)
1: self.assertGreater(td.current_tf_total, 0.0)
1: def test_resis_tf_coil_run(self):
1: model = build_model()
1: TFcoil.ResisTFCoil().run(model)
1: td = model.tf_coil_data
1: self.assertGreater(td.area_tf_wp_with_insulation_inboard, 0.0)
1: self.assertGreater(td.current_density_tf_outboard_avg, 0.0)
2: class TestCoverageHelper(unittest.TestCase):
1: """可选:统计 TFcoil.py 行覆盖率。"""
2: @unittest.skipUnless(_HAS_COVERAGE, "coverage 未安装")
2: def test_tfcoil_line_coverage(self):
import coverage
cov = coverage.Coverage(source=["TFcoil"])
cov.start()
suite = unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__])
for group in (TestTFGlobalGeom, TestTFPoloidalLength, TestTFResisArea, TestTFCurrent, TestTFCoilModuleIntegration):
sub = unittest.defaultTestLoader.loadTestsFromTestCase(group)
for test in sub:
test.debug()
cov.stop()
cov.save()
total = cov.report(file=sys.stdout, show_missing=True)
self.assertGreaterEqual(total, 95.0)
1: def run_all_tests(verbosity: int = 2) -> unittest.TestResult:
1: loader = unittest.TestLoader()
1: suite = unittest.TestSuite()
6: for case in (
1: TestTFGlobalGeom,
1: TestTFPoloidalLength,
1: TestTFResisArea,
1: TestTFCurrent,
1: TestTFCoilModuleIntegration,
):
5: suite.addTests(loader.loadTestsFromTestCase(case))
1: runner = unittest.TextTestRunner(verbosity=verbosity)
1: return runner.run(suite)
1: if __name__ == "__main__":
1: logging.basicConfig(level=logging.ERROR)
1: result = run_all_tests()
1: if not result.wasSuccessful():
sys.exit(1)