The Leapfrog Method
Last time we saw that the naive implementation of the finite difference approximation is unstable. We maintain that the central difference approximation yields better results than the forward difference approximation at the cost of additional memory.
The central difference approximates the time derivative using the difference between the previous and the next timestep.
We can reuse the same logic we applied to the Schrödinger equation the first time, to lead us to the an implementation of this modified approach. This typifies how understanding general principles and concepts is far more valuable than focusing on specific recipes. While some of the details are different, the concepts and thought processes that we employ now are very much the same as the first time. Also seeing the process again, with a slightly different application, will strengthen your understanding.
Substituting this approximation into the Schrödinger equation and isolating the term on the left gives our guiding equation for evolving the wave function forward in time.
Remember that the terms on the right all became texture reads in the code. This equation shows that we will now need to read, and therefore keep, wavefunction values for as well as . The wave function for becomes the wavefunction from plus adjustments from . This gives raise to the name of the method, leapfrog.
Once again we have to cast the equation in a form that can be manipulated in JavaScript and GLSL shaders, which lack an explicit complex data type. Start by breaking the wave function into real and imaginary parts.
We put this into the Schrödinger equation, then gather the real and imaginary parts into their own equations.
We reuse our earlier substitution for , which the clever reader will note uses elements of the wave function from and , and is already a central difference expression.
Now we have approximations of both the time and space derivatives in terms of values on our grid. We use these in the Schrödinger and write out equations for the evolution of the real and imaginary parts of the wave function.
For the real part of the wave function we have
and to evolve the imaginary component of the wave function in time