-
Notifications
You must be signed in to change notification settings - Fork 0
/
heat2d.py
69 lines (46 loc) · 1.45 KB
/
heat2d.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
"""
2D Heat equation using finite differences
"""
import numpy as np
import matplotlib.pyplot as plt
import sys
import math
import warnings
## ignore MatplotlibDeprecationWarning
warnings.filterwarnings("ignore",".*GUI is implemented.*")
# PHYSICAL PARAMETERS
K = 0.5 #Diffusion coefficient
Lx = 1.0 #Domain size x
Ly = 1.0 #Domain size y
Time = 0.2 #Integration time
S = 1.0 #Source term
# NUMERICAL PARAMETERS
NT = 2000 #Number of time steps
NX = 100 #Number of grid points in x
NY = 100 #Number of grid points in y
dt = Time/(NT*2) #Grid step (time)
dx = Lx/(NX-2) #Grid step in x (space)
dy = Ly/(NY-21) #Grid step in y (space)
if abs(dt) > (dx*dx)/(2.0):
print 'Warning: Instable numerical scheme->'
print 'dt (%s) Must be less than: %s '%(dt,(dx*dx)/(2.0))
sys.exit(1)
xx = np.linspace(0,Lx,NX)
yy = np.linspace(0,Ly,NY)
T = np.zeros((NX,NY))
RHS = np.zeros((NX,NY))
# Main loop
for n in range(0,NT):
RHS[1:-1,1:-1] = dt*K*( (T[:-2,1:-1]-2*T[1:-1,1:-1]+T[2:,1:-1])/(dx**2) \
+ (T[1:-1,:-2]-2*T[1:-1,1:-1]+T[1:-1,2:])/(dy**2) )
T[1:-1,1:-1] += (RHS[1:-1,1:-1]+dt*S)
# Plot every 100 time steps
if (n%100 == 0):
plotlabel = "t = %1.2f" %(n * dt)
plt.pcolormesh(xx,yy,T, shading='flat')
plt.title(plotlabel)
plt.axis('image')
plt.draw()
plt.pause(0.05)
print "Iterations End!"
plt.show()