In [1]:
## 1 dimensional wave equation at constant speed
In [109]:
%pylab inline
import pylab as pl
import math
import numpy as np
import matplotlib.pyplot as plt
import csv
import zutil
import zutil.post as post
# the domain goes from [-40 to 40]
u = 1.5 ;
time = 5.0 ;
# define the number of grid points, domain length
x_max = 20 ;
length = 80.0 ;
# define the grid vector
grid_1D = np.zeros(x_max) ;
#calculate the spacing
dx = length / (x_max-1) ;
# calculate the time step
dt = dx/u
#populate with values the grid vector
grid_1D[0] = -40.0 ;
#grid_1D[x_max-1] = 40.0 ;
for i in range(1,x_max):
grid_1D[i] = grid_1D[i-1] + dx
print ('The vector which contains the spatial coordinates is = '), grid_1D
# compute the exact solution
sign_f = np.zeros(x_max)
# boundary conditions for the exact solution
sign_f[0] = 0.0 ;
sign_f[x_max-1] = 1.0 ;
# loop which calculates the exact values of the signum functions based on the sign(x)
for j in range(1,x_max-1):
if (grid_1D[j] - (1.5*time)) < 0.0:
sign_f[j] = 0.0
elif (grid_1D[j]-(1.5*time))==0.0:
sign_f[j] = 0.5
else:
sign_f[j] = 1.0
print ('The exact solution is = '),sign_f
# approximate the wave equation using UPWIND TECHNIQUE
# define Courant or CFL number....For this explicit scheme the Courant must be CFL < 1
print ('The time step delta dt is = '), dt
# calculate Courant
In [ ]: