Skip to content

Commit

Permalink
init fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed Feb 25, 2025
1 parent 0937cc6 commit 8c54b15
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
7 changes: 3 additions & 4 deletions docs/src/batch_linearization.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ This example will demonstrate how to linearize a nonlinear ModelingToolkit model
The model will be a simple Duffing oscillator:
```@example BATCHLIN
using ControlSystemsMTK, ModelingToolkit, MonteCarloMeasurements, ModelingToolkitStandardLibrary.Blocks
using ModelingToolkit: getdefault
unsafe_comparisons(true)
# Create a model
Expand All @@ -26,15 +25,15 @@ eqs = [D(x) ~ v
y.u ~ x]
@named duffing = ODESystem(eqs, t, systems=[y, u], defaults=[u.u => 0])
@named duffing = ODESystem(eqs, t, systems=[y, u])
```

## Batch linearization
To perform batch linearization, we create a vector of operating points, and then linearize the model around each of these points. The function [`batch_ss`](@ref) does this for us, and returns a vector of `StateSpace` models, one for each operating point. An operating point is a `Dict` that maps variables in the MTK model to numerical values. In the example below, we simply sample the variables uniformly within their bounds specified when we created the variables (normally, we might want to linearize on stationary points)
```@example BATCHLIN
N = 16 # Number of samples
xs = range(getbounds(x)[1], getbounds(x)[2], length=N)
ops = Dict.(x .=> xs)
ops = Dict.(u.u => 0, x .=> xs)
```

Just like [`ModelingToolkit.linearize`](@ref), [`batch_ss`](@ref) takes the set of inputs and the set of outputs to linearize between.
Expand Down Expand Up @@ -155,7 +154,7 @@ The generated code starts by defining the interpolation vector `xs`, this variab
We can linearize around a trajectory obtained from `solve` using the function [`trajectory_ss`](@ref). We provide it with a vector of time points along the trajectory at which to linearize, and in this case we specify the inputs and outputs to linearize between as analysis points `r` and `y`.
```@example BATCHLIN
timepoints = 0:0.01:8
Ps2, ssys = trajectory_ss(closed_loop, closed_loop.r, closed_loop.y, sol; t=timepoints)
Ps2, ssys = trajectory_ss(closed_loop, closed_loop.r, closed_loop.y, sol; t=timepoints, initialize=true, verbose=true)
bodeplot(Ps2, w, legend=false)
```

Expand Down
8 changes: 5 additions & 3 deletions src/ode_system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,9 @@ function trajectory_ss(sys, inputs, outputs, sol; t = _max_100(sol.t), allow_inp
minimum(t) < minimum(sol.t) && @warn("The minimum time in `t`: $(minimum(t)), is smaller than the minimum time in `sol.t`: $(minimum(sol.t)).")

# NOTE: we call linearization_funciton twice :( The first call is to get x=unknowns(ssys), the second call provides the operating points.
lin_fun, ssys = linearization_function(sys, inputs, outputs; kwargs...)
# lin_fun, ssys = linearization_function(sys, inputs, outputs; warn_initialize_determined = false, kwargs...)
lin_fun, ssys = linearization_function(sys, inputs, outputs; warn_initialize_determined = false, kwargs...)

x = unknowns(ssys)
defs = ModelingToolkit.defaults(sys)
ops = map(t) do ti
Expand All @@ -538,7 +540,7 @@ function trajectory_ss(sys, inputs, outputs, sol; t = _max_100(sol.t), allow_inp
ops = reduce(vcat, opsv)
t = repeat(t, inner = length(ops) ÷ length(t))
end
lin_fun, ssys = linearization_function(sys, inputs, outputs; op=ops[1], kwargs...)
# lin_fun, ssys = linearization_function(sys, inputs, outputs; op=ops[1], initialize, kwargs...)
lins = map(zip(ops, t)) do (op, t)
linearize(ssys, lin_fun; op, t, allow_input_derivatives)
# linearize(sys, inputs, outputs; op, t, allow_input_derivatives, initialize=false)[1]
Expand Down Expand Up @@ -676,7 +678,7 @@ function GainScheduledStateSpace(systems, vt; interpolator, x = zeros(systems[1]
@variables x(t)[1:nx]=x [
description = "State variables of gain-scheduled statespace system $name",
]
@variables v(t) = 0 [
@variables v(t) [
description = "Scheduling variable of gain-scheduled statespace system $name",
]

Expand Down

0 comments on commit 8c54b15

Please sign in to comment.