-
Notifications
You must be signed in to change notification settings - Fork 52
/
1D_Linear Convection_Fine_Mesh.py
59 lines (42 loc) · 1.84 KB
/
1D_Linear Convection_Fine_Mesh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import numpy #here we load numpy library that provides a bunch of useful matrix operations akin to MATLAB
from matplotlib import pyplot #2D plotting library that we will use to plot our results
lineSingle = '------------------------------------------------'
print("Solving 1D Linear Equation with Fine Mesh using Finite Difference Method\n")
nx = 800 #grip points
dx = 2 / (nx -1) #grid spacing
nt = 220 #number of timesteps
dt = 0.0025 #timestep size
c = 1 #wavespeed
#innitial condition
print(lineSingle)
print("Computing Innitial Solution...")
x = numpy.linspace(0, 2, nx)
u = numpy.ones(nx) #u = 1 across all grid points
for j in range(nx):
if 0.5 <= x[j] and x[j] <= 1:
u[j] = 2 #u = 2 from x = 0.5 to 1, Square Profile
else:
u[j] = 1
print("Printing Innitial Solution...")
print(lineSingle)
print(u)
pyplot.plot(numpy.linspace(0, 2, nx), u, label='Initial Solution');
#discritization
un = numpy.ones(nx)
for n in range(nt): #time marching
un = u.copy()
for i in range(1, nx): #Space marching
u[i] = un[i] - c*dt/dx*(un[i] - un[i - 1]) #Backward Differnece Scheme
print(lineSingle)
print("Printing Numerical Solution......")
print(lineSingle)
print(u)
print(lineSingle)
print("Plotting Innitial & Numerical Solution")
print(lineSingle)
pyplot.plot(numpy.linspace(0, 2, nx), u, label='Convected Solution');
pyplot.title('1D Linear Convecction')
pyplot.xlabel('Grid Space')
pyplot.ylabel('Velocity')
pyplot.legend()
pyplot.show()