-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
133 lines (113 loc) · 3.64 KB
/
Copy pathcli.py
File metadata and controls
133 lines (113 loc) · 3.64 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
"""
MechCalc FEM CLI
python -m mechcalc run model.json
python -m mechcalc validate
python -m mechcalc benchmark
python -m mechcalc profile
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
# 確保專案根目錄在 path
sys.path.insert(0, str(Path(__file__).resolve().parent))
def cmd_run(args: argparse.Namespace) -> int:
"""執行模型分析。"""
input_path = args.input
if not input_path:
print("Error: --input required for 'run' command")
return 1
path = Path(input_path)
if not path.exists():
print(f"Error: file not found: {path}")
return 1
print(f"Running model: {input_path}")
# TODO: 載入 JSON 並呼叫 api.run()
print(" (JSON model loading not yet implemented)")
return 0
def cmd_validate(args: argparse.Namespace) -> int:
"""執行驗證套件。"""
print("Running validation suite...")
try:
from validation.run_all import run_full_validation
run_full_validation()
return 0
except ImportError as e:
print(f"Error: {e}")
return 1
def cmd_benchmark(args: argparse.Namespace) -> int:
"""執行 benchmark 套件。"""
print("Running benchmark suite...")
try:
from validation.buckling_column import buckling_column_benchmark
from validation.plastic_tension_test import plastic_tension_benchmark
from validation.contact_benchmark import run_contact_benchmark_suite
from validation.benchmark_matrix import run_benchmark_matrix
buckling_column_benchmark(verbose=True)
plastic_tension_benchmark(verbose=True)
run_contact_benchmark_suite(verbose=True)
matrix = run_benchmark_matrix()
try:
print("\n" + matrix.to_markdown())
except UnicodeEncodeError:
print("\n[Benchmark matrix - see CSV for details]")
if args.output:
out = Path(args.output)
out.parent.mkdir(parents=True, exist_ok=True)
matrix.to_csv(str(out))
yaml_path = out.with_suffix(".yaml")
matrix.to_yaml(str(yaml_path))
print(f"\nBenchmark matrix saved to: {args.output}, {yaml_path}")
return 0
except ImportError as e:
print(f"Error: {e}")
return 1
def cmd_profile(args: argparse.Namespace) -> int:
"""效能分析。"""
print("Running profile...")
try:
from core.profiler import profile_example
profile_example()
return 0
except ImportError:
print(" (Profiler not available)")
return 0
def main() -> int:
parser = argparse.ArgumentParser(
description="MechCalc FEM — 有限元素分析工具",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python -m mechcalc run model.json
python -m mechcalc validate
python -m mechcalc benchmark --output results/benchmark.csv
python -m mechcalc profile
""",
)
parser.add_argument(
"command",
choices=["run", "validate", "benchmark", "profile"],
help="Command to execute",
)
parser.add_argument(
"--input", "-i",
type=str,
default=None,
help="Input model file (JSON) for 'run' command",
)
parser.add_argument(
"--output", "-o",
type=str,
default=None,
help="Output path for benchmark CSV",
)
args = parser.parse_args()
handlers = {
"run": cmd_run,
"validate": cmd_validate,
"benchmark": cmd_benchmark,
"profile": cmd_profile,
}
return handlers[args.command](args)
if __name__ == "__main__":
sys.exit(main())