项目作者: nschloe

项目描述 :
Finite volume discretization tools for Python.
高级语言: Python
项目地址: git://github.com/nschloe/pyfvm.git
创建时间: 2013-02-05T13:15:53Z
项目社区:https://github.com/nschloe/pyfvm

开源协议:GNU General Public License v3.0

下载


pyfvm

PyPi Version
PyPI pyversions
GitHub stars
PyPi downloads

Discord

Creating finite volume equation systems with ease.

pyfvm provides everything that is needed for setting up finite volume equation
systems. The user needs to specify the finite volume formulation in a
configuration file, and pyfvm will create the matrix/right-hand side or the
nonlinear system for it. This package is for everyone who wants to quickly
construct FVM systems.

Examples

Linear equation systems

pyfvm works by specifying the residuals, so for solving Poisson’s equation with
Dirichlet boundary conditions, simply do

  1. import meshplex
  2. import meshzoo
  3. import numpy as np
  4. from scipy.sparse import linalg
  5. import pyfvm
  6. from pyfvm.form_language import Boundary, dS, dV, integrate, n_dot_grad
  7. class Poisson:
  8. def apply(self, u):
  9. return integrate(lambda x: -n_dot_grad(u(x)), dS) - integrate(lambda x: 1.0, dV)
  10. def dirichlet(self, u):
  11. return [(lambda x: u(x) - 0.0, Boundary())]
  12. # Create mesh using meshzoo
  13. vertices, cells = meshzoo.rectangle_tri(
  14. np.linspace(0.0, 2.0, 401), np.linspace(0.0, 1.0, 201)
  15. )
  16. mesh = meshplex.Mesh(vertices, cells)
  17. matrix, rhs = pyfvm.discretize_linear(Poisson(), mesh)
  18. u = linalg.spsolve(matrix, rhs)
  19. mesh.write("out.vtk", point_data={"u": u})

This example uses meshzoo for creating a
simple mesh, but anything else that provides vertices and cells works as well.
For example, reading from a wide variety of mesh files is supported (via
meshio):

  1. mesh = meshplex.read("pacman.e")

Likewise, PyAMG is a much faster solver for
this problem

  1. import pyamg
  2. ml = pyamg.smoothed_aggregation_solver(matrix)
  3. u = ml.solve(rhs, tol=1e-10)

More examples are contained in the examples directory.

Nonlinear equation systems

Nonlinear systems are treated almost equally; only the discretization and
obviously the solver call is different. For Bratu’s problem:

  1. import pyfvm
  2. from pyfvm.form_language import *
  3. import meshzoo
  4. import numpy as np
  5. from sympy import exp
  6. import meshplex
  7. class Bratu:
  8. def apply(self, u):
  9. return integrate(lambda x: -n_dot_grad(u(x)), dS) - integrate(
  10. lambda x: 2.0 * exp(u(x)), dV
  11. )
  12. def dirichlet(self, u):
  13. return [(u, Boundary())]
  14. vertices, cells = meshzoo.rectangle_tri(
  15. np.linspace(0.0, 2.0, 101), np.linspace(0.0, 1.0, 51)
  16. )
  17. mesh = meshplex.Mesh(vertices, cells)
  18. f, jacobian = pyfvm.discretize(Bratu(), mesh)
  19. def jacobian_solver(u0, rhs):
  20. from scipy.sparse import linalg
  21. jac = jacobian.get_linear_operator(u0)
  22. return linalg.spsolve(jac, rhs)
  23. u0 = np.zeros(len(vertices))
  24. u = pyfvm.newton(f.eval, jacobian_solver, u0)
  25. mesh.write("out.vtk", point_data={"u": u})

Note that the Jacobian is computed symbolically from the Bratu class.

Instead of pyfvm.newton, you can use any solver that accepts the residual
computation f.eval, e.g.,

  1. import scipy.optimize
  2. u = scipy.optimize.newton_krylov(f.eval, u0)