Parallel Spectral Numerical Methods/The Klein-Gordon Equation

= The Klein-Gordon Equation =

Background
The focusing/defocusing nonlinear Klein-Gordon equation describes the evolution of a possible complex scalar field $$u$$ according to,

where $$+$$ is the focusing case and $$-$$ the defocusing case in a similar manner to the nonlinear Schrödinger equation. Blow up of three dimensional radially symmetric real solutions to this equation have recently been numerically studied by Donninger and Schlag. Two dimensional simulations of the Klein-Gordon equation can be found in Yang. The linear Klein-Gordon equation occurs as a modification of the linear Schrödinger equation that is consistent with special relativity, see for example Landau or Grenier. At the present time, there have been no numerical studies of blow up of solutions to this equation without the assumption of radial symmetry. This equation has generated a large mathematical literature and is still poorly understood. Most of this mathematical literature has concentrated on analyzing the equation on an infinite three dimensional space with initial data that either decays exponentially as one tends to infinity or is nonzero on a finite set of the domain. Here, we will simulate this equation in a periodic setting. Since this equation is a wave equation, it has a finite speed of propagation of information, much as a sound wave in air takes time to move from one point to another. Consequently for short time simulations, a simulation of a solution that is only nonzero on a finite part of the domain is similar to a simulation on an infinite domain. However, over long times, the solution can spread out and interact with itself on a periodic domain, whereas on an infinite domain, the interaction over long times is significantly reduced and the solution primarily spreads out. Understanding the interactions in a periodic setting is an interesting mathematical problem. The Klein-Gordon equation has a conserved energy given by

$$\int \frac{1}{2}\left( \frac{\partial u}{\partial t}\right)^2 + \frac{u^2}{2}+\frac{1}{2}\left| \nabla u \right|^2 \mp \frac{\left| u \right|^4}{4} \mathrm{d}\mathbf{x}$$.

The equation is also time reversible. For long time simulations, one wants to construct numerical methods that approximately conserve this energy and are also time reversible. When using Fourier spectral methods, we primarily need to ensure that the time discretization preserves these properties, since the spectral spatial discretization will typically automatically satisfy these properties. Following Donninger and Schlag, we use two schemes. First, an implicit-explicit time stepping scheme which is time reversible but only conserves the energy approximately and is given by

and second, a fully implicit time stepping scheme with fixed point iteration

which conserves a discrete energy exactly

As before, the superscript $$n$$ denotes the time step and $$k$$ denotes the iterate in the fixed point iteration scheme. Iterations are stopped once the difference between two successive iterates falls below a certain tolerance.

Matlab and Python Programs
Listings $$, $$, $$ and $$ demonstrate Matlab implementations of these time stepping schemes. In one dimension, the Klein-Gordon equation has easily computable exact solutions, (see for example Nakanishi and Schlag ) which can be used to test the accuracy of the numerical schemes. These equations seem to display three possibilities for the behavior of solutions which are dependent on the initial conditions:


 * the solutions could disperse or thermalize, that is a given localized initial condition spreads out over the entire space
 * the solutions blow up or become infinite
 * a portion of the solution travels around as a localized particle while the rest of the solution disperses.

Since the equations are reversible, there is also the possibility that a solution which is initially distributed over the spatial domain localizes itself.

A Matlab program to solve the 1-dimensional Klein Gordon equation $$ using the time discretization in eq. $$ Code download

A Python program to solve the 1-dimensional Klein Gordon equation $$ using the time discretization in eq. $$. Code download

A Python program to solve the 1-dimensional Klein Gordon equation $$ using the time discretization in eq. $$. Code download

A Matlab program to solve the 2-dimensional Klein Gordon equation $$ using the time discretization in eq. $$Code download

A Python program to solve the 2-dimensional Klein Gordon equation $$ using the time discretization in eq. $$. Code download

A Python program to solve the 2-dimensional Klein Gordon equation $$ using the time discretization in eq. $$. Code download

A Matlab program to solve the 3-dimensional Klein Gordon equation $$ using the time discretization in eq. $$ Code download

A Python program to solve the 3-dimensional Klein Gordon equation, eq. $$, using the time discretization in eq. $$ Code download

A Two-Dimensional OpenMP Fortran Program
The programs that we have developed in Fortran have become rather long. Here we add subroutines to make the programs shorter and easier to maintain. Listing $$ is the main Fortran program which uses OpenMP to solve the 2D Klein-Gordon equation. Notice that by using subroutines, we have made the main program significantly shorter and easier to read. It is still not as simple to read as the Matlab program, but is significantly better than some of the previous Fortran programs. It is also much easier to maintain, and once the subroutines have been written and debugged, they may be reused in other programs. The only drawback in using too many subroutines is that one may encounter a slight decrease in performance due to the overhead of calling a subroutine and passing data to it. The subroutines are in listings $$, $$, $$, $$, $$, $$ and an example makefile is in listing $$. Finally listing $$ contains a Matlab program which produces pictures from the binary files that have been computed. One can then use another program to take the images and create a video.

A Fortran program to solve the 2D Klein-Gordon equation Code download

A Fortran subroutine to get the grid to solve the 2D Klein-Gordon equation on Code download

A Fortran program to save a field from the solution of the 2D Klein-Gordon equation Code download

A Fortran subroutine to update arrays when solving the 2D Klein-Gordon equation Code download

A Fortran subroutine to calculate the energy when solving the 2D Klein-Gordon equation Code download

A Fortran subroutine to save final results after solving the 2D Klein-Gordon equation Code download

