-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Fast path for Two Point BVPs #109
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3388de9
Fast path for Two Point BVPs
avik-pal acf1276
Remove unnecessary imports
avik-pal a6f4592
Precompute a conservative colorvec
avik-pal c49f72b
Formatting
avik-pal 755a818
Using wrong mesh
avik-pal 918e94e
Add BVPSOL and BVPM2
avik-pal 7a9608e
Rename extension
avik-pal a57a54f
Make it consistent with out parameters
avik-pal 68d9ded
Add tests for ODEInterface integration and remove compat with old ver…
f913b9e
Add compat entries
avik-pal 5f0564b
Try with NonlinearSolve 1
avik-pal aea5795
Old versions can't reliably handle prototypes
avik-pal 8924e95
Fix Dispatches if Differential Equations is loaded
avik-pal b250867
Send back to 4 to test
ChrisRackauckas 2d5dc83
Update ext/BoundaryValueDiffEqODEInterfaceExt.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
module BoundaryValueDiffEqODEInterfaceExt | ||
|
||
using SciMLBase, BoundaryValueDiffEq, ODEInterface | ||
import ODEInterface: OptionsODE, OPT_ATOL, OPT_RTOL, OPT_METHODCHOICE, OPT_DIAGNOSTICOUTPUT, | ||
OPT_ERRORCONTROL, OPT_SINGULARTERM, OPT_MAXSTEPS, OPT_BVPCLASS, OPT_SOLMETHOD, | ||
OPT_RHS_CALLMODE, RHS_CALL_INSITU, evalSolution | ||
import ODEInterface: Bvpm2, bvpm2_init, bvpm2_solve, bvpm2_destroy, bvpm2_get_x | ||
import ODEInterface: bvpsol | ||
|
||
function _test_bvpm2_bvpsol_problem_criteria(_, ::SciMLBase.StandardBVProblem, alg::Symbol) | ||
throw(ArgumentError("$(alg) does not support standard BVProblem. Only TwoPointBVProblem is supported.")) | ||
end | ||
function _test_bvpm2_bvpsol_problem_criteria(prob, ::TwoPointBVProblem, alg::Symbol) | ||
@assert isinplace(prob) "$(alg) only supports inplace TwoPointBVProblem!" | ||
end | ||
|
||
#------ | ||
# BVPM2 | ||
#------ | ||
## TODO: We can specify Drhs using forwarddiff if we want to | ||
function SciMLBase.__solve(prob::BVProblem, alg::BVPM2; dt = 0.0, reltol = 1e-3, kwargs...) | ||
_test_bvpm2_bvpsol_problem_criteria(prob, prob.problem_type, :BVPM2) | ||
|
||
has_initial_guess = prob.u0 isa AbstractVector{<:AbstractArray} | ||
no_odes, n, u0 = if has_initial_guess | ||
length(first(prob.u0)), (length(prob.u0) - 1), reduce(hcat, prob.u0) | ||
else | ||
dt ≤ 0 && throw(ArgumentError("dt must be positive")) | ||
length(prob.u0), Int(cld((prob.tspan[2] - prob.tspan[1]), dt)), prob.u0 | ||
end | ||
|
||
mesh = collect(range(prob.tspan[1], stop = prob.tspan[2], length = n + 1)) | ||
|
||
no_left_bc = length(first(prob.f.bcresid_prototype.x)) | ||
|
||
initial_guess = Bvpm2() | ||
bvpm2_init(initial_guess, no_odes, no_left_bc, mesh, u0, eltype(u0)[], | ||
alg.max_num_subintervals) | ||
|
||
bvp2m_f(t, u, du) = prob.f(du, u, prob.p, t) | ||
bvp2m_bc(ya, yb, bca, bcb) = prob.bc((bca, bcb), (ya, yb), prob.p) | ||
|
||
opt = OptionsODE(OPT_RTOL => reltol, OPT_METHODCHOICE => alg.method_choice, | ||
OPT_DIAGNOSTICOUTPUT => alg.diagnostic_output, | ||
OPT_SINGULARTERM => alg.singular_term, OPT_ERRORCONTROL => alg.error_control) | ||
|
||
sol, retcode, stats = bvpm2_solve(initial_guess, bvp2m_f, bvp2m_bc, opt) | ||
retcode = retcode ≥ 0 ? ReturnCode.Success : ReturnCode.Failure | ||
|
||
x_mesh = bvpm2_get_x(sol) | ||
sol_final = DiffEqBase.build_solution(prob, alg, x_mesh, | ||
eachcol(evalSolution(sol, x_mesh)); retcode, stats) | ||
|
||
bvpm2_destroy(initial_guess) | ||
bvpm2_destroy(sol) | ||
|
||
return sol_final | ||
end | ||
|
||
#------- | ||
# BVPSOL | ||
#------- | ||
function SciMLBase.__solve(prob::BVProblem, alg::BVPSOL; maxiters = 1000, reltol = 1e-3, | ||
dt = 0.0, verbose = true, kwargs...) | ||
_test_bvpm2_bvpsol_problem_criteria(prob, prob.problem_type, :BVPSOL) | ||
@assert isa(prob.p, SciMLBase.NullParameters) "BVPSOL only supports NullParameters!" | ||
@assert isa(prob.u0, AbstractVector{<:AbstractArray}) "BVPSOL requires a vector of initial guesses!" | ||
n, u0 = (length(prob.u0) - 1), reduce(hcat, prob.u0) | ||
mesh = collect(range(prob.tspan[1], stop = prob.tspan[2], length = n + 1)) | ||
|
||
opt = OptionsODE(OPT_RTOL => reltol, OPT_MAXSTEPS => maxiters, | ||
OPT_BVPCLASS => alg.bvpclass, OPT_SOLMETHOD => alg.sol_method, | ||
OPT_RHS_CALLMODE => RHS_CALL_INSITU) | ||
|
||
f!(t, u, du) = prob.f(du, u, prob.p, t) | ||
function bc!(ya, yb, r) | ||
ra = first(prob.f.bcresid_prototype.x) | ||
rb = last(prob.f.bcresid_prototype.x) | ||
prob.bc((ra, rb), (ya, yb), prob.p) | ||
r[1:length(ra)] .= ra | ||
r[(length(ra) + 1):(length(ra) + length(rb))] .= rb | ||
return r | ||
end | ||
|
||
sol_t, sol_x, retcode, stats = bvpsol(f!, bc!, mesh, u0, alg.odesolver, opt) | ||
|
||
if verbose | ||
if retcode == -3 | ||
@warn "Integrator failed to complete the trajectory" | ||
elseif retcode == -4 | ||
@warn "Gauss Newton method failed to converge" | ||
elseif retcode == -5 | ||
@warn "Given initial values inconsistent with separable linear bc" | ||
elseif retcode == -6 | ||
@warn """Iterative refinement faild to converge for `sol_method=0` | ||
Termination since multiple shooting condition or | ||
condition of Jacobian is too bad for `sol_method=1`""" | ||
elseif retcode == -8 | ||
@warn "Condensing algorithm for linear block system fails, try `sol_method=1`" | ||
elseif retcode == -9 | ||
@warn "Sparse linear solver failed" | ||
elseif retcode == -10 | ||
@warn "Real or integer work-space exhausted" | ||
elseif retcode == -11 | ||
@warn "Rank reduction failed - resulting rank is zero" | ||
end | ||
end | ||
|
||
return DiffEqBase.build_solution(prob, alg, sol_t, eachcol(sol_x); | ||
retcode = retcode ≥ 0 ? ReturnCode.Success : ReturnCode.Failure, stats) | ||
end | ||
|
||
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
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
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.
Not so sure about that.
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.
This change to NewtonRaphson was before the TR bugs were fixed. We can revert this part. The
autodiff = true
is redundant wince that is the current default.