# The Cubic Nonlinear Schrödinger Equation
## Background

The cubic nonlinear Schrödinger equation occurs in a variety of areas, including, quantum mechanics, nonlinear optics and surface water waves. A general introduction can be found at http://en.wikipedia.org/wiki/Schrodinger_equation and http://en.wikipedia.org/wiki/Nonlinear_Schrodinger_equation. A mathematical introduction to Schrödinger equations can be found in Sulem and Sulem and Yang. In this section we will introduce the idea of operator splitting and then go on to explain how this can be applied to the nonlinear Schrödinger equation in one, two and three dimensions. In one dimension, one can show that the cubic nonlinear Schrödinger equation is subcritical, and hence one has solutions which exist for all time. In two dimensions, it is $H^{1}$ critical, and so solutions may exhibit blow-up of the $H^{1}$ norm, that is the integral of the square of the gradient of the solution can become infinite in finite time. Finally, in three dimensions, the nonlinear Schrödinger equation is $L^{2}$ supercritical, and so the integral of the square of the solution can also become infinite in finite time. For an introduction to norms and Hilbert spaces, see a textbook on partial differential equations or analysis, such as Evans, Linares and Ponce, Lieb and Loss or Renardy and Rogers. A question of interest is how this blow-up occurs and numerical simulations are often used to understand this; see Sulem and Sulem for examples of this. The cubic nonlinear Schrödinger equation is given by
$i\psi _{t}+\Delta \psi \pm |\psi |^{2}\psi =0,$
where $\psi$ is the wave function and $\Delta$ is the Laplacian operator, so in one dimension it is $\partial _{xx}$, in two dimensions, $\partial _{xx}+\partial _{yy}$ and in three dimensions it is $\partial _{xx}+\partial _{yy}+\partial _{zz}$. The $+$ corresponds to the focusing cubic nonlinear Schrödinger equation and the $-$ corresponds to the defocusing cubic nonlinear Schrödinger equation. This equation has many conserved quantities, including the “mass”,
$\int _{\Omega }|\psi |^{2}{\text{d}}^{n}\mathbf {x}$
and the “energy”,
$\int _{\Omega }{\frac {1}{2}}|\nabla \psi |^{2}\mp {\frac {1}{4}}|\psi |^{4}{\text{d}}^{n}\mathbf {x}$
where $n$ is the dimension and $\Omega$ is the domain of the solution. As explained by Klein, these two quantities can provide useful checks on the accuracy of numerically generated solutions.

## Splitting

We will consider a numerical method to solve this equation known as splitting. This method occurs in several applications, and is a useful numerical method when the equation can be split into two separate equations, each of which can either be solved exactly, or each part is best solved by a different numerical method. Introductions to splitting can be found in Holden et al., McLachlan and Quispel, Thalhammer, Shen, Tang and Wang, Weideman and Herbst and Yang, and also at http://en.wikipedia.org/wiki/Split-step_method. For those interested in a comparison of time stepping methods for the nonlinear Schrödinger equation, see Klein. To describe the basic idea of the method, we consider an example given in Holden et al., which is the ordinary differential equation,
    $u_{t}=u(u-1)$ with $u(t=0)=0.8$.         ( 1)
We can solve this equation relatively simply by separation of variables to find that
$u(t)={\frac {4}{4+\exp(t)}}.$
Now, an interesting observation is that we can also solve the equations $u_{t}=u^{2}$ and $u_{t}=-u$ individually. For the first we get that $u(t)={\frac {u(0)}{1-tu(0)}}$ and for the second we get that $u(t)=u(0)\exp(-t)$. The principle behind splitting is to solve these two separate equations alternately for short periods of time. We will describe Strang splitting, although there are other forms of splitting, such as Godunov splitting and also additive splittings. We will not describe these here, but refer you to the previously mentioned references, in particular Holden et al.. To understand how we can solve the differential equation using splitting, consider the linear ordinary differential equation
$u_{t}=u+2u$ with $u(0)=1$.
We can first solve $p_{t}=p$ for a time $\delta t/2$ and then using $q(0)=p(\delta t/2)$, we solve $q_{t}=2q$ also for a time $\delta t$ to get $q(\delta t)$ and finally solve $r_{t}=r$ for a time $\delta t/2$ with initial data $r(0)=q(\delta t)$. Thus in this case $p(\delta t)=\exp(\delta t/2)$, $q(\delta t)=p(\delta t/2)\exp(2\delta t)=\exp(5\delta t/2)$ and $u(\delta t)\approx r(\delta t/2)=q(\delta t)\exp(\delta t/2)=\exp(3\delta t)$, which in this case is the exact solution. One can perform a similar splitting for matrix differential equations. Consider solving $\mathbf {u} _{t}=(\mathbf {A} +\mathbf {B} )\mathbf {u}$, where $\mathbf {A}$ and $\mathbf {B}$ are $n\times n$ matrices, the exact solution is $\mathbf {u} =\exp \left((\mathbf {A} +\mathbf {B} )t\right)\mathbf {u} (t=0)$, and an approximate solution produced after one time step of splitting is $u(\delta t)\approx u(0)\exp(\mathbf {A} \delta t)\exp(\mathbf {B} \delta t)$, which is not in general equal to $u(t=0)\exp \left((\mathbf {A} +\mathbf {B} )\delta t\right)$ unless the matrices $\mathbf {A}$ and $\mathbf {B}$ commute, and so the error in doing splitting in this case is of the form $(\mathbf {A} \mathbf {B} -\mathbf {B} \mathbf {A} )\delta t$. Listing A uses Matlab to demonstrate how to do splitting for eq. 1 .

