Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
642 lines (549 loc) · 22.7 KB
/
Copy pathtest.py
File metadata and controls
642 lines (549 loc) · 22.7 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
"""TFcoil.py 单元测试:覆盖所有方法与主要分支。"""
from __future__ import annotations
import logging
import sys
import unittest
from typing import Any
import numpy as np
import TFcoil
from constant import Const
from DataStructure import (
BuildDataStructure,
GeometryDataStructure,
InputDataStructure,
ProfileDataStructure,
TFCoilDataStructure,
)
try:
import importlib.util
_HAS_COVERAGE = importlib.util.find_spec("coverage") is not None
except ImportError:
_HAS_COVERAGE = False
class Model:
def __init__(self) -> None:
self.tf_coil_data = TFCoilDataStructure.TFCoilData()
self.build_data = BuildDataStructure.BuildData()
self.geom_data = GeometryDataStructure.GeometryData()
self.prof_data = ProfileDataStructure.ProfileData()
self.input_data = InputDataStructure.InputData()
def build_model(**overrides) -> Model:
"""构造带合理默认值的 model,可通过 overrides 覆盖字段。"""
model = Model()
set_fields(
model.geom_data,
itart=0,
rmajor=6.0,
rminor=2.0,
)
set_fields(
model.build_data,
r_tf_inboard_in=1.0,
r_tf_inboard_out=1.5,
dr_tf_inboard=0.5,
dr_tf_outboard=0.4,
r_tf_outboard_in=6.5,
r_cp_top=1.2,
z_tf_inside_max=3.0,
z_tf_outside_max=4.0,
dz_tf_upper=0.2,
dz_tf_lower=0.2,
)
set_fields(
model.tf_coil_data,
n_tf_coil=18,
n_tf_coil_turn=4.0,
f_cool_conductor=0.1,
i_tf_inboard_shape=1,
i_f_dr_tf_plasma_case_inboard=0,
dr_tf_plasma_case_inboard_input=0.05,
i_f_dr_side_case_inboard=0,
dr_side_case_inboard_input=0.03,
dr_side_case_inboard_min=0.02,
dr_tf_nose_case_inboard=0.01,
r_tf_wp_inboard_in=1.05,
r_tf_wp_inboard_out=1.35,
dr_tf_wp_insulation_inboard=0.01,
dr_tf_turn_insulation_inboard=0.005,
dr_tf_wp_no_insulation_inboard=0.25,
dx_tf_inboard_out=0.5,
area_tf_inboard=0.2,
i_tf_shape=1,
current_tf_total=5.0e6,
current_tf_single=5.0e6 / 18.0,
)
set_fields(
model.prof_data,
b_toroidal_rmajor=5.0,
)
set_fields(
model.input_data,
i_dimension=0,
)
for key, value in overrides.items():
target, name = key.split(".", 1)
setattr(getattr(model, target), name, value)
return model
def set_fields(obj, **kwargs):
for name, value in kwargs.items():
setattr(obj, name, value)
return obj
def apply_process_d_shape_arcs(model: Model, z_tf_inside_half: float | None = None) -> None:
"""Set r_tf_arc / z_tf_arc using the PROCESS D-shape convention."""
if z_tf_inside_half is None:
z_tf_inside_half = model.build_data.z_tf_inside_max
f_straight = 0.6
rmajor = model.geom_data.rmajor
rminor = model.geom_data.rminor
model.tf_coil_data.r_tf_arc = np.array(
[
model.build_data.r_tf_inboard_out,
rmajor - 0.2 * rminor,
model.build_data.r_tf_outboard_in,
rmajor - 0.2 * rminor,
model.build_data.r_tf_inboard_out,
],
dtype=float,
)
model.tf_coil_data.z_tf_arc = np.array(
[
f_straight * z_tf_inside_half,
z_tf_inside_half,
0.0,
-z_tf_inside_half,
-f_straight * z_tf_inside_half,
],
dtype=float,
)
def build_model_for_self_inductance(**overrides) -> Model:
"""Prepare geometry inputs required by tf_self_inductance."""
model = build_model(**overrides)
TFcoil.TFCoilModule.TFCoilGeom.tf_global_geom(model)
TFcoil.TFCoilModule.TFCoilGeom.tf_poloidal_length(model)
set_fields(
model.build_data,
r_tf_inboard_mid=model.build_data.r_tf_inboard_in + 0.5 * model.build_data.dr_tf_inboard,
r_tf_outboard_mid=model.build_data.r_tf_outboard_in + 0.5 * model.build_data.dr_tf_outboard,
)
apply_process_d_shape_arcs(model)
return model
def process_tf_coil_self_inductance(
dr_tf_inboard: float,
r_tf_arc: np.ndarray,
z_tf_arc: np.ndarray,
itart: int,
i_tf_shape: int,
z_tf_inside_half: float,
dr_tf_outboard: float,
r_tf_outboard_mid: float,
r_tf_inboard_mid: float,
) -> float:
"""
Reference implementation of PROCESS ``TFCoil.tf_coil_self_inductance``.
Ported from ``process/tf_coil.py`` so tests do not require importing PROCESS.
"""
n_intervals = 100
r_tf_arc = np.asarray(r_tf_arc, dtype=float)
z_tf_arc = np.asarray(z_tf_arc, dtype=float)
if itart == 0 and i_tf_shape == 1:
x0 = r_tf_arc[1]
y0 = z_tf_arc[1]
ai = r_tf_arc[1] - r_tf_arc[0]
bi = (z_tf_arc[1] - z_tf_arc[3]) / 2.0 - z_tf_arc[0]
ao = ai + dr_tf_inboard
bo = bi + dr_tf_inboard
dr = ao / n_intervals
r = x0 - dr / 2.0
ind_tf_coil = 0.0
for _ in range(n_intervals):
b = Const.MU0 / (2.0 * np.pi * r)
if x0 - r < ai:
h_bore = y0 + bi * np.sqrt(1.0 - ((r - x0) / ai) ** 2)
h_thick = bo * np.sqrt(1.0 - ((r - x0) / ao) ** 2) - h_bore
else:
h_bore = 0.0
h_thick = bo * np.sqrt(1.0 - ((r - x0) / ao) ** 2) + z_tf_arc[0]
ind_tf_coil += b * dr * (2.0 * h_bore + h_thick)
r -= dr
ai = r_tf_arc[2] - r_tf_arc[1]
bi = (z_tf_arc[1] - z_tf_arc[3]) / 2.0
ao = ai + dr_tf_inboard
bo = bi + dr_tf_inboard
dr = ao / n_intervals
r = x0 + dr / 2.0
for _ in range(n_intervals):
b = Const.MU0 / (2.0 * np.pi * r)
if r - x0 < ai:
h_bore = y0 + bi * np.sqrt(1.0 - ((r - x0) / ai) ** 2)
h_thick = bo * np.sqrt(1.0 - ((r - x0) / ao) ** 2) - h_bore
else:
h_bore = 0.0
h_thick = bo * np.sqrt(1.0 - ((r - x0) / ao) ** 2)
ind_tf_coil += b * dr * (2.0 * h_bore + h_thick)
r += dr
return float(ind_tf_coil)
return float(
(z_tf_inside_half + dr_tf_outboard)
* Const.MU0
/ np.pi
* np.log(r_tf_outboard_mid / r_tf_inboard_mid)
)
def compare_tf_self_inductance_with_process(
model: Model,
*,
z_tf_inside_half: float | None = None,
verbose: bool = False,
) -> dict[str, Any]:
"""
Compare ``TFCoilModule.tf_self_inductance`` with PROCESS reference.
PROCESS reference: ``process/tf_coil.py::TFCoil.tf_coil_self_inductance``.
Returns a dict with local/process values, absolute/relative differences,
and the arc geometry used for the comparison.
"""
if z_tf_inside_half is None:
z_tf_inside_half = model.build_data.z_tf_inside_max
local = float(TFcoil.TFCoilModule.tf_self_inductance(model))
process = process_tf_coil_self_inductance(
model.build_data.dr_tf_inboard,
model.tf_coil_data.r_tf_arc,
model.tf_coil_data.z_tf_arc,
model.geom_data.itart,
model.tf_coil_data.i_tf_shape,
z_tf_inside_half,
model.build_data.dr_tf_outboard,
model.build_data.r_tf_outboard_mid,
model.build_data.r_tf_inboard_mid,
)
abs_diff = local - process
denom = max(abs(process), np.finfo(float).eps)
rel_err_pct = 100.0 * abs_diff / denom
result = {
"case": "custom",
"local_h": local,
"process_h": process,
"local_uh": local * 1.0e6,
"process_uh": process * 1.0e6,
"abs_diff_h": abs_diff,
"rel_err_pct": rel_err_pct,
"itart": model.geom_data.itart,
"i_tf_shape": model.tf_coil_data.i_tf_shape,
"r_tf_arc": model.tf_coil_data.r_tf_arc.copy(),
"z_tf_arc": model.tf_coil_data.z_tf_arc.copy(),
}
if verbose:
print(format_tf_self_inductance_comparison(result))
return result
def format_tf_self_inductance_comparison(result: dict[str, Any]) -> str:
lines = [
"TF coil self-inductance: TFcoil.py vs PROCESS",
f" case = {result.get('case', 'custom')}",
f" itart = {result['itart']}",
f" i_tf_shape= {result['i_tf_shape']}",
f" local = {result['local_h']:.12g} H ({result['local_uh']:.6f} uH)",
f" process = {result['process_h']:.12g} H ({result['process_uh']:.6f} uH)",
f" abs diff = {result['abs_diff_h']:.6e} H",
f" rel err = {result['rel_err_pct']:+.6f} %",
]
return "\n".join(lines)
def run_tf_self_inductance_comparison(**overrides) -> list[dict[str, Any]]:
"""
Run standard Local vs PROCESS self-inductance comparisons.
Cases:
1. D-shape, PROCESS arc convention (baseline for cross-code check)
2. D-shape, arcs from local ``tf_poloidal_length`` only
3. Picture-frame fallback branch (non-D-shape)
"""
results: list[dict[str, Any]] = []
model_process_arcs = build_model_for_self_inductance(**overrides)
result_process_arcs = compare_tf_self_inductance_with_process(model_process_arcs)
result_process_arcs["case"] = "d_shape_process_arcs"
results.append(result_process_arcs)
model_local_arcs = build_model(**overrides)
TFcoil.TFCoilModule.TFCoilGeom.tf_global_geom(model_local_arcs)
TFcoil.TFCoilModule.TFCoilGeom.tf_poloidal_length(model_local_arcs)
set_fields(
model_local_arcs.build_data,
r_tf_inboard_mid=model_local_arcs.build_data.r_tf_inboard_in
+ 0.5 * model_local_arcs.build_data.dr_tf_inboard,
r_tf_outboard_mid=model_local_arcs.build_data.r_tf_outboard_in
+ 0.5 * model_local_arcs.build_data.dr_tf_outboard,
)
result_local_arcs = compare_tf_self_inductance_with_process(model_local_arcs)
result_local_arcs["case"] = "d_shape_local_poloidal_arcs"
results.append(result_local_arcs)
model_picture = build_model_for_self_inductance(
**{
"geom_data.itart": 0,
"tf_coil_data.i_tf_shape": 0,
**overrides,
}
)
result_picture = compare_tf_self_inductance_with_process(model_picture)
result_picture["case"] = "picture_frame_fallback"
results.append(result_picture)
return results
def print_tf_self_inductance_comparison_report(**overrides) -> list[dict[str, Any]]:
"""Print and return all standard self-inductance comparison cases."""
results = run_tf_self_inductance_comparison(**overrides)
print("=== TF self-inductance comparison (TFcoil.py vs PROCESS) ===")
for idx, result in enumerate(results, start=1):
if idx > 1:
print()
print(format_tf_self_inductance_comparison(result))
return results
class TestTFSelfInductance(unittest.TestCase):
def test_process_reference_is_self_consistent(self):
model = build_model_for_self_inductance()
args = (
model.build_data.dr_tf_inboard,
model.tf_coil_data.r_tf_arc,
model.tf_coil_data.z_tf_arc,
model.geom_data.itart,
model.tf_coil_data.i_tf_shape,
model.build_data.z_tf_inside_max,
model.build_data.dr_tf_outboard,
model.build_data.r_tf_outboard_mid,
model.build_data.r_tf_inboard_mid,
)
first = process_tf_coil_self_inductance(*args)
second = process_tf_coil_self_inductance(*args)
self.assertAlmostEqual(first, second, places=15)
def test_compare_returns_finite_values(self):
result = compare_tf_self_inductance_with_process(build_model_for_self_inductance())
self.assertTrue(np.isfinite(result["local_h"]))
self.assertTrue(np.isfinite(result["process_h"]))
self.assertGreater(result["process_h"], 0.0)
def test_d_shape_process_arcs_comparison(self):
results = run_tf_self_inductance_comparison()
d_shape = next(r for r in results if r["case"] == "d_shape_process_arcs")
self.assertEqual(d_shape["itart"], 0)
self.assertEqual(d_shape["i_tf_shape"], 1)
# Local integration scheme in TFcoil.py still differs from PROCESS (~few %).
self.assertLess(abs(d_shape["rel_err_pct"]), 15.0)
def test_picture_frame_branch_runs(self):
results = run_tf_self_inductance_comparison()
picture = next(r for r in results if r["case"] == "picture_frame_fallback")
self.assertEqual(picture["i_tf_shape"], 0)
self.assertTrue(np.isfinite(picture["local_h"]))
self.assertTrue(np.isfinite(picture["process_h"]))
class TestTFGlobalGeom(unittest.TestCase):
def run_geom(self, **overrides):
model = build_model(**overrides)
TFcoil.TFCoilModule.TFCoilGeom.tf_global_geom(model)
return model
def test_inboard_shape_arc(self):
model = self.run_geom(**{"tf_coil_data.i_tf_inboard_shape": 1})
expected = np.pi * (1.5 ** 2 - 1.0 ** 2)
self.assertAlmostEqual(model.tf_coil_data.area_tf_inboard, expected)
def test_inboard_shape_straight(self):
model = self.run_geom(**{"tf_coil_data.i_tf_inboard_shape": 0})
n = model.tf_coil_data.n_tf_coil
angle = np.pi / n
expected = 1.5 ** 2 * np.sin(2 * angle) / 2 * n - np.pi * 1.0 ** 2
self.assertAlmostEqual(model.tf_coil_data.area_tf_inboard, expected)
def test_plasma_case_from_factor(self):
model = self.run_geom(
**{
"tf_coil_data.i_f_dr_tf_plasma_case_inboard": 1,
"tf_coil_data.f_dr_tf_plasma_case_inboard": 0.4,
}
)
self.assertAlmostEqual(model.tf_coil_data.dr_tf_plasma_case_inboard, 0.5 * 0.4)
def test_plasma_case_from_input(self):
model = self.run_geom(
**{
"tf_coil_data.i_f_dr_tf_plasma_case_inboard": 0,
"tf_coil_data.dr_tf_plasma_case_inboard_input": 0.06,
}
)
self.assertAlmostEqual(model.tf_coil_data.dr_tf_plasma_case_inboard, 0.06)
def test_plasma_case_clamped_to_minimum(self):
with self.assertLogs("TFcoil", level="ERROR") as logs:
model = self.run_geom(
**{
"tf_coil_data.i_f_dr_tf_plasma_case_inboard": 0,
"tf_coil_data.dr_tf_plasma_case_inboard_input": 1.0e-6,
}
)
angle = np.pi / model.tf_coil_data.n_tf_coil
expected_min = 1.5 * (1.0 - np.cos(angle))
self.assertAlmostEqual(model.tf_coil_data.dr_tf_plasma_case_inboard, expected_min)
self.assertTrue(any("dr_tf_plasma_case_inboard is less than" in msg for msg in logs.output))
def test_side_case_from_factor(self):
model = self.run_geom(
**{
"tf_coil_data.i_f_dr_side_case_inboard": 1,
"tf_coil_data.f_dr_side_case_inboard": 0.5,
}
)
angle = model.tf_coil_data.angle_tf_inboard_half
expected = (0.01 + 1.0) * np.tan(angle) * 0.5
self.assertAlmostEqual(model.tf_coil_data.dr_side_case_inboard, expected)
def test_side_case_from_input(self):
model = self.run_geom(
**{
"tf_coil_data.i_f_dr_side_case_inboard": 0,
"tf_coil_data.dr_side_case_inboard_input": 0.04,
}
)
self.assertAlmostEqual(model.tf_coil_data.dr_side_case_inboard, 0.04)
def test_side_case_from_minimum(self):
model = build_model(
**{
"tf_coil_data.i_f_dr_side_case_inboard": 0,
"tf_coil_data.dr_side_case_inboard_min": 0.025,
}
)
model.tf_coil_data.dr_side_case_inboard_input = None
TFcoil.TFCoilModule.TFCoilGeom.tf_global_geom(model)
self.assertAlmostEqual(model.tf_coil_data.dr_side_case_inboard, 0.025)
def test_outputs_are_written(self):
model = self.run_geom()
td = model.tf_coil_data
self.assertGreater(td.angle_tf_inboard_half, 0.0)
self.assertGreater(td.dx_tf_inboard_out, 0.0)
self.assertGreater(td.area_tf_outboard, 0.0)
class TestTFPoloidalLength(unittest.TestCase):
def run_length(self, i_tf_shape: int, itart: int) -> Model:
model = build_model(
**{
"tf_coil_data.i_tf_shape": i_tf_shape,
"geom_data.itart": itart,
}
)
TFcoil.TFCoilModule.TFCoilGeom.tf_poloidal_length(model)
return model
def _assert_arc_outputs(self, model: Model):
td = model.tf_coil_data
self.assertIsInstance(td.length_tf, float)
self.assertEqual(td.r_tf_arc.shape, (5,))
self.assertEqual(td.z_tf_arc.shape, (5,))
self.assertGreater(td.length_tf, 0.0)
def test_d_shape_non_tart(self):
model = self.run_length(i_tf_shape=1, itart=0)
self._assert_arc_outputs(model)
self.assertAlmostEqual(model.tf_coil_data.r_tf_arc[0], 1.5)
self.assertAlmostEqual(model.tf_coil_data.z_tf_arc[0], model.tf_coil_data.z_tf_arc[1] * 0.6)
def test_d_shape_tart(self):
model = self.run_length(i_tf_shape=1, itart=1)
self._assert_arc_outputs(model)
self.assertAlmostEqual(model.tf_coil_data.r_tf_arc[0], 1.2)
self.assertAlmostEqual(model.tf_coil_data.z_tf_arc[0], model.tf_coil_data.z_tf_arc[1])
def test_rect_non_tart(self):
model = self.run_length(i_tf_shape=0, itart=0)
self._assert_arc_outputs(model)
self.assertAlmostEqual(model.tf_coil_data.r_tf_arc[0], 1.5)
def test_rect_tart(self):
model = self.run_length(i_tf_shape=0, itart=1)
self._assert_arc_outputs(model)
self.assertAlmostEqual(model.tf_coil_data.r_tf_arc[0], 1.2)
class TestTFResisArea(unittest.TestCase):
def run_resis(self, **overrides):
model = build_model(**overrides)
TFcoil.TFCoilModule.TFCoilGeom.tf_resis_area(model)
return model
def test_normal_geometry(self):
model = self.run_resis()
td = model.tf_coil_data
self.assertGreater(td.area_tf_wp_with_insulation_inboard, 0.0)
self.assertGreater(td.area_tf_wp_no_insulation_inboard, 0.0)
self.assertGreaterEqual(td.area_tf_wp_insulation_inboard, 0.0)
self.assertGreater(td.area_tf_plasma_case_inboard, 0.0)
self.assertGreater(td.area_tf_conductor_coolant_inboard, 0.0)
self.assertGreater(td.area_tf_conductor_coolant_outboard, 0.0)
self.assertGreater(td.area_tf_conductor_inboard, 0.0)
self.assertGreaterEqual(td.area_tf_turn_insulation_inboard, 0.0)
self.assertGreater(td.current_tf_single_turn, 0.0)
self.assertGreater(td.current_density_tf_outboard_avg, 0.0)
def test_n_tf_coil_turn_near_zero_is_clamped(self):
model = self.run_resis(**{"tf_coil_data.n_tf_coil_turn": 0.0})
self.assertEqual(model.tf_coil_data.n_tf_coil_turn, 1.0)
def test_negative_wp_areas_log_errors(self):
with self.assertLogs("TFcoil", level="ERROR") as logs:
model = self.run_resis(
**{
"tf_coil_data.r_tf_wp_inboard_in": 1.40,
"tf_coil_data.r_tf_wp_inboard_out": 1.10,
"tf_coil_data.dr_tf_wp_insulation_inboard": 0.20,
}
)
self.assertLess(model.tf_coil_data.area_tf_wp_with_insulation_inboard, 0.0)
self.assertLess(model.tf_coil_data.area_tf_wp_no_insulation_inboard, 0.0)
self.assertEqual(len([m for m in logs.output if "Winding pack cross-section problem" in m]), 2)
class TestTFCurrent(unittest.TestCase):
def run_current(self, **overrides):
model = build_model(**overrides)
TFcoil.TFCoilModule.TFCoilGeom.tf_global_geom(model)
TFcoil.TFCoilModule.tf_current(model)
return model
def test_i_dimension_zero(self):
model = self.run_current(
**{
"tf_coil_data.dr_tf_plasma_case_inboard": 0.05,
"tf_coil_data.dr_tf_wp_insulation_inboard": 0.01,
"tf_coil_data.dr_tf_wp_insertion_gap_inboard": 0.005,
}
)
td = model.tf_coil_data
r_bmax = 1.5 - 0.05 - 0.01 - 0.005
self.assertAlmostEqual(td.b_tf_inboard_max, 5.0 * 6.0 / r_bmax)
self.assertGreater(td.current_tf_total, 0.0)
self.assertAlmostEqual(td.current_tf_single, td.current_tf_total / td.n_tf_coil)
self.assertAlmostEqual(td.current_density_tf_inboard_avg, td.current_tf_total / td.area_tf_inboard)
def test_i_dimension_nonzero_hits_else_branch(self):
"""else 分支目前仅 pass,后续赋值会因局部变量未定义而报错。"""
model = build_model(**{"input_data.i_dimension": 1})
TFcoil.TFCoilModule.TFCoilGeom.tf_global_geom(model)
with self.assertRaises(UnboundLocalError):
TFcoil.TFCoilModule.tf_current(model)
class TestTFCoilModuleIntegration(unittest.TestCase):
def test_run_base(self):
model = build_model()
TFcoil.TFCoilModule().run_base(model)
td = model.tf_coil_data
self.assertGreater(td.area_tf_inboard, 0.0)
self.assertGreater(td.length_tf, 0.0)
self.assertGreater(td.current_tf_total, 0.0)
def test_resis_tf_coil_run(self):
model = build_model()
TFcoil.ResisTFCoil().run(model)
td = model.tf_coil_data
self.assertGreater(td.area_tf_wp_with_insulation_inboard, 0.0)
self.assertGreater(td.current_density_tf_outboard_avg, 0.0)
class TestCoverageHelper(unittest.TestCase):
"""可选:统计 TFcoil.py 行覆盖率。"""
@unittest.skipUnless(_HAS_COVERAGE, "coverage 未安装")
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)
def run_all_tests(verbosity: int = 2) -> unittest.TestResult:
loader = unittest.TestLoader()
suite = unittest.TestSuite()
for case in (
TestTFSelfInductance,
TestTFGlobalGeom,
TestTFPoloidalLength,
TestTFResisArea,
TestTFCurrent,
TestTFCoilModuleIntegration,
):
suite.addTests(loader.loadTestsFromTestCase(case))
runner = unittest.TextTestRunner(verbosity=verbosity)
return runner.run(suite)
if __name__ == "__main__":
logging.basicConfig(level=logging.ERROR)
if len(sys.argv) > 1 and sys.argv[1] == "--compare-inductance":
print_tf_self_inductance_comparison_report()
sys.exit(0)
result = run_all_tests()
if not result.wasSuccessful():
sys.exit(1)