# How write a integration loop in fortran, leapfrog scheme to solvind PDE (advection)?

I want to resolve numerically this equation using of difference finite method with Leapfrog Scheme $$\frac{\partial{u}}{\partial t}+ v \frac{\partial{u}}{\partial x}= 0$$

I'm trying to write a code that resolves that PDE wit $v=1$ in leapfrog scheme (http://www4.ncsu.edu/~zhilin/TEACHING/MA584/Chapter5_fd_fem.pdf page 7), this scheme uses two level of time, but, I don't know how to write it in my code

$$u_j^{n+1}=u_j^{n-1}-\frac{\Delta t}{\Delta x}\left[u_{j+1}^{n}-u_{j-1}^{n}\right]+\mathcal O(\Delta t^3,\Delta x^2)$$ So I guess this is how could be.

do l=1,Nt
t = t + dt
u_n_p_o=u_n_o
u_n_p=u_n


I calculated $u^{n-1}$ to using FTCS(centering difference) Scheme

do i=1,Nx-1
u_n_o(i)= u_n_p_o(i)- (dt/dx)*(u_n_p_o(i+1)- u_n_p_o(i-1))
end do


Actually u_n_o is correct ?

u_n_p_o(i)=u_n_o(i-1) !2do reciclado de variable

do i=1,Nx-1
u_n(i)= u_n_p_o(i)- (dt/dx)*(u_n_p(i+1)-u_n_p(i-1))
end do

u_e = amp * exp( - ( x - x0 - t)**2 / sigma**2 ) + 1e-20
call save1Ddata(Nx,t,x,u_e,'u_e',1)
call save1Ddata(Nx,t,x,u_n,'u_n',1)
call save1Ddata(Nx,t,x,u_n,'u_n_o',1)
end do


What are the features of Leapfrog? I know that in this case, it's no matter the Courant factor (because it is stable); however, the solution numerically dissipates in its amplitude. So how can you test your numeric solution?

• I do not entirely see what do you mean by leapfrog. From my knowledge (electromagnetics based and therefore Yee's leapfrog scheme to solve Maxwell equations), you have two quantities ($E$ and $H$ in Maxwell's) which you sample at different grids in time, usually, so-called, half-intervals. What is the equation you are solving and over what quantities are you intending to leapfrog over? – Anton Menshov Aug 28 '18 at 16:36
• Leapfrog is usually used as a time integrator and has very well-known properties for advection equations. Are you asking about those properties or if your code is correct (or how to check if your code is correct)? – Kyle Mandli Aug 28 '18 at 18:45
• @kyleMandii I'm asking both – PCat27 Aug 29 '18 at 3:58

There are a number of ways to check if a code is correct. The best way in this instance is to perform a convergence check on your code. This requires that you perform the same computation with smaller and smaller values of $\Delta x$ and $\Delta t$ and see if the expected rates of convergence are demonstrated. The usual way to go about this for PDEs is to establish a relationship between them, such as the Courant number. I have some course notes at https://github.com/mandli/numerical-methods-pdes that might be useful, in particular 7-IVP and 10-Hyperbolic, which contain some Python code snippets and describes issues with Leapfrog.