diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 9c11f65cfd..c057096ebe 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -472,6 +472,9 @@ function build_explicit_observed_function(sys, ts; ps = DestructuredArgs.(ps, inbounds = !checkbounds) elseif has_index_cache(sys) && get_index_cache(sys) !== nothing ps = DestructuredArgs.(reorder_parameters(get_index_cache(sys), ps)) + if isempty(ps) && inputs !== nothing + ps = (:EMPTY,) + end else ps = (DestructuredArgs(ps, inbounds = !checkbounds),) end @@ -479,6 +482,7 @@ function build_explicit_observed_function(sys, ts; if inputs === nothing args = [dvs, ps..., ivs...] else + inputs = unwrap.(inputs) ipts = DestructuredArgs(inputs, inbounds = !checkbounds) args = [dvs, ipts, ps..., ivs...] end diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index c63116638b..778e02db0a 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -378,3 +378,16 @@ matrices, ssys = linearize(augmented_sys, # P = ss(A,B,C,0) # G = ss(matrices...) # @test sminreal(G[1, 3]) ≈ sminreal(P[1,1])*dist + +@testset "Observed functions with inputs" begin + @variables x(t)=0 u(t)=0 [input = true] + eqs = [ + D(x) ~ -x + u + ] + + @named sys = ODESystem(eqs, t) + (; io_sys,) = ModelingToolkit.generate_control_function(sys, simplify = true) + obsfn = ModelingToolkit.build_explicit_observed_function( + io_sys, [x + u * t]; inputs = [u]) + @test obsfn([1.0], [2.0], nothing, 3.0) == [7.0] +end