-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDE.py
More file actions
378 lines (305 loc) · 11.3 KB
/
PDE.py
File metadata and controls
378 lines (305 loc) · 11.3 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""
Numerical Methods Package: Partial Differential Equations
@author: Graeme Wiltrout
@advisor: T. Fogarty
"""
import numpy as np
import LinearAlgebra as M341
def FTCSE(a, b, d, f, g, h, nx, nt, alpha):
"""
Solves the one-dimensional parabolic PDE using the forward-time centered-space finite difference method.
Parameters:
a, b: float
The spatial domain [a, b].
d: float
The time interval [0, d].
f: function
The initial value function u(x, 0) = f(x).
g: function
The boundary condition at x = a, u(a, t) = g(t).
h: function
The boundary condition at x = b, u(b, t) = h(t).
nx: int
The number of spatial steps.
nt: int
The number of time steps.
alpha: float
The thermal diffusivity or similar coefficient in the PDE.
Returns:
u: 2D numpy array
The solution matrix with shape (nt+1, nx+1).
x: 1D numpy array
The spatial grid points.
t: 1D numpy array
The time grid points.
"""
# Spatial grid
x = np.linspace(a, b, nx+1)
dx = (b - a) / nx
# Time grid
t = np.linspace(0, d, nt+1)
dt = d / nt
# Stability condition
assert alpha * dt / dx**2 <= 0.5, "Stability condition not met, reduce dt or increase dx."
# Initialize solution matrix
u = np.zeros((nt+1, nx+1))
# Initial condition
for i in range(nx+1):
u[0, i] = f(x[i])
# Boundary conditions
for n in range(nt+1):
u[n, 0] = g(t[n])
u[n, nx] = h(t[n])
# Time-stepping loop
for n in range(0, nt):
for i in range(1, nx):
u[n+1, i] = u[n, i] + alpha * dt / dx**2 * (u[n, i+1] - 2 * u[n, i] + u[n, i-1])
return u, x, t
def BTCSI(a, b, d, f, g, h, nx, nt, alpha):
"""
Solves the one-dimensional parabolic PDE using the backward-time centered-space finite difference method.
Parameters:
a, b: float
The spatial domain [a, b].
d: float
The time interval [0, d].
f: function
The initial value function u(x, 0) = f(x).
g: function
The boundary condition at x = a, u(a, t) = g(t).
h: function
The boundary condition at x = b, u(b, t) = h(t).
nx: int
The number of spatial steps.
nt: int
The number of time steps.
alpha: float
The thermal diffusivity or similar coefficient in the PDE.
Returns:
u: 2D numpy array
The solution matrix with shape (nt+1, nx+1).
x: 1D numpy array
The spatial grid points.
t: 1D numpy array
The time grid points.
"""
# Spatial grid
x = np.linspace(a, b, nx+1)
dx = (b - a) / nx
# Time grid
t = np.linspace(0, d, nt+1)
dt = d / nt
# Coefficient
r = alpha * dt / dx**2
# Initialize solution matrix
u = np.zeros((nt+1, nx+1))
# Initial condition
for i in range(nx+1):
u[0, i] = f(x[i])
# Boundary conditions
for n in range(nt+1):
u[n, 0] = g(t[n])
u[n, nx] = h(t[n])
# Set up the coefficient matrix for the implicit method
A = np.zeros((nx-1, nx-1))
np.fill_diagonal(A, 1 + 2*r)
np.fill_diagonal(A[:-1, 1:], -r)
np.fill_diagonal(A[1:, :-1], -r)
# Time-stepping loop
for n in range(0, nt):
b = u[n, 1:-1]
b[0] += r * u[n+1, 0] # Include boundary condition g(t) at x = a
b[-1] += r * u[n+1, nx] # Include boundary condition h(t) at x = b
augmented_matrix = np.hstack((A, b.reshape(-1, 1)))
u[n+1, 1:-1] = M341.gaussian_elimination_spp(nx-1, augmented_matrix)
return u, x, t
def CrankNicholson(a, b, d, f, g, h, nx, nt, alpha):
"""
Solves the one-dimensional parabolic PDE using the Crank-Nicholson method.
Parameters:
a, b: float
The spatial domain [a, b].
d: float
The time interval [0, d].
f: function
The initial value function u(x, 0) = f(x).
g: function
The boundary condition at x = a, u(a, t) = g(t).
h: function
The boundary condition at x = b, u(b, t) = h(t).
nx: int
The number of spatial steps.
nt: int
The number of time steps.
alpha: float
The thermal diffusivity or similar coefficient in the PDE.
Returns:
u: 2D numpy array
The solution matrix with shape (nt+1, nx+1).
x: 1D numpy array
The spatial grid points.
t: 1D numpy array
The time grid points.
"""
# Spatial grid
x = np.linspace(a, b, nx+1)
dx = (b - a) / nx
# Time grid
t = np.linspace(0, d, nt+1)
dt = d / nt
# Coefficient
r = alpha * dt / (2 * dx**2)
# Initialize solution matrix
u = np.zeros((nt+1, nx+1))
# Initial condition
for i in range(nx+1):
u[0, i] = f(x[i])
# Boundary conditions
for n in range(nt+1):
u[n, 0] = g(t[n])
u[n, nx] = h(t[n])
# Set up the coefficient matrices
A = np.zeros((nx-1, nx-1))
B = np.zeros((nx-1, nx-1))
np.fill_diagonal(A, 1 + 2*r)
np.fill_diagonal(A[:-1, 1:], -r)
np.fill_diagonal(A[1:, :-1], -r)
np.fill_diagonal(B, 1 - 2*r)
np.fill_diagonal(B[:-1, 1:], r)
np.fill_diagonal(B[1:, :-1], r)
# Time-stepping loop
for n in range(0, nt):
b = np.dot(B, u[n, 1:-1])
b[0] += r * (u[n, 0] + u[n+1, 0]) # Include boundary condition g(t) at x = a
b[-1] += r * (u[n, nx] + u[n+1, nx]) # Include boundary condition h(t) at x = b
augmented_matrix = np.hstack((A, b.reshape(-1, 1)))
u[n+1, 1:-1] = M341.gaussian_elimination_spp(nx-1, augmented_matrix)
return u, x, t
def CFD_5pt(F, f, g, p, r, a, b, c, d, h, k):
# Grid points
x = np.arange(a, b + h, h)
y = np.arange(c, d + k, k)
nx, ny = len(x), len(y)
# Number of unknowns
N = (nx - 2) * (ny - 2)
# Initialize matrix and RHS vector for the linear system
A = np.zeros((N, N))
B = np.zeros(N)
# Helper function to map (i, j) to linear index
def index(i, j):
return (j - 1) * (nx - 2) + (i - 1)
# Fill the matrix A and vector B
for j in range(1, ny-1):
for i in range(1, nx-1):
idx = index(i, j)
A[idx, idx] = -2 * (1/h**2 + 1/k**2)
if i > 1:
A[idx, index(i-1, j)] = 1/h**2
else:
B[idx] -= f(y[j]) / h**2
if i < nx-2:
A[idx, index(i+1, j)] = 1/h**2
else:
B[idx] -= g(y[j]) / h**2
if j > 1:
A[idx, index(i, j-1)] = 1/k**2
else:
B[idx] -= p(x[i]) / k**2
if j < ny-2:
A[idx, index(i, j+1)] = 1/k**2
else:
B[idx] -= r(x[i]) / k**2
# Add F(x, y) term
B[idx] -= F(x[i], y[j])
# Solve the linear system using NumPy's linear solver
solution_vector = np.linalg.solve(A, B)
# Reshape solution vector back to grid
u = np.zeros((nx, ny))
for j in range(1, ny-1):
for i in range(1, nx-1):
u[i, j] = solution_vector[index(i, j)]
# Apply boundary conditions
u[0, :] = f(y) # u(a, y) = f(y)
u[-1, :] = g(y) # u(b, y) = g(y)
u[:, 0] = p(x) # u(x, c) = p(x)
u[:, -1] = r(x) # u(x, d) = r(x)
return u, x, y
def solve_vibrating_string(f, F, a, T, c2, dx, dt):
# Discretize the space and time domains
x = np.arange(0, a + dx, dx)
t = np.arange(0, T + dt, dt)
nx, nt = len(x), len(t)
# Stability condition
r = c2 * (dt / dx)**2
assert r <= 1, "Stability condition not met, reduce dt or increase dx."
# Initialize solution matrix
u = np.zeros((nt, nx))
# Apply initial conditions
u[0, :] = f(x)
u[1, 1:-1] = f(x[1:-1]) + dt * F(x[1:-1]) + 0.5 * r * (f(x[2:]) - 2 * f(x[1:-1]) + f(x[:-2]))
# Time-stepping loop
for n in range(1, nt - 1):
u[n+1, 1:-1] = 2 * (1 - r) * u[n, 1:-1] - u[n-1, 1:-1] + r * (u[n, 2:] + u[n, :-2])
return u, x, t
def solve_wave_2d(f, F, a, b, T, c2, dx, dy, dt):
# Discretize the space and time domains
x = np.arange(0, a + dx, dx)
y = np.arange(0, b + dy, dy)
t = np.arange(0, T + dt, dt)
nx, ny, nt = len(x), len(y), len(t)
# Stability condition (CFL condition)
r = c2 * dt**2 * (1/dx**2 + 1/dy**2)
assert r <= 1, "Stability condition not met, reduce dt, increase dx or dy."
# Initialize solution matrices
u = np.zeros((nt, nx, ny))
# Apply initial conditions
u[0, :, :] = f(x[:, None], y[None, :])
u[1, 1:-1, 1:-1] = (f(x[1:-1, None], y[None, 1:-1]) +
dt * F(x[1:-1, None], y[None, 1:-1]) +
0.5 * r * (
f(x[2:, None], y[None, 1:-1]) +
f(x[:-2, None], y[None, 1:-1]) +
f(x[1:-1, None], y[None, 2:]) +
f(x[1:-1, None], y[None, :-2]) -
4 * f(x[1:-1, None], y[None, 1:-1])
))
# Time-stepping loop
for n in range(1, nt-1):
u[n+1, 1:-1, 1:-1] = (2 * (1 - 2 * r) * u[n, 1:-1, 1:-1] -
u[n-1, 1:-1, 1:-1] +
r * (u[n, 2:, 1:-1] + u[n, :-2, 1:-1] +
u[n, 1:-1, 2:] + u[n, 1:-1, :-2]))
return u, x, y, t
def solve_wave_2d_polar(f, F, r_max, theta_max, T, c2, dr, dtheta, dt):
# Discretize the space and time domains
r = np.arange(0, r_max + dr, dr)
theta = np.arange(0, theta_max + dtheta, dtheta)
t = np.arange(0, T + dt, dt)
nr, ntheta, nt = len(r), len(theta), len(t)
# Stability condition (CFL condition)
r_factor = 1 / dr**2 + 1 / (r[1:]**2 * dtheta**2)
assert np.all(c2 * dt**2 * r_factor <= 1), "Stability condition not met, reduce dt, increase dr or dtheta."
# Initialize solution matrices
u = np.zeros((nt, nr, ntheta))
# Apply initial conditions
R, Theta = np.meshgrid(r, theta, indexing='ij')
u[0, :, :] = f(R, Theta)
u[1, 1:-1, :] = (f(R[1:-1, :], Theta[1:-1, :]) +
dt * F(R[1:-1, :], Theta[1:-1, :]) +
0.5 * c2 * dt**2 * (
(f(R[2:, :], Theta[2:, :]) - 2 * f(R[1:-1, :], Theta[1:-1, :]) + f(R[:-2, :], Theta[:-2, :])) / dr**2 +
(1 / R[1:-1, :] * (f(R[2:, :], Theta[2:, :]) - f(R[:-2, :], Theta[:-2, :]))) / (2 * dr) +
(1 / R[1:-1, :]**2 * (f(R[1:-1, 2:], Theta[1:-1, 2:]) - 2 * f(R[1:-1, 1:-1], Theta[1:-1, 1:-1]) + f(R[1:-1, :-2], Theta[1:-1, :-2]))) / dtheta**2))
# Apply boundary condition u(r = r_max, t) = 0
u[:, -1, :] = 0
# Time-stepping loop
for n in range(1, nt-1):
u[n+1, 1:-1, 1:-1] = (2 * (1 - c2 * dt**2 * (1 / dr**2 + 1 / (R[1:-1, 1:-1]**2 * dtheta**2))) * u[n, 1:-1, 1:-1] -
u[n-1, 1:-1, 1:-1] +
c2 * dt**2 * (
(u[n, 2:, 1:-1] - 2 * u[n, 1:-1, 1:-1] + u[n, :-2, 1:-1]) / dr**2 +
(1 / R[1:-1, 1:-1] * (u[n, 2:, 1:-1] - u[n, :-2, 1:-1])) / (2 * dr) +
(1 / R[1:-1, 1:-1]**2 * (u[n, 1:-1, 2:] - 2 * u[n, 1:-1, 1:-1] + u[n, 1:-1, :-2])) / dtheta**2))
# Apply boundary condition at each time step
u[n+1, -1, :] = 0
return u, r, theta, t