Skip to content

WIP change from JSC to SynchToolkit #27

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
12 changes: 6 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ authors = ["Fredrik Bagge Carlson"]
version = "0.1.1"

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

[compat]
DiffEqCallbacks = "~3.8"
JuliaSimCompiler = "0.1.19"
HeptagonToolkit = "1.0.0"
ModelingToolkit = "9"
ModelingToolkitStandardLibrary = "2"
OrdinaryDiffEq = "6.89"
OrdinaryDiffEqTsit5 = "1.1.0"
Random = "1"
StableRNGs = "1"
SynchToolkit = "0.1.0"
julia = "1.10"

[extras]
Expand Down
8 changes: 4 additions & 4 deletions docs/src/examples/dc_motor_pi.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using ModelingToolkit: t_nounits as t
using ModelingToolkitStandardLibrary.Electrical
using ModelingToolkitStandardLibrary.Mechanical.Rotational
using ModelingToolkitStandardLibrary.Blocks
using OrdinaryDiffEq
using OrdinaryDiffEqTsit5
using Plots

R = 0.5 # [Ohm] armature resistance
Expand Down Expand Up @@ -110,7 +110,7 @@ Below, we re-model the system, this time with a discrete-time controller: [`Disc

```@example dc_motor_pi
using ModelingToolkitSampledData
using JuliaSimCompiler
using SynchToolkit

z = ShiftIndex()
@mtkmodel DiscreteClosedLoop begin
Expand Down Expand Up @@ -159,7 +159,7 @@ end

@named disc_model = DiscreteClosedLoop()
disc_model = complete(disc_model)
ssys = structural_simplify(IRSystem(disc_model)) # Conversion to an IRSystem from JuliaSimCompiler is required for sampled-data systems
ssys = structural_simplify(disc_model; additional_passes=[SynchToolkit.compile_lustre])

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))
disc_sol = solve(disc_prob, Tsit5())
Expand Down Expand Up @@ -213,7 +213,7 @@ end

