Skip to main content

Finite volume discretizations for Python

Project description

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

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 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):

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

Likewise, PyAMG is a much faster solver for this problem

import pyamg

ml = pyamg.smoothed_aggregation_solver(matrix)
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:

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

import scipy.optimize

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyfvm-0.5.2a1-cp314-none-any.whl (65.0 kB view details)

Uploaded CPython 3.14

pyfvm-0.5.2a1-cp313-none-any.whl (62.8 kB view details)

Uploaded CPython 3.13

pyfvm-0.5.2a1-cp312-none-any.whl (62.7 kB view details)

Uploaded CPython 3.12

pyfvm-0.5.2a1-cp311-none-any.whl (69.5 kB view details)

Uploaded CPython 3.11

pyfvm-0.5.2a1-cp310-none-any.whl (33.4 kB view details)

Uploaded CPython 3.10

File details

Details for the file pyfvm-0.5.2a1-cp314-none-any.whl.

File metadata

  • Download URL: pyfvm-0.5.2a1-cp314-none-any.whl
  • Upload date:
  • Size: 65.0 kB
  • Tags: CPython 3.14
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyfvm-0.5.2a1-cp314-none-any.whl
Algorithm Hash digest
SHA256 81a1b4657dd3e18c24c471ceda0e9fe1f88a0ed7fa9dcd99ea09b5fe328379c2
MD5 c32001f1c6e4a04494832d9913a17e94
BLAKE2b-256 357bedf875cdd7cf0df6bd386350cf591eb6fe7daa9b962b0705079377877d85

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyfvm-0.5.2a1-cp314-none-any.whl:

Publisher: release.yml on meshpro/pyfvm-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyfvm-0.5.2a1-cp313-none-any.whl.

File metadata

  • Download URL: pyfvm-0.5.2a1-cp313-none-any.whl
  • Upload date:
  • Size: 62.8 kB
  • Tags: CPython 3.13
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyfvm-0.5.2a1-cp313-none-any.whl
Algorithm Hash digest
SHA256 8f450708175b09afed3cac062104ce9951459e32b63b3013c80302bb9ad2b4fe
MD5 1cb14e521fcd3165013f43fe4b8ee4ee
BLAKE2b-256 a090a9ac01fda38364cab8389276bc01a08ef9b11bae0fc821e8c794c653a318

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyfvm-0.5.2a1-cp313-none-any.whl:

Publisher: release.yml on meshpro/pyfvm-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyfvm-0.5.2a1-cp312-none-any.whl.

File metadata

  • Download URL: pyfvm-0.5.2a1-cp312-none-any.whl
  • Upload date:
  • Size: 62.7 kB
  • Tags: CPython 3.12
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyfvm-0.5.2a1-cp312-none-any.whl
Algorithm Hash digest
SHA256 cf175b73a0df66a54dfedbee6e9a73338b60874e5241bb36285e98e9cc07be5d
MD5 7303edbb6581df3f6d4ca7d87dda5390
BLAKE2b-256 60f68e16158aecbbab160416e6f25423b2c9911d155fa1274bfbdae26e8c1f76

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyfvm-0.5.2a1-cp312-none-any.whl:

Publisher: release.yml on meshpro/pyfvm-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyfvm-0.5.2a1-cp311-none-any.whl.

File metadata

  • Download URL: pyfvm-0.5.2a1-cp311-none-any.whl
  • Upload date:
  • Size: 69.5 kB
  • Tags: CPython 3.11
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyfvm-0.5.2a1-cp311-none-any.whl
Algorithm Hash digest
SHA256 d74bdf3c9f7c810b0166579c12907f087ff22a091372de8996542533aef3b318
MD5 9b61ded6675ca1d3857e2c74c123c0fd
BLAKE2b-256 f4446a0e72a98317a0dd2360847d9401ecab8bb7891c87450f47d6c9ac55aada

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyfvm-0.5.2a1-cp311-none-any.whl:

Publisher: release.yml on meshpro/pyfvm-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyfvm-0.5.2a1-cp310-none-any.whl.

File metadata

  • Download URL: pyfvm-0.5.2a1-cp310-none-any.whl
  • Upload date:
  • Size: 33.4 kB
  • Tags: CPython 3.10
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyfvm-0.5.2a1-cp310-none-any.whl
Algorithm Hash digest
SHA256 c61e3b3719d9749992520b046d0c2594ebcd3416b3d4f13454516a416c0fbb0f
MD5 0953ce4b4458cb7c793be206d389ac7a
BLAKE2b-256 5aa19b5bc6310f771ee68a65e0427456ea8101780fb4fb6a56704da60767ab58

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyfvm-0.5.2a1-cp310-none-any.whl:

Publisher: release.yml on meshpro/pyfvm-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page