-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
249 lines (200 loc) · 9.55 KB
/
Copy pathapp.py
File metadata and controls
249 lines (200 loc) · 9.55 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
"""
BEAMFORM — Backend (Flask)
===========================
تشغيل:
pip install flask flask-cors
python app.py
افتح المتصفح على: http://localhost:5000
"""
import json
import math
import os
import time
from pathlib import Path
from flask import Flask, jsonify, request, send_from_directory
from flask_cors import CORS
# ── مسارات المشروع ────────────────────────────────────────────
BASE_DIR = Path(__file__).parent # مكان app.py
FRONTEND_DIR = BASE_DIR / "frontend" # مجلد الفرونت اند
DATA_DIR = BASE_DIR / "data" # مجلد البيانات
SCENARIOS_FILE = DATA_DIR / "scenarios.json" # ملف السيناريوهات
DATA_DIR.mkdir(exist_ok=True)
app = Flask(__name__)
CORS(app)
# ═══════════════════════════════════════════════════════════════
# Helpers — Scenarios
# ═══════════════════════════════════════════════════════════════
def load_scenarios() -> dict:
try:
if SCENARIOS_FILE.exists():
return json.loads(SCENARIOS_FILE.read_text(encoding="utf-8"))
except Exception:
pass
return {}
def save_scenarios(data: dict):
SCENARIOS_FILE.write_text(
json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8"
)
# ═══════════════════════════════════════════════════════════════
# Helpers — Physics
# ═══════════════════════════════════════════════════════════════
def _bessel_i0(x: float) -> float:
s, t = 1.0, 1.0
for k in range(1, 21):
t *= (x / 2 / k) ** 2
s += t
return s
def _window(n: int, window_type: str, kaiser_beta: float) -> list:
N = n - 1 or 1
out = []
for i in range(n):
if window_type == "hanning":
w = 0.5 - 0.5 * math.cos(2 * math.pi * i / N)
elif window_type == "hamming":
w = 0.54 - 0.46 * math.cos(2 * math.pi * i / N)
elif window_type == "blackman":
w = (0.42
- 0.5 * math.cos(2 * math.pi * i / N)
+ 0.08 * math.cos(4 * math.pi * i / N))
elif window_type == "kaiser":
r = 2 * i / N - 1
w = _bessel_i0(kaiser_beta * math.sqrt(max(0.0, 1 - r**2))) / _bessel_i0(kaiser_beta)
else:
w = 1.0
out.append(w)
return out
def _beam_pattern(num_el, spacing, freq, steer=0.0,
win="hamming", beta=6.0, speed=3e8) -> list:
lam = speed / freq
d = spacing * lam
weights = _window(num_el, win, beta)
out = []
for i in range(360):
theta = (i / 360) * 2 * math.pi - math.pi
re = im = 0.0
for n in range(num_el):
ph = (2 * math.pi * d / lam) * n * (math.sin(theta) - math.sin(steer))
re += weights[n] * math.cos(ph)
im += weights[n] * math.sin(ph)
out.append(math.sqrt(re**2 + im**2) / num_el)
return out
def _steering_delays(positions, steer, speed=1540.0) -> list:
dx, dy = math.sin(steer), math.cos(steer)
projs = [p["x"] * dx + p["y"] * dy for p in positions]
mx = max(projs)
return [(mx - p) / speed for p in projs]
def _focusing_delays(positions, focal, speed=1540.0) -> list:
fx, fy = focal["x"], focal["y"]
dists = [math.hypot(p["x"] - fx, p["y"] - fy) for p in positions]
mx = max(dists)
return [(mx - d) / speed for d in dists]
# ═══════════════════════════════════════════════════════════════
# Frontend — بيخدم كل ملفات الـ frontend/
# ═══════════════════════════════════════════════════════════════
@app.route("/")
def root():
return send_from_directory(FRONTEND_DIR, "simulator.html")
@app.route("/<path:filename>")
def frontend(filename):
# أي مسار مش /api يروح للفرونت اند
if filename.startswith("api/"):
return jsonify({"error": "Not found"}), 404
return send_from_directory(FRONTEND_DIR, filename)
# ═══════════════════════════════════════════════════════════════
# API — Health
# ═══════════════════════════════════════════════════════════════
@app.route("/api/health")
def health():
return jsonify({"status": "ok", "time": time.time()})
# ═══════════════════════════════════════════════════════════════
# API — Physics
# ═══════════════════════════════════════════════════════════════
@app.route("/api/physics/beam-pattern", methods=["POST"])
def api_beam_pattern():
b = request.get_json(force=True)
try:
pattern = _beam_pattern(
num_el = int(b["numElements"]),
spacing = float(b["spacing"]),
freq = float(b["frequency"]),
steer = float(b.get("steerAngle", 0)),
win = str(b.get("windowType", "hamming")),
beta = float(b.get("kaiserBeta", 6)),
speed = float(b.get("waveSpeed", 3e8)),
)
return jsonify({"pattern": pattern})
except (KeyError, ValueError) as e:
return jsonify({"error": str(e)}), 400
@app.route("/api/physics/steering-delays", methods=["POST"])
def api_steering_delays():
b = request.get_json(force=True)
try:
delays = _steering_delays(
positions = b["elementPositions"],
steer = float(b["steerAngle"]),
speed = float(b.get("waveSpeed", 1540)),
)
return jsonify({"delays": delays})
except (KeyError, ValueError) as e:
return jsonify({"error": str(e)}), 400
@app.route("/api/physics/focusing-delays", methods=["POST"])
def api_focusing_delays():
b = request.get_json(force=True)
try:
delays = _focusing_delays(
positions = b["elementPositions"],
focal = b["focalPoint"],
speed = float(b.get("waveSpeed", 1540)),
)
return jsonify({"delays": delays})
except (KeyError, ValueError) as e:
return jsonify({"error": str(e)}), 400
@app.route("/api/physics/window")
def api_window():
try:
weights = _window(
n = int(request.args.get("numElements", 32)),
window_type = str(request.args.get("windowType", "hamming")),
kaiser_beta = float(request.args.get("kaiserBeta", 6)),
)
return jsonify({"weights": weights})
except ValueError as e:
return jsonify({"error": str(e)}), 400
# ═══════════════════════════════════════════════════════════════
# API — Scenarios
# ═══════════════════════════════════════════════════════════════
@app.route("/api/scenarios", methods=["GET"])
def scenarios_list():
return jsonify(load_scenarios())
@app.route("/api/scenarios/<key>", methods=["GET"])
def scenario_get(key):
data = load_scenarios()
if key not in data:
return jsonify({"error": "Not found"}), 404
return jsonify(data[key])
@app.route("/api/scenarios", methods=["POST"])
def scenario_save():
b = request.get_json(force=True)
key = b.get("key", "").strip()
scenario = b.get("scenario", {})
if not key or not scenario.get("mode") or not scenario.get("name"):
return jsonify({"error": "key و scenario.mode و scenario.name مطلوبين"}), 400
data = load_scenarios()
data[key] = {**scenario, "custom": True,
"savedAt": time.strftime("%Y-%m-%dT%H:%M:%SZ")}
save_scenarios(data)
return jsonify({"ok": True, "key": key})
@app.route("/api/scenarios/<key>", methods=["DELETE"])
def scenario_delete(key):
data = load_scenarios()
if key not in data:
return jsonify({"error": "Not found"}), 404
del data[key]
save_scenarios(data)
return jsonify({"ok": True})
# ═══════════════════════════════════════════════════════════════
# تشغيل
# ═══════════════════════════════════════════════════════════════
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=port, debug=True)