【发布时间】:2017-05-25 00:19:48
【问题描述】:
我试图使用以下公式用 python 模拟铝中的 2D 热传递:
dT/dt = K*(d^2T/d^2x + d^2T/d^2y)
来源: https://www.youtube.com/watch?v=V00p-TgQML0
python 代码使用 dx = dy = 1 (mm) 但如果 dx 和 dy 变小我会得到一个溢出错误,我不知道如何避免。
import numpy as np
import matplotlib.pyplot as plt
from copy import deepcopy
from matplotlib.animation import FuncAnimation
import time
x = 11
y = 11
sd = 1
nx = x*sd
ny = y*sd
dx = 1/float(sd)
dy = dx
#Initial Temperature
T0 = 25
# Source: https://en.wikipedia.org/wiki/Thermal_diffusivity
# Aluminium Thermal diffusivity (mm**2/s)
K = 97
#Time
t0 = 0
te = 1
st = 1000
dt = 1/float(st)
#Iterations
N = (te - t0)*st
T = np.zeros(shape=(nx, ny))
T[:,:] = T0
# Dirichlet Condition
T[nx/2,ny/2] = 1000
MM = []
for n in range(N):
Tb = deepcopy(T)
for i in range(nx):
for j in range(ny):
#Neumann Boundary Conditions
#TOP
if i == 0:
T[i,j] = Tb[i+1,j]
#RIGHT
elif j == ny1-1 and i != 0 and i != nx1-1:
T[i,j] = Tb[i,j-1]
#BOTTOM
elif i == nx-1:
T[i,j] = Tb[i-1,j]
#LEFT
elif j==0 and i != 0 and i != nx-1:
T[i,j] = Tb[i,j+1]
else:
T[i,j] = Tb[i,j] + K*(dt/dx**2)*(Tb[i+1,j]+Tb[i-1,j]+Tb[i,j+1]+Tb[i,j-1]-4*Tb[i,j])
T[nx/2,ny/2] = 200
MM.append(T.copy())
fig = plt.figure()
pcm = plt.pcolormesh(MM[0])
plt.colorbar()
# Function called to update the graphic
def step(i):
if i >= len(MM): return 0
pcm.set_array(MM[i].ravel())
plt.title("Time: {0} s\n".format(i*dt))
plt.draw()
anim = FuncAnimation(fig, step, interval=3)
plt.show()
我为了重现溢出错误将sd的值更改为10(每个毫米将分为10个元素)。
【问题讨论】:
-
您好,nx1 和 ny1 是什么?他们分别是 nx 和 ny 吗?
-
哇,我刚刚运行它,它真的很酷。但是,我想不出解决该错误的方法,我唯一能想到的就是将数据类型更改为长整数或其他内容。
-
@Ouss 我刚刚将 ny1 和 nx1 更改为 nx 和 ny,因为它们没有被定义。
-
@Ouss 是的,你是对的! ny1 和 nx1 应该替换为 ny nx,抱歉。
标签: python numpy simulation