Public
Edited
Nov 13, 2024
1 fork
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
x = nj.arange(numx).multiply(dx).subtract(xmax).reshape(numx,1)
Insert cell
Insert cell
k = fftfreq(x)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
V = {
let outside_right = nj.zeros(Math.round((numx-1)/2)) // this should make the potential free region start at 0
let barrier = nj.ones(Math.round(barrier_width/dx)).multiply(barrier_height)
let well = nj.zeros(Math.round(well_width/dx))
let outside_left = nj.ones(numx - Math.round(well_width/dx) - Math.round(barrier_width/dx) - Math.round((numx-1)/2) ).multiply(barrier_height)


let V = nj.concatenate(outside_left,well,barrier,outside_right)
return V.reshape(numx,1)

}
Insert cell
Insert cell
well_start = numx - Math.round(well_width/dx) - Math.round(barrier_width/dx) - Math.round((numx-1)/2)
Insert cell
well_end = well_start + Math.round(well_width/dx)
Insert cell
Insert cell
V_plot = V.divide(barrier_height)
Insert cell
Insert cell
expK = cexp(k.pow(2).multiply(-dt*(2*Math.PI)**2.0))
Insert cell
expV = cexp(V.multiply(-dt/2))
Insert cell
expABC = {
let absorb = nj.tanh(x.subtract(xmax).divide(w)).add(2).subtract(nj.tanh(x.add(xmax).divide(w))).multiply(-lambda*dt)
let expABC = nj.exp(absorb)
expABC = nj.concatenate(expABC, nj.zeros([numx,1]) ) // need to add 2nd column of zeros for the zero imaginary part
return expABC

}
Insert cell
Insert cell
Insert cell
Insert cell
x0 = - barrier_width - well_width/2
Insert cell
psi0 = {
let sigma2 = 2
let norm_constant = Math.pow(2 / (Math.PI * sigma2), 0.25);
let psi0 = nj.concatenate(nj.exp(x.subtract(x0).pow(2).multiply(-1).divide(sigma2)), nj.zeros([numx,1]) )
psi0 = cmultiply(psi0,cexp(x.multiply(p0)))

return psi0.multiply(norm_constant);
}
Insert cell
Insert cell
psi0_squared = cnorm2(psi0)
Insert cell
Insert cell
P_well0 = psi0_squared.slice([well_start,well_end]).sum()*dx
Insert cell
Insert cell
mutable psi=psi0 // This sets the initial condition for the simulation of psi

// Note, for more info on "mutable" see https://observablehq.com/@mbostock/mutable-values
Insert cell
Insert cell
mutable psi_plot = psi0
Insert cell
Insert cell
mutable P_well = nj.array([P_well0]) // this creates an array to store the evolving values of P_well
Insert cell
mutable time = nj.array([0]) // this creates an array to store the evolving time for plotting along with P_well
Insert cell
mutable i = 0 // this keeps track of the number of the elapsed timesteps
Insert cell
Insert cell
reset = function(){
mutable psi = psi0;
mutable psi_plot = psi0;
mutable time = nj.array([0])
mutable P_well = nj.array([P_well0])
mutable i =0
}
Insert cell
Insert cell
{
if(simulate===true) { // Only simulate when simulation checkbox has been checked
mutable psi = psi_dt(psi, expV, expK, expABC)
// Update the timestep
mutable i+=1
// Only update the psi_plot for plotting every n time-steps
if(i%n==0) {
mutable psi_plot = psi // Note this line will trigger psi_squared for the plots and also for the next step below
// Add new data to the P_well and time arrays by concatenating old array with new value
mutable P_well = nj.concatenate(mutable P_well,nj.array([psi_squared.slice([well_start,well_end]).sum()*dx]))
mutable time = nj.concatenate(mutable time, nj.array([i*dt]))
}


}
return "Indefinite simulation loop"
}
Insert cell
psi_squared = cnorm2(psi_plot)
Insert cell
Insert cell
graph.update([x,psi_squared,V_plot]) // Update the simulation plot when new values of psi_plot are available
Insert cell
graph_P_well.update([nj.stack([time, P_well], -1)]) // Update the simulation plot when new values of P_well are available
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more