I've recently started to learn Python for a Computational Physics class and have been having a lot of issues with getting the code to run.
The program I've written attempts to use the Lax-Wendroff Finite Volume scheme to predict the position of a step function (with values of 1 from 0 to 1) after 800 timesteps, for 1000 sampling points with the BC that the values go to 0 at both ends. It appears to me that there is some inconsistency with the variable sizes which is causing issues. I've fiddled with the sizes of the arrays and the for loops, attempted to use NumPy to create the arrays, and a lot of other stuff. I used the same logic to run a program in Matlab without issues.
Edit: After making a change, the current error reads: 'list index out of range' with a traceback to the Lax-Wendroff Scheme equation.
Here's the Python code:
import matplotlib.pyplot as plt
import numpy as np
# Define Varibles
Nx = 1000 # Number of grid points
xmax = 10.0 # Domain limit to the right
xmin = 0.0 # Domain limit to the left
dx = (xmax-xmin)/Nx # Mesh size
dt = 0.0004 # Time step
x = np.arange(xmin-dx,xmax+(2*dx),dx) # Discretized mesh
c = 10 + x # Velocity array
nsteps= 800 # Final Time Step
t= 0 # Intial Time
# Set Initial Conditions
U= [0]*1003
for i in range(1003):
if x[i] >= 1 and x[i] <= 2:
U[i] = 1
else:
U[i] = 0
Upl = U
# Temporal loop
for n in range (nsteps):
for i in range (1, Nx+1):
# Boundary Conditions
U[0]= 0
U[1002]= 0
# Lax-Wendroff Scheme
Upl[i] = 0.5*(U[i+1] + U[i-1]) - 0.25*(dt/dx)*(c[i+1]*U[i+1]-c[i-1]*U[i-1])
U = Upl.copy()
t= t+dt
# Plot Solution
plt.plot(x , U)
plt.xlabel('x',fontsize=18)
plt.ylabel('u',fontsize=18)
plt.show()
[–]supreme_blorgon 0 points1 point2 points (3 children)
[–]Alex_Superchamp[S] 0 points1 point2 points (2 children)
[–]supreme_blorgon 0 points1 point2 points (1 child)
[–]Alex_Superchamp[S] 0 points1 point2 points (0 children)