Skip to content

feat: allow calling generate_control_function with unsimplified system #3699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/inputoutput.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,11 @@ function generate_control_function(sys::AbstractSystem, inputs = unbound_inputs(
simplify = false,
eval_expression = false,
eval_module = @__MODULE__,
check_simplified = true,
kwargs...)
# Remove this when the ControlFunction gets merged.
if check_simplified && !iscomplete(sys)
error("A completed `ODESystem` is required. Call `complete` or `mtkcompile` on the system before creating the control function.")
end
isempty(inputs) && @warn("No unbound inputs were found in system.")
if !iscomplete(sys)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sys may be complete without being mtkcompiled?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ModelingToolkit.isscheduled is for structural simplification, you can complete without that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed the user may provide a handwritten model in the correct format that they complete without running mtkcompile. If this is not a case we want to handle, then I can just use isscheduled.

sys = mtkcompile(sys; inputs, disturbance_inputs)
end
if disturbance_inputs !== nothing
# add to inputs for the purposes of io processing
inputs = [inputs; disturbance_inputs]
Expand Down
23 changes: 11 additions & 12 deletions test/input_output_handling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ end
]

@named sys = System(eqs, t)
sys = mtkcompile(sys, inputs = [u])
f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys; simplify, split)
f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(
sys, [u]; simplify, split)

@test isequal(dvs[], x)
@test isempty(ps)
Expand All @@ -183,8 +183,8 @@ end
]

@named sys = System(eqs, t)
sys = mtkcompile(sys, inputs = [u], disturbance_inputs = [d])
f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys; simplify, split)
f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(
sys, [u], [d]; simplify, split)

@test isequal(dvs[], x)
@test isempty(ps)
Expand All @@ -201,9 +201,9 @@ end
]

@named sys = System(eqs, t)
sys = mtkcompile(sys, inputs = [u], disturbance_inputs = [d])
f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(
sys; simplify, split, disturbance_argument = true)
sys, [u], [d];
simplify, split, disturbance_argument = true)

@test isequal(dvs[], x)
@test isempty(ps)
Expand Down Expand Up @@ -267,8 +267,8 @@ eqs = [connect_sd(sd, mass1, mass2)
@named _model = System(eqs, t)
@named model = compose(_model, mass1, mass2, sd);

model = mtkcompile(model, inputs = [u])
f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(model, simplify = true)
f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(
model, [u]; simplify = true)
@test length(dvs) == 4
p = MTKParameters(io_sys, [io_sys.u => NaN])
x = ModelingToolkit.varmap_to_vars(
Expand Down Expand Up @@ -435,8 +435,8 @@ matrices = ModelingToolkit.reorder_unknowns(
]

@named sys = System(eqs, t)
sys = mtkcompile(sys, inputs = [u])
(; io_sys,) = ModelingToolkit.generate_control_function(sys, simplify = true)
(; io_sys,) = ModelingToolkit.generate_control_function(
sys, [u]; simplify = true)
obsfn = ModelingToolkit.build_explicit_observed_function(
io_sys, [x + u * t]; inputs = [u])
@test obsfn([1.0], [2.0], MTKParameters(io_sys, []), 3.0) ≈ [7.0]
Expand All @@ -458,8 +458,7 @@ end
@parameters p(::Real) = (x -> 2x)
eqs = [D(x) ~ -x + p(u)]
@named sys = System(eqs, t)
sys = mtkcompile(sys, inputs = [u])
f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys)
f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys, [u])
p = MTKParameters(io_sys, [])
u = [1.0]
x = [1.0]
Expand Down
Loading