-
-
Notifications
You must be signed in to change notification settings - Fork 236
Creating BVProblem from ODESystem
#3251
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
Changes from 20 commits
9733460
d95e4a7
b3da813
86c82ce
4affeac
a3429ea
f751fbb
a9fdfd6
a9f2106
18fdd5f
9d65a33
999ec30
9226ad6
0cb4893
67d8164
25988f3
bb28d4f
b2bf7c0
3751c2a
ef1f089
d23d6f7
0fda8a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -469,6 +469,90 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, | |
| initializeprobpmap = initializeprobpmap) | ||
| end | ||
|
|
||
| """ | ||
| ```julia | ||
| SciMLBase.BVProblem{iip}(sys::AbstractODESystem, u0map, tspan, | ||
| parammap = DiffEqBase.NullParameters(); | ||
| version = nothing, tgrad = false, | ||
| jac = true, sparse = true, | ||
| simplify = false, | ||
| kwargs...) where {iip} | ||
| ``` | ||
|
|
||
| Create a `BVProblem` from the [`ODESystem`](@ref). The arguments `dvs` and | ||
| `ps` are used to set the order of the dependent variable and parameter vectors, | ||
| respectively. `u0map` should be used to specify the initial condition, or be a function returning an initial condition. | ||
| """ | ||
| function SciMLBase.BVProblem(sys::AbstractODESystem, args...; kwargs...) | ||
| BVProblem{true}(sys, args...; kwargs...) | ||
| end | ||
|
|
||
| function SciMLBase.BVProblem(sys::AbstractODESystem, | ||
| u0map::StaticArray, | ||
| args...; | ||
| kwargs...) | ||
| BVProblem{false, SciMLBase.FullSpecialize}(sys, u0map, args...; kwargs...) | ||
| end | ||
|
|
||
| function SciMLBase.BVProblem{true}(sys::AbstractODESystem, args...; kwargs...) | ||
| BVProblem{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) | ||
| end | ||
|
|
||
| function SciMLBase.BVProblem{false}(sys::AbstractODESystem, args...; kwargs...) | ||
| BVProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) | ||
| end | ||
|
|
||
| function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [], | ||
| tspan = get_tspan(sys), | ||
| parammap = DiffEqBase.NullParameters(); | ||
| version = nothing, tgrad = false, | ||
| callback = nothing, | ||
| check_length = true, | ||
| warn_initialize_determined = true, | ||
| eval_expression = false, | ||
| eval_module = @__MODULE__, | ||
| kwargs...) where {iip, specialize} | ||
| if !iscomplete(sys) | ||
| error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `BVProblem`") | ||
| end | ||
|
|
||
| f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; | ||
| t = tspan !== nothing ? tspan[1] : tspan, | ||
| check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) | ||
|
|
||
| cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) | ||
| kwargs = filter_kwargs(kwargs) | ||
|
|
||
| kwargs1 = (;) | ||
| if cbs !== nothing | ||
| kwargs1 = merge(kwargs1, (callback = cbs,)) | ||
| end | ||
|
|
||
| # Construct initial conditions. | ||
| _u0 = u0 isa Function ? u0(tspan[1]) : u0 | ||
vyudu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Define the boundary conditions. | ||
| bc = if iip | ||
| (residual, u, p, t) -> (residual .= u[1] .- _u0) | ||
| else | ||
| (u, p, t) -> (u[1] - _u0) | ||
| end | ||
|
|
||
| return BVProblem{iip}(f, bc, _u0, tspan, p; kwargs1..., kwargs...) | ||
| end | ||
|
|
||
| get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") | ||
|
|
||
| @inline function SymbolicUtils.Code.create_array(::Type{<:Base.ReinterpretArray}, ::Nothing, | ||
| ::Val{1}, ::Val{dims}, elems...) where {dims} | ||
| [elems...] | ||
| end | ||
|
|
||
| @inline function SymbolicUtils.Code.create_array( | ||
| ::Type{<:Base.ReinterpretArray}, T, ::Val{1}, ::Val{dims}, elems...) where {dims} | ||
| T[elems...] | ||
| end | ||
|
||
|
|
||
| """ | ||
| ```julia | ||
| DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| using BoundaryValueDiffEq, OrdinaryDiffEq | ||
| using ModelingToolkit | ||
| using ModelingToolkit: t_nounits as t, D_nounits as D | ||
|
|
||
| solvers = [MIRK4, RadauIIa5, LobattoIIIa3] | ||
|
|
||
| @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 | ||
| @variables x(t)=1.0 y(t)=2.0 | ||
|
|
||
| eqs = [D(x) ~ α * x - β * x * y, | ||
| D(y) ~ -γ * y + δ * x * y] | ||
|
|
||
| u0map = [:x => 1.0, :y => 2.0] | ||
| parammap = [:α => 7.5, :β => 4, :γ => 8.0, :δ => 5.0] | ||
vyudu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| tspan = (0.0, 10.0) | ||
|
|
||
| @mtkbuild lotkavolterra = ODESystem(eqs, t) | ||
| op = ODEProblem(lotkavolterra, u0map, tspan, parammap) | ||
| osol = solve(op, Vern9()) | ||
|
|
||
| bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( | ||
| lotkavolterra, u0map, tspan, parammap; eval_expression = true) | ||
|
|
||
| for solver in solvers | ||
| sol = solve(bvp, solver(), dt = 0.01) | ||
| @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) | ||
| @test sol.u[1] == [1.0, 2.0] | ||
| end | ||
|
|
||
| # Test out of place | ||
| bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}( | ||
| lotkavolterra, u0map, tspan, parammap; eval_expression = true) | ||
|
|
||
| for solver in solvers | ||
| sol = solve(bvp2, solver(), dt = 0.01) | ||
| @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) | ||
| @test sol.u[1] == [1.0, 2.0] | ||
| end | ||
|
|
||
| ### Testing on pendulum | ||
|
|
||
| @parameters g=9.81 L=1.0 | ||
| @variables θ(t) = π / 2 | ||
|
|
||
| eqs = [D(D(θ)) ~ -(g / L) * sin(θ)] | ||
|
|
||
| @mtkbuild pend = ODESystem(eqs, t) | ||
|
|
||
| u0map = [θ => π / 2, D(θ) => π / 2] | ||
| parammap = [:L => 1.0, :g => 9.81] | ||
| tspan = (0.0, 6.0) | ||
|
|
||
| op = ODEProblem(pend, u0map, tspan, parammap) | ||
| osol = solve(op, Vern9()) | ||
|
|
||
| bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) | ||
| for solver in solvers | ||
| sol = solve(bvp, solver(), dt = 0.01) | ||
| @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) | ||
| @test sol.u[1] == [π / 2, π / 2] | ||
| end | ||
|
|
||
| # Test out-of-place | ||
| bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) | ||
|
|
||
| for solver in solvers | ||
| sol = solve(bvp2, solver(), dt = 0.01) | ||
| @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) | ||
| @test sol.u[1] == [π / 2, π / 2] | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.