Skip to content

handle SampleTime operator in discrete compile #3582

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

Open
wants to merge 1 commit into
base: bc/external-synchronous
Choose a base branch
from
Open
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
22 changes: 21 additions & 1 deletion src/systems/systemstructure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@
if state.sys isa ODESystem
# split_system returns one or two systems and the inputs for each
# mod clock inference to be binary
# if it's continous keep going, if not then error unless given trait impl in additional passes

Check warning on line 715 in src/systems/systemstructure.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"continous" should be "continuous".
ci = ModelingToolkit.ClockInference(state)
ci = ModelingToolkit.infer_clocks!(ci)
time_domains = merge(Dict(state.fullvars .=> ci.var_domain),
Expand Down Expand Up @@ -749,7 +749,10 @@
deleteat!(additional_passes, discrete_pass_idx)
# in the case of a hybrid system, the discrete_compile pass should take the currents of sys.discrete_subsystems
# and modifies discrete_subsystems to bea tuple of the io and anything else, while adding or manipulating the rest of sys as needed
sys = discrete_compile(sys, tss[[i for i in eachindex(tss) if i != continuous_id]], inputs, ci)
discrete_ids = [i for i in eachindex(tss) if i != continuous_id]
discrete_partitions = tss[discrete_ids]
substitute_sampletime!(discrete_partitions, id_to_clock[discrete_ids])
sys = discrete_compile(sys, discrete_partitions, inputs, ci)
else
throw(HybridSystemNotSupportedException("Hybrid continuous-discrete systems are currently not supported with the standard MTK compiler. This system requires JuliaSimCompiler.jl, see https://help.juliahub.com/juliasimcompiler/stable/"))
end
Expand Down Expand Up @@ -809,3 +812,20 @@

ModelingToolkit.invalidate_cache!(sys), input_idxs
end

"""
substitute_sampletime(discrete_partitions, clocks)

Substitutes the `SampleTime()` operators for its value, e.g., `SampleTime()*x(k)` becomes `sampletime(clock(x))*x(k)`
"""
function substitute_sampletime!(discrete_partitions, clocks)
@show clocks
for (part, clock) in zip(discrete_partitions, clocks)
@show dt = sampletime(clock)
submap = SampleTime() => dt
new_eqs = map(part.original_eqs) do eq
substitute(eq, submap)
end
part.original_eqs .= new_eqs
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
end
end

Loading