-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6. Implicitscheme.py
57 lines (49 loc) · 1.33 KB
/
6. Implicitscheme.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
import numpy as np
import matplotlib.pyplot as plt
xmin = 0
xmax = 0.2 # length of the rod
N = 10 # number of nodes
dx = (xmax - xmin) / (N - 1) # grid size
x = np.linspace(xmin, xmax, N)
dt = 1e-3 # time step
tmax = 0.5 # max time to simulate
t = np.arange(0, tmax + dt, dt)
alpha = 0.05 # diffusion coefficient/material property
Tcurrent = np.ones(N) * 20 # Initial Conditions
Tb = 200 # Boundary condition, T Base
Ttip = 20 # Boundary condition, T Tip
r = alpha * dt / (2 * dx**2)
A = np.zeros((N, N))
for i in range(1, N-1):
A[i, i-1] = -r
A[i, i] = 1 + 2*r
A[i, i+1] = -r
A[0, 0] = 1
A[-1, -1] = 1
B = np.zeros((N, N))
for i in range(1, N-1):
B[i, i-1] = r
B[i, i] = 1 - 2*r
B[i, i+1] = r
B[0, 0] = 1
B[-1, -1] = 1
for j in range(1, len(t)): # loop for time step
T = Tcurrent.copy()
T[0] = Tb
T[-1] = Ttip
b = B @ T
b[0] = Tb
b[-1] = Ttip
Tcurrent = np.linalg.solve(A, b)
time = j * dt
plt.plot(x, Tcurrent)
plt.xlabel('Length of the rod [m]', fontsize=14)
plt.ylabel('Temperature [°C]', fontsize=14)
plt.gca().tick_params(axis='both', which='major', labelsize=16)
str1 = f'value of r = {r:.5f}'
str2 = f'Time value = {time:.3f} s'
plt.text(0.12, 150, str1, fontsize=14)
plt.text(0.12, 130, str2, fontsize=14)
plt.pause(0.01)
plt.clf()
plt.show()