-
Notifications
You must be signed in to change notification settings - Fork 94
Add integral and localintegral #1327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+499
−38
Closed
Changes from 44 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
d2e040a
Add integral and localintegral
juliohm fcb384f
Add tests from MeshIntegrals.jl
juliohm f1dd042
Increase default order to 100
juliohm 9bd228c
Rename order --> n
juliohm 55c1649
Propagate number type
juliohm 98bdd41
Change default to n=3
juliohm 8f3c1cf
Update tests
juliohm fcb05d3
Fix formatting issues
juliohm ecbcf48
Add integration method for ConeSurface
juliohm e5795f0
Add more special cases
juliohm da77b44
Add method for Domain
juliohm c5dd5ed
Fix typo
juliohm 623861b
Remove method for Point
juliohm d5754f8
Add more tests
juliohm 9252b6e
Fix Cylinder/CylinderSurface bottom and top
juliohm f9fd569
Minor refactor
juliohm a6d59f1
Refactor tests
juliohm 00c6e9e
Add methods for geometries with infinite measure
juliohm 4a612cf
Minor refactor
juliohm 55fe6d9
Add TODO in test/integration.jl
juliohm 07c68ae
Add more TODO tests
juliohm 98e17fe
Add more implementations
juliohm 0b09024
Fix formatting issues
juliohm 29e4507
Fix composition in ray method
juliohm e903f57
fix CylinderSurface test
JoshuaLampert 8e08a41
Apply suggestions from code review
JoshuaLampert 433bf24
Remove localintegral methods
juliohm f014999
Add tests for infinite geometries
juliohm 36c42d5
Remove 0 <= t <= 1 check from Chain
juliohm f46c538
Add more tests
juliohm 5692ebd
Refactor change of variable
juliohm b52f395
Fix parametrization of Chain
juliohm 7bd93a7
Fix tests for Rope and Ring
juliohm ffb13cc
Minor refactor
juliohm 2a0089a
Relax rtol in Ring test
juliohm b56976e
Fix formatting issues
juliohm 408c516
Add more tests
juliohm 0b982e4
Fix formatting issues
juliohm 5685d5e
Refactor trans kwarg for multivariate case
juliohm c6f2fa1
Add methods for simplices
juliohm 32de14c
Add more tests
juliohm 9daa5dd
Add method for Multi
juliohm 947f967
Fix formatting issues
juliohm dd4082d
Add more tests
juliohm bcf0275
Incorporate suggestions
juliohm d316707
Add comments
juliohm afff1a5
Rely on IntegrationInterface.jl
juliohm f0acc81
Refactor in terms of backends
juliohm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # ------------------------------------------------------------------ | ||
| # Licensed under the MIT License. See LICENSE in the project root. | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| """ | ||
| integral(fun, geom; n=3) | ||
|
|
||
| Calculate the integral over the `geom`etry of the `fun`ction that maps | ||
| [`Point`](@ref)s to values in a linear space. | ||
|
|
||
| integral(fun, dom; n=3) | ||
|
|
||
| Alternatively, calculate the integral over the `dom`ain (e.g., mesh) by | ||
| summing the integrals for each constituent geometry. | ||
|
|
||
| Polynomials of degree up to `2n-1` are integrated exactly. | ||
|
|
||
| See also [`localintegral`](@ref). | ||
| """ | ||
| integral(fun, geom::Geometry; n=3) = localintegral(fun ∘ geom, geom; n) | ||
|
|
||
| integral(fun, cylsurf::CylinderSurface; n=3) = | ||
| localintegral(fun ∘ cylsurf, cylsurf; n) + integral(fun, top(cylsurf); n) + integral(fun, bottom(cylsurf); n) | ||
|
|
||
| integral(fun, conesurf::ConeSurface; n=3) = | ||
| localintegral(fun ∘ conesurf, conesurf; n) + integral(fun, base(conesurf); n) | ||
|
|
||
| integral(fun, frustumsurf::FrustumSurface; n=3) = | ||
| localintegral(fun ∘ frustumsurf, frustumsurf; n) + | ||
| integral(fun, top(frustumsurf); n) + | ||
| integral(fun, bottom(frustumsurf); n) | ||
|
|
||
| integral(fun, multi::Multi; n=3) = sum(integral(fun, geom; n) for geom in parent(multi)) | ||
|
|
||
| integral(fun, dom::Domain; n=3) = sum(integral(fun, geom; n) for geom in dom) | ||
|
|
||
| """ | ||
| localintegral(fun, geom; n=3) | ||
|
|
||
| Calculate the integral over the `geom`etry of the `fun`ction that maps | ||
| parametric coordinates `uvw` to values in a linear space. | ||
|
|
||
| Polynomials of degree up to `2n-1` are integrated exactly. | ||
|
|
||
| See also [`integral`](@ref). | ||
| """ | ||
| localintegral(fun, geom::Geometry; n=3) = _uvwintegral(fun, geom, n) | ||
|
|
||
| # ray is parametrized over [0, ∞] interval | ||
| localintegral(fun, ray::Ray; n=3) = _uvwintegral(fun, ray, n, trans=t -> @. t / (1 - t)) | ||
|
|
||
| # line is parametrized over [-∞, ∞] interval | ||
| localintegral(fun, line::Line; n=3) = _uvwintegral(fun, line, n, trans=t -> @. log(t) - log(1 - t)) | ||
|
|
||
| # plane is parametrized over [-∞, ∞] interval | ||
| localintegral(fun, plane::Plane; n=3) = _uvwintegral(fun, plane, n, trans=t -> @. log(t) - log(1 - t)) | ||
|
|
||
| # triangle is parametrized with barycentric coordinates | ||
| localintegral(fun, tri::Triangle; n=3) = _uvwintegral(fun, tri, n, trans=t -> (t[1] * t[2], t[2] - t[1] * t[2])) | ||
|
|
||
| # specialize quadrangle for performance | ||
| localintegral(fun, quad::Quadrangle; n=3) = _uvwintegral(fun, quad, n) | ||
|
|
||
| # tetrahedron is parametrized with barycentric coordinates | ||
| localintegral(fun, tetra::Tetrahedron; n=3) = | ||
| _uvwintegral(fun, tetra, n, trans=t -> (t[2] * t[3] - t[1] * t[2] * t[3], t[1] * t[2] * t[3], t[3] - t[2] * t[3])) | ||
|
|
||
| # ----------------- | ||
| # HELPER FUNCTIONS | ||
| # ----------------- | ||
|
|
||
| function _uvwintegral(fun, geom, n; trans=identity) | ||
| # parametric dimension and number type | ||
| N = paramdim(geom) | ||
| T = numtype(lentype(geom)) | ||
|
|
||
| # Gauss-Legendre quadrature points and weights | ||
| ts, ws = gausslegendre(n) | ||
| tgrid = Iterators.product(ntuple(_ -> T.(ts), N)...) | ||
| wgrid = Iterators.product(ntuple(_ -> T.(ws), N)...) | ||
|
|
||
| # map quadrature points in [-1, 1] to parametric coordinates in [0, 1], | ||
| # and then map parametric coordinates in [0, 1] to uvw parametrization | ||
| g = trans ∘ (t -> @. (t + 1) / 2) | ||
|
|
||
| # compute integral with change of variable and differential element | ||
| Σwᵢfᵢ = sum(zip(tgrid, wgrid)) do (t, w) | ||
| uvw = g(t) | ||
| prod(w) * fun(uvw...) * differential(geom, uvw) | ||
| end | ||
|
|
||
| # adjust for change of variable | ||
| Σwᵢfᵢ / 2^N | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this specialization improve performance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We had a method for general
Polygonthat got erased. The method for general polygon relied on discretization, and this method forQuadranglerelies on direct sampling with the parametric function. I will review the code to see if this method is still necessary.