【问题标题】:Applying neumann boundary conditions to the diffusion equation将纽曼边界条件应用于扩散方程
【发布时间】:2018-11-28 14:10:52
【问题描述】:

我已经绘制了扩散方程 du/dt=D(d^2 u/dx^2) + Cu 的数值解的代码,其中 u 是 x 和 t 的函数 - 我已经解决了数值并用 direchtlet 边界条件 u(-L/2,t)=u(L/2,t)=0 绘制它,临界长度是函数以指数方式爆炸之前的值,我已经计算出成为圆周率。我正在尝试将正确的边界条件更改为 Neumann 边界条件 u_x(L/2)=0,这应该会减少临界长度并导致函数呈指数增长,但我不太确定该怎么做而且我的似乎不太正确-有人可以看看他们是否可以确定我哪里出错了吗?谢谢!

L=np.pi # value chosen for the critical length
s=101 # number of steps in x
t=10002 # number of timesteps
ds=L/(s-1) # step in x
dt=0.0001 # time step
D=1 # diffusion constant, set equal to 1
C=1 # creation rate of neutrons, set equal to 1
Alpha=(D*dt)/(ds*ds) # constant for diffusion term
Beta=C*dt # constant for u term

x = np.linspace(-L/2, 0, num=51)
x = np.concatenate([x, np.linspace(x[-1] - x[-2], L/2, num=50)]) # setting x in the specified interval

u=np.zeros(shape=(s,t))
u[50,0]=1/ds # delta function
for k in range(0,t-1):
    u[0,k]=0 #direchtlet boundary condition
    for i in range(1,s-1):
        u[i,k+1]=(1+Beta-2*Alpha)*u[i,k]+Alpha*u[i+1,k]+Alpha*u[i-1,k] # numerical solution 
    u[s-1,k+1]=u[s-2,k+1] # neumann boundary condition
    if k == 50 or k == 100 or k == 250 or k == 500 or k == 1000 or k == 10000: # plotting at times
        plt.plot(x,u[:,k])

plt.show()

【问题讨论】:

  • 您是指绘图中最后一个k 的曲线,即k=10000,其中右侧的曲线不为零?请解释清楚
  • 根据您的问题,您只需使用u[s-1,k+1] = 0 而不是u[s-1,k+1]=u[s-2,k+1]
  • 对不起,基本上我认为曲线应该随着时间的增加而呈指数增长,但它似乎根本没有这样做。
  • 我之前尝试过使用'u[s-1,k+1] = 0',但如果没有边界条件,它根本不会改变图形。
  • 我使用了u[s-1,k+1] = 0 并得到了最后 k 个不同的曲线,其中曲线的右边缘在 0 处

标签: python numpy matplotlib math numerical-methods


【解决方案1】:

首先,您需要在时间循环的开始和当前时间步中应用左右边界条件(而不是在k+1,就像您在右侧 BC 上所做的那样) .

import numpy as np
import matplotlib.pyplot as plt
L=np.pi # value chosen for the critical length
s=101 # number of steps in x
t=10002 # number of timesteps
ds=L/(s-1) # step in x
dt=0.0001 # time step
D=1 # diffusion constant, set equal to 1
C=1 # creation rate of neutrons, set equal to 1
Alpha=(D*dt)/(ds*ds) # constant for diffusion term
Beta=C*dt # constant for u term

x = np.linspace(-L/2, 0, num=51)
x = np.concatenate([x, np.linspace(x[-1] - x[-2], L/2, num=50)]) # setting x in the specified interval

u=np.zeros(shape=(s,t))
u[50,0]=1/ds # delta function
for k in range(0,t-1):
    u[0,k] = 0 # left direchtlet boundary condition
    u[s-1,k] = 0 # right dirichlet boundary condition

    for i in range(1,s-1):
        u[i,k+1]=(1+Beta-2*Alpha)*u[i,k]+Alpha*u[i+1,k]+Alpha*u[i-1,k] # numerical solution 
    if k == 50 or k == 100 or k == 250 or k == 500 or k == 1000 or k == 10000: # plotting at times
        plt.plot(x,u[:,k])

plt.savefig('test1.png')
plt.close()

对于 dirichlet BC,您会得到: (可能与以前相同,因为对于扩散问题,它不会有太大的不同,但无论如何都是不正确的)。然后你改变你的冯诺依曼BC的右边界条件

u[s-1,k] = u[s-3,k] # right von-neumann boundary condition

因为我看到您使用的是中心差分方案,所以 Von-Neumann BC 在边界处声明 du/dx=0。用右边界的中心差分方案离散这个导数是(u[s-1,k]-u[s-3,k])/dx = 0,所以u[s-1,k]=u[s-3,k]。有了这个改变,你得到

【讨论】:

  • 非常感谢!很抱歉提出另一个问题,但是如果我要增加 L,图表也应该会遇到类似的情况(因为超过 L 的临界长度,图表会呈指数增长),但我的似乎也没有真正改变 - 你知道吗?我定义 x 的方式有问题吗?
  • L 只是空间域。 “由于超过 L 的临界长度,图形呈指数增长”是什么意思?如果您增加域,该函数将需要更多时间才能到达边界,并且这些对 soltuino 真的有任何影响。
  • 当我使用初始 Dirichlet 边界条件解析函数时,我得到了 L=pi,这是超出函数中断并呈指数增长的值。我不确定我的数值解是否真的考虑到了这一点。
  • L=pi 是什么意思?您获得了在您的域中定义的分析解决方案,没有别的,对吗?
  • 抱歉回复晚了,我被告知要“在区间内求解上述方程,−L/2 ≤ x ≤L/2,受限于以下边界条件:u( −L/ 2,t)= 0 和 u(L/2,t) = 0。分析确定系统的临界长度 (C, D = 1.0),超过该临界长度总是导致解呈指数增长”。我知道它是 L=pi,所以在我的代码中,如果我将它更改为 L=2pi,它肯定会开始增加?这可能是数学堆栈交换的更多问题,抱歉!
猜你喜欢
  • 1970-01-01
  • 2019-10-07
  • 2020-08-21
  • 2020-03-18
  • 2019-04-13
  • 2020-05-11
  • 2020-12-14
  • 2020-08-17
  • 2022-01-23
相关资源
最近更新 更多