|
| 1 | +""" |
| 2 | + state_values(p) |
| 3 | +
|
| 4 | +Return an indexable collection containing the values of all states in the integrator or |
| 5 | +problem `p`. |
| 6 | +""" |
| 7 | +function state_values end |
| 8 | + |
| 9 | +""" |
| 10 | + current_time(p) |
| 11 | +
|
| 12 | +Return the current time in the integrator or problem `p`. |
| 13 | +""" |
| 14 | +function current_time end |
| 15 | + |
| 16 | +""" |
| 17 | + getu(sys, sym) |
| 18 | +
|
| 19 | +Return a function that takes an integrator or problem of `sys`, and returns the value of |
| 20 | +the symbolic `sym`. `sym` can be a direct index into the state vector, a symbolic state, |
| 21 | +a symbolic expression involving symbolic quantities in the system `sys`, or an |
| 22 | +array/tuple of the aforementioned. |
| 23 | +
|
| 24 | +At minimum, this requires that the integrator or problem implement [`state_values`](@ref). |
| 25 | +To support symbolic expressions, the integrator or problem must implement |
| 26 | +[`observed`](@ref), [`parameter_values`](@ref) and [`current_time`](@ref). |
| 27 | +
|
| 28 | +This function typically does not need to be implemented, and has a default implementation |
| 29 | +relying on the above functions. |
| 30 | +""" |
| 31 | +function getu(sys, sym) |
| 32 | + symtype = symbolic_type(sym) |
| 33 | + elsymtype = symbolic_type(eltype(sym)) |
| 34 | + |
| 35 | + if symtype != NotSymbolic() |
| 36 | + _getu(sys, symtype, sym) |
| 37 | + else |
| 38 | + _getu(sys, elsymtype, sym) |
| 39 | + end |
| 40 | +end |
| 41 | + |
| 42 | +function _getu(sys, ::NotSymbolic, sym) |
| 43 | + return function getter(prob) |
| 44 | + return state_values(prob)[sym] |
| 45 | + end |
| 46 | +end |
| 47 | + |
| 48 | +function _getu(sys, ::ScalarSymbolic, sym) |
| 49 | + if is_variable(sys, sym) |
| 50 | + idx = variable_index(sys, sym) |
| 51 | + return function getter1(prob) |
| 52 | + return state_values(prob)[idx] |
| 53 | + end |
| 54 | + elseif is_observed(sys, sym) |
| 55 | + fn = observed(sys, sym) |
| 56 | + if is_time_dependent(sys) |
| 57 | + function getter2(prob) |
| 58 | + return fn(state_values(prob), parameter_values(prob), current_time(prob)) |
| 59 | + end |
| 60 | + else |
| 61 | + function getter3(prob) |
| 62 | + return fn(state_values(prob), parameter_values(prob)) |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | + error("Invalid symbol $sym for `getu`") |
| 67 | +end |
| 68 | + |
| 69 | +function _getu(sys, ::ScalarSymbolic, sym::Union{<:Tuple,<:AbstractArray}) |
| 70 | + getters = getu.((sys,), sym) |
| 71 | + _call(getter, prob) = getter(prob) |
| 72 | + return function getter(prob) |
| 73 | + return _call.(getters, (prob,)) |
| 74 | + end |
| 75 | +end |
| 76 | + |
| 77 | +function _getu(sys, ::ArraySymbolic, sym) |
| 78 | + return getu(sys, collect(sym)) |
| 79 | +end |
| 80 | + |
| 81 | +""" |
| 82 | + setu(sys, sym) |
| 83 | +
|
| 84 | +Return a function that takes an integrator or problem of `sys` and a value, and sets the |
| 85 | +the state `sym` to that value. Note that `sym` can be a direct numerical index, a symbolic state, or an array/tuple of the aforementioned. |
| 86 | +
|
| 87 | +Requires that the integrator implement [`state_values`](@ref) and the |
| 88 | +returned collection be a mutable reference to the state vector in the integrator/problem. |
| 89 | +In case `state_values` cannot return such a mutable reference, `setu` needs to be |
| 90 | +implemented manually. |
| 91 | +""" |
| 92 | +function setu(sys, sym) |
| 93 | + symtype = symbolic_type(sym) |
| 94 | + elsymtype = symbolic_type(eltype(sym)) |
| 95 | + |
| 96 | + if symtype != NotSymbolic() |
| 97 | + _setu(sys, symtype, sym) |
| 98 | + else |
| 99 | + _setu(sys, elsymtype, sym) |
| 100 | + end |
| 101 | +end |
| 102 | + |
| 103 | +function _setu(sys, ::NotSymbolic, sym) |
| 104 | + return function setter!(prob, val) |
| 105 | + state_values(prob)[sym] = val |
| 106 | + end |
| 107 | +end |
| 108 | + |
| 109 | +function _setu(sys, ::ScalarSymbolic, sym) |
| 110 | + is_variable(sys, sym) || error("Invalid symbol $sym for `setu`") |
| 111 | + idx = variable_index(sys, sym) |
| 112 | + return function setter!(prob, val) |
| 113 | + state_values(prob)[idx] = val |
| 114 | + end |
| 115 | +end |
| 116 | + |
| 117 | +function _setu(sys, ::ScalarSymbolic, sym::Union{<:Tuple,<:AbstractArray}) |
| 118 | + setters = setu.((sys,), sym) |
| 119 | + _call!(setter!, prob, val) = setter!(prob, val) |
| 120 | + return function setter!(prob, val) |
| 121 | + _call!.(setters, (prob,), val) |
| 122 | + end |
| 123 | +end |
| 124 | + |
| 125 | +function _setu(sys, ::ArraySymbolic, sym) |
| 126 | + return setu(sys, collect(sym)) |
| 127 | +end |
0 commit comments