Skip to content

Commit a6248ed

Browse files
committed
WIP change from JSC to SynchToolkit
1 parent a99c306 commit a6248ed

File tree

10 files changed

+43
-44
lines changed

10 files changed

+43
-44
lines changed

Project.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@ authors = ["Fredrik Bagge Carlson"]
44
version = "0.1.1"
55

66
[deps]
7-
DiffEqCallbacks = "459566f4-90b8-5000-8ac3-15dfb0a30def"
8-
JuliaSimCompiler = "8391cb6b-4921-5777-4e45-fd9aab8cb88d"
7+
HeptagonToolkit = "530fcfb7-09ad-4628-8fc4-9627497dc745"
98
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
109
ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739"
1110
OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a"
1211
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1312
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
13+
SynchToolkit = "f500ffa8-9682-42ac-8d34-0ca7926c2e94"
1414

1515
[compat]
16-
DiffEqCallbacks = "~3.8"
16+
HeptagonToolkit = "1.0.0"
1717
ModelingToolkit = "9"
1818
ModelingToolkitStandardLibrary = "2"
1919
OrdinaryDiffEqTsit5 = "1.1.0"
2020
Random = "1"
2121
StableRNGs = "1"
22+
SynchToolkit = "0.1.0"
2223
julia = "1.10"
2324

2425
[extras]

