-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsir_simulator.html
More file actions
232 lines (201 loc) · 7.01 KB
/
Copy pathsir_simulator.html
File metadata and controls
232 lines (201 loc) · 7.01 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>SIR Simulator</title>
<style>
body { font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif; margin: 16px; }
.row { display: grid; grid-template-columns: 120px 1fr 90px; gap: 10px; align-items: center; margin: 10px 0; }
input[type="range"] { width: 100%; }
.panel { max-width: 980px; margin: 0 auto; }
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin: 10px 0 16px; }
.card { border: 1px solid #ddd; border-radius: 10px; padding: 10px 12px; }
.big { font-size: 18px; font-weight: 650; }
canvas { width: 100%; height: 460px; border: 1px solid #ddd; border-radius: 10px; }
.hint { color:#444; font-size: 13px; margin-top: 6px; }
code { background:#f6f6f6; padding:2px 6px; border-radius:6px; }
</style>
</head>
<body>
<div class="panel">
<h2>SIR Simulator</h2>
<div class="row">
<div><label for="beta">β</label></div>
<input id="beta" type="range" min="0" max="0.01" step="0.0001" value="0.001">
<div id="betaVal" class="big"></div>
</div>
<div class="row">
<div><label for="gamma">γ</label></div>
<input id="gamma" type="range" min="0.01" max="1" step="0.01" value="0.2">
<div id="gammaVal" class="big"></div>
</div>
<div class="row">
<div><label for="S0">S0</label></div>
<input id="S0" type="range" min="0" max="1000" step="1" value="500">
<div id="S0Val" class="big"></div>
</div>
<div class="row">
<div><label for="I0">I0</label></div>
<input id="I0" type="range" min="1" max="50" step="1" value="1">
<div id="I0Val" class="big"></div>
</div>
<div class="row">
<div><label for="T">Days</label></div>
<input id="T" type="range" min="10" max="300" step="1" value="160">
<div id="TVal" class="big"></div>
</div>
<div class="stats">
<div class="card">
<div>R0 (≈ β·S0/γ)</div>
<div id="R0Val" class="big"></div>
<div class="hint">For your assignment’s parameters, disease spreads when R0 > 1.</div>
</div>
<div class="card">
<div>Spreads?</div>
<div id="spreadVal" class="big"></div>
<div class="hint">Based on whether I initially increases.</div>
</div>
<div class="card">
<div>Peak I (and day)</div>
<div id="peakVal" class="big"></div>
<div class="hint">Useful for “small outbreak” vs “epidemic”.</div>
</div>
</div>
<canvas id="plot" width="1100" height="460"></canvas>
<div class="hint">
Curves: <b>S</b> (blue), <b>I</b> (red), <b>R</b> (green). Values are counts (not proportions).
Model: <code>dS/dt=-βSI</code>, <code>dI/dt=βSI-γI</code>, <code>dR/dt=γI</code>.
</div>
</div>
<script>
const el = id => document.getElementById(id);
const sliders = ["beta","gamma","S0","I0","T"].map(el);
const labels = { beta:"betaVal", gamma:"gammaVal", S0:"S0Val", I0:"I0Val", T:"TVal" };
function fmt(x, digits=4){
const n = Number(x);
if (Math.abs(n) >= 100) return n.toFixed(0);
if (Math.abs(n) >= 10) return n.toFixed(2);
return n.toFixed(digits);
}
function simulate(beta, gamma, S0, I0, T){
const dt = 0.05; // time step (days)
const steps = Math.floor(T/dt) + 1;
let S = S0, I = I0, R = 0;
const t = new Array(steps);
const Ss = new Array(steps);
const Is = new Array(steps);
const Rs = new Array(steps);
for (let k=0; k<steps; k++){
const time = k*dt;
t[k]=time; Ss[k]=S; Is[k]=I; Rs[k]=R;
// Euler step
const dS = -beta * S * I;
const dI = beta * S * I - gamma * I;
const dR = gamma * I;
S = Math.max(0, S + dS*dt);
I = Math.max(0, I + dI*dt);
R = Math.max(0, R + dR*dt);
}
return {t,Ss,Is,Rs,dt};
}
function draw({t,Ss,Is,Rs}, T){
const canvas = el("plot");
const ctx = canvas.getContext("2d");
// layout
const W = canvas.width, H = canvas.height;
const padL=55, padR=20, padT=20, padB=40;
const x0=padL, y0=H-padB, x1=W-padR, y1=padT;
const plotW = x1-x0, plotH = y0-y1;
// clear
ctx.clearRect(0,0,W,H);
// max y
let ymax = 1;
for (let i=0;i<t.length;i++){
ymax = Math.max(ymax, Ss[i], Is[i], Rs[i]);
}
// axes
ctx.strokeStyle="#333";
ctx.lineWidth=1;
ctx.beginPath();
ctx.moveTo(x0,y1); ctx.lineTo(x0,y0); ctx.lineTo(x1,y0);
ctx.stroke();
// ticks
ctx.fillStyle="#111";
ctx.font="12px system-ui";
const xTicks = 8, yTicks = 5;
for (let i=0;i<=xTicks;i++){
const tx = x0 + (i/xTicks)*plotW;
const val = (i/xTicks)*T;
ctx.strokeStyle="#999";
ctx.beginPath(); ctx.moveTo(tx,y0); ctx.lineTo(tx,y0+5); ctx.stroke();
ctx.fillText(val.toFixed(0), tx-8, y0+20);
}
for (let j=0;j<=yTicks;j++){
const ty = y0 - (j/yTicks)*plotH;
const val = (j/yTicks)*ymax;
ctx.strokeStyle="#999";
ctx.beginPath(); ctx.moveTo(x0-5,ty); ctx.lineTo(x0,ty); ctx.stroke();
ctx.fillText(val.toFixed(0), 8, ty+4);
}
ctx.fillText("time (days)", (x0+x1)/2-35, H-10);
ctx.fillText("people", 10, 16);
function xy(i, series){
const x = x0 + (t[i]/T)*plotW;
const y = y0 - (series[i]/ymax)*plotH;
return [x,y];
}
function line(series, stroke){
ctx.strokeStyle=stroke;
ctx.lineWidth=5;
ctx.beginPath();
let [x,y]=xy(0,series);
ctx.moveTo(x,y);
for (let i=1;i<t.length;i++){
[x,y]=xy(i,series);
ctx.lineTo(x,y);
}
ctx.stroke();
}
// simple distinct colors
line(Ss, "#2b6cb0");
line(Is, "#c53030");
line(Rs, "#2f855a");
// legend
ctx.font="13px system-ui";
const lx = x1-140, ly=y1+10;
ctx.fillStyle="#2b6cb0"; ctx.fillText("S", lx, ly+10);
ctx.fillStyle="#c53030"; ctx.fillText("I", lx+30, ly+10);
ctx.fillStyle="#2f855a"; ctx.fillText("R", lx+60, ly+10);
}
function update(){
const beta = Number(el("beta").value);
const gamma = Number(el("gamma").value);
const S0 = Number(el("S0").value);
const I0 = Number(el("I0").value);
const T = Number(el("T").value);
el(labels.beta).textContent = fmt(beta,4);
el(labels.gamma).textContent = fmt(gamma,3);
el(labels.S0).textContent = fmt(S0,0);
el(labels.I0).textContent = fmt(I0,0);
el(labels.T).textContent = fmt(T,0);
const R0 = (beta*S0)/gamma;
el("R0Val").textContent = fmt(R0,3);
const sim = simulate(beta,gamma,S0,I0,T);
// determine spread: does I increase initially?
const inc = sim.Is[10] > sim.Is[0]; // ~0.5 days in
el("spreadVal").textContent = inc ? "YES" : "NO";
// peak I
let peakI = -1, peakIdx = 0;
for (let i=0;i<sim.Is.length;i++){
if (sim.Is[i] > peakI){ peakI = sim.Is[i]; peakIdx = i; }
}
const peakDay = sim.t[peakIdx];
el("peakVal").textContent = `${fmt(peakI,2)} (day ${fmt(peakDay,1)})`;
draw(sim, T);
}
sliders.forEach(s => s.addEventListener("input", update));
update();
</script>
</body>
</html>