Skip to content

Commit ed0d720

Browse files
committed
handle SampleTime operator in discrete compile
1 parent dc70f88 commit ed0d720

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/systems/systemstructure.jl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,10 @@ function structural_simplify!(state::TearingState, io = nothing; simplify = fals
749749
deleteat!(additional_passes, discrete_pass_idx)
750750
# in the case of a hybrid system, the discrete_compile pass should take the currents of sys.discrete_subsystems
751751
# and modifies discrete_subsystems to bea tuple of the io and anything else, while adding or manipulating the rest of sys as needed
752-
sys = discrete_compile(sys, tss[[i for i in eachindex(tss) if i != continuous_id]], inputs, ci)
752+
discrete_ids = [i for i in eachindex(tss) if i != continuous_id]
753+
discrete_partitions = tss[discrete_ids]
754+
substitute_sampletime!(discrete_partitions, id_to_clock[discrete_ids])
755+
sys = discrete_compile(sys, discrete_partitions, inputs, ci)
753756
else
754757
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/"))
755758
end
@@ -809,3 +812,20 @@ function _structural_simplify!(state::TearingState, io; simplify = false,
809812

810813
ModelingToolkit.invalidate_cache!(sys), input_idxs
811814
end
815+
816+
"""
817+
substitute_sampletime(discrete_partitions, clocks)
818+
819+
Substitutes the `SampleTime()` operators for its value, e.g., `SampleTime()*x(k)` becomes `sampletime(clock(x))*x(k)`
820+
"""
821+
function substitute_sampletime!(discrete_partitions, clocks)
822+
@show clocks
823+
for (part, clock) in zip(discrete_partitions, clocks)
824+
@show dt = sampletime(clock)
825+
submap = SampleTime() => dt
826+
new_eqs = map(part.original_eqs) do eq
827+
substitute(eq, submap)
828+
end
829+
part.original_eqs .= new_eqs
830+
end
831+
end

0 commit comments

Comments
 (0)