二维平板Python上的热扩散

2024-09-26 22:13:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要完成一项任务,而我却有点不知所措。我不知道解算器出了什么问题,也不知道如何可视化数据。 我的任务是模拟二维平板上的热扩散。它必须有两个加热器,在板的中间有两个孔。bc总是为零。在

我的问题是: 如何使模拟运行在时间上?我问这个是因为当我这样画的时候,它显示了一个盘子上的加热器,但是所有的热量都集中在它们身上。另一个问题是,如何将结果可视化为视频或图片? 这是我的密码。谢谢您。在

import numpy as np
import matplotlib.pyplot as plt

dt=0.1
dx=0.1

L=50                        #length of the plate
Ly=np.linspace(0,L,50)
B=50                        #width of the plate
Bx=np.linspace(0,B,50)

M=np.zeros([L,B])           #matrix

#heating device shaped like X
Gr=np.eye(10)*2000
for iGr in range(10):
    Gr[iGr,-iGr-1]=2000

#implementing heaters to matrix    
M[20:30,10:20]=Gr
M[20:30,30:40]=Gr

t=0
#wannabe solver
while t<10:
    t=0.1+t
    for j in range(1,L-1):
        for i in range(1,B-1):            
            if 24<j<28:
                if 29<i<32:
                    k=0
                elif 23<i<20: # holes for liquid
                    k=0
                else:
                    break
            else:
                k=0.5
                break

            M[i,j]=M[i,j]+k*((dt)/dx**2)*(M[i,j+1]-2*M[i,j]+M[i,j-1])+k*((dt)/dx**2)*(M[i+1,j]-2*M[i,j]+M[i-1,j])


plt.pcolormesh(M)
plt.colorbar()
plt.show()

Tags: oftheinimportfor可视化asnp
1条回答
网友
1楼 · 发布于 2024-09-26 22:13:27

这可能会让你开始。我不熟悉你们的热传递函数(或一般的热传递函数),所以我用了不同的。在

下面的代码为每个步骤dt计算M,并将其附加到一个列表MM。在

然后我们使用FuncAnimation来逐步检查MM的元素(回想一下,MM的元素是矩阵M的快照)并显示它们。在

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

dt=0.1
dx=0.1

L=50                        # length of the plate
B=50                        # width of the plate


#heating device shaped like X
Gr=np.eye(10)*2000
for iGr in range(10):
    Gr[iGr,-iGr-1]=2000

# Function to set M values corresponding to non-zero Gr values
def assert_heaters(M, Gr):
    M[20:30,10:20] = np.where(Gr > 0, Gr, M[20:30,10:20])
    M[20:30,30:40] = np.where(Gr > 0, Gr, M[20:30,30:40])


M=np.zeros([L,B])           # matrix
assert_heaters(M, Gr)

# Build MM, a list of matrices, each element corresponding to M at a given step
T = np.arange(0,10,dt)
MM = []
for i in xrange(len(T)):
    for j in range(1,L-1):
        for i in range(1,B-1):
            k=0.5  # default k
            if 24<j<28:
                # holes for liquid
                if 29<i<32 or 23<i<20: k=0

            #dm = k * ((dt)/dx**2) * (M[i,j+1] + M[i,j-1] - 2*M[i,j]) + \
            #     k * ((dt)/dx**2) * (M[i+1,j] + M[i-1,j] - 2*M[i,j])
            #M[i,j] += dm
            M[i,j] = (M[i-1,j] + M[i+1,j] + M[i,j-1] + M[i,j+1])/4

    # Re-assert heaters
    assert_heaters(M, Gr)

    MM.append(M.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
    pcm.set_array(MM[i].ravel())
    plt.draw()

anim = FuncAnimation(fig, step, interval=50)
plt.show()

当然,你得把传递函数修正成你自己的。在

这段代码产生类似this animation(注意,它是3M,太大了,不能嵌入到答案中)

压缩版本:

enter image description here

相关问题 更多 >

    热门问题