## Exercises

* Modify the Matlab code to calculate the error at time 1 for several different choices of timestep. Numerically verify that Strang splitting is second order accurate.
* Modify the Matlab code to use Godunov splitting where one solves $u1_{t}=u1$ for a time $\delta t$ and then using $u1(\delta t)$ as initial data solves $u2_{t}=2u2$ also for a time $\delta t$ to get the approximation to $u(\delta t)$. Calculate the error at time 1 for several different choices of timestep. Numerically verify that Godunov splitting is first order accurate.

## Serial

For the nonlinear Schrödinger equation
    $i\psi _{t}\pm |\psi |^{2}\psi +\Delta \psi =0$,         ( 2)
we first solve
    $i\psi _{t}+\Delta \psi =0$         ( 3)
exactly using the Fourier transform to get
$\psi (\delta {t}/2,\cdot )$.
We then solve
    $i\psi _{t}\pm |\psi |^{2}\psi =0$         ( 4)
with
$\psi (\delta {t}/2,\cdot )$
as initial data for a time step of $\delta t$. As explained by Klein and Thalhammer, this can be solved exactly in real space because in eq. 4 , $|\psi |^{2}$ is a conserved quantity at every point in space and time. To show this, let $\psi ^{*}$ denote the complex conjugate of $\psi$, so that
${\frac {\mathrm {d} |\psi |^{2}}{\mathrm {d} t}}=\psi ^{*}{\frac {\mathrm {d} \psi }{\mathrm {d} t}}+{\frac {\mathrm {d} \psi ^{*}}{\mathrm {d} t}}\psi =\psi ^{*}\left(\pm i|\psi |^{2}\psi \right)+\left(\pm i|\psi |^{2}\psi \right)^{*}\psi =0$.
Another half step using eq. 3 is then computed using the solution produced by solving eq. 4 to obtain the approximate solution at time $\delta t$. Example Matlab codes demonstrating splitting follow.

### Example Matlab and Python Programs for the Nonlinear Schrödinger Equation

The program in listing B computes an approximation to an explicitly known exact solution to the focusing nonlinear Schrödinger equation.

## Example One-Dimensional Fortran Program for the Nonlinear Schrödinger Equation

Before considering parallel programs, we need to understand how to write a Fortran code for the one-dimensional nonlinear Schrödinger equation. Below is an example Fortran program followed by a Matlab plotting script to visualize the results. In compiling the Fortran program a standard Fortran compiler and the FFTW library are required. Since the commands required for this are similar to those in the makefile for the heat equation, we do not include them here.

## Shared Memory Parallel: OpenMP

We recall that OpenMP is a set of compiler directives that can allow one to easily make a Fortran, C or C++ program run on a shared memory machine – that is a computer for which all compute processes can access the same globally addressed memory space. It allows for easy parallelization of serial programs which have already been written in one of the aforementioned languages.
We will demonstrate one form of parallelizm for the two dimensional nonlinear Schrödinger equation in which we will parallelize the loops using OpenMP commands, but will use the threaded FFTW library to parallelize the transforms for us. The example programs are in listing E . A second method to parallelize the loops and Fast Fourier transforms explicitly using OpenMP commands is outlined in the exercises.
The example assumes one is using Flux and has loaded environments for the GCC compiler as well as the GCC compiled version of FFTW. To use the Intel compiler to with this code, the OMP stack size needs to be explicitly set to be large enough. If one is using the PGI compilers instead of the GCC compilers, change the flag $-fopenmp$ to $-mp$ in the makefile.

## Exercises

* Download the example Matlab programs which accompany the pre-print by Klein, Muite and Roidot. Examine how the mass and energy for these Schrödinger like equations are computed. Add code to check conservation of mass and energy to the Matlab programs for the nonlinear Schrödinger equation.
* The Gross-Pitaevskii equation is given by $i\psi _{t}+|\psi |^{2}\psi +V(\mathbf {x} )\psi =0$ where we will take $V(\mathbf {x} )=\lVert \mathbf {x} \rVert _{l^{2}}^{2}=\sum _{k=1}^{N}x_{k}^{2}$ in which $N$ is the space dimension. Show that this equation can be solved by splitting it into and Be sure to explain how eqs. 5 , 6 are solved.
    $i\psi _{t}+\Delta \psi =0$         ( 5)
    $i\psi _{t}+|\psi |^{2}\psi +V(\mathbf {x} )\psi =0$.         ( 6)
