added toroid simulator

This commit is contained in:
2026-02-17 11:11:47 -06:00
parent a881a0a381
commit 4df588e09c
6 changed files with 323 additions and 108 deletions

79
app.py
View File

@@ -203,6 +203,85 @@ def api_design():
return jsonify({"success": False, "error": str(exc)}), 400
@app.route("/api/simulate", methods=["POST"])
def api_simulate():
"""
Compute a single operating point.
Request body (JSON):
{
// Same core + windings as /api/design ...
"fill_factor": 0.35,
"primary_tap": 1,
"secondary_tap": 1,
"Vp_rms": 12.0,
"freq_hz": 1000.0,
"R_load": 100.0,
"X_load": 0.0,
"constraints": { ... } // optional
}
"""
try:
data = request.get_json(force=True)
core = _parse_core(data)
specs = _parse_windings(data)
fill_factor = float(data.get("fill_factor", 0.35))
results = design_transformer(core, specs, fill_factor=fill_factor)
if len(results) < 2:
return jsonify({"success": False, "error": "Need at least 2 windings"}), 400
sim = ToroidSimulator(core=core, primary=results[0], secondary=results[1])
cdata = data.get("constraints", {})
constraints = SimConstraints(
B_max_T=float(cdata.get("B_max_T", 0.3)),
Vp_max=float(cdata.get("Vp_max", float("inf"))),
Vs_max=float(cdata.get("Vs_max", float("inf"))),
Ip_max=float(cdata.get("Ip_max", float("inf"))),
Is_max=float(cdata.get("Is_max", float("inf"))),
P_out_max_W=float(cdata.get("P_out_max_W", float("inf"))),
) if cdata else None
r = sim.simulate(
Vp_rms=float(data["Vp_rms"]),
freq_hz=float(data["freq_hz"]),
primary_tap=int(data["primary_tap"]),
secondary_tap=int(data["secondary_tap"]),
Z_load=(float(data.get("R_load", 0.0)), float(data.get("X_load", 0.0))),
constraints=constraints,
)
def _f(v):
return None if (isinstance(v, float) and math.isnan(v)) else v
return jsonify({
"success": True,
"Np_eff": r.Np_eff,
"Ns_eff": r.Ns_eff,
"turns_ratio": _f(round(r.turns_ratio, 4)),
"B_peak_T": _f(round(r.B_peak_T, 4)),
"Vp_rms": _f(round(r.Vp_rms_applied, 4)),
"Vs_rms": _f(round(r.Vs_rms, 4)),
"Ip_rms": _f(round(r.Ip_rms, 4)),
"Is_rms": _f(round(r.Is_rms, 4)),
"P_out_W": _f(round(r.P_out_W, 4)),
"P_cu_W": _f(round(r.P_cu_W, 4)),
"P_cu_primary_W": _f(round(r.P_cu_primary_W, 4)),
"P_cu_secondary_W": _f(round(r.P_cu_secondary_W, 4)),
"P_core_W": _f(round(r.P_core_W, 4)),
"P_in_W": _f(round(r.P_in_W, 4)),
"efficiency_pct": _f(round(r.efficiency * 100, 2)),
"violations": r.violations,
})
except Exception as exc:
import traceback
return jsonify({"success": False, "error": str(exc),
"traceback": traceback.format_exc()}), 400
@app.route("/api/sweep", methods=["POST"])
def api_sweep():
"""