Fipy中的Dirac delta源项

2024-09-27 21:31:55 发布

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

我想知道如何将狄拉克三角函数表示为Fipy中的源项。我想解下面的方程 enter image description here

我尝试了以下代码

from fipy import *
nx = 50
ny = 1
dx = dy = 0.025  # grid spacing
L = dx * nx
mesh = Grid2D(dx=dx, dy=dy, nx=nx, ny=ny)
phi = CellVariable(name="solution variable", mesh=mesh, value=0.)
Gamma=1
delta=1  # I want knowing how to make this right. 
eqG = TransientTerm() == DiffusionTerm(coeff=Gamma)+delta
valueTopLeft = 0
valueBottomRight = 1
X, Y = mesh.faceCenters
facesTopLeft = ((mesh.facesLeft & (Y > L / 2)) | (mesh.facesTop & (X < L / 2)))
facesBottomRight = ((mesh.facesRight & (Y < L / 2)) |
                    (mesh.facesBottom & (X > L / 2)))
phi.constrain(valueTopLeft, facesTopLeft)
phi.constrain(valueBottomRight, facesBottomRight)
timeStepDuration = 10 * 0.9 * dx ** 2 / (2 * 0.8)
steps = 100
results=[]
for step in range(steps):
    eqG.solve(var=phi, dt=timeStepDuration)
    results.append(phi.value)

代码正在运行,但我想要确切的狄拉克三角函数。我查了numerix模块,但找不到这样的函数。Sx1和Sy1是常数。我正在使用Python2.7


Tags: 代码valuedeltanxphimeshgammady
1条回答
网友
1楼 · 发布于 2024-09-27 21:31:55

像扩散界面方法一样平滑Dirac delta函数可能是一个好主意(见方程11、12和13here)。所以,这是一个选择

def delta_func(x, epsilon):
    return ((x < epsilon) & (x > -epsilon)) * \
        (1 + numerix.cos(numerix.pi * x / epsilon)) / 2 / epsilon

2 * epsilon是Dirac delta函数的宽度,被选择为几个网格间距。也可以使用1 / dx并选择距离Dirac delta函数位置最近的网格点。但是,我认为这会变得更加依赖于网格。这是1D中的工作代码

from fipy import *
nx = 50
dx = dy = 0.025  # grid spacing
L = dx * nx
mesh = Grid1D(dx=dx, nx=nx)
phi = CellVariable(name="solution variable", mesh=mesh, value=0.)
Gamma=1

def delta_func(x, epsilon):
    return ((x < epsilon) & (x > -epsilon)) * \
        (1 + numerix.cos(numerix.pi * x / epsilon)) / 2 / epsilon

x0 = L / 2.
eqG = TransientTerm() == DiffusionTerm(coeff=Gamma)+ delta_func(mesh.x - x0, 2 * dx)
valueTopLeft = 0
valueBottomRight = 1

timeStepDuration = 10 * 0.9 * dx ** 2 / (2 * 0.8)
steps = 100
viewer = Viewer(phi)
for step in range(steps):
    res = eqG.solve(var=phi, dt=timeStepDuration)
    print(step)
    viewer.plot()

input('stopped')

这里,epsilon = 2 * dx是一个任意选择,delta函数以L / 2为中心。2D只需要将函数相乘。你知道吗

相关问题 更多 >

    热门问题