-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
58 lines (50 loc) · 1.53 KB
/
app.py
File metadata and controls
58 lines (50 loc) · 1.53 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
# app.py
from flask import Flask, jsonify, request, send_from_directory
import numpy as np
from swarm import MosquitoSwarm
app = Flask(__name__, static_folder=".", static_url_path="")
# Global simulation state
bounds = np.array([[-50, -50, -50], [50, 50, 50]])
swarm = MosquitoSwarm(
N=10000,
bounds=bounds,
sigma=0.5,
attractor=np.zeros(3),
attractor_strength=0.1,
repulsion_radius=1.0,
repulsion_strength=0.05,
)
@app.route("/")
def index():
return send_from_directory(".", "index.html")
@app.route("/init", methods=["POST"])
def init_swarm():
"""Re-initialize the swarm with posted parameters."""
data = request.json
global swarm
swarm = MosquitoSwarm(
N=data["N"],
bounds=bounds,
sigma=data["sigma"],
attractor=np.array(data.get("attractor", [0,0,0])),
attractor_strength=data["attractor_strength"],
repulsion_radius=data["repulsion_radius"],
repulsion_strength=data["repulsion_strength"],
)
return ("", 204)
@app.route("/step")
def step_swarm():
"""Advance the simulation by dt and return flat position list."""
dt = float(request.args.get("dt", 0.016))
swarm.step(dt)
# Return positions as a flat list
return jsonify(swarm.state().tolist())
@app.route("/step_multi")
def step_multi():
dt = float(request.args.get("dt", 0.016))
steps = int(request.args.get("steps", 1))
for _ in range(steps):
swarm.step(dt)
return jsonify(swarm.state().tolist())
if __name__ == "__main__":
app.run(port=8000)