有限体积PDE求解器--基于Python的FiPy



http://www.ctcms.nist.gov/fipy/index.html

Bookmark and Share FiPy: A Finite Volume PDE Solver Using Python
Version 3.1

Questions? Suggestions?

Join the mailing list

Recent mailing list traffic recorded at GMANE (links leave NIST): 

You can also open an issue at thetracker.

Contact

FiPy developers
Jonathan Guyer
Daniel Wheeler
James Warren

Join our mailing list

100 Bureau Drive, M/S 6555
Gaithersburg, MD 20899

301-975-5329 Telephone
301-975-4553 Facsimile

FiPy: A Finite Volume PDE Solver Using Python

FiPy is an object oriented, partial differential equation (PDE) solver, written in Python, based on a standard finite volume (FV) approach. The framework has been developed in the Materials Science and Engineering Division (MSED) and Center for Theoretical and Computational Materials Science (CTCMS), in the Material Measurement Laboratory (MML) at the National Institute of Standards and Technology (NIST).

The solution of coupled sets of PDEs is ubiquitous to the numerical simulation of science problems. Numerous PDE solvers exist, using a variety of languages and numerical approaches. Many are proprietary, expensive and difficult to customize. As a result, scientists spend considerable resources repeatedly developing limited tools for specific problems. Our approach, combining the FV method and Python, provides a tool that is extensible, powerful and freely available. A significant advantage to Python is the existing suite of tools for array calculations, sparse matrices and data rendering.

The FiPy framework includes terms for transient diffusion, convection and standard sources, enabling the solution of arbitrary combinations of coupled elliptic, hyperbolic and parabolic PDEs. Currently implemented models include phase field [BoettingerReview:2002] [ChenReview:2002] [McFaddenReview:2002] treatments of polycrystalline, dendritic, and electrochemical phase transformations as well as a level set treatment of the electrodeposition process[NIST:damascene:2001].

If you use FiPy in your research, please cite: (bibtex) (endnote) (pdf)

J. E. Guyer, D. Wheeler & J. A. Warren, "FiPy: Partial Differential Equations with Python," Computing in Science & Engineering 11(3) pp. 6—15 (2009), doi:10.1109/MCSE.2009.52http://www.ctcms.nist.gov/fipy

Documentation

Applications

FiPy@MatForge

Indices and tables


一个求解Cahn-Hilliard方程的实例:

http://www.ctcms.nist.gov/fipy/examples/cahnHilliard/generated/examples.cahnHilliard.mesh2D.html


examples.cahnHilliard.mesh2D

The spinodal decomposition phenomenon is a spontaneous separation of an initially homogenous mixture into two distinct regions of different properties (spin-up/spin-down, component A/component B). It is a “barrierless” phase separation process, such that under the right thermodynamic conditions, any fluctuation, no matter how small, will tend to grow. This is in contrast to nucleation, where a fluctuation must exceed some critical magnitude before it will survive and grow. Spinodal decomposition can be described by the “Cahn-Hilliard” equation (also known as “conserved Ginsberg-Landau” or “model B” of Hohenberg & Halperin)

\frac{\partial \phi}{\partial t}= \nabla\cdot D \nabla\left( \frac{\partial f}{\partial \phi}   - \epsilon^2 \nabla^2 \phi\right).

where \phi is a conserved order parameter, possibly representing alloy composition or spin. The double-well free energy function f = (a^2/2) \phi^2 (1 - \phi)^2 penalizes states with intermediate values of \phi between 0 and 1. The gradient energy term \epsilon^2 \nabla^2\phi, on the other hand, penalizes sharp changes of \phi. These two competing effects result in the segregation of \phi into domains of 0 and 1, separated by abrupt, but smooth, transitions. The parameters a and \epsilondetermine the relative weighting of the two effects and D is a rate constant.

We can simulate this process in FiPy with a simple script:

>>> from fipy import *

(Note that all of the functionality of NumPy is imported along with FiPy, although much is augmented for FiPy‘s needs.)

>>> if __name__ == "__main__":
...     nx = ny = 1000
... else:
...     nx = ny = 20
>>> mesh = Grid2D(nx=nx, ny=ny, dx=0.25, dy=0.25)
>>> phi = CellVariable(name=r"$\phi$", mesh=mesh)

We start the problem with random fluctuations about \phi = 1/2

>>> phi.setValue(GaussianNoiseVariable(mesh=mesh,
...                                    mean=0.5,
...                                    variance=0.01))

