-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
364 lines (310 loc) · 10.4 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body style="text-align:center; margin-left:auto; margin-right:auto;">
<canvas id="draw" width="1265" height="492"></canvas>
<script>
function SQR(x) { return x*x; }
class Complex {
constructor(re,im) {
this.re = re;
this.im = im;
}
}
class Vector {
constructor(x,y) {
this.x = x;
this.y = y;
}
}
const canvas = document.querySelector('#draw');
const ctx = canvas.getContext('2d');
// Gaussian //
// Physical constants //
const sigma = 25;
const energy = 31100.0;
const x0 = 0.3 * canvas.width;
// e**(ix) == Math.cos(x) + i * Math.sin(x)
function initialWavePacketAtTimeAndX(time, x) {
const tS = 0.5*SQR(sigma);
const t_tS = time/tS;
const sigmaT = sigma*Math.sqrt(1 + SQR(t_tS));
const p0 = Math.sqrt(energy);
const d = 2*p0*time - x + x0;
const d_sigmaT = d/sigmaT;
// const phase = p0*(x-x0) - energy*time;
// const abs = Math.exp(-0.5*SQR(x-x0))/SQR(sigma);
const phase = SQR(p0)*time - p0*(x - x0) - 0.5*t_tS*SQR(d_sigmaT) + 0.5*Math.atan(t_tS);
const abs = Math.exp(-0.5*SQR(d_sigmaT))/Math.sqrt(Math.sqrt(Math.PI)*sigmaT);
const psiExact = new Complex(
abs*Math.cos(phase),
abs*Math.cos(phase + Math.PI/2.0)
);
return psiExact;
}
function initialWavePacketAtX(x) {
const psiOld = initialWavePacketAtTimeAndX(-dt, x);
const psiNow = initialWavePacketAtTimeAndX(0, x);
const psiNew = initialWavePacketAtTimeAndX(dt, x);
const wave = {};
wave[tNow] = psiNow;
wave[tNew] = psiNew;
wave[tOld] = psiOld;
return wave;
}
// Physical Model //
// Physical Constants //
const dt_dx2 = 0.006;
const dx = 0.004;
const dt = dt_dx2 * SQR(dx);
// Wave state vars //
const N_UNIVERSE = 1265; //4000.0;
const psi = [];
let _secondPsi = [];
let tOld = 0;
let tNow = 1;
let tNew = 2;
let psiIntegral = null;
// Potential state vars //
let vLeft = 0;
let vCenter = 12000.0;
let vRight = 0;
let xLeft = 0.47*N_UNIVERSE*dx;
let xRight = 0.52*N_UNIVERSE*dx;
function expectationValue(complexValue) {
return SQR(complexValue.re) + SQR(complexValue.im);
}
function potential(x) {
if (x < xLeft/dx && x > 0) { return vLeft; }
if (x >= xLeft/dx && x < xRight/dx) { return vCenter; }
if (x >= xRight/dx && x < canvas.width/dx) { return vRight; }
}
function positionWaveFunction(x) {
return psi[x][tNow];
}
function momentumWavefunction(p) { // Doesn't work yet
let wve = psi.reduce((sum, psiAtX, x) => {
return new Complex(
sum.re + psiAtX[tNow].re * Math.cos(-x*p) + psiAtX[tNow].im * Math.sin(-x*p),
sum.im + psiAtX[tNow].im * Math.cos(-x*p) + psiAtX[tNow].re * Math.sin(-x*p)
);
}, new Complex(0,0));
return new Complex(
wve.re * 1.0/Math.sqrt(2*Math.PI),
wve.im * 1.0/Math.sqrt(2*Math.PI)
);
}
function momentumToPositionWaveFunction(phi, x) { // Doesn't work yet
let wve = phi.reduce((sum, phiAtX, p) => {
return new Complex(
sum.re + phiAtX.re * Math.cos(x*p) + phiAtX.im * Math.sin(x*p),
sum.im + phiAtX.im * Math.cos(x*p) + phiAtX.re * Math.sin(x*p)
);
}, new Complex(0,0));
return new Complex(
wve.re * 1.0/Math.sqrt(2*Math.PI),
wve.im * 1.0/Math.sqrt(2*Math.PI)
);
}
function confirmPositionRepresentation() {
phi = psi.map((p,x) => momentumWavefunction(x));
_secondPsi = phi.map((_, p) => momentumToPositionWaveFunction(phi, p));
}
function secondPsi(x) {
if (x in _secondPsi) { return _secondPsi[x]; }
console.log('could not find x in secondPsi');
return 0;
}
function evolve() {
for (let iter=0; iter<nDtPerDisplay; iter++) {
for (let x=1; x<N_UNIVERSE; x++) {
const laplace = new Complex(
2.0*dt_dx2 * (psi[x-1][tNow].re - 2.0*psi[x][tNow].re + psi[x+1][tNow].re),
2.0*dt_dx2 * (psi[x-1][tNow].im - 2.0*psi[x][tNow].im + psi[x+1][tNow].im)
);
psi[x][tNew].re = psi[x][tOld].re - laplace.im + 2.0*dt*potential(x)*psi[x][tNow].im;
psi[x][tNew].im = psi[x][tOld].im + laplace.re - 2.0*dt*potential(x)*psi[x][tNow].re;
}
const _tOld = tOld;
tOld = tNow;
tNow = tNew;
tNew = _tOld;
psiIntegral = null;
}
// confirmPositionRepresentation();
}
// console.log(initialWavePacketAtX);
for (let x=1; x<N_UNIVERSE; x++) {
psi[x] = initialWavePacketAtX(x);
_secondPsi[x] = Object.assign({}, psi[x]);
}
psi[0] = {
0: new Complex(0,0),
1: new Complex(0,0),
2: new Complex(0,0)
}
psi[N_UNIVERSE] = {
0: new Complex(0,0),
1: new Complex(0,0),
2: new Complex(0,0)
}
_secondPsi[0] = {
0: new Complex(0,0),
1: new Complex(0,0),
2: new Complex(0,0)
}
_secondPsi[N_UNIVERSE] = {
0: new Complex(0,0),
1: new Complex(0,0),
2: new Complex(0,0)
}
// Display Model //
// Display vars //
let animReqId = null;
const xScale = N_UNIVERSE/canvas.width; //3.0;
const psi2Scale = canvas.height * 30.0;
const nDtPerDisplay = 220;
const potentialScale = 0.0105;
function drawCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'lightgrey';
ctx.fillRect(0, 0, canvas.width, canvas.height);
drawWave(x => expectationValue(psi[x][tNow]), psi2Scale);
// drawWave(x => expectationValue(secondPsi(x)), 0.001*psi2Scale, "red");
// drawWave(x => expectationValue(momentumWavefunction(0.002*(x-400))), psi2Scale*0.0009);
drawPotentialEnergy();
drawMomentumAxes();
}
function drawMomentumAxes() {
ctx.beginPath();
ctx.moveTo(400, canvas.height);
ctx.lineTo(400, 0);
ctx.stroke();
ctx.strokeStyle = "yellow";
// const p0Loc = 176.35//0.002 + 400;
// console.log('p0Loc', p0Loc);
// ctx.beginPath();
// ctx.moveTo(p0Loc, canvas.height);
// ctx.lineTo(p0Loc, 0);
// ctx.stroke();
}
function drawWave(waveFn, scale, color="green") {
ctx.beginPath();
ctx.lineWidth = "2";
ctx.strokeStyle = color;
ctx.moveTo(0, canvasLocationY(waveFn(0), scale));
for (let x=1; x<=canvas.width; x++) {
const y = canvasLocationY(waveFn(x), scale);
ctx.lineTo(x, y);
}
ctx.stroke();
}
function drawPotentialEnergy(color="blue") {
ctx.beginPath();
ctx.lineWidth = "1";
ctx.strokeStyle = color;
// ctx.moveTo(0, canvasLocationY(energy,potentialScale));
// ctx.lineTo(canvas.width, canvasLocationY(energy,potentialScale));
// ctx.stroke();
ctx.moveTo(0, canvasLocationY(vLeft, potentialScale));
ctx.lineTo(xLeft/dx, canvasLocationY(vLeft, potentialScale));
ctx.lineTo(xLeft/dx, canvasLocationY(vCenter, potentialScale));
ctx.lineTo(xRight/dx, canvasLocationY(vCenter, potentialScale));
ctx.lineTo(xRight/dx, canvasLocationY(vRight, potentialScale));
ctx.lineTo(canvas.width, canvasLocationY(vRight, potentialScale));
ctx.stroke();
}
function canvasLocationY(value, scale) {
return canvas.height - scale*value;
}
function eventToPotentialValue(e) {
return (canvas.height - e.offsetY)/potentialScale;
}
// Controller //
function mainCycle() {
if (evolving) { evolve(); }
drawCanvas();
animReqId = requestAnimFrame(mainCycle);
}
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 100);
};
})();
let evolving = true;
let movingLeftPotential = false;
let movingCenterPotential = false;
let movingRightPotential = false;
let movingLeftPartition = false;
let movingRightPartition = false;
canvas.addEventListener('mousedown', onCanvasClick);
canvas.addEventListener('mousemove', draggingPotential);
canvas.addEventListener('mouseup', stopDrag);
canvas.addEventListener('mouseout', stopDrag);
function onCanvasClick(event) {
if (event.offsetY >= canvasLocationY(vLeft, potentialScale) - 15
&& event.offsetY <= canvasLocationY(vLeft, potentialScale) + 15
&& event.offsetX <= xLeft/dx) {
evolving = false;
movingLeftPotential = true;
}
if (event.offsetY >= canvasLocationY(vCenter, potentialScale) - 15
&& event.offsetY <= canvasLocationY(vCenter, potentialScale) + 15
&& event.offsetX >= xLeft/dx
&& event.offsetX <= xRight/dx) {
evolving = false;
movingCenterPotential = true;
}
if (event.offsetY >= canvasLocationY(vRight, potentialScale) - 15
&& event.offsetY <= canvasLocationY(vRight, potentialScale) + 15
&& event.offsetX >= xRight/dx) {
evolving = false;
movingRightPotential = true;
}
if (event.offsetY >= Math.min(canvasLocationY(vLeft, potentialScale), canvasLocationY(vCenter, potentialScale))
&& event.offsetY <= Math.max(canvasLocationY(vLeft, potentialScale), canvasLocationY(vCenter, potentialScale))
&& event.offsetX >= xLeft/dx - 15
&& event.offsetX <= xLeft/dx + 15) {
evolving = false;
movingLeftPartition = true;
}
if (event.offsetY >= Math.min(canvasLocationY(vCenter, potentialScale), canvasLocationY(vRight, potentialScale))
&& event.offsetY <= Math.max(canvasLocationY(vCenter, potentialScale), canvasLocationY(vRight, potentialScale))
&& event.offsetX >= xRight/dx - 15
&& event.offsetX <= xRight/dx + 15) {
evolving = false;
movingRightPartition = true;
}
}
function draggingPotential(event) {
if (movingLeftPotential) { vLeft = eventToPotentialValue(event); }
if (movingCenterPotential) { vCenter = eventToPotentialValue(event); }
if (movingRightPotential) { vRight = eventToPotentialValue(event); }
if (movingLeftPartition) { xLeft = event.offsetX*dx; }
if (movingRightPartition) { xRight = event.offsetX*dx; }
}
function stopDrag() {
evolving = true;
movingLeftPotential = false;
movingCenterPotential = false;
movingRightPotential = false;
movingLeftPartition = false;
movingRightPartition = false;
}
function eventLocationOnCanvas(event) {
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
return new Vector(x,y);
}
// confirmPositionRepresentation();
drawCanvas();
requestAnimFrame(mainCycle);
</script>
</body>
</html>