Skip to content

Commit 31b9954

Browse files
committed
correct error messages
1 parent 6451788 commit 31b9954

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ end
697697
```julia
698698
DiffEqBase.ODEProblem{iip}(sys::AbstractODESystem, u0map, tspan,
699699
parammap = DiffEqBase.NullParameters();
700+
allow_cost = false,
700701
version = nothing, tgrad = false,
701702
jac = false,
702703
checkbounds = false, sparse = false,
@@ -730,6 +731,7 @@ end
730731
function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map = [],
731732
tspan = get_tspan(sys),
732733
parammap = DiffEqBase.NullParameters();
734+
allow_cost = false,
733735
callback = nothing,
734736
check_length = true,
735737
warn_initialize_determined = true,
@@ -745,9 +747,10 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map =
745747
Consider a BVProblem instead.")
746748
end
747749

748-
if !isempty(get_costs(sys))
749-
error("An ODESystem with costs cannot be solved using a regular ODEProblem.
750-
Solvers for optimal control problems are forthcoming.")
750+
if !isempty(get_costs(sys)) && !allow_cost
751+
error("ODEProblem will not optimize solutions of ODESystems that have associated cost functions.
752+
Solvers for optimal control problems are forthcoming. In order to bypass this error (e.g.
753+
to check the cost of a regular solution), pass `allow_cost` = true into the constructor.")
751754
end
752755

753756
f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap;
@@ -801,21 +804,19 @@ If an ODESystem without `constraints` is specified, it will be treated as an ini
801804
802805
```julia
803806
@parameters g t_c = 0.5
804-
@variables x(..) y(t) [state_priority = 10] λ(t)
807+
@variables x(..) y(t) λ(t)
805808
eqs = [D(D(x(t))) ~ λ * x(t)
806809
D(D(y)) ~ λ * y - g
807810
x(t)^2 + y^2 ~ 1]
808811
cstr = [x(0.5) ~ 1]
809-
@named cstrs = ConstraintsSystem(cstr, t)
810-
@mtkbuild pend = ODESystem(eqs, t)
812+
@mtkbuild pend = ODESystem(eqs, t; constraints = cstrs)
811813
812814
tspan = (0.0, 1.5)
813815
u0map = [x(t) => 0.6, y => 0.8]
814816
parammap = [g => 1]
815817
guesses = [λ => 1]
816-
constraints = [x(0.5) ~ 1]
817818
818-
bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false)
819+
bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false)
819820
```
820821
821822
If the `ODESystem` has algebraic equations, like `x(t)^2 + y(t)^2`, the resulting
@@ -844,6 +845,7 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = []
844845
tspan = get_tspan(sys),
845846
parammap = DiffEqBase.NullParameters();
846847
guesses = Dict(),
848+
allow_cost = false,
847849
version = nothing, tgrad = false,
848850
callback = nothing,
849851
check_length = true,
@@ -857,9 +859,10 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = []
857859
end
858860
!isnothing(callback) && error("BVP solvers do not support callbacks.")
859861

860-
if !isempty(get_costs(sys))
861-
error("An ODESystem with costs cannot be solved using a regular DAEProblem.
862-
Solvers for optimal control problems are forthcoming.")
862+
if !isempty(get_costs(sys)) && !allow_cost
863+
error("BVProblem will not optimize solutions of ODESystems that have associated cost functions.
864+
Solvers for optimal control problems are forthcoming. In order to bypass this error (e.g.
865+
to check the cost of a regular solution), pass `allow_cost` = true into the constructor.")
863866
end
864867

865868
has_alg_eqs(sys) &&
@@ -958,16 +961,19 @@ end
958961

959962
function DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan,
960963
parammap = DiffEqBase.NullParameters();
964+
allow_cost = false,
961965
warn_initialize_determined = true,
962966
check_length = true, eval_expression = false, eval_module = @__MODULE__, kwargs...) where {iip}
963967
if !iscomplete(sys)
964968
error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DAEProblem`")
965969
end
966970

967-
if !isempty(get_costs(sys))
968-
error("An ODESystem with costs cannot be solved using a regular DAEProblem.
969-
Solvers for optimal control problems are forthcoming.")
971+
if !isempty(get_costs(sys)) && !allow_cost
972+
error("DAEProblem will not optimize solutions of ODESystems that have associated cost functions.
973+
Solvers for optimal control problems are forthcoming. In order to bypass this error (e.g.
974+
to check the cost of a regular solution), pass `allow_cost` = true into the constructor.")
970975
end
976+
971977
f, du0, u0, p = process_SciMLProblem(DAEFunction{iip}, sys, u0map, parammap;
972978
implicit_dae = true, du0map = du0map, check_length,
973979
t = tspan !== nothing ? tspan[1] : tspan,

test/optimal_control.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ end
290290
@mtkbuild lksys = ODESystem(eqs, t; costs, consolidate)
291291
@test_throws ErrorException @mtkbuild lksys2 = ODESystem(eqs, t; costs)
292292

293-
prob = ODEProblem(lksys, u0map, tspan, parammap)
293+
@test_throws ErrorException ODEProblem(lksys, u0map, tspan, parammap)
294+
prob = ODEProblem(lksys, u0map, tspan, parammap; allow_cost = true)
294295
sol = solve(prob, Tsit5())
295296
costfn = ModelingToolkit.generate_cost_function(lksys)
296297
_t = tspan[2]
@@ -303,7 +304,7 @@ end
303304
@mtkbuild lksys = ODESystem(eqs, t; costs, consolidate)
304305
@test t_c Set(parameters(lksys))
305306
push!(parammap, t_c => 0.56)
306-
prob = ODEProblem(lksys, u0map, tspan, parammap)
307+
prob = ODEProblem(lksys, u0map, tspan, parammap; allow_cost = true)
307308
sol = solve(prob, Tsit5())
308309
costfn = ModelingToolkit.generate_cost_function(lksys)
309310
@test costfn(sol, prob.p, _t) log(sol(0.56)[2] + sol(0.)[1]) - sol(0.4)[1]^2

0 commit comments

Comments
 (0)