# How do I use FastGaussQuadrature.jl to compute the integral of a 2d triangle element and 3d tetrahedron element

**URL:** <https://discourse.julialang.org/t/how-do-i-use-fastgaussquadrature-jl-to-compute-the-integral-of-a-2d-triangle-element-and-3d-tetrahedron-element/102376>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 1, 2023, 10:31pm UTC](https://discourse.julialang.org/t/how-do-i-use-fastgaussquadrature-jl-to-compute-the-integral-of-a-2d-triangle-element-and-3d-tetrahedron-element/102376 "2023-08-01T22:31:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [August 1, 2023, 10:31pm UTC](https://discourse.julialang.org/t/how-do-i-use-fastgaussquadrature-jl-to-compute-the-integral-of-a-2d-triangle-element-and-3d-tetrahedron-element/102376/1 "2023-08-01T22:31:36Z")

</div>

I am learning about finite element methods, and was trying to implement some examples in Julia. So  
one of the common steps in FEM is implementing Gauss Quadrature for approximating the integral of each element in the mesh.

I used `QuadGK.jl` for computing the integral in 1d. However, the `QuadGK.jl` package docs indicate that the package only solves [1-dimensional integrals](https://juliamath.github.io/QuadGK.jl/stable/). However, for solving 2 and 3 dimensional elements, I imagine I need to use a package like `FastGaussQuadrature.jl`. The QuadGK function also includes an api or function signature to handle the change of interval, such as `integral, error = quadgk(x -> cos(200x), 0, 1)`. However, I did not find something similar for `FastGaussQuadrature`.

My specific question is, how do I use `FastGaussQuadrature` to compute the integral of a 2d (triangle) or 3d (tetrahedral) element. That means, given a set of nodes/points and their coordinates, `[x, y, z]`, how would I use the package to compute the integral for the loading function `f` over a mesh element, or how would I use this package to compute the integral over a mesh element for the stiffness side of the equation.

I believe that FastGaussQuadrature computes the quadrature weights assuming an interval from [-1.0, 1.0]. So if my elements are of a different size in 2 or 3 dimensions, how would I adapt that interval to match the [-1.0, 1.0].

Thanks.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 1, 2023, 11:56pm UTC](https://discourse.julialang.org/t/how-do-i-use-fastgaussquadrature-jl-to-compute-the-integral-of-a-2d-triangle-element-and-3d-tetrahedron-element/102376/2 "2023-08-01T23:56:43Z")

</div>

> [@00krishna](#):
>
> However, for solving 2 and 3 dimensional elements, I imagine I need to use a package like `FastGaussQuadrature.jl`.

FastGaussQuadrature constructs 1d quadrature rules. Of course, you can integrate over a 2d or 3d region by doing nested integrals (either with QuadGK or via tensor products of quadrature rules), or you can use a multi-dimensional integration package like [HCubature.jl](https://github.com/JuliaMath/HCubature.jl). See also

> [@2D integration over non-rectangular domain using cubature](https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991):
>
> Hello everyone, I have a very basic question. I’m looking for the most efficient way to perform the following very classical integral: [image] For now I’m using hquadrature of the cubature package in two steps. This works, but I’m not sure whether this is the most efficient way. More generally, I would like to know, how to perform multidimensional integration over non-rectangular domains. It seems cubature does not allow bounds to be functions. Am I correct? Many thanks in advance Olivi…

If you want a fixed-order quadrature rule for a 2d triangle or 3d tetrahedron, see e.g. the [SimplexQuad.jl package](https://github.com/eschnett/SimplexQuad.jl), or [FEMQuad.jl](https://github.com/JuliaFEM/FEMQuad.jl), or the [quadrature routines in NESSie.jl](https://tkemmer.github.io/NESSie.jl/latest/lib/quadrature/) or …

---

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [August 2, 2023, 5:09am UTC](https://discourse.julialang.org/t/how-do-i-use-fastgaussquadrature-jl-to-compute-the-integral-of-a-2d-triangle-element-and-3d-tetrahedron-element/102376/3 "2023-08-02T05:09:05Z")

</div>

@stevengj yes, this makes more sense now. I can use nested integrals, that is the piece that I was missing.

So I can basically use `hcubature` for the multidimensional integral, and then just integrate over the first two points, and then integrate over the third point. I found a good article that explains this piece a bit better. Thanks again for the help.
