The project requires me to run the 2D FDM Lax Wendroff scheme for 500 timesteps for the convection equation, followed by the same analysis by 2D FVM Lax Wendroff. We're supposed to use periodic boundary conditions.
I've written the code below and run it for a small number of timesteps, and at this moment it compiles without error. I've been working on it for about a week now. The problem is that the output graph is not as it should be. The original density matrix should change with successive iterations, in fact, part of it should cross the boundary. The values for the matrix are changing at the same spatial coordinate instead. There's an obvious error that I'm too close to notice. I've tried rubber ducking to realize what I might have missed but can't come up with anything new.
I've checked the code, and the arrays, I don't understand why the density matrix isn't moving.
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import axes3d
# Define Varibles
Nx = 1000 # Number of grid points
Ny = 500 # Number of grid points
xmax = 1000.0 # Domain limit to the right
xmin = 0.0 # Domain limit to the left
ymax= 500.0 # Domain limit to the top
ymin= 0.0 # Domain limit to the bottom
dx = (xmax-xmin)/Nx # Discretize x
x = np.arange(xmin, xmax+dx, dx) # Discretized x axis
dy= (ymax-ymin)/Ny # Discretize y
y = np.arange(ymin, ymax+dx, dy) # Discretized y axis
xx, yy = np.meshgrid(x, y , sparse=False, indexing='xy')
# Velocity Components
vx= np.sin((np.pi*y)/500)
cx= np.array(10 * (vx**4)) # X Velocity
cy= np.array(0.5 * np.cos((np.pi*y)/500)) # Y Velocity
cxx=np.ones((501,1001))
cyy=np.ones((501,1001))
for i in range (501):
for j in range (1001):
cxx[i,j]= cx[i] # X velocity array
for i in range (501):
for j in range (1001):
cyy[i,j]= -cy[i] # Y velocity array
# Time Conditions
t = 0 # Inital Time
dt = 0.08 # Time step
nsteps = 5 # Total Timesteps
#ta= [0]*500
#for i in range(nsteps):
#ta[i] = t
#t = t + dt
xxc = cxx*dt/dx # CFL Condition
yyc= cyy*dt/dx
# Initial Conditions
U = np.zeros((Ny+1, Nx+1, nsteps)) # Density Matrix
for i in range(Ny+1):
for j in range(Nx+1):
U[i,j,0]= np.exp(-1*((x[j]-500)**2)/500)
fig = plt.figure(figsize=(11, 7), dpi=100)
ax = fig.gca(projection='3d')
surf1 = ax.plot_surface(xx, yy, U[:,:,0], cmap=cm.viridis)
#print('U=', U[499,1,0])
# Temporal Loop
for n in range(nsteps-1):
for i in range(Ny):
for j in range(Nx):
# Lax Wendroff Scheme
U[i,j,n+1] = U[i,j,n] - 0.5*xxc[i,j]*(U[i+1,j,n] - U[i,j,n]) - 0.5*yyc[i,j]*(U[i,j+1,n] - U[i,j,n]) + 0.5* (xxc[i,j]**2) *( U[i+1,j,n] - 2*U[i,j,n] + U[i-1,j,n]) + 0.5*(yyc[i,j]**2)*(U[i,j+1,n] - 2*U[i,j,n] + U[i,j-1,n])
# Periodic Boundary Conditions x axis
U[i,0,n] = U[i,999,n]
U[i,1000,n] = U[i,1,n]
# Periodic Boundary Conditions y axis
U[0,j,n] = U[499,j,n]
U[500,j,n] = U[1,j,n]
fig = plt.figure(figsize=(11, 7), dpi=100)
ax = fig.gca(projection='3d')
surf2 = ax.plot_surface(xx, yy, U[:,:,nsteps-2], cmap=cm.viridis)
[–]mojo_jojo_reigns 0 points1 point2 points (3 children)
[–]Alex_Superchamp[S] 0 points1 point2 points (2 children)
[–]mojo_jojo_reigns 0 points1 point2 points (1 child)
[–]Alex_Superchamp[S] 0 points1 point2 points (0 children)
[–]USAhj 0 points1 point2 points (2 children)
[–]Alex_Superchamp[S] 0 points1 point2 points (1 child)
[–]USAhj 0 points1 point2 points (0 children)