- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 214
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
BVProblem with constraints #3323
Merged
+559
−12
Merged
Changes from 56 commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
9733460
init
vyudu d95e4a7
Merge remote-tracking branch 'origin/master' into MTK
vyudu b3da813
up
vyudu 86c82ce
Merge remote-tracking branch 'origin/master' into MTK
vyudu 4affeac
up
vyudu a3429ea
up
vyudu f751fbb
up
vyudu a9fdfd6
up
vyudu a9f2106
up
vyudu 18fdd5f
up
vyudu 9d65a33
fixing create_array
vyudu 999ec30
revert Project.toml
vyudu 9226ad6
Up
vyudu 0cb4893
Merge remote-tracking branch 'origin/master' into MTK
vyudu 67d8164
formatting
vyudu 25988f3
up
vyudu bb28d4f
up
vyudu b2bf7c0
fix
vyudu 3751c2a
up
vyudu ef1f089
up
vyudu d23d6f7
Merge remote-tracking branch 'origin/master' into MTK
vyudu 2a25200
extend BVProblem for constraint equations
vyudu 50504ab
adding tests
vyudu 5d082ab
up
vyudu b83e003
refactor the bc creation function
vyudu db5eb66
up
vyudu e802946
test update
vyudu e74e047
fix
vyudu 86d4144
test more solvers:
vyudu ec386fe
Refactor constraints
vyudu 90ce80d
refactor tests
vyudu a15c670
fix sym validation
vyudu c6ef04a
remove file
vyudu 7878225
up
vyudu 5bcfdff
up
vyudu 0493b5d
remove lines
vyudu 1d32b6e
up
vyudu 2b3ca96
up
vyudu 0324522
fix typo
vyudu 2a079be
Fix setter
vyudu d70a470
fix
vyudu 37092f1
lower tol
vyudu e5eb8bd
fix Project.toml
vyudu 2ae79ae
revert to OrdinaryDiffEq
vyudu 8ae2803
merge master
vyudu 13a242c
update to use updated codegen
vyudu 2fcb9c9
up
vyudu 25b56d7
working codegen
vyudu c35b797
revert to OrdinaryDiffEqDefault
vyudu 25e84db
use MIRK
vyudu e6a6932
up
vyudu 5e5c24c
revert to OrdinaryDiffEq
vyudu 5338d4f
tests passing
vyudu 810d4fa
remove problematic tests, codegen assumes MTKParameters
vyudu 6740b8c
test fix
vyudu 603c894
Update src/systems/diffeqs/odesystem.jl
ChrisRackauckas b10a4a6
Merge branch 'master' into BVP-with-constraints
ChrisRackauckas 9b492cd
Merge remote-tracking branch 'vyudu/BVP-with-constraints' into BVP-wi…
vyudu dce19c7
Merge remote-tracking branch 'origin' into BVP-with-constraints
vyudu 3642e1b
Merge branch 'master' into BVP-with-constraints
vyudu 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 |
---|---|---|
|
@@ -964,6 +964,7 @@ for prop in [:eqs | |
:structure | ||
:op | ||
:constraints | ||
:constraintsystem | ||
:controls | ||
:loss | ||
:bcs | ||
|
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,265 @@ | ||
### TODO: update when BoundaryValueDiffEqAscher is updated to use the normal boundary condition conventions | ||
|
||
using OrdinaryDiffEq | ||
using BoundaryValueDiffEqMIRK, BoundaryValueDiffEqAscher | ||
using BenchmarkTools | ||
using ModelingToolkit | ||
using SciMLBase | ||
using ModelingToolkit: t_nounits as t, D_nounits as D | ||
|
||
### Test Collocation solvers on simple problems | ||
solvers = [MIRK4] | ||
daesolvers = [Ascher2, Ascher4, Ascher6] | ||
|
||
let | ||
@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] | ||
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) | ||
|
||
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) | ||
|
||
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 | ||
end | ||
|
||
### Testing on pendulum | ||
let | ||
@parameters g=9.81 L=1.0 | ||
@variables θ(t) = π / 2 θ_t(t) | ||
|
||
eqs = [D(θ) ~ θ_t | ||
D(θ_t) ~ -(g / L) * sin(θ)] | ||
|
||
@mtkbuild pend = ODESystem(eqs, t) | ||
|
||
u0map = [θ => π / 2, θ_t => π / 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 | ||
end | ||
|
||
################################################################## | ||
### ODESystem with constraint equations, DAEs with constraints ### | ||
################################################################## | ||
|
||
# Test generation of boundary condition function using `generate_function_bc`. Compare solutions to manually written boundary conditions | ||
let | ||
@parameters α=1.5 β=1.0 γ=3.0 δ=1.0 | ||
@variables x(..) y(..) | ||
eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), | ||
D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] | ||
|
||
tspan = (0., 1.) | ||
@mtkbuild lksys = ODESystem(eqs, t) | ||
|
||
function lotkavolterra!(du, u, p, t) | ||
du[1] = p[1]*u[1] - p[2]*u[1]*u[2] | ||
du[2] = -p[4]*u[2] + p[3]*u[1]*u[2] | ||
end | ||
|
||
function lotkavolterra(u, p, t) | ||
[p[1]*u[1] - p[2]*u[1]*u[2], -p[4]*u[2] + p[3]*u[1]*u[2]] | ||
end | ||
|
||
# Test with a constraint. | ||
constr = [y(0.5) ~ 2.] | ||
@mtkbuild lksys = ODESystem(eqs, t; constraints = constr) | ||
|
||
function bc!(resid, u, p, t) | ||
resid[1] = u(0.0)[1] - 1. | ||
resid[2] = u(0.5)[2] - 2. | ||
end | ||
function bc(u, p, t) | ||
[u(0.0)[1] - 1., u(0.5)[2] - 2.] | ||
end | ||
|
||
u0 = [1., 1.] | ||
tspan = (0., 1.) | ||
p = [1.5, 1., 1., 3.] | ||
bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, u0, tspan, p) | ||
bvpi2 = SciMLBase.BVProblem(lotkavolterra, bc, u0, tspan, p) | ||
bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.]) | ||
bvpi4 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.]) | ||
|
||
sol1 = @btime solve($bvpi1, MIRK4(), dt = 0.01) | ||
sol2 = @btime solve($bvpi2, MIRK4(), dt = 0.01) | ||
sol3 = @btime solve($bvpi3, MIRK4(), dt = 0.01) | ||
sol4 = @btime solve($bvpi4, MIRK4(), dt = 0.01) | ||
@test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why | ||
end | ||
|
||
function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.05, atol = 1e-2) | ||
for solver in solvers | ||
println("Solver: $solver") | ||
sol = @btime solve($prob, $solver(), dt = $dt, abstol = $atol) | ||
@test SciMLBase.successful_retcode(sol.retcode) | ||
p = prob.p; t = sol.t; bc = prob.f.bc | ||
ns = length(prob.u0) | ||
if isinplace(prob.f) | ||
resid = zeros(ns) | ||
bc(resid, sol, p, t) | ||
@test isapprox(zeros(ns), resid; atol) | ||
@show resid | ||
else | ||
@test isapprox(zeros(ns), bc(sol, p, t); atol) | ||
@show bc(sol, p, t) | ||
end | ||
|
||
for (k, v) in u0map | ||
@test sol[k][1] == v | ||
end | ||
|
||
# for cons in constraints | ||
# @test sol[cons.rhs - cons.lhs] ≈ 0 | ||
# end | ||
|
||
for eq in equations | ||
@test sol[eq] ≈ 0 | ||
end | ||
end | ||
end | ||
|
||
# Simple ODESystem with BVP constraints. | ||
let | ||
@parameters α=1.5 β=1.0 γ=3.0 δ=1.0 | ||
@variables x(..) y(..) | ||
|
||
eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), | ||
D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] | ||
|
||
u0map = [] | ||
tspan = (0.0, 1.0) | ||
guess = [x(t) => 4.0, y(t) => 2.0] | ||
constr = [x(.6) ~ 3.5, x(.3) ~ 7.] | ||
@mtkbuild lksys = ODESystem(eqs, t; constraints = constr) | ||
|
||
bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses = guess) | ||
test_solvers(solvers, bvp, u0map, constr; dt = 0.05) | ||
|
||
# Testing that more complicated constraints give correct solutions. | ||
constr = [y(.2) + x(.8) ~ 3., y(.3) ~ 2.] | ||
@mtkbuild lksys = ODESystem(eqs, t; constraints = constr) | ||
bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, u0map, tspan; guesses = guess) | ||
test_solvers(solvers, bvp, u0map, constr; dt = 0.05) | ||
|
||
constr = [α * β - x(.6) ~ 0.0, y(.2) ~ 3.] | ||
@mtkbuild lksys = ODESystem(eqs, t; constraints = constr) | ||
bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses = guess) | ||
test_solvers(solvers, bvp, u0map, constr) | ||
end | ||
|
||
# Cartesian pendulum from the docs. | ||
# DAE IVP solved using BoundaryValueDiffEq solvers. | ||
# let | ||
# @parameters g | ||
# @variables x(t) y(t) [state_priority = 10] λ(t) | ||
# eqs = [D(D(x)) ~ λ * x | ||
# D(D(y)) ~ λ * y - g | ||
# x^2 + y^2 ~ 1] | ||
# @mtkbuild pend = ODESystem(eqs, t) | ||
# | ||
# tspan = (0.0, 1.5) | ||
# u0map = [x => 1, y => 0] | ||
# pmap = [g => 1] | ||
# guess = [λ => 1] | ||
# | ||
# prob = ODEProblem(pend, u0map, tspan, pmap; guesses = guess) | ||
# osol = solve(prob, Rodas5P()) | ||
# | ||
# zeta = [0., 0., 0., 0., 0.] | ||
# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses = guess) | ||
# | ||
# for solver in solvers | ||
# sol = solve(bvp, solver(zeta), dt = 0.001) | ||
# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) | ||
# conditions = getfield.(equations(pend)[3:end], :rhs) | ||
# @test isapprox([sol[conditions][1]; sol[x][1] - 1; sol[y][1]], zeros(5), atol = 0.001) | ||
# end | ||
# | ||
# bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) | ||
# for solver in solvers | ||
# sol = solve(bvp, solver(zeta), dt = 0.01) | ||
# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) | ||
# conditions = getfield.(equations(pend)[3:end], :rhs) | ||
# @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0 | ||
# end | ||
# end | ||
|
||
# Adding a midpoint boundary constraint. | ||
# Solve using BVDAE solvers. | ||
# let | ||
# @parameters g | ||
# @variables x(..) y(t) [state_priority = 10] λ(t) | ||
# eqs = [D(D(x(t))) ~ λ * x(t) | ||
# D(D(y)) ~ λ * y - g | ||
# x(t)^2 + y^2 ~ 1] | ||
# constr = [x(0.5) ~ 1] | ||
# @mtkbuild pend = ODESystem(eqs, t; constr) | ||
# | ||
# tspan = (0.0, 1.5) | ||
# u0map = [x(t) => 0.6, y => 0.8] | ||
# parammap = [g => 1] | ||
# guesses = [λ => 1] | ||
# | ||
# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) | ||
# test_solvers(daesolvers, bvp, u0map, constr) | ||
# | ||
# bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) | ||
# test_solvers(daesolvers, bvp2, u0map, constr, get_alg_eqs(pend)) | ||
# | ||
# # More complicated constr. | ||
# u0map = [x(t) => 0.6] | ||
# guesses = [λ => 1, y(t) => 0.8] | ||
# | ||
# constr = [x(0.5) ~ 1, | ||
# x(0.3)^3 + y(0.6)^2 ~ 0.5] | ||
# @mtkbuild pend = ODESystem(eqs, t; constr) | ||
# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) | ||
# test_solvers(daesolvers, bvp, u0map, constr, get_alg_eqs(pend)) | ||
# | ||
# constr = [x(0.4) * g ~ y(0.2), | ||
# y(0.7) ~ 0.3] | ||
# @mtkbuild pend = ODESystem(eqs, t; constr) | ||
# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) | ||
# test_solvers(daesolvers, bvp, u0map, constr, get_alg_eqs(pend)) | ||
# 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
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.
What's up with these ones?
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.
The interface for specifying boundary conditions is different for the BVDAE solvers at the moment (you specify the time points as a separate argument), was gonna wait until they get updated to be the same.
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.
@ErikQQY
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.
On it, Ascher methods need lots of refactorizations