如何将ODE系统与FEM系统相结合

Cal*_*ngh 23 python finite-element-analysis differential-equations fipy assimulo

我将动态模型设置为ODE的(僵硬)系统.我目前用CVODE解决了这个问题(来自Assimulo python包中的SUNDIALS包),一切都很好.

我现在想要为问题添加一个新的3D散热器(具有与温度相关的热参数).我的想法是使用现有的FEM或FVM框架为我提供一个界面,使我可以轻松地为(t, y)常规提供3D块,并获得,而不是从头开始为3D热方程编写所有方程式.支持残差y'.原理是使用FEM系统中的方程而不是求解器.CVODE可以利用稀疏性,但预计组合系统的解决速度将慢于FEM系统自身解决的速度,并为此量身定制.

# pseudocode of a residuals function for CVODE
def residual(t, y):

    # ODE system of n equations 
    res[0] = <function of t,y>;
    res[1] = <function of t,y>;
    ...
    res[n] = <function of t,y>;

    # Here we add the FEM/FVM residuals
    for i in range(FEMcount):
        res[n+1+i] = FEMequations[FEMcount](t,y)

    return res
Run Code Online (Sandbox Code Playgroud)

我的问题是(a)这种方法是否合理,(b)是否有一个FEM或FVM库可以轻松地让我将其视为一个方程组,这样我就可以"将它"粘贴到我现有的一套ODE方程.

如果不能让两个系统共享同一个时间轴,那么我将不得不以步进模式运行它们,我在那里运行一个模型一小段时间,更新另一个模型的边界条件,运行那个,更新第一个模型的BC,等等.

我对精彩的图书馆FiPy有一些经验,我期望最终以上述方式使用该库.但我想知道其他系统在这种性质问题上的经验,以及我错过的其他方法.


编辑:我现在有一些看似有效的示例代码,显示了如何使用CVODE解决FiPy网格扩散残差.然而,这只是一种方法(使用FiPy),其余的问题和疑虑仍然存在.欢迎任何建议.

from fipy import *
from fipy.solvers.scipy import DefaultSolver
solverFIPY = DefaultSolver()

from assimulo.solvers import CVode as solverASSIMULO
from assimulo.problem import Explicit_Problem as Problem

# FiPy Setup - Using params from the Mesh1D example
###################################################
nx = 50; dx = 1.; D = 1.
mesh = Grid1D(nx = nx, dx = dx)
phi = CellVariable(name="solution variable", mesh=mesh, value=0.)
valueLeft, valueRight = 1., 0.

phi.constrain(valueRight, mesh.facesRight)
phi.constrain(valueLeft, mesh.facesLeft)

# Instead of eqX = TransientTerm() == ExplicitDiffusionTerm(coeff=D),
# Rather just operate on the diffusion term. CVODE will calculate the
# Transient side
edt = ExplicitDiffusionTerm(coeff=D)

timeStepDuration = 0.9 * dx**2 / (2 * D)
steps = 100

# For comparison with an analytical solution - again,
# taken from the Mesh1D.py example
phiAnalytical = CellVariable(name="analytical value", mesh=mesh)
x = mesh.cellCenters[0]
t = timeStepDuration * steps
from scipy.special import erf
phiAnalytical.setValue(1 - erf(x / (2 * numerix.sqrt(D * t))))
if __name__ == '__main__':
    viewer = Viewer(vars=(phi, phiAnalytical))#, datamin=0., datamax=1.)
    viewer.plot()

raw_input('Press a key...')

# Now for the Assimulo/Sundials solver setup
############################################

def residual(t, X):
    # Pretty straightforward, phi is the unknown
    phi.value = X # This is a vector, 50 elements
    # Can immediately return the residuals, CVODE sees this vector
    # of 50 elements as X'(t), which is like TransientTerm() from FiPy
    return edt.justResidualVector(var=phi, solver=solverFIPY)

x0 = phi.value
t0 = 0.
model = Problem(residual, x0, t0)
simulation = solverASSIMULO(model)
tfinal = steps * timeStepDuration # s,

cell_tol = [1.0e-8]*50
simulation.atol = cell_tol
simulation.rtol = 1e-6
simulation.iter = 'Newton'

t, x = simulation.simulate(tfinal, 0)

print x[-1]
# Write back the answer to compare
phi.value = x[-1]
viewer.plot()
raw_input('Press a key...')
Run Code Online (Sandbox Code Playgroud)

这将生成一个显示完美匹配的图表:

完美的一对

小智 3

ODE 是一维微分方程。

FEM 模型适用于空间问题,即更高维度的问题。您需要有限差分法。从 ODE 世界的人的角度来解决和理解更容易。

我认为您真正应该问的问题是如何使用 ODE,并将其转移到 3D 问题空间。

多维偏微分方程很难求解,但我将向您推荐 CFD 方法来解决这一问题,但仅限于二维。http://lorenabarba.com/blog/cfd-python-12-steps-to-navier-stokes/

你应该需要一个充实的下午才能完成这个任务!祝你好运!