axial_probe.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding: utf-8 -*-
  2. """
  3. 探测 Motor-CAD AFM 轴向力图名/变量名 (第2轮)
  4. ============================================
  5. 依据: MotorCAD.exe (v261) UTF-16 字符串中有 "_Axial_Force_Rotor" /
  6. "_Axial_Force_Stator" 后缀 (前缀运行时拼接), 以及 GUI 显示名 "Axial Force"。
  7. 一次求解后穷举前缀组合, 用 get_magnetic_graph / get_variable 逐个试,
  8. 命中与否全部如实记录。
  9. 用法: python axial_probe.py [--quit]
  10. """
  11. import json
  12. import os
  13. import sys
  14. import time
  15. BASE = os.path.dirname(os.path.abspath(__file__))
  16. MOT_SRC = os.path.join(BASE, "MARS-12S10P_SSSR_D76-C150_V5.0-0819.mot")
  17. OUT_DIR = os.path.join(BASE, "output_motorcad")
  18. PREFIXES = ["", "OC", "OC_", "OC ", "OL", "OL_", "OL ", "Load", "Load_",
  19. "OnLoad_", "OnLoad", "OpenCircuit_", "OpenCircuit", "NoLoad_",
  20. "Th1_", "Th1", "1_", "Transient_", "Static_"]
  21. SUFFIXES = ["Axial_Force_Rotor", "Axial_Force_Stator"]
  22. EXTRA_GRAPHS = [
  23. "Axial_Force", "Axial Force", "Axial Force Rotor", "Axial Force Stator",
  24. "Axial Force (Rotor)", "Axial Force (Stator)", "AxialForceRotor",
  25. "AxialForceStator", "Fz_Rotor_OL_Lumped", "Fz_Stator_OL_Lumped",
  26. "Fz_Rotor_OC_Lumped", "Fz_Stator_OC_Lumped", "Fa_Rotor_OL_Lumped",
  27. "Ft_Rotor_OL_Lumped", # 已知径向机存在的命名, 作对照验证探测方法本身
  28. "TorqueVsAngle", # 已知图, 验证 get_magnetic_graph 可用
  29. ]
  30. VAR_CANDS = (["AxialForceRotor", "AxialForceStator"] +
  31. [p + s for p in ("", "OC_", "OL_", "Load_") for s in SUFFIXES])
  32. def stats(ys):
  33. if not ys:
  34. return None
  35. return {"mean": sum(ys) / len(ys), "min": min(ys), "max": max(ys),
  36. "pk2pk": max(ys) - min(ys), "n": len(ys)}
  37. def main(argv):
  38. quit_after = "--quit" in argv
  39. os.makedirs(OUT_DIR, exist_ok=True)
  40. ts = time.strftime("%m%d_%H%M%S")
  41. from ansys.motorcad.core import MotorCAD
  42. print("启动 Motor-CAD (前台) ...")
  43. mc = MotorCAD()
  44. results = {"when": ts, "probe_round": 2}
  45. try:
  46. mc.load_from_file(MOT_SRC)
  47. out_mot = os.path.join(OUT_DIR, "MARS_SSSR_probe_%s.mot" % ts)
  48. mc.save_to_file(out_mot)
  49. results["work_mot"] = out_mot
  50. # 电流口径侦察 (上轮空载疑似未生效: CurrentDefinition=1)
  51. cur = {}
  52. for n in ["CurrentDefinition", "PeakCurrent", "RMSCurrent",
  53. "Imax", "Irms", "RMS_Current", "LineCurrent"]:
  54. try:
  55. cur[n] = mc.get_variable(n)
  56. except Exception:
  57. cur[n] = "<not found>"
  58. results["current_vars"] = cur
  59. print("电流相关变量: %s" % json.dumps(cur, ensure_ascii=False))
  60. for var in ["ElectromagneticForcesCalc_Load",
  61. "ElectromagneticForcesCalc_OC"]:
  62. mc.set_variable(var, True)
  63. print("单次求解 (负载点, OC/Load 力同时计算) ...")
  64. t0 = time.time()
  65. mc.do_magnetic_calculation()
  66. print(" 耗时 %.1f s" % (time.time() - t0))
  67. # ---- 图名穷举 ----
  68. graph_names = ([p + s for p in PREFIXES for s in SUFFIXES]
  69. + EXTRA_GRAPHS)
  70. hits, misses = {}, []
  71. for g in graph_names:
  72. try:
  73. x, y = mc.get_magnetic_graph(g)
  74. hits[g] = {"stats": stats(list(y)), "x0": x[0], "x_end": x[-1],
  75. "x": list(x), "y": list(y)}
  76. print(" [命中] %s: %s" % (g, hits[g]["stats"]))
  77. except Exception:
  78. misses.append(g)
  79. results["graph_hits"] = {k: {kk: vv for kk, vv in v.items()
  80. if kk != "x"} for k, v in hits.items()}
  81. results["graph_hits_full"] = hits
  82. results["graph_misses"] = misses
  83. print("图名: 命中 %d / 未中 %d" % (len(hits), len(misses)))
  84. # ---- 输出变量穷举 ----
  85. var_hits = {}
  86. for n in VAR_CANDS:
  87. try:
  88. var_hits[n] = mc.get_variable(n)
  89. print(" [变量命中] %s = %s" % (n, var_hits[n]))
  90. except Exception:
  91. pass
  92. results["variable_hits"] = var_hits
  93. res_path = os.path.join(OUT_DIR, "probe_results_%s.json" % ts)
  94. with open(res_path, "w", encoding="utf-8") as f:
  95. json.dump(results, f, ensure_ascii=False, indent=2)
  96. print("RESULTS: %s" % res_path)
  97. return 0
  98. finally:
  99. if quit_after:
  100. try:
  101. mc.quit()
  102. except Exception:
  103. pass
  104. else:
  105. print("[提示] Motor-CAD 保持前台打开供检查。")
  106. if __name__ == "__main__":
  107. sys.exit(main(sys.argv[1:]))