forked from ProbstenHias/diffusion2d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffusion2d.py
More file actions
72 lines (55 loc) · 1.95 KB
/
diffusion2d.py
File metadata and controls
72 lines (55 loc) · 1.95 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
"""
Solving the two-dimensional diffusion equation
Example acquired from https://scipython.com/book/chapter-7-matplotlib/examples/the-two-dimensional-diffusion-equation/
"""
from .output import create_plot, output_plots
import numpy as np
import matplotlib.pyplot as plt
# intervals in x-, y- directions, mm
dx = dy = 0.1
# Thermal diffusivity of steel, mm^2/s
D = 4.
def solve(dx=dx, dy=dy, D=D):
# plate size, mm
w = h = 10.
# Initial cold temperature of square domain
T_cold = 300
# Initial hot temperature of circular disc at the center
T_hot = 700
# Number of discrete mesh points in X and Y directions
nx, ny = int(w / dx), int(h / dy)
# Computing a stable time step
dx2, dy2 = dx * dx, dy * dy
dt = dx2 * dy2 / (2 * D * (dx2 + dy2))
print("dt = {}".format(dt))
u0 = T_cold * np.ones((nx, ny))
u = u0.copy()
# Initial conditions - circle of radius r centred at (cx,cy) (mm)
r, cx, cy = 2, 5, 5
r2 = r ** 2
for i in range(nx):
for j in range(ny):
p2 = (i * dx - cx) ** 2 + (j * dy - cy) ** 2
if p2 < r2:
u0[i, j] = T_hot
def do_timestep(u_nm1, u, D, dt, dx2, dy2):
# Propagate with forward-difference in time, central-difference in space
u[1:-1, 1:-1] = u_nm1[1:-1, 1:-1] + D * dt * (
(u_nm1[2:, 1:-1] - 2 * u_nm1[1:-1, 1:-1] + u_nm1[:-2, 1:-1]) / dx2
+ (u_nm1[1:-1, 2:] - 2 * u_nm1[1:-1, 1:-1] + u_nm1[1:-1, :-2]) / dy2)
u_nm1 = u.copy()
return u_nm1, u
# Number of timesteps
nsteps = 101
# Output 4 figures at these timesteps
n_output = [0, 10, 50, 100]
fig_counter = 0
fig = plt.figure()
# Time loop
for n in range(nsteps):
u0, u = do_timestep(u0, u, D, dt, dx2, dy2)
# Create figure
if n in n_output:
fig_counter += 1
im = create_plot(fig, u, T_cold, T_hot, dt, n, fig_counter)
output_plots(fig, im)