-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
add --QuadratureFunction-- QuadratureRule #176
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
25a1a2d
initial commit
lxvm ed92d5d
add documentation
lxvm 126ae5f
typo
lxvm 274a049
rename to QuadratureRule
lxvm 045c3c2
apply format
lxvm 7d7d7ba
Update docs/src/solvers/IntegralSolvers.md
ChrisRackauckas 5a7b144
Update src/quadrules.jl
ChrisRackauckas 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 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 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 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 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,39 @@ | ||
function evalrule(f, p, lb, ub, nodes, weights) | ||
scale = map((u, l) -> (u - l) / 2, ub, lb) | ||
shift = (lb + ub) / 2 | ||
f_ = x -> f(x, p) | ||
xw = ((map(*, scale, x) + shift, w) for (x, w) in zip(nodes, weights)) | ||
# we are basically computing sum(w .* f.(x)) | ||
# unroll first loop iteration to get right types | ||
next = iterate(xw) | ||
next === nothing && throw(ArgumentError("empty quadrature rule")) | ||
(x0, w0), state = next | ||
I = w0 * f_(x0) | ||
next = iterate(xw, state) | ||
while next !== nothing | ||
(xi, wi), state = next | ||
I += wi * f_(xi) | ||
next = iterate(xw, state) | ||
end | ||
return prod(scale) * I | ||
end | ||
|
||
function init_cacheval(alg::QuadratureRule, ::IntegralProblem) | ||
return alg.q(alg.n) | ||
end | ||
|
||
function Integrals.__solvebp_call(cache::IntegralCache, alg::QuadratureRule, | ||
sensealg, lb, ub, p; | ||
reltol = nothing, abstol = nothing, | ||
maxiters = nothing) | ||
prob = build_problem(cache) | ||
if isinplace(prob) | ||
error("QuadratureRule does not support inplace integrands.") | ||
end | ||
@assert prob.batch == 0 | ||
|
||
val = evalrule(cache.f, cache.p, lb, ub, cache.cacheval...) | ||
|
||
err = nothing | ||
SciMLBase.build_solution(prob, alg, val, err, retcode = ReturnCode.Success) | ||
end |
This file contains 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,101 @@ | ||
using Integrals | ||
using FastGaussQuadrature | ||
using StaticArrays | ||
using Test | ||
|
||
f = (x, p) -> prod(y -> cos(p * y), x) | ||
exact_f = (lb, ub, p) -> prod(lu -> (sin(p * lu[2]) - sin(p * lu[1])) / p, zip(lb, ub)) | ||
|
||
# single dim | ||
|
||
""" | ||
trapz(n::Integer) | ||
|
||
Return the weights and nodes on the standard interval [-1,1] of the [trapezoidal | ||
rule](https://en.wikipedia.org/wiki/Trapezoidal_rule). | ||
""" | ||
function trapz(n::Integer) | ||
@assert n > 1 | ||
r = range(-1, 1, length = n) | ||
x = collect(r) | ||
halfh = step(r) / 2 | ||
h = step(r) | ||
w = [(i == 1) || (i == n) ? halfh : h for i in 1:n] | ||
return (x, w) | ||
end | ||
|
||
alg = QuadratureRule(trapz, n = 1000) | ||
|
||
lb = -1.2 | ||
ub = 3.5 | ||
p = 2.0 | ||
|
||
prob = IntegralProblem(f, lb, ub, p) | ||
u = solve(prob, alg).u | ||
|
||
@test u≈exact_f(lb, ub, p) rtol=1e-3 | ||
|
||
# multi-dim | ||
|
||
# here we just form a tensor product of 1d rules to make a 2d rule | ||
function trapz2(n) | ||
x, w = trapz(n) | ||
return [SVector(y, z) for (y, z) in Iterators.product(x, x)], w .* w' | ||
end | ||
|
||
alg = QuadratureRule(trapz2, n = 100) | ||
|
||
lb = SVector(-1.2, -1.0) | ||
ub = SVector(3.5, 3.7) | ||
p = 1.2 | ||
|
||
prob = IntegralProblem(f, lb, ub, p) | ||
u = solve(prob, alg).u | ||
|
||
@test u≈exact_f(lb, ub, p) rtol=1e-3 | ||
|
||
# 1d with inf limits | ||
|
||
g = (x, p) -> p / (x^2 + p^2) | ||
|
||
alg = QuadratureRule(gausslegendre, n = 1000) | ||
|
||
lb = -Inf | ||
ub = Inf | ||
p = 1.0 | ||
|
||
prob = IntegralProblem(g, lb, ub, p) | ||
|
||
@test solve(prob, alg).u≈pi rtol=1e-4 | ||
|
||
# 1d with nout | ||
|
||
g2 = (x, p) -> [p[1] / (x^2 + p[1]^2), p[2] / (x^2 + p[2]^2)] | ||
|
||
alg = QuadratureRule(gausslegendre, n = 1000) | ||
|
||
lb = -Inf | ||
ub = Inf | ||
p = (1.0, 1.3) | ||
|
||
prob = IntegralProblem(g2, lb, ub, p) | ||
|
||
@test solve(prob, alg).u≈[pi, pi] rtol=1e-4 | ||
|
||
#= derivative tests | ||
|
||
using Zygote | ||
|
||
function testf(lb, ub, p, f = f) | ||
prob = IntegralProblem(f, lb, ub, p) | ||
solve(prob, QuadratureRule(trapz, n=200))[1] | ||
end | ||
|
||
lb = -1.2 | ||
ub = 2.0 | ||
p = 3.1 | ||
|
||
dp = Zygote.gradient(p -> testf(lb, ub, p), p) | ||
|
||
@test dp ≈ f(ub, p)-f(lb, p) rtol=1e-4 | ||
=# |
This file contains 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
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.
Any reason it doesn't? That seems like it should be possible. I guess open an issue on this as something to do 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.
We don't know the element type of the output array to allocate. This will be fixed by the IntegralFunction PR