Skip to content

Commit afc2930

Browse files
fix: fix observed function generation for systems with inputs
1 parent 0d450de commit afc2930

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/systems/diffeqs/odesystem.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,17 @@ function build_explicit_observed_function(sys, ts;
472472
ps = DestructuredArgs.(ps, inbounds = !checkbounds)
473473
elseif has_index_cache(sys) && get_index_cache(sys) !== nothing
474474
ps = DestructuredArgs.(reorder_parameters(get_index_cache(sys), ps))
475+
if isempty(ps)
476+
ps = (:EMPTY,)
477+
end
475478
else
476479
ps = (DestructuredArgs(ps, inbounds = !checkbounds),)
477480
end
478481
dvs = DestructuredArgs(unknowns(sys), inbounds = !checkbounds)
479482
if inputs === nothing
480483
args = [dvs, ps..., ivs...]
481484
else
485+
inputs = unwrap.(inputs)
482486
ipts = DestructuredArgs(inputs, inbounds = !checkbounds)
483487
args = [dvs, ipts, ps..., ivs...]
484488
end

test/input_output_handling.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,16 @@ matrices, ssys = linearize(augmented_sys,
378378
# P = ss(A,B,C,0)
379379
# G = ss(matrices...)
380380
# @test sminreal(G[1, 3]) ≈ sminreal(P[1,1])*dist
381+
382+
@testset "Observed functions with inputs" begin
383+
@variables x(t)=0 u(t)=0 [input = true]
384+
eqs = [
385+
D(x) ~ -x + u
386+
]
387+
388+
@named sys = ODESystem(eqs, t)
389+
(; io_sys,) = ModelingToolkit.generate_control_function(sys, simplify = true)
390+
obsfn = ModelingToolkit.build_explicit_observed_function(
391+
io_sys, [x + u * t]; inputs = [u])
392+
@test obsfn([1.0], [2.0], nothing, 3.0) == [7.0]
393+
end

0 commit comments

Comments
 (0)