-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2dheateq.py
63 lines (52 loc) · 2.02 KB
/
2dheateq.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
import numpy as np
import matplotlib.pyplot as plt
class preprocessing:
max_time = 10000
grid_size = 100
def __init__(self, time):
self.time = time
# initial conditions
self.alpha = 1.5
self.del_x = 1
# boundary conditions
self.first_wall = 200.0
self.all_other_walls = 20.0
def make_arr(self):
arr = np.zeros((self.time, self.grid_size, self.grid_size))
arr[:, self.grid_size - 1:, :] = self.first_wall
arr[:, :, 0] = self.all_other_walls
arr[:, :, -1] = self.all_other_walls
return arr
# using finite difference method
class calc:
def __init__(self, time):
self.inst = preprocessing(time)
self.del_t = ((self.inst.del_x) ** 2) / (5 * self.inst.alpha)
def value_calc(self):
mat = self.inst.make_arr()
for i in range(0, self.inst.time - 1):
for j in range(1, self.inst.grid_size - 1, self.inst.del_x):
for k in range(1, self.inst.grid_size - 1, self.inst.del_x):
mat[i + 1, j, k] = ((self.inst.alpha * self.del_t) / (self.inst.del_x ** 2) )* (
mat[i, j + 1, k] + mat[i, j - 1, k] + mat[i, j, k + 1] + mat[i, j, k - 1] - 4 * mat[
i, j, k]) + mat[i, j, k]
return mat
inp = int(input(f"Enter time less than {preprocessing.max_time} and greater than 0:"))
def graph(k,t):
plt.clf()
plt.title("Heat Equation graph")
plt.xlabel("x")
plt.ylabel("y")
a=preprocessing(t)
plt.pcolormesh(obj.value_calc()[k], cmap=plt.cm.jet, vmin=0, vmax=a.first_wall)
plt.colorbar()
plt.show()
if inp>0 and inp<preprocessing.max_time:
obj = calc(inp)
inp1=int(input("Enter an instance on which you want to view the graph(must be less than the time entered earlier):"))
if inp1<inp and inp1>0:
graph(inp1,inp)
else:
print("Invalid value for instance")
else:
print("Invalid value for time")