Skip to content

Commit 961e1b4

Browse files
refactor: modularize compatibility checks
1 parent 0c45505 commit 961e1b4

File tree

3 files changed

+87
-49
lines changed

3 files changed

+87
-49
lines changed

src/problems/compatibility.jl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""
2+
function check_compatible_system(T::Type, sys::System)
3+
4+
Check if `sys` can be used to construct a problem/function of type `T`.
5+
"""
6+
function check_compatible_system end
7+
8+
struct SystemCompatibilityError <: Exception
9+
msg::String
10+
end
11+
12+
function Base.showerror(io::IO, err::SystemCompatibilityError)
13+
println(io, err.msg)
14+
println(io)
15+
print(io, "To disable this check, pass `check_compatibility = false`.")
16+
end
17+
18+
function check_time_dependent(sys::System, T)
19+
if !is_time_dependent(sys)
20+
throw(SystemCompatibilityError("""
21+
`$T` requires a time-dependent system.
22+
"""))
23+
end
24+
end
25+
26+
function check_is_dde(sys::System)
27+
altT = get_noise_eqs(sys) === nothing ? ODEProblem : SDEProblem
28+
if !is_dde(sys)
29+
throw(SystemCompatibilityError("""
30+
The system does not have delays. Consider an `$altT` instead.
31+
"""))
32+
end
33+
end
34+
35+
function check_not_dde(sys::System)
36+
altT = get_noise_eqs(sys) === nothing ? DDEProblem : SDDEProblem
37+
if is_dde(sys)
38+
throw(SystemCompatibilityError("""
39+
The system has delays. Consider a `$altT` instead.
40+
"""))
41+
end
42+
end
43+
44+
function check_no_cost(sys::System, T)
45+
cost = ModelingToolkit.cost(sys)
46+
if !_iszero(cost)
47+
throw(SystemCompatibilityError("""
48+
`$T` will not optimize solutions of systems that have associated cost \
49+
functions. Solvers for optimal control problems are forthcoming. In order to \
50+
bypass this error (e.g. to check the cost of a regular solution), pass \
51+
`allow_cost = true` into the constructor.
52+
"""))
53+
end
54+
end
55+
56+
function check_no_constraints(sys::System, T)
57+
if !isempty(constraints(sys))
58+
throw(SystemCompatibilityError("""
59+
A system with constraints cannot be used to construct a `$T`.
60+
"""))
61+
end
62+
end
63+
64+
function check_no_jumps(sys::System, T)
65+
if !isempty(jumps(sys))
66+
throw(SystemCompatibilityError("""
67+
A system with jumps cannot be used to construct a `$T`. Consider a \
68+
`JumpProblem` instead.
69+
"""))
70+
end
71+
end
72+
73+
function check_no_noise(sys::System, T)
74+
altT = is_dde(sys) ? SDDEProblem : SDEProblem
75+
if get_noise_eqs(sys) !== nothing
76+
throw(SystemCompatibilityError("""
77+
A system with noise cannot be used to construct a `$T`. Consider an \
78+
`$altT` instead.
79+
"""))
80+
end
81+
end

src/problems/odeproblem.jl

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -73,41 +73,10 @@ end
7373
end
7474

7575
function check_compatible_system(T::Union{Type{ODEFunction}, Type{ODEProblem}}, sys::System)
76-
if !is_time_dependent(sys)
77-
throw(SystemCompatibilityError("""
78-
`$T` requires a time-dependent system.
79-
"""))
80-
end
81-
82-
cost = get_costs(sys)
83-
if cost isa Vector && !isempty(cost) ||
84-
cost isa Union{BasicSymbolic, Real} && !_iszero(cost)
85-
throw(SystemCompatibilityError("""
86-
`$T` will not optimize solutions of systems that have associated cost \
87-
functions. Solvers for optimal control problems are forthcoming. In order to \
88-
bypass this error (e.g. to check the cost of a regular solution), pass \
89-
`allow_cost = true` into the constructor.
90-
"""))
91-
end
92-
93-
if !isempty(constraints(sys))
94-
throw(SystemCompatibilityError("""
95-
A system with constraints cannot be used to construct an `$T`. Consider a \
96-
`BVProblem` instead.
97-
"""))
98-
end
99-
100-
if !isempty(jumps(sys))
101-
throw(SystemCompatibilityError("""
102-
A system with jumps cannot be used to construct an `$T`. Consider a \
103-
`JumpProblem` instead.
104-
"""))
105-
end
106-
107-
if get_noise_eqs(sys) !== nothing
108-
throw(SystemCompatibilityError("""
109-
A system with jumps cannot be used to construct an `$T`. Consider an \
110-
`SDEProblem` instead.
111-
"""))
112-
end
76+
check_time_dependent(sys, T)
77+
check_not_dde(sys, T)
78+
check_no_cost(sys, T)
79+
check_no_constraints(sys, T)
80+
check_no_jumps(sys, T)
81+
check_no_noise(sys, T)
11382
end

src/systems/codegen.jl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,3 @@ function isautonomous(sys::System)
286286
tgrad = calculate_tgrad(sys; simplify = true)
287287
all(iszero, tgrad)
288288
end
289-
290-
function check_compatible_system end
291-
292-
struct SystemCompatibilityError <: Exception
293-
msg::String
294-
end
295-
296-
function Base.showerror(io::IO, err::SystemCompatibilityError)
297-
println(io, err.msg)
298-
println(io)
299-
print(io, "To disable this check, pass `check_compatibility = false`.")
300-
end

0 commit comments

Comments
 (0)