An example makefile for compiling the OpenMP program in listing $$ Code download

A Matlab program to plot the fields produced by listing $$ Code download

A Three-Dimensional MPI Fortran Program using 2DECOMP&amp;FFT
For this section, we will use the library 2DECOMP&FFT available from http://www.2decomp.org/index.html. The website includes some examples which indicate how this library should be used, in particular the sample code at http://www.2decomp.org/case_study1.html is a very helpful indication of how one converts a code that uses FFTW to one that uses MPI and the aforementioned library.

We now give a program for the three-dimensional nonlinear Klein-Gordon equation. The program uses the same subroutine structure as the two-dimensional code. To make the program easy to reuse, the subroutine listed in listing $$ has been created to read an INPUTFILE which specifies the parameters to use for the program and so the program does not need to be recompiled every time it is run. To enable the program to scale better, the arrays which hold the Fourier frequencies and grid points have also been decomposed so that only the portions of the arrays used on each processor are created and stored on the processor. The program is listed in listing $$, to use this program simply compile it using MPI wrappers and gfortran (other compilers such as Intel fortran, PGI fortran or IBM XL fortran should also work), and then run it in the directory from which the INPUTFILE and data are stored. An example submission script is in listing $$, this requires an environment variable named "inputfile" to be set with the location of the INPUTFILE that specifies problem parameters, such as grid size and number of timesteps.

A further addition is a short postprocessing program to create header files to allow one to use the bov (brick of values) format that allows one to use the parallel visualization software VisIt. The program VisIt can be downloaded from https://wci.llnl.gov/codes/visit/home.html. This program also run on laptops, desktops as well as parallel computer clusters. Documentation on using VisIt is available here https://wci.llnl.gov/codes/visit/manuals.html and here http://www.visitusers.org/index.php?title=Main_Page. A short video tutorial on how to use VisIt remotely is available at http://cac.engin.umich.edu/resources/software/visit.html. An alternative program to create images is Paraview which can be downloaded from http://www.paraview.org/. To use Paraview, xdmf format header files can be used to get Paraview to read in the binary data output files. A short video tutorial on rendering with Paraview can be found at http://www.youtube.com/watch?v=rqCXFr73sfk&list=PL4FFYwZhtT0BlDBtjsSkQ4LcM1v9K24No. A Fortran program to create these header files is in listing $$.

A Fortran program to solve the 3D Klein-Gordon equation Code download

A Fortran subroutine to get the grid to solve the 3D Klein-Gordon equation on Code download

A Fortran subroutine to get the initial data to solve the 3D Klein-Gordon equation for Code download

A Fortran program to save a field from the solution of the 3D Klein-Gordon equation Code download

A Fortran subroutine to update arrays when solving the 3D Klein-Gordon equation Code download

A Fortran subroutine to calculate the energy when solving the 3D Klein-Gordon equation Code download

A Fortran subroutine to save final results after solving the 3D Klein-Gordon equation Code download

A Fortran subroutine to read in the parameters to use when solving the 3D Klein-Gordon equation Code download

An example makefile for compiling the MPI program in listing $$ Code download

A Fortran subroutine to create BOV (Brick of Values) header files after solving the 3D Klein-Gordon equation Code download

A Fortran subroutine to create XDMF header files after solving the 3D Klein-Gordon equation for helping Paraview and VisIt read in the data Code download

An example submission script for use on Flux. Change {your_username} appropriately. Code download

Exercises
then the two coupled equations are equivalent to the nonlinear Klein-Gordon equation
 * 1) Compare the accuracy of the implicit and semi-implicit time stepping schemes in eqs. $$ and $$. Which scheme produces the most accurate results in the least amount of real time?
 * 2) Write serial Fortran programs to solve the two- and three-dimensional Klein-Gordon equations using the fully implicit time stepping scheme in eq. $$.
 * 3) Write OpenMP parallel Fortran programs to solve the two- and three-dimensional Klein-Gordon equations using the fully implicit time stepping scheme in eq. $$.
 * 4) The MPI command MPI_BCAST is used in the subroutine readinputfile, listed in list $$. Look up this command (possibly using one of the references listed in the introduction to programming section) and explain what it does.
 * 5) Write an MPI parallel Fortran program to solve the two- and three-dimensional Klein-Gordon equations using the fully implicit time stepping scheme in eq. $$.
 * 6) Compare the results of fully three-dimensional simulations with periodic boundary conditions ($$\mathbb{T}^3$$) with analytical predictions for blow up on the entire real space ($$\mathbb{R}^3$$) summarized in Donninger and Schlag.
 * 7) Grenier explains that the linear Klein-Gordon equation can be written as two coupled Schrödinger equations. One can extend this formulation to the nonlinear Klein-Gordon equation. If we let
 * 1) Fill in the details to explain why eqs. $$ and $$ are equivalent to eq. $$. In particular show that by adding and subtracting the two equations in eqs. $$ and $$, we get $$i\frac{\partial}{\partial t}\left(\phi+\xi\right)= -\left(\phi-\xi\right)$$ and $$i\frac{\partial}{\partial t}\left(\phi-\xi\right)=-\Delta \left(\phi+\xi\right) - \left(\phi+\xi\right) \pm \left| \phi+\xi \right|^2\left(\phi+\xi\right)$$. Differentiating the first of these equations and substituting it into the second, then recalling that we defined $$u=\phi+\xi$$ in eq. $$ gives us the Klein-Gordon equation in eq. $$.
 * 2) Solve these two equations using either the implicit midpoint rule or the Crank Nicolson method.