diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 33cdc59909..9a082abf5e 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -567,12 +567,18 @@ function build_explicit_observed_function(sys, ts; sys, ts, args...; p_start, p_end, filter_observed = obsfilter, output_type, mkarray, try_namespaced = true, expression = Val{true}) if fns isa Tuple + if expression + return return_inplace ? fns : fns[1] + end oop, iip = eval_or_rgf.(fns; eval_expression, eval_module) f = GeneratedFunctionWrapper{( p_start + is_dde(sys), length(args) - length(ps) + 1 + is_dde(sys), is_split(sys))}( oop, iip) return return_inplace ? (f, f) : f else + if expression + return fns + end f = eval_or_rgf(fns; eval_expression, eval_module) f = GeneratedFunctionWrapper{( p_start + is_dde(sys), length(args) - length(ps) + 1 + is_dde(sys), is_split(sys))}( diff --git a/test/odesystem.jl b/test/odesystem.jl index 87be5bbc54..970a9afaad 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1724,3 +1724,15 @@ end @mtkbuild ode = ODESystem(D(x(t)) ~ mat * x(t), t; constraints = cons) @test length(constraints(ModelingToolkit.get_constraintsystem(ode))) == 5 end + +@testset "`build_explicit_observed_function` with `expression = true` returns `Expr`" begin + @variables x(t) + @mtkbuild sys = ODESystem(D(x) ~ 2x, t) + obsfn_expr = ModelingToolkit.build_explicit_observed_function( + sys, 2x + 1, expression = true) + @test obsfn_expr isa Expr + obsfn_expr_oop, obsfn_expr_iip = ModelingToolkit.build_explicit_observed_function( + sys, [x + 1, x + 2, x + t], return_inplace = true, expression = true) + @test obsfn_expr_oop isa Expr + @test obsfn_expr_iip isa Expr +end