* Modify the Matlab codes to solve the Gross-Pitaevskii equation in one, two and three dimensions.
* Modify the serial Fortran codes to solve the Gross-Pitaevskii equation in one, two and three dimensions.
* Listings J and K give an alternate method of parallelizing an OpenMP program. Make the program in listing G as efficient as possible and as similar to that in J , but without changing the parallelization strategy. Compare the speed of the two different programs. Try to vary the number of grid points and cores used. Which code is faster on your system? Why do you think this is?         ( J) An OpenMP Fortran program to solve the 2D nonlinear Schrödinger equation using splitting Code download !-------------------------------------------------------------------- ! ! ! PURPOSE ! ! This program solves nonlinear Schrodinger equation in 2 dimensions ! i*u_t+Es*|u|^2u+u_{xx}+u_{yy}=0 ! using a second order time spectral splitting scheme ! ! The boundary conditions are u(x=0,y)=u(2*Lx*\pi,y), ! u(x,y=0)=u(x,y=2*Ly*\pi) ! The initial condition is u=exp(-x^2-y^2) ! ! .. Parameters .. ! Nx = number of modes in x - power of 2 for FFT ! Ny = number of modes in y - power of 2 for FFT ! Nt = number of timesteps to take ! Tmax = maximum simulation time ! plotgap = number of timesteps between plots ! FFTW_IN_PLACE = value for FFTW input ! FFTW_MEASURE = value for FFTW input ! FFTW_EXHAUSTIVE = value for FFTW input ! FFTW_PATIENT = value for FFTW input ! FFTW_ESTIMATE = value for FFTW input ! FFTW_FORWARD = value for FFTW input ! FFTW_BACKWARD = value for FFTW input ! pi = 3.14159265358979323846264338327950288419716939937510d0 ! Lx = width of box in x direction ! Ly = width of box in y direction ! ES = +1 for focusing and -1 for defocusing ! .. Scalars .. ! i = loop counter in x direction ! j = loop counter in y direction ! n = loop counter for timesteps direction ! allocatestatus = error indicator during allocation ! start = variable to record start time of program ! finish = variable to record end time of program ! count_rate = variable for clock count rate ! planfx = Forward 1d fft plan in x ! planbx = Backward 1d fft plan in x ! planfy = Forward 1d fft plan in y ! planby = Backward 1d fft plan in y ! dt = timestep ! .. Arrays .. ! u = approximate solution ! v = Fourier transform of approximate solution ! unax = temporary field ! vnax = temporary field ! vnbx = temporary field ! vnay = temporary field ! vnby = temporary field ! potx = potential ! .. Vectors .. ! kx = fourier frequencies in x direction ! ky = fourier frequencies in y direction ! x = x locations ! y = y locations ! time = times at which save data ! name_config = array to store filename for data to be saved ! fftfx = array to setup x Fourier transform ! fftbx = array to setup x Fourier transform ! fftfy = array to setup y Fourier transform ! fftby = array to setup y Fourier transform ! ! REFERENCES ! ! ACKNOWLEDGEMENTS ! ! ACCURACY ! ! ERROR INDICATORS AND WARNINGS ! ! FURTHER COMMENTS ! Check that the initial iterate is consistent with the ! boundary conditions for the domain specified !-------------------------------------------------------------------- ! External routines required ! ! External libraries required ! FFTW3 -- Fast Fourier Transform in the West Library ! (http://www.fftw.org/) ! OpenMP library PROGRAM main USE omp_lib IMPLICIT NONE ! Declare variables INTEGER(kind=4), PARAMETER :: Nx=2**8 INTEGER(kind=4), PARAMETER :: Ny=2**8 INTEGER(kind=4), PARAMETER :: Nt=20 INTEGER(kind=4), PARAMETER :: plotgap=5 REAL(kind=8), PARAMETER :: & pi=3.14159265358979323846264338327950288419716939937510d0 REAL(kind=8), PARAMETER :: Lx=2.0d0 REAL(kind=8), PARAMETER :: Ly=2.0d0 REAL(kind=8), PARAMETER :: Es=0.0d0 REAL(kind=8) :: dt=0.10d0/Nt COMPLEX(kind=8), DIMENSION(:), ALLOCATABLE :: kx,ky REAL(kind=8), DIMENSION(:), ALLOCATABLE :: x,y COMPLEX(kind=8), DIMENSION(:,:), ALLOCATABLE:: unax,vnax,vnbx,potx COMPLEX(kind=8), DIMENSION(:,:), ALLOCATABLE:: vnay,vnby REAL(kind=8), DIMENSION(:), ALLOCATABLE :: time INTEGER(kind=4) :: i,j,k,n,allocatestatus INTEGER(kind=4) :: start, finish, count_rate INTEGER(kind=8), PARAMETER :: FFTW_IN_PLACE=8, FFTW_MEASURE=0,& FFTW_EXHAUSTIVE=8, FFTW_PATIENT=32,& FFTW_ESTIMATE=64 INTEGER(kind=8),PARAMETER :: FFTW_FORWARD=-1, FFTW_BACKWARD=1 COMPLEX(kind=8), DIMENSION(:), ALLOCATABLE :: fftfx,fftbx,fftfy,fftby INTEGER(kind=8) :: planfx,planbx,planfy,planby CHARACTER*100 :: name_config ALLOCATE(kx(1:Nx),ky(1:Nx),x(1:Nx),y(1:Nx),unax(1:Nx,1:Ny),& vnax(1:Nx,1:Ny),vnbx(1:Nx,1:Ny),potx(1:Nx,1:Ny),fftfx(1:Nx),& fftbx(1:Nx),fftfy(1:Nx),fftby(1:Nx),vnay(1:Ny,1:Nx),& vnby(1:Ny,1:Nx),time(1:1+Nt/plotgap),stat=allocatestatus) IF (allocatestatus .ne. 0) stop PRINT *,'allocated memory' ! set up ffts CALL dfftw_plan_dft_1d_(planfx,Nx,fftfx(1:Nx),fftbx(1:Nx),& FFTW_FORWARD,FFTW_ESTIMATE) CALL dfftw_plan_dft_1d_(planbx,Nx,fftbx(1:Nx),fftfx(1:Nx),& FFTW_BACKWARD,FFTW_ESTIMATE) CALL dfftw_plan_dft_1d_(planfy,Ny,fftfy(1:Ny),fftby(1:Ny),& FFTW_FORWARD,FFTW_ESTIMATE) CALL dfftw_plan_dft_1d_(planby,Ny,fftby(1:Ny),fftfy(1:Ny),& FFTW_BACKWARD,FFTW_ESTIMATE) PRINT *,'Setup FFTs' ! setup fourier frequencies !$OMP PARALLEL DO PRIVATE(i) SCHEDULE(static) DO i=1,1+Nx/2 kx(i)= cmplx(0.0d0,1.0d0)*REAL(i-1,kind(0d0))/Lx END DO !$OMP END PARALLEL DO kx(1+Nx/2)=0.0d0 !$OMP PARALLEL DO PRIVATE(i) SCHEDULE(static) DO i = 1,Nx/2 -1 kx(i+1+Nx/2)=-kx(1-i+Nx/2) END DO !$OMP END PARALLEL DO !$OMP PARALLEL DO PRIVATE(i) SCHEDULE(static) DO i=1,Nx x(i)=(-1.0d0+2.0d0*REAL(i-1,kind(0d0))/REAL(Nx,kind(0d0)) )*pi*Lx END DO !$OMP END PARALLEL DO !$OMP PARALLEL DO PRIVATE(j) SCHEDULE(static) DO j=1,1+Ny/2 ky(j)= cmplx(0.0d0,1.0d0)*REAL(j-1,kind(0d0))/Ly END DO !$OMP END PARALLEL DO ky(1+Ny/2)=0.0d0 !$OMP PARALLEL DO PRIVATE(j) SCHEDULE(static) DO j = 1,Ny/2 -1 ky(j+1+Ny/2)=-ky(1-j+Ny/2) END DO !$OMP END PARALLEL DO !$OMP PARALLEL DO PRIVATE(j) SCHEDULE(static) DO j=1,Ny y(j)=(-1.0d0+2.0d0*REAL(j-1,kind(0d0))/REAL(Ny,kind(0d0)) )*pi*Ly END DO !$OMP END PARALLEL DO PRINT *,'Setup grid and fourier frequencies' !$OMP PARALLEL DO PRIVATE(j) SCHEDULE(static) DO j=1,Ny DO i=1,Nx unax(i,j)=exp(-1.0d0*(x(i)**2 +y(j)**2)) END DO END DO !$OMP END PARALLEL DO name_config = 'uinitial.dat' OPEN(unit=11,FILE=name_config,status="UNKNOWN") REWIND(11) DO j=1,Ny DO i=1,Nx WRITE(11,*) abs(unax(i,j))**2 END DO END DO CLOSE(11) !$OMP PARALLEL DO PRIVATE(j) SCHEDULE(static) DO j=1,Ny DO i=1,Nx CALL dfftw_execute_dft_(planfx,unax(i,j),vnax(i,j)) END DO END DO !$OMP END PARALLEL DO vnay(1:Ny,1:Nx)=TRANSPOSE(vnax(1:Nx,1:Ny)) ! transform initial data and do first half time step !$OMP PARALLEL DO PRIVATE(i) SCHEDULE(static) DO i=1,Nx CALL dfftw_execute_dft_(planfy,vnay(1:Ny,i),vnby(1:Ny,i)) DO j=1,Ny vnby(j,i)=exp(0.5d0*dt*(kx(i)*kx(i) + ky(j)*ky(j))& *cmplx(0.0d0,1.0d0))*vnby(j,i) END DO CALL dfftw_execute_dft_(planby,vnby(j,i),vnay(j,i)) END DO !$OMP END PARALLEL DO PRINT *,'Got initial data, starting timestepping' time(1)=0.0d0 CALL system_clock(start,count_rate) DO n=1,Nt vnbx(1:Nx,1:Ny)=TRANSPOSE(vnay(1:Ny,1:Nx))/REAL(Ny,kind(0d0)) !$OMP PARALLEL DO PRIVATE(j) SCHEDULE(static) DO j=1,Ny CALL dfftw_execute_dft_(planbx,vnbx(1:Nx,j),unax(1:Nx,j)) DO i=1,Nx unax(i,j)=unax(1:Nx,j)/REAL(Nx,kind(0d0)) potx(i,j)=Es*unax(i,j)*conjg(unax(i,j)) unax(i,j)=exp(cmplx(0.0d0,-1.0d0)*dt*potx(i,j))& *unax(i,j) END DO CALL dfftw_execute_dft_(planfx,unax(1:Nx,j),vnax(1:Nx,j)) END DO !$OMP END PARALLEL DO vnby(1:Ny,1:Nx)=TRANSPOSE(vnax(1:Nx,1:Ny)) !$OMP PARALLEL DO PRIVATE(i) SCHEDULE(static) DO i=1,Nx CALL dfftw_execute_dft_(planfy,vnby(1:Ny,i),vnay(1:Ny,i)) DO j=1,Ny vnby(j,i)=exp(dt*(kx(i)*kx(i) + ky(j)*ky(j))& *cmplx(0.0d0,1.0d0))*vnay(j,i) END DO CALL dfftw_execute_dft_(planby,vnby(1:Ny,i),vnay(1:Ny,i)) END DO !$OMP END PARALLEL DO IF (mod(n,plotgap)==0) then time(1+n/plotgap)=n*dt PRINT *,'time',n*dt END IF END DO PRINT *,'Finished time stepping' CALL system_clock(finish,count_rate) PRINT*,'Program took ',REAL(finish-start)/REAL(count_rate),& 'for Time stepping' ! transform back final data and do another half time step vnbx(1:Nx,1:Ny)=transpose(vnay(1:Ny,1:Nx))/REAL(Ny,kind(0d0)) !$OMP PARALLEL DO PRIVATE(j) SCHEDULE(static) DO j=1,Ny CALL dfftw_execute_dft_(planbx,vnbx(1:Nx,j),unax(1:Nx,j)) unax(1:Nx,j)=unax(1:Nx,j)/REAL(Nx,kind(0d0)) potx(1:Nx,j)=Es*unax(1:Nx,j)*conjg(unax(1:Nx,j)) unax(1:Nx,j)=exp(cmplx(0,-1)*dt*potx(1:Nx,j))*unax(1:Nx,j) CALL dfftw_execute_dft_(planfx,unax(1:Nx,j),vnax(1:Nx,j)) END DO !$OMP END PARALLEL DO vnby(1:Ny,1:Nx)=TRANSPOSE(vnax(1:Nx,1:Ny)) !$OMP PARALLEL DO PRIVATE(i) SCHEDULE(static) DO i=1,Nx CALL dfftw_execute_dft_(planfy,vnby(1:Ny,i),vnay(1:Ny,i)) vnby(1:Ny,i)=exp(0.5d0*dt*(kx(i)*kx(i) + ky(1:Ny)*ky(1:Ny))& *cmplx(0,1))*vnay(1:Ny,i) CALL dfftw_execute_dft_(planby,vnby(1:Ny,i),vnay(1:Ny,i)) END DO !$OMP END PARALLEL DO vnbx(1:Nx,1:Ny)=TRANSPOSE(vnay(1:Ny,1:Nx))/REAL(Ny,kind(0d0)) !$OMP PARALLEL DO PRIVATE(j) SCHEDULE(static) DO j=1,Ny CALL dfftw_execute_dft_(planbx,vnbx(1:Nx,j),unax(1:Nx,j)) unax(1:Nx,j)=unax(1:Nx,j)/REAL(Nx,kind(0d0)) END DO !$OMP END PARALLEL DO name_config = 'ufinal.dat' OPEN(unit=11,FILE=name_config,status="UNKNOWN") REWIND(11) DO j=1,Ny DO i=1,Nx WRITE(11,*) abs(unax(i,j))**2 END DO END DO CLOSE(11) name_config = 'tdata.dat' OPEN(unit=11,FILE=name_config,status="UNKNOWN") REWIND(11) DO j=1,1+Nt/plotgap WRITE(11,*) time(j) END DO CLOSE(11) name_config = 'xcoord.dat' OPEN(unit=11,FILE=name_config,status="UNKNOWN") REWIND(11) DO i=1,Nx WRITE(11,*) x(i) END DO CLOSE(11) name_config = 'ycoord.dat' OPEN(unit=11,FILE=name_config,status="UNKNOWN") REWIND(11) DO j=1,Ny WRITE(11,*) y(j) END DO CLOSE(11) PRINT *,'Saved data' CALL dfftw_destroy_plan_(planbx) CALL dfftw_destroy_plan_(planfx) CALL dfftw_destroy_plan_(planby) CALL dfftw_destroy_plan_(planfy) CALL dfftw_cleanup_ DEALLOCATE(unax,vnax,vnbx,potx, vnay,vnby,stat=allocatestatus) IF (allocatestatus .ne. 0) STOP PRINT *,'Deallocated memory' PRINT *,'Program execution complete' END PROGRAM main         ( K) An example makefile for compiling the OpenMP program in listing J Code download #define the complier COMPILER = gfortran # compilation settings, optimization, precision, parallelization FLAGS = -O0 -fopenmp # libraries LIBS = -L/usr/local/lib -lfftw3 -lm # source list for main program SOURCES = NLSsplitting.f90 test: $(SOURCES) ${COMPILER} -o NLSsplitting $(FLAGS) $(SOURCES) $(LIBS) clean: rm *.o clobber: rm NLSsplitting The example assumes one is using Flux and has loaded environments for the intel compiler as well as the Intel compiled version of FFTW. If one is using the freely available GCC compilers instead of the Intel compilers, change the flag $-openmp$ to $-fopenmp$ in the makefile.
* Modify the OpenMP Fortran codes to solve the Gross-Pitaevskii equation in two and three dimensions.
* Some quantum hydrodynamic models for plasmas are very similar to the nonlinear Schrodinger equation and can also be numerically approximated using splitting methods. A model for a plasma used by Eliasson and Shukla is and where $\Psi$ is the effective wave function, $\phi$ the electrostatic potential and $D$ the dimension, typically 1,2 or 3. This equation can be solved in a similar manner to the Davey-Stewartson equations in Klein, Muite and Roidot. Specifically, first solve using the Fourier transform so that $P(\delta t)=\exp \left(-i\Delta \delta t\right)P(0)$ where $P(0)=\Psi (0)$. Then solve using the Fourier transform. Finally, solve using the fact that at each grid point $\phi -|Q|^{4/D}$ is a constant, so the solution is $Q(\delta t)=\exp \left[i\left(\phi -|\Phi |^{4/D}\right)\delta t\right]Q(0)$ with $Q(0)=P(\delta t)$ and $\Psi (\delta t)\approx Q(\delta t)$.
    $i\Psi _{t}+\Delta \Psi +\phi \Psi -|\Psi |^{4/D}\Psi =0$         ( 8)
    $\Delta \phi =|\Psi |^{2}-1$         ( 9)
    $iP_{t}+\Delta P=0$         ( 10)
    $\phi =\Delta ^{-1}\left(|P|^{2}-1\right)$         ( 11)
    $iQ_{t}+\phi Q-|Q|^{4/D}Q=0$         ( 12)
* The operator splitting method can be used for equations other than the nonlinear Schrödinger equation. Another equation for which operator splitting can be used is the complex Ginzburg-Landau equation ${\frac {\partial A}{\partial t}}=A+(1+i\alpha )\Delta A-(1+i\beta )|A|^{2}A,$ where $A$ is a complex function, typically of one, two or three variables. An example one dimensional code is provided in listing L , based on an earlier finite difference code by Blanes, Casa, Chartier and Miura, using the methods described in Blanes et al.. By using complex coefficients, Blanes et al. can create high order splitting methods for parabolic equations. Previous attempts to do this have failed since if only real coefficients are used, a backward step which is required for methods higher than second order leads to numerical instability. Modify the example code to solve the complex Ginzburg-Landau equation in one, two and then in three spatial dimensions. The linear part ${\frac {\partial A}{\partial t}}=A+(1+i\alpha )\Delta A$ can be solved explicitly using the Fourier transform. To solve the nonlinear part, ${\frac {\partial A}{\partial t}}=-(1+i\beta )|A|^{2}A$ consider ${\frac {\partial |A|^{2}}{\partial t}}={\frac {\partial A}{\partial t}}A^{*}+{\frac {\partial A^{*}}{\partial t}}A=2|A|^{4}$ and solve this exactly for $|A|^{2}$. To recover the phase, observe that ${\frac {\partial \log(A)}{\partial t}}=-(1+i\beta )|A|^{2}$ which can also be integrated explicitly since $|A|^{2}(t)$ is known.         ( L) A Matlab program which uses 16th order splitting to solve the cubic nonlinear Schrödinger equation Code download % A program to solve the nonlinear Schr\"{o}dinger equation using a % splitting method. The numerical solution is compared to an exact % solution. % S. Blanes, F. Casas, P. Chartier and A. Murua % "Optimized high-order splitting methods for some classes of parabolic % equations" % ArXiv pre-print 1102.1622v2 % Forthcoming Mathematics of Computation clear all; format compact; format short; set(0,'defaultaxesfontsize',30,'defaultaxeslinewidth',.7,... 'defaultlinelinewidth',6,'defaultpatchlinewidth',3.7,... 'defaultaxesfontweight','bold') % set up grid Lx = 20; % period 2*pi * L Nx = 16384; % number of harmonics Nt = 2000; % number of time slices dt = 0.25*pi/Nt;% time step U=zeros(Nx,Nt/10); method=3; % splitting method: 1 Strang, 2 CCDV10, 3 Blanes et al 2012 % initialise variables x = (2*pi/Nx)*(-Nx/2:Nx/2 -1)'*Lx; % x coordinate kx = 1i*[0:Nx/2-1 0 -Nx/2+1:-1]'/Lx; % wave vector % initial conditions t=0; tdata(1)=t; u=4*exp(1i*t)*(cosh(3*x)+3*exp(8*1i*t)*cosh(x))... ./(cosh(4*x)+4*cosh(2*x)+3*cos(8*t)); v=fft(u); figure(1); clf; plot(x,u);xlim([-2,2]); drawnow; U(:,1)=u; % mass ma = fft(abs(u).^2); ma0 = ma(1); if method==1, % % Strang-Splitting % s=2; a=[1;0]; b=[1/2;1/2]; % elseif method==2, % % Method of Castella, Chartier, Descombes and Vilmart % BIT Numerical Analysis vol 49 pp 487-508, 2009 % s=5; a=[1/4;1/4;1/4;1/4;0]; b=[1/10-1i/30;4/15+2*1i/15;4/15-1i/5;4/15+2*1i/15;1/10-1i/30]; % elseif method==3, % % Method of Blanes, Casas, Chartier and Murua 2012 % s=17; a=1/16*[1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;0]; b=[0.028920177910074098791 - 0.005936580835725746103*1i; 0.056654351383649876160 + 0.020841963949772627119*1i; 0.067258385822722143569 - 0.039386393748812362460*1i; 0.070333980553260772061 + 0.058952097930307840316*1i; 0.077095100838099173580 - 0.038247636602014810025*1i; 0.042022140317231098258 - 0.033116379859951038579*1i; 0.050147397749937784280 + 0.061283684958324249562*1i; 0.047750191909146143447 - 0.032332468814362628262*1i; 0.119636547031757819706 + 0.015883426044923736862*1i; 0.047750191909146143447 - 0.032332468814362628262*1i; 0.050147397749937784280 + 0.061283684958324249562*1i; 0.042022140317231098258 - 0.033116379859951038579*1i; 0.077095100838099173580 - 0.038247636602014810025*1i; 0.070333980553260772061 + 0.058952097930307840316*1i; 0.067258385822722143569 - 0.039386393748812362460*1i; 0.056654351383649876160 + 0.020841963949772627119*1i; 0.028920177910074098791 - 0.005936580835725746103*1i]; end; % solve pde and plot results for n =2:Nt+1 for m=1:(s-1) vna=exp(b(m)*1i*dt*kx.*kx).*v; una=ifft(vna); pot=(2*una.*conj(una)); unb=exp(-1i*a(m)*(-1)*dt*pot).*una; v=fft(unb); end v=exp(b(s)*1i*dt*kx.*kx).*v; u=ifft(v); t=(n-1)*dt; if (mod(n,10)==0) tdata(n/10)=t; u=ifft(v); U(:,n/10)=u; uexact=... 4*exp(1i*t)*(cosh(3*x)+3*exp(8*1i*t)*cosh(x))... ./(cosh(4*x)+4*cosh(2*x)+3*cos(8*t)); figure(1); clf; plot(x,abs(u).^2); ... xlim([-0.5,0.5]); title(num2str(t)); figure(2); clf; loglog(abs(v(1:Nx/2))); ... title('Fourier Coefficients'); figure(3); clf; plot(x,abs(u-uexact).^2); ... xlim([-0.5,0.5]); title('error'); drawnow; ma = fft(abs(u).^2); ma = ma(1); test = log10(abs(1-ma/ma0)) end end figure(4); clf; mesh(tdata(1:(n-1)/10),x,abs(U(:,1:(n-1)/10)).^2); xlim([0,t]);

## Distributed Memory Parallel: MPI

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.
Before creating a parallel MPI code using 2DECOMP&FFT, we will generate a serial Fortran code that uses splitting to solve the 3D nonlinear Schrodinger equation. Rather than using loop-based parallelization to do a sequence of one dimensional fast Fourier transforms, we will use FFTW's three dimensional FFT, so that the serial version and MPI parallel version have the same structure. The serial version is in listing E . This file can be compiled in a similar manner to that in Listing A of the chapter Fortran Programs.
In comparison to the previous programs, the program in listing E writes out its final data as a binary file. This is often significantly faster than writing out a text file, and the resulting file is usually much smaller in size. This is important when many such files are written and/or if individual files are large. Due to the formatting change, the binary file also needs to be read in slightly differently. The Matlab script in listing N shows how to do this.
We now modify the above code to use MPI and the library 2DECOMP&FFT. The library 2DECOMP&FFT hides most of the details of MPI although there are a few commands which it is useful for the user to understand. These commands are:
* USE mpi or INCLUDE 'mpif.h'
* MPI_INIT
* MPI_COMM_SIZE
* MPI_COMM_RANK
* MPI_FINALIZE
The program is listed in listing O , please compare this to the serial code in E . The library 2DECOMP&FFT does a domain decomposition of the arrays so that separate parts of the arrays are on separate processors. The library can also perform a Fourier transform on the arrays even though they are stored on different processors – the library does all the necessary message passing and transpositions required to perform the Fourier transform. It should be noted that the order of the entries in the arrays after the Fourier transform is not necessarily the same as the order used by FFTW. However, the correct ordering of the entries is returned by the structure decomp and so this structure is used to obtain starting and stopping entries for the loops. We assume that the library 2DECOMP&FFT has been installed in an appropriate location.
To compile the code modify the makefile in listing P appropriately for your system. Type make and then you should be ready to run it after it compiles. It is assumed that FFTW is the underlying one dimensional fast Fourier transform library called by 2DECOMP&FFT, so this should be changed if another library is used. Note that you will need to rebuild the program if you want to change the problem size or the runtime.

## Exercises

* The ASCII character set requires 7 bits per character and so at least 7 bits are required to store a digit between 0 and 9. A double precision number in IEEE arithmetic requires 64 bits to store a double precision number with approximately 15 decimal digits and approximately a 3 decimal digit exponent. How many bits are required to store a IEEE double precision number? Suppose a file has $10^{6}$ double precision numbers. What is the minimum size of the file if the numbers are stored as IEEE double precision numbers? What is the minimum size of the file if the numbers are stored as characters?
* Write an MPI code using 2DECOMP&FFT to solve the Gross-Pitaevskii equation in three dimensions.
* Learn to use either VisIt (https://wci.llnl.gov/codes/visit/) or Paraview (http://www.paraview.org/) and write a script to visualize two and three dimensional output in a manner that is similar to the Matlab codes.

## Notes

* ↑ Sulem and Sulem (1999)
* ↑ Yang (2010)
* ↑ Evans (2010)
* ↑ Linares and Ponce (2009)
* ↑ Lieb and Loss (2003)
* ↑ Rennardy and Rogers (2004)
* ↑ Sulem and Sulem (1989)
* ↑ To simplify the presentation, we primarily consider the focusing cubic nonlinear Schrödinger equation.
* ↑ Klein (2008)
* ↑ Holden et al (2011)
* ↑ McLachlan and Quispel (2002)
* ↑ Thalhammer
* ↑ Shen, Tang and Wang (2011)
* ↑ Weideman and Herbst (1986)
* ↑ Yang (2010)
* ↑ Klein (2008)
* ↑ Holden et al (2011)
* ↑ Holden et al (2011)
* ↑ That is$\mathbf {A} \mathbf {B} =\mathbf {B} \mathbf {A}$.
* ↑ One can derive this by using the series expansion of the exponential function, $\exp(\mathbf {A} t)=\sum _{n=0}^{\infty }{\frac {(\mathbf {A} t)^{n}}{n!}}$, and subtracting $\exp((\mathbf {A} +\mathbf {B} )\delta t)$ from $\exp(\mathbf {A} \delta t)\exp(\mathbf {B} \delta t).$
* ↑ Klein (2008)
* ↑ Thalhammer (2008)
* ↑ Klein, Muite and Roidot (2011)
* ↑ http://en.wikipedia.org/wiki/Gross\%E2\%80\%93Pitaevskii\_equation
* ↑ This question is due to a project by Joshua Kirschenheiter.
* ↑ Eliasson and Shukla (2009)
* ↑ Klein, Muite and Roidot (2011)
* ↑ This question is due to a project by Kohei Harada and Matt Warnez.
* ↑ Blanes et al
* ↑ Blanes et al

## References

Blanes, S.; Casas, F.; Chartier, P.; Murua, A. "Splitting methods with complex coefficients for some classes of evolution equations". Mathematics of Computation.
Shukla, P.K. (2009). "Nonlinear aspects of quantum plasma physics: Nanoplasmonics and nanostructures in dense plasmas". Plasma and Fusion Research: Review Articles. 4: 32.
Evans, L.C. (2010). Partial Differential Equations. American Mathematical Society.
Holden, H.; Karlsen, K.H.; Risebro, N.H.; Tao, T. (2011). "Operator splitting for the KdV equation". Mathematics of Computation. 80: 821–846.
Klein, C. (2008). "Fourth order time-stepping for low dispersion Korteweg-De Vries and nonlinear Schrödinger equations". Electronic Transactions on Numerical Analysis. 29: 116–135.
Klein, C.; Muite, B.K.; Roidot, K. (2011). "Numerical Study of Blowup in the Davey-Stewartson System". {{cite journal}}: Cite journal requires |journal= (help)
Klein, C.; Roidot, K. (2011). "Fourth order time-stepping for Kadomstev-Petviashvili and Davey-Stewartson Equations". SIAM Journal on Scientific Computation. 33: 3333–3356.
Loss, M. (2003). Analysis. American Mathematical Society.
Ponce, G. (2009). Introduction to Nonlinear Dispersive Equations. Springer.
McLachlan, R.I.; Quispel, G.R.W. (2002). "Splitting Methods". Acta Numerica. 11: 341–434.
Rogers, R.C. (2004). An Introduction to Partial Differential Equations. Springer.
Wang, L.-L. (2011). Spectral Methods: Algorithms, Analysis and Applications. Springer.
Sulem, P.L. (1999). The Nonlinear Schrödinger equation: Self-Focusing and Wave Collapse. Springer.
Thalhammer, M. Time-Splitting Spectral Methods for Nonlinear Schrödinger Equations (PDF). {{cite book}}: Cite has empty unknown parameter: |coauthors= (help)
Weideman, J.A.C.; Herbst, B.M. (1986). "Split-step methods for the solution of the nonlinear Schrödinger equation". SIAM Journal on Numerical Analysis. SIAM. 23 (3): 485–507.
Yang, J. (2010). Nonlinear Waves in Integrable and Nonintegrable Systems. SIAM.