Skip to main content

cubature rules on triangles

Project description

Triangle Cubature Rules

This repo serves as a collection of well-tested triangle cubature rules, i.e. numerical integration schemes for integrals of the form

$$ \int_K f(x, y) ~\mathrm{d}x ~\mathrm{d}y, $$

where $K \subset \mathbb{R}^2$ is a triangle. All cubature rules are based on [1].

Usage

Using the cubature schemes is fairly simple.

from triangle_cubature.cubature_rule import CubatureRuleEnum
from triangle_cubature.integrate import integrate_on_mesh
from triangle_cubature.integrate import integrate_on_triangle
import numpy as np

# specifying the mesh
coordinates = np.array([
  [0., 0.],
  [1., 0.],
  [1., 1.],
  [0., 1.]
])

elements = np.array([
  [0, 1, 2],
  [0, 2, 3]
], dtype=int)


# defining the function to be integrated
# NOTE the function must be able to handle coordinates as array
# of shape (N, 2)
def constant(coordinates: np.ndarray):
    """returns 1"""
    return np.ones(coordinates.shape[0])


# integrating over the whole mesh
integral_on_mesh = integrate_on_mesh(
    f=constant,
    coordinates=coordinates,
    elements=elements,
    cubature_rule=CubatureRuleEnum.MIDPOINT)

# integrating over a single triangle, e.g.
# in this case, the "first" element of the mesh
integral_on_triangle = integrate_on_triangle(
    f=constant,
    triangle=coordinates[elements[0], :],
    cubature_rule=CubatureRuleEnum.MIDPOINT)

print(f'Integral value on mesh: {integral_on_mesh}')
print(f'Integral value on triangle: {integral_on_triangle}')

Available Rules

The available cubature rules can be found in triangle_cubature/cubature_rule.py.

  • CubatureRuleEnum.MIDPOINT
    • degree of exactness: 1
    • Ref: [1]
  • CubatureRuleEnum.LAUFFER_LINEAR
    • degree of exactness: 1
    • Ref: [1]
  • CubatureRuleEnum.SMPLX1
    • degree of exactness: 2
    • Ref: [1]

(Unit) Tests

To run auto tests, you do

python -m unittest discover tests/auto/

The unit tests use sympy to verify the degree of exactness of the implemented cubature rules, i.e. creates random polynomials $p_d$ of the expected degree of exactness $d$ and compares the exact result of $\int_K p_d(x, y) ~\mathrm{d}x ~\mathrm{d}y$ to the value obtained with the cubature rule at hand.

References

  • [1] Stenger, Frank. 'Approximate Calculation of Multiple Integrals (A. H. Stroud)'. SIAM Review 15, no. 1 (January 1973): 234-35. https://doi.org/10.1137/1015023. p. 306-315

Project details


Download files

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

Source Distribution

triangle_cubature-1.0.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

triangle_cubature-1.0.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file triangle_cubature-1.0.0.tar.gz.

File metadata

  • Download URL: triangle_cubature-1.0.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.18

File hashes

Hashes for triangle_cubature-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c0eb048bbeb9712df6a6ed96a33c550e5a56811041f052ffe26eddd86f83a232
MD5 a983a652da0249e120e7169b4627be58
BLAKE2b-256 99378047b0b020f75b751a60f11b77e0992e29205854f683dea1ad404209aa8e

See more details on using hashes here.

File details

Details for the file triangle_cubature-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for triangle_cubature-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 27523cd7482df999df8d49861137afcfdd53e7f27f06f64600fa95e1344a8997
MD5 1ff6005a7555419b475e469da94eeb44
BLAKE2b-256 a80156127f485e0a2fde7151ba0054fe2ec22793173ae38a94ecab91476d102b

See more details on using hashes here.

Supported by

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