From ee710350a8305595d3711ab3e45790daa76fc5ac Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Thu, 1 Aug 2024 19:03:23 +0200 Subject: [PATCH] bump compat DataInterpolations (#68) * Update Project.toml * up tests * add IC * up * more up * bump version --- Project.toml | 4 ++-- docs/src/batch_linearization.md | 10 +++++----- docs/src/index.md | 7 ++++--- test/test_ODESystem.jl | 14 ++++++++++---- test/test_batchlin.jl | 2 +- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Project.toml b/Project.toml index 0c065ef..8ec8df3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ControlSystemsMTK" uuid = "687d7614-c7e5-45fc-bfc3-9ee385575c88" authors = ["Fredrik Bagge Carlson"] -version = "2.1.0" +version = "2.2.0" [deps] ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e" @@ -16,7 +16,7 @@ UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed" [compat] ControlSystemsBase = "1.0.1" -DataInterpolations = "3, 4" +DataInterpolations = "3, 4, 5, 6" ModelingToolkit = "9.0" ModelingToolkitStandardLibrary = "2" MonteCarloMeasurements = "1.1" diff --git a/docs/src/batch_linearization.md b/docs/src/batch_linearization.md index c87a042..bf68d2c 100644 --- a/docs/src/batch_linearization.md +++ b/docs/src/batch_linearization.md @@ -26,7 +26,7 @@ eqs = [D(x) ~ v y.u ~ x] -@named duffing = ODESystem(eqs, t, systems=[y, u]) +@named duffing = ODESystem(eqs, t, systems=[y, u], defaults=[u.u => 0]) ``` ## Batch linearization @@ -61,7 +61,7 @@ bodeplot(P, w, legend=:bottomright) # Should look similar to the one above ``` ## Controller tuning -Let's also do some controller tuning for the linearized models above. The function `batch_tune` is not really required here, but it shows how we might go about building more sophisticated tools for batch tuning. In this example, we will tune a PID controller using the function [`loopshapingPID`](@ref). +Let's also do some controller tuning for the linearized models above. The function `batch_tune` is not really required here, but it shows how we might go about building more sophisticated tools for batch tuning. In this example, we will tune a PID controller using the function [`loopshapingPID`](@ref). Note, this procedure is not limited to tuning a gain-scheduled PID controller, it should work for gain-scheduling of any LTI controller. ```@example BATCHLIN function batch_tune(f, Ps) f.(Ps) @@ -119,7 +119,7 @@ for C in Cs connect(Ci.output, duffing.u) ] @named closed_loop = ODESystem(eqs, t, systems=[duffing, Ci, fb, ref, F]) - prob = ODEProblem(structural_simplify(closed_loop), [], (0.0, 8.0)) + prob = ODEProblem(structural_simplify(closed_loop), [F.xd => 0], (0.0, 8.0)) sol = solve(prob, Rodas5P(), abstol=1e-8, reltol=1e-8) plot!(sol, idxs=[duffing.y.u, duffing.u.u], layout=2, lab="") end @@ -133,8 +133,8 @@ eqs = [ connect(duffing.y, Cgs.scheduling_input) # Don't forget to connect the scheduling variable! ] @named closed_loop = ODESystem(eqs, t, systems=[duffing, Cgs, fb, ref, F]) -prob = ODEProblem(structural_simplify(closed_loop), [], (0.0, 8.0)) -sol = solve(prob, Rodas5P(), abstol=1e-8, reltol=1e-8) +prob = ODEProblem(structural_simplify(closed_loop), [F.xd => 0], (0.0, 8.0)) +sol = solve(prob, Rodas5P(), abstol=1e-8, reltol=1e-8, initializealg=NoInit()) plot!(sol, idxs=[duffing.y.u, duffing.u.u], l=(2, :red), lab="Gain scheduled") plot!(sol, idxs=F.output.u, l=(1, :black, :dash, 0.5), lab="Ref") ``` diff --git a/docs/src/index.md b/docs/src/index.md index 018d5bc..3c4dadf 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -151,8 +151,8 @@ m1 = 1 m2 = 1 k = 1000 # Spring stiffness c = 10 # Damping coefficient -@named inertia1 = Inertia(; J = m1) -@named inertia2 = Inertia(; J = m2) +@named inertia1 = Inertia(; J = m1, w=0) +@named inertia2 = Inertia(; J = m2, w=0) @named spring = Spring(; c = k) @named damper = Damper(; d = c) @named torque = Torque(use_support=false) @@ -178,7 +178,7 @@ model = SystemModel() |> complete ### Numeric linearization We can linearize this model numerically using `named_ss`, this produces a `NamedStateSpace{Continuous, Float64}` ```@example LINEAIZE_SYMBOLIC -lsys = named_ss(model, [model.torque.tau.u], [model.inertia1.phi, model.inertia2.phi]) +lsys = named_ss(model, [model.torque.tau.u], [model.inertia1.phi, model.inertia2.phi], op = Dict(model.torque.tau.u => 0)) ``` ### Symbolic linearization If we instead call `linearize_symbolic` and pass the jacobians into `ss`, we get a `StateSpace{Continuous, Num}` @@ -192,6 +192,7 @@ That's pretty cool, but even nicer is to generate some code for this symbolic sy ```@example LINEAIZE_SYMBOLIC defs = ModelingToolkit.defaults(simplified_sys) +defs = merge(Dict(unknowns(model) .=> 0), defs) x, pars = ModelingToolkit.get_u0_p(simplified_sys, defs, defs) # Extract the default state and parameter values fun = Symbolics.build_function(symbolic_sys, unknowns(simplified_sys), ModelingToolkit.parameters(simplified_sys); diff --git a/test/test_ODESystem.jl b/test/test_ODESystem.jl index a5a6f79..7c0546f 100644 --- a/test/test_ODESystem.jl +++ b/test/test_ODESystem.jl @@ -2,6 +2,7 @@ using ControlSystemsMTK, ControlSystemsBase, ModelingToolkit, OrdinaryDiffEq, RobustAndOptimalControl import ModelingToolkitStandardLibrary.Blocks as Blocks conn = ModelingToolkit.connect +connect = ModelingToolkit.connect ## Test SISO (single input, single output) system @parameters t @@ -22,7 +23,9 @@ fb = structural_simplify(fb0) # @test length(unknowns(P)) == 3 # 1 + u + y # @test length(unknowns(C)) == 4 # 2 + u + y -x0 = Pair[loopgain.P.x=>1.0] +x0 = Pair[ + collect(loopgain.P.x) .=> 1.0; +] prob = ODEProblem(fb, x0, (0.0, 10.0)) sol = solve(prob, Rodas5()) @@ -187,8 +190,8 @@ m2 = 1 k = 1000 # Spring stiffness c = 10 # Damping coefficient -@named inertia1 = Inertia(; J = m1) -@named inertia2 = Inertia(; J = m2) +@named inertia1 = Inertia(; J = m1, w=0) +@named inertia2 = Inertia(; J = m2, w=0) @named spring = Spring(; c = k) @named damper = Damper(; d = c) @@ -229,9 +232,12 @@ sys = ss((mats...,)[1:4]...) defs = ModelingToolkit.defaults(ssys) -sympars = ModelingToolkit.parameters(ssys) +defs = merge(Dict(unknowns(model) .=> 0), defs) _, p = ModelingToolkit.get_u0_p(ssys, defs, defs) + +sympars = ModelingToolkit.parameters(ssys) + fun = Symbolics.build_function(sys, sympars; expression=Val{false}, force_SA=true) fun(p) @test @allocated(fun(p)) <= 256 diff --git a/test/test_batchlin.jl b/test/test_batchlin.jl index a51ccf9..94b2eb7 100644 --- a/test/test_batchlin.jl +++ b/test/test_batchlin.jl @@ -64,7 +64,7 @@ closed_loop_eqs = [ @named closed_loop = ODESystem(closed_loop_eqs, t, systems=[duffing, C, fb, ref, F]) ssys = structural_simplify(closed_loop) -prob = ODEProblem(ssys, [], (0.0, 8.0)) +prob = ODEProblem(ssys, [F.xd => 0], (0.0, 8.0)) sol = solve(prob, Rodas5P(), abstol=1e-8, reltol=1e-8) # plot(sol)