@named cascade = Cascade()
cascade = complete(cascade)
ssys = structural_simplify(IRSystem(cascade))
ssys = structural_simplify(cascade; additional_passes=[SynchToolkit.compile_lustre])
i = cascade.inner
cascade_prob = ODEProblem(ssys, [
unknowns(cascade) .=> 0.0;
Expand Down
6 changes: 3 additions & 3 deletions docs/src/examples/onoffcontroller.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ where ``τ = 100`` is a time constant. The controller ``u = f(x)`` is an on/off


```@example ONOFF
using ModelingToolkit, ModelingToolkitSampledData, OrdinaryDiffEq, Plots
using ModelingToolkit, ModelingToolkitSampledData, OrdinaryDiffEqTsit5, Plots
using ModelingToolkit: t_nounits as t, D_nounits as D
using ModelingToolkitStandardLibrary.Blocks
using JuliaSimCompiler
using SynchToolkit
cl = Clock(1)
z = ShiftIndex(cl)
@mtkmodel OnOffModel begin
Expand All @@ -29,7 +29,7 @@ z = ShiftIndex(cl)
end
@named m = OnOffModel()
m = complete(m)
ssys = structural_simplify(IRSystem(m))
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
prob = ODEProblem(ssys, [m.onoff.y(z-1) => 0], (0.0, 40.0))
sol = solve(prob, Tsit5(), dtmax=0.1)
plot(sol, idxs=[m.x, m.onoff.y], title="On-off control of an unstable first-order system")
Expand Down
11 changes: 6 additions & 5 deletions docs/src/examples/sliding_mode_control.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ ė = -e
which yields the switching variable ``s = ė + e``, encoded in the function ``s = σ(x, t)``

```@example ONOFF
using ModelingToolkit, ModelingToolkitSampledData, OrdinaryDiffEq, Plots
using ModelingToolkit, ModelingToolkitSampledData, OrdinaryDiffEqTsit5, Plots
using ModelingToolkit: t_nounits as t, D_nounits as D
using ModelingToolkitStandardLibrary.Blocks
using JuliaSimCompiler
using SynchToolkit
dt = 0.01
clock = Clock(dt)
z = ShiftIndex(clock)
Expand Down Expand Up @@ -42,7 +42,7 @@ end
end
@parameters begin
k = 1, [description = "Control gain"]
k2 = 1.1, [description = "Tuning parameter, often set to 1.1"]
k2 = 0.5, [description = "Tuning parameter"]
end
@variables begin
s(t) = 0.0, [description = "Sliding surface"]
Expand Down Expand Up @@ -80,12 +80,13 @@ end
end
@named m = ClosedLoopModel()
m = complete(m)
ssys = structural_simplify(IRSystem(m))
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
prob = ODEProblem(ssys, [m.plant.x => -1, m.plant.xd => 0, m.controller.x(z-1) => 0], (0.0, 2π))
sol = solve(prob, Tsit5(), dtmax=0.01)
figy = plot(sol, idxs=[m.plant.x])
plot!(sol.t, qr.(sol.t), label="Reference")
figu = plot(sol, idxs=[m.zoh.y, m.disturbance], label=["Control signal" "Disturbance"])
figu = plot(sol, idxs=m.controller.y, label="Control signal")
plot!(sol, idxs=m.disturbance, label="Disturbance")
plot(figy, figu, layout=(2,1))
```

Expand Down
7 changes: 4 additions & 3 deletions docs/src/tutorials/SampledData.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,11 @@ connections = [r ~ (t >= 5) # reference signal
cl = complete(cl)
```

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`
We can now simulate the system.
```@example clocks
using JuliaSimCompiler, Plots
ssys = structural_simplify(IRSystem(cl))
using Plots
using SynchToolkit
ssys = structural_simplify(cl; additional_passes=[SynchToolkit.compile_lustre])

prob = ODEProblem(ssys, [
cl.f.x(k-1) => 0,
Expand Down
10 changes: 5 additions & 5 deletions docs/src/tutorials/noise.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using ModelingToolkitStandardLibrary.Electrical
using ModelingToolkitStandardLibrary.Mechanical.Rotational
using ModelingToolkitStandardLibrary.Blocks
using ModelingToolkitSampledData
using JuliaSimCompiler
using SynchToolkit
using OrdinaryDiffEq
using Plots

Expand Down Expand Up @@ -72,7 +72,7 @@ end

@named noisy_model = NoisyClosedLoop()
noisy_model = complete(noisy_model)
ssys = structural_simplify(IRSystem(noisy_model)) # Conversion to an IRSystem from JuliaSimCompiler is required for sampled-data systems
ssys = structural_simplify(noisy_model; additional_passes=[SynchToolkit.compile_lustre])

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))
noise_sol = solve(noise_prob, Tsit5())
Expand Down Expand Up @@ -105,7 +105,7 @@ A signal may be quantized to a fixed number of levels (e.g., 8-bit) using the [`
using ModelingToolkit, ModelingToolkitSampledData, OrdinaryDiffEq, Plots
using ModelingToolkit: t_nounits as t, D_nounits as D
using ModelingToolkitStandardLibrary.Blocks
using JuliaSimCompiler

z = ShiftIndex(Clock(0.1))
@mtkmodel QuantizationModel begin
@components begin
Expand All @@ -123,7 +123,7 @@ z = ShiftIndex(Clock(0.1))
end
@named m = QuantizationModel()
m = complete(m)
ssys = structural_simplify(IRSystem(m))
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
prob = ODEProblem(ssys, [], (0.0, 2.0))
sol = solve(prob, Tsit5())
plot(sol, idxs=m.input.output.u)
Expand Down Expand Up @@ -170,7 +170,7 @@ The block [`SampleWithADEffects`](@ref) combines an ideal [`Sampler`](@ref), a [
end
@named m = PracticalSamplerModel()
m = complete(m)
ssys = structural_simplify(IRSystem(m))
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
prob = ODEProblem(ssys, [m.sampling.noise.y(z-1) => 0], (0.0, 2.0))
sol = solve(prob, Tsit5())
plot(sol, idxs=m.input.output.u)
Expand Down
4 changes: 1 addition & 3 deletions src/ModelingToolkitSampledData.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ModelingToolkitSampledData
using ModelingToolkit
using JuliaSimCompiler
using SynchToolkit
using StableRNGs

export get_clock
Expand All @@ -14,6 +14,4 @@ export DiscreteOnOffController
include("discrete_blocks.jl")


runtime(ssys::JuliaSimCompiler.ScheduledSystem) = ssys.discrete_info.callback.affect!.affect!.x

end
5 changes: 2 additions & 3 deletions src/discrete_blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ end
"""
@mtkmodel ClockChanger begin
begin
isdefined(Main, :JuliaSimCompiler) || error("JuliaSimCompiler must be defined in the Main module for the ClockChanger component to work. Run `import JuliaSimCompiler`.")
@warn "The ClockChanger component is experimental and has known correctness issues. Please use with caution."
end
@extend u, y = siso = SISO()
Expand All @@ -355,7 +354,7 @@ end
O = 0 # This is just a dummy to workaround a bug in JSComp
end
@equations begin
y(ShiftIndex(to)) ~ Main.JuliaSimCompiler.ClockChange(; to, from)(u(ShiftIndex(from))) + O
y(ShiftIndex(to)) ~ ClockChange(; to, from)(u(ShiftIndex(from))) + O
end
end

Expand Down Expand Up @@ -918,7 +917,7 @@ z = ShiftIndex(cl)
end
@named m = SlweRateLimiterModel()
m = complete(m)
ssys = structural_simplify(IRSystem(m))
ssys = structural_simplify(m; additional_passes=[SynchToolkit.compile_lustre])
prob = ODEProblem(ssys, [m.limiter.y(z-1) => 0], (0.0, 2.0))
sol = solve(prob, Tsit5(), dtmax=0.01)
plot(sol, idxs=[m.input.output.u, m.limiter.y], title="Slew rate limited sine wave")
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using ModelingToolkitSampledData
using ModelingToolkit
using JuliaSimCompiler
using SynchToolkit
using Test

@testset "ModelingToolkitSampledData.jl" begin
Expand Down
Loading
Loading