|
| 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 |
0 commit comments