39 lines
974 B
Python
39 lines
974 B
Python
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
import time, sys
|
|
|
|
"""
|
|
Simulation comments :
|
|
|
|
"""
|
|
|
|
nx = 51 # Number of grid points.
|
|
dx = 2 / (nx-1) # Distance between any pair of adjacent grid points.
|
|
nt = 20 # Number of timesteps we want to calculate.
|
|
dt = 0.025 # Amount of time each timesteps covers (delta t)
|
|
|
|
# Boundary conditions.
|
|
# Starting all values as 1 m/s.
|
|
u = np.ones(nx)
|
|
# Setting u = 2 between 0.5 and 1 as per out initial conditions.
|
|
u[int(0.5 / dx):int(1 / dx + 1)] = 2
|
|
|
|
# PLot initial conditions.
|
|
fig, ax = plt.subplots(nrows=1, ncols =2, sharey=True)
|
|
|
|
ax[0].plot(np.linspace(0,2,nx),u)
|
|
|
|
# Temporary array for the solutions.
|
|
un = np.ones(nx)
|
|
|
|
for n in range(nt): # Loop for values of n from 0 to nt.
|
|
un = u.copy() # Copy the existing values of u into un.
|
|
|
|
for i in range(1, nx): # Loop for values in u from 1 to nx.
|
|
u[i] = un[i] - u[i] * dt / dx * (un[i] - un[i-1])
|
|
|
|
# PLot the solution at t = t_final.
|
|
ax[1].plot(np.linspace(0,2,nx), u)
|
|
|
|
plt.show()
|