Skip to content

Commit f1e04f6

Browse files
committed
test: linearization tests with AutoFiniteDiff
1 parent b6e9de7 commit f1e04f6

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

test/1_test_sim_model.jl

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ end
152152

153153
@testitem "NonLinModel construction" setup=[SetupMPCtests] begin
154154
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
155+
using DifferentiationInterface
156+
import FiniteDiff
155157
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
156158
f1(x,u,_,model) = model.A*x + model.Bu*u
157159
h1(x,_,model) = model.C*x
@@ -243,8 +245,9 @@ end
243245
@test xnext zeros(2)
244246
nonlinemodel7.h!(y, [0; 0], [0], nonlinemodel7.p)
245247
@test y zeros(1)
248+
nonlinemodel8 = NonLinModel(f2!, h2!, 1.0, 1, 2, 1, 1, p=p, jacobian=AutoFiniteDiff())
249+
@test nonlinemodel8.jacobian == AutoFiniteDiff()
246250

247-
248251
@test_throws ErrorException NonLinModel(
249252
(x,u)->linmodel1.A*x + linmodel1.Bu*u,
250253
(x,_,_)->linmodel1.C*x, Ts, 2, 4, 2, 1, solver=nothing)
@@ -280,7 +283,9 @@ end
280283
end
281284

282285
@testitem "NonLinModel linearization" setup=[SetupMPCtests] begin
283-
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, ForwardDiff
286+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
287+
using DifferentiationInterface
288+
import ForwardDiff, FiniteDiff
284289
Ts = 1.0
285290
f1(x,u,d,_) = x.^5 .+ u.^4 .+ d.^3
286291
h1(x,d,_) = x.^2 .+ d
@@ -292,12 +297,20 @@ end
292297
@test linmodel1.Bd 3*d.^2
293298
@test linmodel1.C 2*x.^1
294299
@test linmodel1.Dd 1*d.^0
295-
linmodel2 = LinModel(nonlinmodel1; x, u, d)
296-
@test linmodel1.A linmodel2.A
297-
@test linmodel1.Bu linmodel2.Bu
298-
@test linmodel1.Bd linmodel2.Bd
299-
@test linmodel1.C linmodel2.C
300-
@test linmodel1.Dd linmodel2.Dd
300+
linmodel1b = LinModel(nonlinmodel1; x, u, d)
301+
@test linmodel1.A linmodel1b.A
302+
@test linmodel1.Bu linmodel1b.Bu
303+
@test linmodel1.Bd linmodel1b.Bd
304+
@test linmodel1.C linmodel1b.C
305+
@test linmodel1.Dd linmodel1b.Dd
306+
307+
nonlinmodel2 = NonLinModel(f1,h1,Ts,1,1,1,1,solver=nothing, jacobian=AutoFiniteDiff())
308+
linmodel2 = linearize(nonlinmodel2; x, u, d)
309+
@test linmodel2.A 5*x.^4 atol=1e-3
310+
@test linmodel2.Bu 4*u.^3 atol=1e-3
311+
@test linmodel2.Bd 3*d.^2 atol=1e-3
312+
@test linmodel2.C 2*x.^1 atol=1e-3
313+
@test linmodel2.Dd 1*d.^0 atol=1e-3
301314

302315
f1!(ẋ, x, u, d, _) = (ẋ .= x.^5 .+ u.^4 .+ d.^3; nothing)
303316
h1!(y, x, d, _) = (y .= x.^2 .+ d; nothing)

0 commit comments

Comments
 (0)