-
Notifications
You must be signed in to change notification settings - Fork 0
/
pde2.py
212 lines (128 loc) · 3.62 KB
/
pde2.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import matplotlib.pyplot as plt
from matplotlib import pyplot, cm
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from scipy.integrate import solve_ivp
'''
Method of lines
'''
# Ex 1
'''
#input values
td = 0.835 #thermal diffusivity
dt = 0.1 #delta t in sec
dx = 2 #delta x in cm
tp = 12 #time period
sr = 10 #spatial range
ic = 0 #initial condition u(x,0)
lbc = 100 #left boundary condition u(0,t)
rbc = 50 #right boundary condition u(sr,t)
#process constants
eq = td*dt/dx**2
pk = 2*(1+eq)
mk = 2*(1-eq)
i = sr/dx+1
k = tp/dt+1
'''
# Ex 2
'''
#input values
td = 0.02 #thermal diffusivity
dt = 0.05 #delta t in sec
dx = 0.01 #delta x in cm
tp = 5.0 #time period
sr = 1.0 #spatial range
ic = 150 #initial condition u(x,0)
lbc = 100 #left boundary condition u(0,t)
rbc = 200 #right boundary condition u(sr,t)
#process constants
eq = td*dt/dx**2
pk = 2*(1+eq)
mk = 2*(1-eq)
i = sr/dx+1
k = tp/dt+1
'''
# Ex 3
'''
#input values
td = 1.0 #thermal diffusivity
dt = 0.01 #delta t in sec
dx = 0.02 #delta x in cm
tp = 1 #time period
sr = 1 #spatial range
ic = 0 #initial condition u(x,0)
lbc = 100 #left boundary condition u(0,t)
rbc = 0 #right boundary condition u(sr,t)
#process constants
eq = td*dt/dx**2
pk = 2*(1+eq)
mk = 2*(1-eq)
i = sr/dx+1
k = tp/dt+1
'''
# Ex 4
# Burgers' Eqn
#'''
#input values
td = 0.1 # 0.01 # #parameter
dt = 0.01 #delta t in sec
dx = 0.02 #delta x in cm
tp = 3 #time period
sr = 1 #spatial range
def ic(x):
return np.sin(np.pi*x) #initial condition u(x,0)
lbc = 0 #left boundary condition u(0,t)
rbc = 0 #right boundary condition u(sr,t)
#process constants
eq = td*dt/dx**2
#eq1 = ic*dt/dx
pk = 2*(1+eq)
mk = 2*(1-eq)
i = sr/dx+1
k = tp/dt+1
#'''
xx = np.linspace(0, sr, int(i))
tt2 = np.linspace(0, tp, int(k))
def fu(t, x):
deriv=[]
deriv.insert(0, 0)
for ii in range(1,int(i)-1):
# heat eqn
#deriv.insert(ii, eq * (x[ii + 1] - 2*x[ii] + x[ii - 1])/dt )
# burger eqn
deriv.insert(ii, eq * (x[ii + 1] - 2*x[ii] + x[ii - 1])/dt - \
0.5 * (x[ii]*dt/dx) * (x[ii + 1] - x[ii - 1])/dt)
deriv.insert(int(i)-1, 0)
return deriv
t = 0
x = np.zeros(int(i))
ss = 0
ii = 0
while ss< sr+dx:
#x[ii] = ic
x[ii] = ic(ss)
ss += dx
ii += 1
x[0] = lbc
x[-1] = rbc
sol = solve_ivp(fu, [t, tp], x,
method='Radau', t_eval=tt2, atol=1.e-6, rtol=1.e-6)
u = sol.y.T
fig = pyplot.figure()
ax = fig.add_subplot(111, projection = '3d')
X, T = np.meshgrid(xx,tt2)
#surf = ax.plot_surface(X, T, u, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
ax.plot_surface(X, T, u, cmap='jet')
ax.set_xlabel('X')
ax.set_ylabel('Y-time')
ax.set_zlabel('Z-Temp')
plt.show()
plt.figure()
for jj in range(0, int(k), int(k/5)):
plt.plot(xx, u[jj,:], label='t={0:1.2f}'.format(tt2[jj]))
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xlabel('X position')
plt.ylabel('Temperature')
plt.grid(True, linestyle='-.')
plt.subplots_adjust(top=0.89, right=0.77)
plt.show()