docs/src/examples/dc_motor_pi.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Below, we re-model the system, this time with a discrete-time controller: [`Disc
110110

111111
```@example dc_motor_pi
112112
using ModelingToolkitSampledData
113-
using JuliaSimCompiler
113+
using SynchToolkit
114114
115115
z = ShiftIndex()
116116
@mtkmodel DiscreteClosedLoop begin
@@ -159,7 +159,7 @@ end
159159
160160
@named disc_model = DiscreteClosedLoop()
161161
disc_model = complete(disc_model)
162-
ssys = structural_simplify(IRSystem(disc_model)) # Conversion to an IRSystem from JuliaSimCompiler is required for sampled-data systems
162+
ssys = structural_simplify(disc_model; additional_passes=[SynchToolkit.compile_lustre])
163163
164164
disc_prob = ODEProblem(ssys, [unknowns(disc_model) .=> 0.0; disc_model.pi_controller.I(z-1) => 0; disc_model.pi_controller.eI(z-1) => 0], (0, 2.0))
165165
disc_sol = solve(disc_prob, Tsit5())
@@ -213,7 +213,7 @@ end
213213
214214
@named cascade = Cascade()
215215
cascade = complete(cascade)
216-
ssys = structural_simplify(IRSystem(cascade))
216+
ssys = structural_simplify(cascade; additional_passes=[SynchToolkit.compile_lustre])
217217
i = cascade.inner
218218
cascade_prob = ODEProblem(ssys, [
219219
unknowns(cascade) .=> 0.0;

docs/src/examples/onoffcontroller.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ where ``τ = 100`` is a time constant. The controller ``u = f(x)`` is an on/off
1010
using ModelingToolkit, ModelingToolkitSampledData, OrdinaryDiffEqTsit5, Plots
1111
using ModelingToolkit: t_nounits as t, D_nounits as D
1212
using ModelingToolkitStandardLibrary.Blocks
13-
using JuliaSimCompiler
13+
using SynchToolkit
1414
cl = Clock(1)
1515
z = ShiftIndex(cl)
1616
@mtkmodel OnOffModel begin
@@ -29,7 +29,7 @@ z = ShiftIndex(cl)
2929
end
3030
@named m = OnOffModel()
3131
m = complete(m)
32-
ssys = structural_simplify(IRSystem(m))
32+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
3333
prob = ODEProblem(ssys, [m.onoff.y(z-1) => 0], (0.0, 40.0))
3434
sol = solve(prob, Tsit5(), dtmax=0.1)
3535
plot(sol, idxs=[m.x, m.onoff.y], title="On-off control of an unstable first-order system")

docs/src/examples/sliding_mode_control.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ which yields the switching variable ``s = ė + e``, encoded in the function ``s
1313
using ModelingToolkit, ModelingToolkitSampledData, OrdinaryDiffEqTsit5, Plots
1414
using ModelingToolkit: t_nounits as t, D_nounits as D
1515
using ModelingToolkitStandardLibrary.Blocks
16-
using JuliaSimCompiler
16+
using SynchToolkit
1717
dt = 0.01
1818
clock = Clock(dt)
1919
z = ShiftIndex(clock)
@@ -80,7 +80,7 @@ end
8080
end
8181
@named m = ClosedLoopModel()
8282
m = complete(m)
83-
ssys = structural_simplify(IRSystem(m))
83+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
8484
prob = ODEProblem(ssys, [m.plant.x => -1, m.plant.xd => 0, m.controller.x(z-1) => 0], (0.0, 2π))
8585
sol = solve(prob, Tsit5(), dtmax=0.01)
8686
figy = plot(sol, idxs=[m.plant.x])

docs/src/tutorials/SampledData.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,11 @@ connections = [r ~ (t >= 5) # reference signal
226226
cl = complete(cl)
227227
```
228228

229-
We can now simulate the system. JuliaSimCompiler is required to simulate hybrid continuous/discrete systems, we thus convert the system to an `JuliaSimCompiler.IRSystem` before calling `structural_simplify`
229+
We can now simulate the system.
230230
```@example clocks
231-
using JuliaSimCompiler, Plots
232-
ssys = structural_simplify(IRSystem(cl))
231+
using Plots
232+
using SynchToolkit
233+
ssys = structural_simplify(cl; additional_passes=[SynchToolkit.compile_lustre])
233234
234235
prob = ODEProblem(ssys, [
235236
cl.f.x(k-1) => 0,

docs/src/tutorials/noise.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using ModelingToolkitStandardLibrary.Electrical
1414
using ModelingToolkitStandardLibrary.Mechanical.Rotational
1515
using ModelingToolkitStandardLibrary.Blocks
1616
using ModelingToolkitSampledData
17-
using JuliaSimCompiler
17+
using SynchToolkit
1818
using OrdinaryDiffEq
1919
using Plots
2020
@@ -72,7 +72,7 @@ end
7272
7373
@named noisy_model = NoisyClosedLoop()
7474
noisy_model = complete(noisy_model)
75-
ssys = structural_simplify(IRSystem(noisy_model)) # Conversion to an IRSystem from JuliaSimCompiler is required for sampled-data systems
75+
ssys = structural_simplify(noisy_model; additional_passes=[SynchToolkit.compile_lustre])
7676
7777
noise_prob = ODEProblem(ssys, [unknowns(noisy_model) .=> 0.0; noisy_model.pi_controller.I(z-1) => 0; noisy_model.pi_controller.eI(z-1) => 0; noisy_model.noise.y(z-1) => 0], (0, 2.0))
7878
noise_sol = solve(noise_prob, Tsit5())
@@ -105,7 +105,7 @@ A signal may be quantized to a fixed number of levels (e.g., 8-bit) using the [`
105105
using ModelingToolkit, ModelingToolkitSampledData, OrdinaryDiffEq, Plots
106106
using ModelingToolkit: t_nounits as t, D_nounits as D
107107
using ModelingToolkitStandardLibrary.Blocks
108-
using JuliaSimCompiler
108+
109109
z = ShiftIndex(Clock(0.1))
110110
@mtkmodel QuantizationModel begin
111111
@components begin
@@ -123,7 +123,7 @@ z = ShiftIndex(Clock(0.1))
123123
end
124124
@named m = QuantizationModel()
125125
m = complete(m)
126-
ssys = structural_simplify(IRSystem(m))
126+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
127127
prob = ODEProblem(ssys, [], (0.0, 2.0))
128128
sol = solve(prob, Tsit5())
129129
plot(sol, idxs=m.input.output.u)
@@ -170,7 +170,7 @@ The block [`SampleWithADEffects`](@ref) combines an ideal [`Sampler`](@ref), a [
170170
end
171171
@named m = PracticalSamplerModel()
172172
m = complete(m)
173-
ssys = structural_simplify(IRSystem(m))
173+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
174174
prob = ODEProblem(ssys, [m.sampling.noise.y(z-1) => 0], (0.0, 2.0))
175175
sol = solve(prob, Tsit5())
176176
plot(sol, idxs=m.input.output.u)

src/ModelingToolkitSampledData.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module ModelingToolkitSampledData
22
using ModelingToolkit
3-
using JuliaSimCompiler
3+
using SynchToolkit
44
using StableRNGs
55

66
export get_clock
@@ -14,6 +14,4 @@ export DiscreteOnOffController
1414
include("discrete_blocks.jl")
1515

1616

17-
runtime(ssys::JuliaSimCompiler.ScheduledSystem) = ssys.discrete_info.callback.affect!.affect!.x
18-
1917
end

src/discrete_blocks.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,6 @@ end
343343
"""
344344
@mtkmodel ClockChanger begin
345345
begin
346-
isdefined(Main, :JuliaSimCompiler) || error("JuliaSimCompiler must be defined in the Main module for the ClockChanger component to work. Run `import JuliaSimCompiler`.")
347346
@warn "The ClockChanger component is experimental and has known correctness issues. Please use with caution."
348347
end
349348
@extend u, y = siso = SISO()
@@ -355,7 +354,7 @@ end
355354
O = 0 # This is just a dummy to workaround a bug in JSComp
356355
end
357356
@equations begin
358-
y(ShiftIndex(to)) ~ Main.JuliaSimCompiler.ClockChange(; to, from)(u(ShiftIndex(from))) + O
357+
y(ShiftIndex(to)) ~ ClockChange(; to, from)(u(ShiftIndex(from))) + O
359358
end
360359
end
361360

@@ -918,7 +917,7 @@ z = ShiftIndex(cl)
918917
end
919918
@named m = SlweRateLimiterModel()
920919
m = complete(m)
921-
ssys = structural_simplify(IRSystem(m))
920+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
922921
prob = ODEProblem(ssys, [m.limiter.y(z-1) => 0], (0.0, 2.0))
923922
sol = solve(prob, Tsit5(), dtmax=0.01)
924923
plot(sol, idxs=[m.input.output.u, m.limiter.y], title="Slew rate limited sine wave")

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using ModelingToolkitSampledData
22
using ModelingToolkit
3-
using JuliaSimCompiler
3+
using SynchToolkit
44
using Test
55

66
@testset "ModelingToolkitSampledData.jl" begin

test/test_discrete_blocks.jl

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using ModelingToolkit, ModelingToolkitStandardLibrary, OrdinaryDiffEqTsit5
33
using ModelingToolkitStandardLibrary.Blocks
44
using ModelingToolkit: t_nounits as t, D_nounits as D
55
using OrdinaryDiffEqTsit5: ReturnCode.Success
6-
using JuliaSimCompiler
6+
using SynchToolkit
77
using Test
88
Difference = ModelingToolkitSampledData.Difference
99

@@ -31,7 +31,7 @@ an integrator with a constant input is often used together with the system under
3131

3232
@named model = TestDiscreteOnly()
3333
model = complete(model)
34-
ssys = structural_simplify(IRSystem(model))
34+
ssys = structural_simplify(model; additional_passes=[SynchToolkit.compile_lustre])
3535
prob = ODEProblem(ssys, [model.x(k-1) => 1.0], (0.0, 10.0))
3636
sol = solve(prob, Tsit5())
3737
@test sol[model.x] == dt .^ (1:21)
@@ -53,7 +53,7 @@ end
5353
t,
5454
systems = [sampler, int, intc, c])
5555
model = complete(iosys)
56-
sys = structural_simplify(IRSystem(model))
56+
sys = structural_simplify(model; additional_passes=[SynchToolkit.compile_lustre])
5757
prob = ODEProblem(sys, Pair[int.x(k - 1) => 1
5858
int.u(k - 1) => 0], (0.0, 1.0))
5959
sol = solve(prob, Tsit5())
@@ -70,7 +70,7 @@ end
7070
t,
7171
systems = [sampler, int, intc, c])
7272
model = complete(iosys)
73-
sys = structural_simplify(IRSystem(model))
73+
sys = structural_simplify(model; additional_passes=[SynchToolkit.compile_lustre])
7474
prob = ODEProblem(sys, Pair[int.x(k - 1) => 1
7575
int.u(k - 1) => 0], (0.0, 1.0))
7676
sol = solve(prob, Tsit5())
@@ -88,7 +88,7 @@ end
8888
t,
8989
systems = [sampler, int, intc, c])
9090
model = complete(iosys)
91-
sys = structural_simplify(IRSystem(model))
91+
sys = structural_simplify(model; additional_passes=[SynchToolkit.compile_lustre])
9292
prob = ODEProblem(sys, Pair[int.x(k - 1) => 1
9393
int.u(k - 1) => 0], (0.0, 1.0))
9494
sol = solve(prob, Tsit5())
@@ -128,7 +128,7 @@ end
128128
@named model = ClosedLoop()
129129
model = complete(model)
130130
# ci, varmap = infer_clocks(expand_connections(model))
131-
ssys = structural_simplify(IRSystem(model))
131+
ssys = structural_simplify(model; additional_passes=[SynchToolkit.compile_lustre])
132132

133133
Tf = 5
134134
timevec = 0:(dt):Tf
@@ -197,7 +197,7 @@ end
197197
@named model = ClosedLoop()
198198
model = complete(model)
199199
# ci, varmap = infer_clocks(expand_connections(model))
200-
ssys = structural_simplify(IRSystem(model))
200+
ssys = structural_simplify(model; additional_passes=[SynchToolkit.compile_lustre])
201201

202202
Tf = 5
203203
timevec = 0:(dt):Tf
@@ -256,7 +256,7 @@ end
256256

257257
@named m = DelayModel()
258258
m = complete(m)
259-
ssys = structural_simplify(IRSystem(m))
259+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
260260
prob = ODEProblem(
261261
ssys, [m.delay.u(k - 3) => 0, m.delay.u(k - 2) => 0, m.delay.u(k - 1) => 0], (
262262
0.0, 10.0))
@@ -289,7 +289,7 @@ using ModelingToolkitStandardLibrary.Blocks
289289

290290
@named m = DiffModel()
291291
m = complete(m)
292-
ssys = structural_simplify(IRSystem(m))
292+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
293293
prob = ODEProblem(ssys, Dict(m.diff.u(k - 1) => 0), (0.0, 10.0))
294294
sol = solve(prob, Tsit5(), dtmax = 0.01)
295295
@test reduce(vcat, sol((0:10) .+ 1e-2))[:][zeros(2); 1; zeros(8)] atol=1e-2
@@ -319,7 +319,7 @@ using Statistics
319319

320320
@named m = NoiseModel()
321321
m = complete(m)
322-
ssys = structural_simplify(IRSystem(m))
322+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
323323
prob = ODEProblem(ssys, [m.noise.y(k-1) => 0], (0.0, 10.0))
324324
sol = solve(prob, Tsit5())
325325
@test !all(iszero, sol.u)
@@ -345,7 +345,7 @@ end
345345

346346
@named m = NoiseModel()
347347
m = complete(m)
348-
ssys = structural_simplify(IRSystem(m))
348+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
349349
prob = ODEProblem(ssys, [m.noise.y(k-1) => 0], (0.0, 10.0))
350350
sol = solve(prob, Tsit5())
351351
@test !all(iszero, sol.u)
@@ -375,7 +375,7 @@ end
375375

376376
# @named m = PlantModel()
377377
# m = complete(m)
378-
# ssys = structural_simplify(IRSystem(m))
378+
# ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
379379
# prob = ODEProblem(ssys, Dict(m.plant.u(k - 1) => 0), (0.0, 10.0))
380380
# sol = solve(prob, Tsit5(), dtmax = 0.01)
381381
# @test reduce(vcat, sol((0:10) .+ 1e-2))[:]≈[zeros(2); 1; zeros(8)] atol=1e-2
@@ -401,7 +401,7 @@ end
401401
end
402402
@named m = SlewRateLimiterModel()
403403
m = complete(m)
404-
ssys = structural_simplify(IRSystem(m))
404+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
405405
prob = ODEProblem(ssys, [m.limiter.y(z-1) => 0], (0.0, 2.0))
406406
sol = solve(prob, Tsit5(), dtmax=0.01)
407407
# plot(sol, idxs=[m.input.output.u, m.limiter.y], title="Slew rate limited sine wave")
@@ -457,7 +457,7 @@ end
457457
end
458458
@named m = QuantizationModel()
459459
m = complete(m)
460-
ssys = structural_simplify(IRSystem(m))
460+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
461461
prob = ODEProblem(ssys, [], (0.0, 10.0))
462462
sol = solve(prob, Tsit5(), dtmax=0.01)
463463
y = sol[m.quant.y]
@@ -476,7 +476,7 @@ end
476476
end
477477
@named m = QuantizationModel2()
478478
m = complete(m)
479-
ssys = structural_simplify(IRSystem(m))
479+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
480480
prob = ODEProblem(ssys, [], (0.0, 10.0))
481481
sol = solve(prob, Tsit5(), dtmax=0.01)
482482
y = sol[m.quant.y]
@@ -505,7 +505,7 @@ end
505505
end
506506
@named m = OnOffModel()
507507
m = complete(m)
508-
ssys = structural_simplify(IRSystem(m))
508+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
509509
prob = ODEProblem(ssys, [m.onoff.y(z-1) => 0], (0.0, 4.0))
510510
sol = solve(prob, Tsit5(), dtmax=0.1)
511511
# plot(sol, idxs=[m.x, m.onoff.y], title="On-off control of an unstable first-order system")
@@ -530,7 +530,7 @@ end
530530
end
531531
@named m = ExponentialFilterModel()
532532
m = complete(m)
533-
ssys = structural_simplify(IRSystem(m))
533+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
534534
prob = ODEProblem(ssys, [m.filter.y(z-1) => 0], (0.0, 10.0))
535535
sol = solve(prob, Tsit5(), dtmax=0.1)
536536
@test sol(10, idxs=m.filter.y) 1 atol=0.001
@@ -558,7 +558,7 @@ end
558558

559559
@named m = MovingAverageFilterModel()
560560
m = complete(m)
561-
ssys = structural_simplify(IRSystem(m))
561+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
562562
prob = ODEProblem(ssys, [m.filter.u(z-i) => 0 for i = 0:3], (0.0, 2.0))
563563
sol = solve(prob, Tsit5(), dtmax=0.1)
564564
# plot(sol, idxs=m.filter.y)
@@ -587,7 +587,7 @@ end
587587
end
588588
@named m = PracticalSamplerModel()
589589
m = complete(m)
590-
ssys = structural_simplify(IRSystem(m))
590+
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
591591
prob = ODEProblem(ssys, [m.sampling.noise.y(z-1) => 0], (0.0, 2.0))
592592
sol = solve(prob, Tsit5())
593593

0 commit comments

Comments
 (0)