Skip to content

Commit d7085c1

Browse files
committed
debug NonLinMPC tests
1 parent b535616 commit d7085c1

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

src/controller/execute.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ function predictstoch!(
226226
isnothing(ym) && error("Predictive controllers with InternalModel need the measured "*
227227
"outputs ym in keyword argument to compute control actions u")
228228
Ŷop, ny, yop = mpc.Ŷop, estim.model.ny, estim.model.yop
229-
ŷd = h(estim.model, estim.x̂d, d - estim.model.dop) .+ estim.model.yop
229+
ŷd = similar(estim.model.yop)
230+
h!(ŷd, estim.model, estim.x̂d, d - estim.model.dop)
231+
ŷd .+= estim.model.yop
230232
ŷs = zeros(NT, estim.model.ny)
231233
ŷs[estim.i_ym] .= @views ym .- ŷd[estim.i_ym] # ŷs=0 for unmeasured outputs
232234
Ŷop_LHS = similar(Ŷop)

src/estimator/internal_model.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ estimate its outputs ``\mathbf{ŷ}(k)``, since the strategy imposes that
272272
"""
273273
function evalŷ(estim::InternalModel{NT}, ym, d) where NT<:Real
274274
= Vector{NT}(undef, estim.model.ny)
275-
= h!(ŷ, estim.model, estim.x̂d, d - estim.model.dop) .+ estim.model.yop
275+
h!(ŷ, estim.model, estim.x̂d, d - estim.model.dop)
276+
.+= estim.model.yop
276277
ŷ[estim.i_ym] = ym
277278
return
278279
end

src/model/nonlinmodel.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ struct NonLinModel{NT<:Real, F<:Function, H<:Function} <: SimModel{NT}
2323
end
2424

2525
@doc raw"""
26-
NonLinModel{NT}(f::Function, h::Function, Ts, nu, nx, ny, nd=0)
26+
NonLinModel{NT}( f::Function, h::Function, Ts, nu, nx, ny, nd=0)
27+
NonLinModel{NT}(f!::Function, h!::Function, Ts, nu, nx, ny, nd=0)
2728
2829
Construct a nonlinear model from discrete-time state-space functions `f` and `h`.
2930
@@ -34,6 +35,14 @@ The state update ``\mathbf{f}`` and output ``\mathbf{h}`` functions are defined
3435
\mathbf{y}(k) &= \mathbf{h}\Big( \mathbf{x}(k), \mathbf{d}(k) \Big)
3536
\end{aligned}
3637
```
38+
They can be specified in two forms:
39+
40+
- non-mutating functions (out-of-place): they must be defined as `f(x, u, d) -> xnext` and
41+
`h(x, d) -> y`
42+
- mutating functions (in-place): they must be defined as `f!(xnext, x, u, d) -> nothing` and
43+
`h!(y, x, d) -> nothing`. This syntax reduces the allocations and potentially the
44+
computational burden as well.
45+
3746
`Ts` is the sampling time in second. `nu`, `nx`, `ny` and `nd` are the respective number of
3847
manipulated inputs, states, outputs and measured disturbances. The optional parameter `NT`
3948
explicitly specifies the number type of vectors (default to `Float64`).

test/test_predictive_control.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,12 @@ end
462462
@test ForwardDiff.gradient(g_Ymax_end, [1.0, 1.0]) [0.0, 0.0]
463463
linmodel3 = LinModel{Float32}(0.5*ones(1,1), ones(1,1), ones(1,1), zeros(1,0), zeros(1,0), 1.0)
464464
nmpc6 = NonLinMPC(linmodel3, Hp=10)
465-
moveinput!(nmpc6, [0]) [0.0]
465+
@test moveinput!(nmpc6, [0]) [0.0]
466466
nonlinmodel2 = NonLinModel{Float32}(f, h, 3000.0, 1, 2, 1, 1)
467467
nmpc7 = NonLinMPC(nonlinmodel2, Hp=10)
468-
nonlinmodel2.h(Float32[0,0], Float32[0])
469-
moveinput!(nmpc7, [0], [0]) [0.0]
468+
y = similar(nonlinmodel2.yop)
469+
nonlinmodel2.h!(y, Float32[0,0], Float32[0])
470+
@test moveinput!(nmpc7, [0], [0]) [0.0]
470471
end
471472

472473
@testset "NonLinMPC step disturbance rejection" begin

test/test_sim_model.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,11 @@ end
112112
@test nonlinmodel1.nu == 2
113113
@test nonlinmodel1.nd == 0
114114
@test nonlinmodel1.ny == 2
115-
@test nonlinmodel1.f!([0,0],[0,0],[0,0],[1]) zeros(2,)
116-
@test nonlinmodel1.h!([0,0],[0,0],[1]) zeros(2,)
115+
xnext, y = similar(nonlinmodel1.x), similar(nonlinmodel1.yop)
116+
nonlinmodel1.f!(xnext,[0,0],[0,0],[1])
117+
@test xnext zeros(2,)
118+
nonlinmodel1.h!(y,[0,0],[1])
119+
@test y zeros(2,)
117120

118121
linmodel2 = LinModel(sys,Ts,i_d=[3])
119122
f2(x,u,d) = linmodel2.A*x + linmodel2.Bu*u + linmodel2.Bd*d
@@ -124,8 +127,11 @@ end
124127
@test nonlinmodel2.nu == 2
125128
@test nonlinmodel2.nd == 1
126129
@test nonlinmodel2.ny == 2
127-
@test nonlinmodel2.f!([0,0,0,0],[0,0,0,0],[0,0],[0]) zeros(4,)
128-
@test nonlinmodel2.h!([0,0],[0,0,0,0],[0]) zeros(2,)
130+
xnext, y = similar(nonlinmodel2.x), similar(nonlinmodel2.yop)
131+
nonlinmodel2.f!(xnext,[0,0,0,0],[0,0],[0])
132+
@test xnext zeros(4,)
133+
nonlinmodel2.h!(y,[0,0,0,0],[0])
134+
@test y zeros(2,)
129135

130136
nonlinemodel3 = NonLinModel{Float32}(f2,h2,Ts,2,4,2,1)
131137
@test isa(nonlinemodel3, NonLinModel{Float32})

0 commit comments

Comments
 (0)