How to Solve the Leapfrog Method (CTCS) with Sequential Calculation of Values in Both Time and Space?

129 views Asked by At

I am working on solving the undisturbed linear Burger's equation using the Leapfrog method. Specifically, I want to calculate the evolution of velocity at each spatial point over time, starting with an initial velocity distribution at time t=0.

Here's the initial velocity distribution I've set at time t=0:

# Define parameters and initial condition
L = 20                # Length of domain
dx = 0.1              # Spatial step size
N = int(L / dx)       # Number of grid points
dt = 0.01             # Time step size
u0 = 1                # Advection speed
T = 10                 # Final time

# Define initial condition
u = 0.5 * (1 + np.cos(2 * np.pi * np.arange(N) * dx / L))

Now, I'm using the FTBS (Forward Time-Backward Space) method to advance in time. Here's a snippet of my FTBS code:

def advection_FTBS(n):
    global u       # Make u a global variable
    # Apply FTBS scheme with periodic boundary conditions
    for i in range(N):
        # Check if point is at the left boundary
        if i == 0:
            # Use the right end point instead of i-1
            u[i] = u[i] - (u0*dt/dx) * (u[i] - u[N-1])
        # Check if point is at the right boundary
        elif i == N-1:
            # Use the left end point instead of i+1
            u[i] = u[i] - (u0*dt/dx) * (u[i] - u[0])
        # Otherwise, use the normal scheme
        else:
            u[i] = u[i] - (u0*dt/dx) * (u[i] - u[i-1])
    # Update the line object with new data
    line.set_data(np.arange(N) * dx, u)
    return line,

My question involves improving the FTBS method into Leap Frog method, which requires both temporal and spatial values simultaneously. I want to enhance the code so that it can compute both the preceding and succeeding values in both time and space.

0

There are 0 answers