I want to start exploring numerical integrators for differential equations and how the accuracy of various methods impacts the quantities measures from the resulting simulations. Many of the differential equations that arise in physics do not have known analytical solutions; numerical integration is the only way of simulating these systems.

My particular background is in molecular dynamics (MD). MD has its own unique requirements with a wide array of techniques used to evaluate the quality of a particular integrator. It can be easier, however, to evaluate integrators using a simple model with a known analytical solution before studying more complex models. As such, I’ll be using it to experiment with integrators and analyses I find in papers. Here, I want to describe the harmonic oscillator and some of its properties.

The potential energy function of the harmonic oscillator is given by:

where is the potential energy, is the position, is the mass, is the angular frequency.

We will simulate the harmonic oscillator in the microcanonical ensemble. This is the formulation seen in a first-year physics class where the total energy (potential plus kinetic) of the system is conserved. The dynamics are captured in the following second-order ordinary differential equation, otherwise known as Newton’s equations of motion:

The ordinary differential equation has two initial conditions: and .

The analytical solution of the ODE is given by:

where is the amplitude of the oscillations, is the time, and is the phase.

We can verify the solution by taking its derivatives

and substituting back into the ODE:

Using the equations for the position and and velocity and our initial conditions ( and , we can derive a system of equations for and and solve:

Note that there are multiple ways to solve for and . This particular solution assumes that .

We now have what we need to simulate a system with and initial conditions . I placed the code in a GitHub repository I’ll be using for these experiments. I simulated the system for 10 seconds using a timestep of 0.01 seconds.

The resulting phase diagram looks like this:

Phase Diagram

The phase diagram will become an important part of our error analyses. Since we have an analytical solution, we can compute the errors in the positions and velocities resulting from the numerical integration. Beyond local and global error analysis, MD requires that integrators are symplectic, or volume preserving in phase space, in order to ensure conservation of energy. The phase diagrams will give us a nice visual way of confirming that property.

I also computed the potential and kinetic energies at each timestep. The kinetic energy is given as

and the total energy is:

for some fixed .

If we plot the kinetic, potential, and total energy, we can see that the total energy (black) is conserved, but is transformed transformed between kinetic (cyan) and potential (magenta) energy over time:

Energy Plots

In MD, it is also common to evaluate integrators by measuring the error in the energies. I’ll explore that further in a future blog post.

For now, I’ve walked through the basic harmonic oscillator model. I’ve discussed some of the physical choices made for the dynamics of the model. And lastly, I briefly explored two properties of the model.