pyfvm


Namepyfvm JSON
Version 0.4.6 PyPI version JSON
download
home_page
SummaryFinite volume discretizations for Python
upload_time2024-03-14 21:38:12
maintainer
docs_urlNone
author
requires_python>=3.8
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pyfvm

[![PyPi Version](https://img.shields.io/pypi/v/pyfvm.svg?style=flat-square)](https://pypi.org/project/pyfvm)
[![PyPI pyversions](https://img.shields.io/pypi/pyversions/pyfvm.svg?style=flat-square)](https://pypi.org/pypi/pyfvm/)
[![GitHub stars](https://img.shields.io/github/stars/meshpro/pyfvm.svg?style=flat-square&logo=github&label=Stars&logoColor=white)](https://github.com/meshpro/pyfvm)
[![PyPi downloads](https://img.shields.io/pypi/dm/pyfvm.svg?style=flat-square)](https://pypistats.org/packages/pyfvm)

[![Discord](https://img.shields.io/static/v1?logo=discord&logoColor=white&label=chat&message=on%20discord&color=7289da&style=flat-square)](https://discord.gg/hnTJ5MRX2Y)

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

```python
import meshplex
import meshzoo
import numpy as np
from scipy.sparse import linalg

import pyfvm
from pyfvm.form_language import Boundary, dS, dV, integrate, n_dot_grad


class Poisson:
    def apply(self, u):
        return integrate(lambda x: -n_dot_grad(u(x)), dS) - integrate(lambda x: 1.0, dV)

    def dirichlet(self, u):
        return [(lambda x: u(x) - 0.0, Boundary())]


# Create mesh using meshzoo
vertices, cells = meshzoo.rectangle_tri(
    np.linspace(0.0, 2.0, 401), np.linspace(0.0, 1.0, 201)
)
mesh = meshplex.Mesh(vertices, cells)

matrix, rhs = pyfvm.discretize_linear(Poisson(), mesh)

u = linalg.spsolve(matrix, rhs)

mesh.write("out.vtk", point_data={"u": u})
```

This example uses [meshzoo](https://pypi.org/project/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](https://pypi.org/project/meshio)):

<!--pytest.mark.skip-->

```python
mesh = meshplex.read("pacman.e")
```

Likewise, [PyAMG](https://github.com/pyamg/pyamg) is a much faster solver for
this problem

<!--pytest.mark.skip-->

```python
import pyamg

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

More examples are contained in the [examples directory](examples/).

#### Nonlinear equation systems

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

```python
import pyfvm
from pyfvm.form_language import *
import meshzoo
import numpy as np
from sympy import exp
import meshplex


class Bratu:
    def apply(self, u):
        return integrate(lambda x: -n_dot_grad(u(x)), dS) - integrate(
            lambda x: 2.0 * exp(u(x)), dV
        )

    def dirichlet(self, u):
        return [(u, Boundary())]


vertices, cells = meshzoo.rectangle_tri(
    np.linspace(0.0, 2.0, 101), np.linspace(0.0, 1.0, 51)
)
mesh = meshplex.Mesh(vertices, cells)

f, jacobian = pyfvm.discretize(Bratu(), mesh)


def jacobian_solver(u0, rhs):
    from scipy.sparse import linalg

    jac = jacobian.get_linear_operator(u0)
    return linalg.spsolve(jac, rhs)


u0 = np.zeros(len(vertices))
u = pyfvm.newton(f.eval, jacobian_solver, u0)

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.,

<!--pytest.mark.skip-->

```python
import scipy.optimize

u = scipy.optimize.newton_krylov(f.eval, u0)
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "pyfvm",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "",
    "author": "",
    "author_email": "Nico Schl\u00f6mer <nico.schloemer@gmail.com>",
    "download_url": "",
    "platform": null,
    "description": "# pyfvm\n\n[![PyPi Version](https://img.shields.io/pypi/v/pyfvm.svg?style=flat-square)](https://pypi.org/project/pyfvm)\n[![PyPI pyversions](https://img.shields.io/pypi/pyversions/pyfvm.svg?style=flat-square)](https://pypi.org/pypi/pyfvm/)\n[![GitHub stars](https://img.shields.io/github/stars/meshpro/pyfvm.svg?style=flat-square&logo=github&label=Stars&logoColor=white)](https://github.com/meshpro/pyfvm)\n[![PyPi downloads](https://img.shields.io/pypi/dm/pyfvm.svg?style=flat-square)](https://pypistats.org/packages/pyfvm)\n\n[![Discord](https://img.shields.io/static/v1?logo=discord&logoColor=white&label=chat&message=on%20discord&color=7289da&style=flat-square)](https://discord.gg/hnTJ5MRX2Y)\n\nCreating finite volume equation systems with ease.\n\npyfvm provides everything that is needed for setting up finite volume equation\nsystems. The user needs to specify the finite volume formulation in a\nconfiguration file, and pyfvm will create the matrix/right-hand side or the\nnonlinear system for it. This package is for everyone who wants to quickly\nconstruct FVM systems.\n\n### Examples\n\n#### Linear equation systems\n\npyfvm works by specifying the residuals, so for solving Poisson's equation with\nDirichlet boundary conditions, simply do\n\n```python\nimport meshplex\nimport meshzoo\nimport numpy as np\nfrom scipy.sparse import linalg\n\nimport pyfvm\nfrom pyfvm.form_language import Boundary, dS, dV, integrate, n_dot_grad\n\n\nclass Poisson:\n    def apply(self, u):\n        return integrate(lambda x: -n_dot_grad(u(x)), dS) - integrate(lambda x: 1.0, dV)\n\n    def dirichlet(self, u):\n        return [(lambda x: u(x) - 0.0, Boundary())]\n\n\n# Create mesh using meshzoo\nvertices, cells = meshzoo.rectangle_tri(\n    np.linspace(0.0, 2.0, 401), np.linspace(0.0, 1.0, 201)\n)\nmesh = meshplex.Mesh(vertices, cells)\n\nmatrix, rhs = pyfvm.discretize_linear(Poisson(), mesh)\n\nu = linalg.spsolve(matrix, rhs)\n\nmesh.write(\"out.vtk\", point_data={\"u\": u})\n```\n\nThis example uses [meshzoo](https://pypi.org/project/meshzoo) for creating a\nsimple mesh, but anything else that provides vertices and cells works as well.\nFor example, reading from a wide variety of mesh files is supported (via\n[meshio](https://pypi.org/project/meshio)):\n\n<!--pytest.mark.skip-->\n\n```python\nmesh = meshplex.read(\"pacman.e\")\n```\n\nLikewise, [PyAMG](https://github.com/pyamg/pyamg) is a much faster solver for\nthis problem\n\n<!--pytest.mark.skip-->\n\n```python\nimport pyamg\n\nml = pyamg.smoothed_aggregation_solver(matrix)\nu = ml.solve(rhs, tol=1e-10)\n```\n\nMore examples are contained in the [examples directory](examples/).\n\n#### Nonlinear equation systems\n\nNonlinear systems are treated almost equally; only the discretization and\nobviously the solver call is different. For Bratu's problem:\n\n```python\nimport pyfvm\nfrom pyfvm.form_language import *\nimport meshzoo\nimport numpy as np\nfrom sympy import exp\nimport meshplex\n\n\nclass Bratu:\n    def apply(self, u):\n        return integrate(lambda x: -n_dot_grad(u(x)), dS) - integrate(\n            lambda x: 2.0 * exp(u(x)), dV\n        )\n\n    def dirichlet(self, u):\n        return [(u, Boundary())]\n\n\nvertices, cells = meshzoo.rectangle_tri(\n    np.linspace(0.0, 2.0, 101), np.linspace(0.0, 1.0, 51)\n)\nmesh = meshplex.Mesh(vertices, cells)\n\nf, jacobian = pyfvm.discretize(Bratu(), mesh)\n\n\ndef jacobian_solver(u0, rhs):\n    from scipy.sparse import linalg\n\n    jac = jacobian.get_linear_operator(u0)\n    return linalg.spsolve(jac, rhs)\n\n\nu0 = np.zeros(len(vertices))\nu = pyfvm.newton(f.eval, jacobian_solver, u0)\n\nmesh.write(\"out.vtk\", point_data={\"u\": u})\n```\n\nNote that the Jacobian is computed symbolically from the `Bratu` class.\n\nInstead of `pyfvm.newton`, you can use any solver that accepts the residual\ncomputation `f.eval`, e.g.,\n\n<!--pytest.mark.skip-->\n\n```python\nimport scipy.optimize\n\nu = scipy.optimize.newton_krylov(f.eval, u0)\n```\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Finite volume discretizations for Python",
    "version": "0.4.6",
    "project_urls": {
        "Home": "https://github.com/meshpro/pyfvm",
        "Issues": "https://github.com/meshpro/pyfvm/issues"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c9246358c74bf61cf3c306a173a754c5b9a852fa02b1c6700160e1037bbb6e28",
                "md5": "322b52b8c61da65d0c888a5bdd0066cd",
                "sha256": "d3c3f6d3ef70b3b4f5daf3054ca7f627abc362abad49c518decdfe89a5d9689e"
            },
            "downloads": -1,
            "filename": "pyfvm-0.4.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "322b52b8c61da65d0c888a5bdd0066cd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 32436,
            "upload_time": "2024-03-14T21:38:12",
            "upload_time_iso_8601": "2024-03-14T21:38:12.076423Z",
            "url": "https://files.pythonhosted.org/packages/c9/24/6358c74bf61cf3c306a173a754c5b9a852fa02b1c6700160e1037bbb6e28/pyfvm-0.4.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-14 21:38:12",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "meshpro",
    "github_project": "pyfvm",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pyfvm"
}
        
Elapsed time: 0.21037s