FiPy doesn’t plot or output anything unless you tell it to:

>>> if __name__ == "__main__":
...     viewer = Viewer(vars=(phi,), datamin=0., datamax=1.)

For FiPy, we need to perform the partial derivative \partial f/\partial \phi manually and then put the equation in the canonical form by decomposing the spatial derivatives so that each Term is of a single, even order:

\frac{\partial \phi}{\partial t} = \nabla\cdot D a^2 \left[ 1 - 6 \phi \left(1 - \phi\right)\right] \nabla \phi- \nabla\cdot D \nabla \epsilon^2 \nabla^2 \phi.

FiPy would automatically interpolate D * a**2 * (1 - 6 * phi * (1 - phi)) onto the faces, where the diffusive flux is calculated, but we obtain somewhat more accurate results by performing a linear interpolation from phi at cell centers to PHI at face centers. Some problems benefit from non-linear interpolations, such as harmonic or geometric means, and FiPy makes it easy to obtain these, too.

>>> PHI = phi.arithmeticFaceValue
>>> D = a = epsilon = 1.
>>> eq = (TransientTerm()
...       == DiffusionTerm(coeff=D * a**2 * (1 - 6 * PHI * (1 - PHI)))
...       - DiffusionTerm(coeff=(D, epsilon**2)))

Because the evolution of a spinodal microstructure slows with time, we use exponentially increasing time steps to keep the simulation “interesting”. The FiPy user always has direct control over the evolution of their problem.

>>> dexp = -5
>>> elapsed = 0.
>>> if __name__ == "__main__":
...     duration = 1000.
... else:
...     duration = 1000.
>>> while elapsed < duration:
...     dt = min(100, numerix.exp(dexp))
...     elapsed += dt
...     dexp += 0.01
...     eq.solve(phi, dt=dt, solver=LinearLUSolver())
...     if __name__ == "__main__":
...         viewer.plot()
...     elif (max(phi.globalValue) > 0.7) and (min(phi.globalValue) < 0.3) and elapsed > 10.:
...         break
>>> print (max(phi.globalValue) > 0.7) and (min(phi.globalValue) < 0.3)
True
evolution of Cahn-Hilliard phase separation at t = 30, 100 and 1000



  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
求解Navier-Stokes(N-S)方程的有限元代码需要相应的程序包和工具箱,如PDE Toolbox和FEATool Multiphysics。下面给出一个简单的示例,以展示如何使用这些工具箱来求解N-S方程。 首先,需将N-S方程转化为变分形式,然后使用有限元方法离散化。这样可以得到一个线性系统Ax=b,其中A是系数矩阵,x是未知解向量,b是右手边向量。使用求解求解该线性系统即可得到N-S方程的解。 下面是一个使用PDE Toolbox和FEATool Multiphysics的求解N-S方程的简单示例。假设我们要求解在单位正方形上的稳态N-S方程,其边界条件为: - 左边界:u=1,v=0; - 右边界:u=0,v=0; - 上边界:u=0,v=0; - 下边界:u=0,v=0。 代码如下: ```matlab % 定义模型 model = createpde(); geometryFromEdges(model,@squareg); % 定义边界条件 applyBoundaryCondition(model,'dirichlet','Edge',1:4,'u',1,'v',0); % 定义初始猜测解 u0 = 0.1*randn(model.Mesh.NumNodes,1); v0 = 0.1*randn(model.Mesh.NumNodes,1); uvc = [u0;v0]; % 定义PDE参数 mu = 1; rho = 1; f = [0;0]; % 定义有限元方法 specifyCoefficients(model,'m',0,'d',0,'c',[1/mu,0;0,1/mu],'a',[1,0;0,1],'f',f); generateMesh(model); % 求解线性系统 [u,v] = solvepdeeig(model,0,'u0',u0,'v0',v0); % 可视化结果 pdeplot(model,'XYData',u,'ZData',u,'ColorMap','jet') ``` 这个示例使用了PDE Toolbox和FEATool Multiphysics来求解N-S方程。首先,我们定义了一个模型,并使用`squareg`函数生成了一个正方形的几何体。接下来,我们定义了边界条件以及初始猜测解。然后,我们定义了N-S方程的参数,并使用`specifyCoefficients`方法将其转化为变分形式。最后,我们使用`solvepdeeig`方法求解线性系统,并使用`pdeplot`方法可视化结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值