// This returns an empty wavefunction of length n.
function wavefunction(n){
var psi = [];
for(var i = 0; i < n; i++){
psi[i]=[];
for(var j = 0; j < n; j++){
psi[i][j] = {real: 0, imag: 0}
}
}
return psi; // for example [{real:0, imag:0}, {real:0, imag:0}, ...]
}
// This takes the starting wavefunction, and returns the wavefunction a short time later.
function timestep(psi)
{
// This is how many units of time we're going to try to step forward.
var dt = 0.02;
psi = timestepV(psi, dt);
psi = timestepT(psi, dt);
return psi;
}
function timestepV(psi, dt)
{
var n = psi.length;
for(var i = 0; i < n; i++)
{
for(var j = 0; j < n; j++){
// This is the x that is in the equation above.
var x = (i-n/2)
var y = (j-n/2)
// This is the potential at this point.
var V = 1.0 / Math.sqrt(x*x + y*y + .01);
var theta = dt * V;
var c = Math.cos(theta);
var s = Math.sin(theta);
var re = psi[i][j].real * c - psi[i][j].imag * s;
var im = psi[i][j].imag * c + psi[i][j].real * s;
psi[i][j].real = re;
psi[i][j].imag = im;
}
}
return psi;
}
function timestepT(psi, dt){
psi = FFT.fft2d(psi);
var n = psi.length;
for(var i = 0; i < n; i++)
{
for(var j=0;j < n; j++)
{
var k = 2 * 3.1415927 * Math.min(i, n-i) / n;
var l = 2 * 3.1415927 * Math.min(j, n-j) / n;
var theta = (k * k + l*l) * dt;
var c = Math.cos(-theta);
var s = Math.sin(-theta);
var re = psi[i][j].real * c - psi[i][j].imag * s;
var im = psi[i][j].imag * c + psi[i][j].real * s;
psi[i][j].real = re;
psi[i][j].imag = im;
}
}
return FFT.fft2d(psi);
}
// This returns the initial state of the simulation.
function init(){
var n = 64;
var psi = wavefunction(n);
for(var i = 0; i < n; i++){
for(var j = 0; j < n; j++){
psi[i][j].real = Math.exp(-((i-20)*(i-20) + (j-20)*(j-20))/(5*5))*4;
psi[i][j].imag = 0;
}
}
return psi;
}
|
|
Quantum Mechanics for programmersA quick demo showing how to make simulations of simple quantum mechanics systems in javascript. |
© Hugo2015. Session @sessionNumber