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


Populating the interactive namespace from numpy and matplotlib
The vector which contains the spatial coordinates is  =  [-40.         -35.78947368 -31.57894737 -27.36842105 -23.15789474
 -18.94736842 -14.73684211 -10.52631579  -6.31578947  -2.10526316
   2.10526316   6.31578947  10.52631579  14.73684211  18.94736842
  23.15789474  27.36842105  31.57894737  35.78947368  40.        ]
The exact solution is  =  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  1.  1.  1.  1.  1.
  1.  1.]
The time step delta dt is =   2.80701754386

In [ ]: