【发布时间】:2022-01-03 08:21:32
【问题描述】:
有没有办法在 Python 中数值求解以下 PDE?
RHS 的第二项对时间和空间都有一个导数。
我尝试在 Python 中使用 Py-PDE 包,它只解决了 dy(x,t)/dt = f(y(x,t)) 的形式,所以我尝试使用类似于 的求根算法scipy fzero 得到 dy(x,t)/dt - f(y(x,t),dy(x,t)/dt) = 0 的解(求解dy(x,t)/dt)。
class nonlinearPDE(pde.PDEBase):
def __init__(self, bc={"derivative":0}):
self.bc = bc #boundary conditions for operators
def _make_pde_rhs_numba(self, state):
"""numba-compiled implementation of the PDE"""
laplace = state.grid.make_operator("laplace", bc=self.bc)
def findroot(f, df, x0, nmax):
"""Newton–Raphson method"""
for i in range(nmax):
x0 = x0 - f(x0)/df(x0)
return x0
@jit
def pde_rhs(y, t):
func = lambda dydt : dydt - a*laplace(y) - b*laplace(dydt)
dydt = findroot(func, lambda x : 1, 0, 1)
return dydt
return pde_rhs
但是,当程序尝试求解 PDE 时,它会抛出错误:
File "...\anaconda3\lib\site-packages\pde\solvers\controller.py", line 191, in run
t = stepper(state, t, t_break)
File "...\anaconda3\lib\site-packages\pde\solvers\scipy.py", line 82, in stepper
sol = integrate.solve_ivp(
File "...\anaconda3\lib\site-packages\scipy\integrate\_ivp\ivp.py", line 542, in solve_ivp
solver = method(fun, t0, y0, tf, vectorized=vectorized, **options)
File "...\anaconda3\lib\site-packages\scipy\integrate\_ivp\rk.py", line 94, in __init__
self.f = self.fun(self.t, self.y)
File "...\anaconda3\lib\site-packages\scipy\integrate\_ivp\base.py", line 138, in fun
return self.fun_single(t, y)
File "...\anaconda3\lib\site-packages\scipy\integrate\_ivp\base.py", line 20, in fun_wrapped
return np.asarray(fun(t, y), dtype=dtype)
File "...\anaconda3\lib\site-packages\pde\solvers\scipy.py", line 74, in rhs_helper
return rhs(state_flat.reshape(shape), t).flat # type: ignore
File "...\anaconda3\lib\site-packages\numba\core\dispatcher.py", line 420, in _compile_for_args
error_rewrite(e, 'typing')
File "...\anaconda3\lib\site-packages\numba\core\dispatcher.py", line 361, in error_rewrite
raise e.with_traceback(None)
TypingError: Cannot capture the non-constant value associated with variable 'y' in a function that will escape.
【问题讨论】:
-
你确定你在右手边有时间和空间的混合导数吗?这会使 PDE 难以用任何语言解决。如果没有时间导数,您就有一个原型抛物线 PDE,您可以对其进行时间步进。
-
是的,它是右侧的混合导数。顺便说一句,问题的答案不一定是一个工作示例,它可以是“伪代码”。
-
您可以将
dy/dt替换为前向或后向差商,然后从那里获取。然后归结为线性时间步长问题。 (不需要fzero。)
标签: python python-3.x numerical-integration pde