-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
372 lines (310 loc) · 13.1 KB
/
app.py
File metadata and controls
372 lines (310 loc) · 13.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
360
361
362
363
364
365
366
367
368
369
370
371
372
import io
import base64
import os
import subprocess
import sys
import threading
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from flask import Flask, jsonify, render_template, request, send_file
from dat_parser import parse_dat
from foil_math import design_foil
from foil_polar import compute_polar, extract_polar_params
from panel_method import run_panel_method_safe
app = Flask(__name__)
# ---------------------------------------------------------------------------
# Shared helpers (used by both /api/calculate and /api/cfd)
# ---------------------------------------------------------------------------
def _mu_from_rho(r):
if r <= 1003: return 0.001
if r <= 1017: return 0.00106
return 0.00108
# ---------------------------------------------------------------------------
# Routes
# ---------------------------------------------------------------------------
@app.route("/")
def index():
return render_template("index.html")
@app.route("/api/system-info", methods=["GET"])
def system_info():
return jsonify({"cpu_count": os.cpu_count() or 1})
@app.route("/api/calculate", methods=["POST"])
def calculate():
data = request.get_json(force=True)
try:
front_profile = parse_dat(data["front_dat"])
except Exception as e:
return jsonify({"error": f"Front foil: {e}"}), 400
rear_dat = data["front_dat"] if data.get("rear_same_as_front") else data.get("rear_dat", "")
try:
rear_profile = parse_dat(rear_dat)
except Exception as e:
return jsonify({"error": f"Rear foil: {e}"}), 400
try:
weight_n = float(data["weight_n"])
v_takeoff_ms = float(data["v_takeoff_ms"])
rho = float(data["rho"])
load_split = float(data["load_split_pct"]) / 100.0
oswald_e = float(data.get("oswald_e", 0.9))
cl_front = float(data.get("cl_front", 0.8))
cl_rear = float(data.get("cl_rear", 0.63))
except (KeyError, ValueError) as e:
return jsonify({"error": f"Invalid input: {e}"}), 400
def _opt_float(key):
v = data.get(key)
if v is None or v == "":
return None
try:
f = float(v)
return f if f > 0 else None
except (TypeError, ValueError):
return None
front_sizing = data.get("front_sizing_mode", "thickness")
rear_sizing = data.get("rear_sizing_mode", "thickness")
max_t_front = _opt_float("max_t_front_mm") if front_sizing == "thickness" else None
chord_front = _opt_float("chord_front_mm") if front_sizing == "chord" else None
max_t_rear = _opt_float("max_t_rear_mm") if rear_sizing == "thickness" else None
chord_rear = _opt_float("chord_rear_mm") if rear_sizing == "chord" else None
if front_sizing == "thickness" and max_t_front is None:
return jsonify({"error": "Front foil: max thickness (mm) is required in thickness mode"}), 400
if front_sizing == "chord" and chord_front is None:
return jsonify({"error": "Front foil: chord (mm) is required in direct chord mode"}), 400
if rear_sizing == "thickness" and max_t_rear is None:
return jsonify({"error": "Rear foil: max thickness (mm) is required in thickness mode"}), 400
if rear_sizing == "chord" and chord_rear is None:
return jsonify({"error": "Rear foil: chord (mm) is required in direct chord mode"}), 400
front_load = weight_n * load_split
rear_load = weight_n * (1.0 - load_split)
mu = _mu_from_rho(rho)
def _design_with_polar(load_n, profile, cl_target, max_t_mm, chord_mm_direct, prefix):
common = dict(
load_n=load_n,
v_takeoff_ms=v_takeoff_ms,
rho=rho,
profile_thickness_pct=profile["max_thickness_pct"],
profile_camber_pct=profile["max_camber_pct"],
cl_target=cl_target,
oswald_e=oswald_e,
max_t_mm=max_t_mm,
chord_mm_direct=chord_mm_direct,
max_span_mm=_opt_float(f"{prefix}_max_span_mm"),
min_span_mm=_opt_float(f"{prefix}_min_span_mm"),
max_chord_mm=_opt_float(f"{prefix}_max_chord_mm"),
min_chord_mm=_opt_float(f"{prefix}_min_chord_mm"),
min_ar=_opt_float(f"{prefix}_min_ar"),
max_ar=_opt_float(f"{prefix}_max_ar"),
)
p1 = design_foil(**common)
chord_m = p1["chord_mm"] / 1000.0
try:
polar = compute_polar(profile["coords"], chord_m, v_takeoff_ms, rho, mu)
except Exception:
polar = None
polar_params = extract_polar_params(polar)
design = design_foil(**common, polar_data=polar_params)
if polar is not None:
design["plot_lift_curve"] = _make_lift_curve(polar, design["aoa_deg"], cl_target)
design["plot_drag_polar"] = _make_drag_polar(polar, cl_target, design["cd"])
else:
design["plot_lift_curve"] = None
design["plot_drag_polar"] = None
flow = run_panel_method_safe(profile["coords"], design["aoa_deg"])
if flow is not None:
design["flow"] = flow
return design
try:
front_design = _design_with_polar(
front_load, front_profile, cl_front, max_t_front, chord_front, "front"
)
rear_design = _design_with_polar(
rear_load, rear_profile, cl_rear, max_t_rear, chord_rear, "rear"
)
except Exception as e:
return jsonify({"error": f"Calculation error: {e}"}), 500
return jsonify({
"front": {
**_profile_summary(front_profile),
**front_design,
"plot": _make_plot(front_profile),
},
"rear": {
**_profile_summary(rear_profile),
**rear_design,
"plot": _make_plot(rear_profile),
},
})
@app.route("/api/step", methods=["POST"])
def download_step():
data = request.get_json(force=True)
foil_label = data.get("foil", "foil")
coords = data["coords"]
chord_mm = float(data["chord_mm"])
span_mm = float(data["span_mm"])
name = data.get("name", "foil").replace(" ", "_")
try:
from step_generator import generate_step_bytes
step_bytes = generate_step_bytes(coords, chord_mm, span_mm)
except ImportError as e:
return jsonify({"error": str(e)}), 500
except Exception as e:
return jsonify({"error": f"STEP generation failed: {e}"}), 500
filename = f"{name}_{foil_label}_foil.step"
return send_file(
io.BytesIO(step_bytes),
mimetype="application/octet-stream",
as_attachment=True,
download_name=filename,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _profile_summary(profile: dict) -> dict:
return {
"name": profile["name"],
"max_thickness_pct": profile["max_thickness_pct"],
"max_thickness_x_pct": profile["max_thickness_x_pct"],
"max_camber_pct": profile["max_camber_pct"],
"max_camber_x_pct": profile["max_camber_x_pct"],
"coords": profile["coords"],
}
def _make_plot(profile: dict) -> str:
coords = profile["coords"]
xs = [p[0] for p in coords]
ys = [p[1] for p in coords]
fig, ax = plt.subplots(figsize=(7, 2.5))
ax.plot(xs, ys, color="#1a6bbf", linewidth=2)
ax.fill(xs, ys, alpha=0.15, color="#1a6bbf")
ax.set_aspect("equal")
ax.set_xlabel("x/c", fontsize=9)
ax.set_ylabel("y/c", fontsize=9)
ax.set_title(profile["name"], fontsize=10, fontweight="bold")
ax.grid(True, alpha=0.25, linestyle="--")
ax.tick_params(labelsize=8)
fig.tight_layout(pad=0.5)
buf = io.BytesIO()
plt.savefig(buf, format="png", dpi=110)
plt.close(fig)
buf.seek(0)
return "data:image/png;base64," + base64.b64encode(buf.read()).decode()
def _make_lift_curve(polar: dict, aoa_deg: float, cl_operating: float) -> str:
alphas = polar["alphas"]
cls = polar["cls"]
alpha_stall = polar["alpha_stall"]
fig, ax = plt.subplots(figsize=(5, 3.5))
ax.plot(alphas, cls, color="#1a6bbf", linewidth=2)
ax.axhline(0, color="#888", linewidth=0.6, alpha=0.5)
ax.axvline(alpha_stall, color="#c0392b", linestyle="--", linewidth=1.2,
alpha=0.85, label=f"Stall {alpha_stall:.1f}°")
ax.plot(aoa_deg, cl_operating, "o", color="#e67e22", markersize=8, zorder=5,
label=f"Op. point α={aoa_deg:.1f}°, CL={cl_operating:.2f}")
ax.set_xlabel("Angle of attack (°)", fontsize=9)
ax.set_ylabel("CL", fontsize=9)
ax.set_title("Lift Curve", fontsize=10, fontweight="bold")
ax.legend(fontsize=8, loc="upper left")
ax.grid(True, alpha=0.25, linestyle="--")
ax.tick_params(labelsize=8)
fig.tight_layout(pad=0.5)
buf = io.BytesIO()
plt.savefig(buf, format="png", dpi=110)
plt.close(fig)
buf.seek(0)
return "data:image/png;base64," + base64.b64encode(buf.read()).decode()
def _make_drag_polar(polar: dict, cl_operating: float, cd_operating) -> str:
cls = polar["cls"]
cds = polar["cds"]
fig, ax = plt.subplots(figsize=(4.5, 3.5))
ax.plot(cds, cls, color="#1a6bbf", linewidth=2)
if cd_operating is not None:
ax.plot(cd_operating, cl_operating, "o", color="#e67e22", markersize=8, zorder=5,
label=f"Op. point\nCD={cd_operating:.4f}")
ax.legend(fontsize=8, loc="upper left")
ax.set_xlabel("CD (profile drag)", fontsize=9)
ax.set_ylabel("CL", fontsize=9)
ax.set_title("Drag Polar", fontsize=10, fontweight="bold")
ax.grid(True, alpha=0.25, linestyle="--")
ax.tick_params(labelsize=8)
fig.tight_layout(pad=0.5)
buf = io.BytesIO()
plt.savefig(buf, format="png", dpi=110)
plt.close(fig)
buf.seek(0)
return "data:image/png;base64," + base64.b64encode(buf.read()).decode()
# ---------------------------------------------------------------------------
# CFD (OpenFOAM via Docker)
# ---------------------------------------------------------------------------
@app.route("/api/cfd", methods=["POST"])
def start_cfd():
data = request.get_json(force=True)
required = ["coords", "chord_mm", "span_mm", "aoa_deg", "v_ms", "rho", "foil", "resolution"]
for field in required:
if field not in data:
return jsonify({"error": f"Missing field: {field}"}), 400
try:
resolution = int(data["resolution"])
assert 1 <= resolution <= 3
except (ValueError, AssertionError):
return jsonify({"error": "resolution must be 1, 2, or 3"}), 400
# Docker pre-flight — run whenever native OpenFOAM is not installed
from foam_runner import _OPENFOAM_AVAILABLE as _foam_native
if not _foam_native:
try:
dr = subprocess.run(["docker", "info"], capture_output=True, timeout=6)
if dr.returncode != 0:
return jsonify({"error": "Docker is not running. Start Docker and try again."}), 503
except FileNotFoundError:
return jsonify({"error": "Docker not found and OpenFOAM is not installed natively."}), 503
except subprocess.TimeoutExpired:
return jsonify({"error": "Docker did not respond in time. Ensure Docker is running."}), 503
try:
from foam_runner import submit_job
except ImportError as e:
return jsonify({"error": f"foam_runner unavailable: {e}"}), 500
rho = float(data["rho"])
params = {
"coords": data["coords"],
"chord_m": float(data["chord_mm"]) / 1000.0,
"span_mm": float(data["span_mm"]),
"aoa_deg": float(data["aoa_deg"]),
"v_ms": float(data["v_ms"]),
"rho": rho,
"nu": _mu_from_rho(rho) / rho,
"foil": data["foil"],
"resolution": resolution,
"n_cores": int(data.get("n_cores", 6)),
"timeout_s": int(data.get("timeout_s", 1200)),
}
job_id = submit_job(params)
return jsonify({"job_id": job_id}), 202
@app.route("/api/cfd/<job_id>", methods=["DELETE"])
def cancel_cfd(job_id):
try:
from foam_runner import cancel_job
except ImportError as e:
return jsonify({"error": str(e)}), 500
ok = cancel_job(job_id)
if not ok:
return jsonify({"error": "Job not found or not running"}), 404
return jsonify({"status": "cancelled"}), 200
@app.route("/api/cfd/<job_id>", methods=["GET"])
def poll_cfd(job_id):
try:
from foam_runner import get_job
except ImportError as e:
return jsonify({"error": str(e)}), 500
job = get_job(job_id)
if job is None:
return jsonify({"error": "Job not found"}), 404
resp = {
"status": job["status"],
"progress": job["progress"],
"message": job.get("message", ""),
}
if job["status"] == "complete":
resp["result"] = job["result"]
elif job["status"] == "error":
resp["error_detail"] = job.get("error_detail", "Unknown error")
return jsonify(resp)
# ---------------------------------------------------------------------------
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=5000, threaded=True)