From 52ef5ea8bf14d301f9af83e321977f0dfb9e2019 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Mon, 16 Nov 2026 14:59:51 -0500 Subject: [PATCH 1/4] added: `xs_0` kwarg to `NonLinModel` This is similar to the same argument for `NonLinModelDAE` types. --- src/model/nonlinmodel.jl | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/model/nonlinmodel.jl b/src/model/nonlinmodel.jl index 4a17d7b34..c64f7fd62 100644 --- a/src/model/nonlinmodel.jl +++ b/src/model/nonlinmodel.jl @@ -53,7 +53,9 @@ struct NonLinModel{ buffer::SimModelBuffer{NT} function NonLinModel{NT}( solver::DS, f!::F, h!::H, Ts, nu, nx, ny, nd, - p::PT, jacobian::JB, linfunc!::LF, + p::PT, + xs_0, + jacobian::JB, linfunc!::LF, unit="s" ) where { NT<:Real, @@ -70,11 +72,11 @@ struct NonLinModel{ dop = zeros(NT, nd) xop = zeros(NT, nx) fop = zeros(NT, nx) - xs_0 = zeros(NT, nx) uname = ["\$u_{$i}\$" for i in 1:nu] yname = ["\$y_{$i}\$" for i in 1:ny] dname = ["\$d_{$i}\$" for i in 1:nd] xname = ["\$x_{$i}\$" for i in 1:nx] + size(xs_0) ≠ (nx,) && throw(DimensionMismatch("xs_0 size $(size(xs_0)) ≠ state size ($nx,)")) x0 = zeros(NT, nx) t = zeros(NT, 1) ni = solver.ni @@ -149,6 +151,7 @@ See also [`LinModel`](@ref), and [`NonLinModelDAE`](@ref) to include algebraic e - `ny`: number of outputs. - `nd=0`: number of measured disturbances. - `p=[]`: parameters of the model (any type). +- `xs_0=zeros(nx)`: initial guess (optimization warm-start) for the states. - `solver=RungeKutta(4)`: a [`DiffSolver`](@ref) object for the discretization of continuous dynamics, use `nothing` for discrete-time models (default to 4th order [`RungeKutta`](@ref)). - `jacobian=AutoForwardDiff()`: an `AbstractADType` backend when [`linearize`](@ref) is @@ -208,7 +211,11 @@ NonLinModel with a sample time Ts = 2.0 s: """ function NonLinModel{NT}( f::Function, h::Function, Ts::Real, nu::Int, nx::Int, ny::Int, nd::Int=0; - p=NT[], solver=RungeKutta(4), jacobian=AutoForwardDiff(), unit="s" + p=NT[], + xs_0 = zeros(NT, nx), + solver=RungeKutta(4), + jacobian=AutoForwardDiff(), + unit="s" ) where {NT<:Real} isnothing(solver) && (solver=EmptySolver()) f!, h! = get_mutating_functions(NT, f, h) @@ -216,15 +223,20 @@ function NonLinModel{NT}( NT, f!, h!, Ts, nu, nx, ny, nd, p, solver, jacobian ) return NonLinModel{NT}( - solver, f!, h!, Ts, nu, nx, ny, nd, p, jacobian, linfunc!, unit + solver, f!, h!, Ts, nu, nx, ny, nd, p, xs_0, + jacobian, linfunc!, unit ) end function NonLinModel( f::Function, h::Function, Ts::Real, nu::Int, nx::Int, ny::Int, nd::Int=0; - p=Float64[], solver=RungeKutta(4), jacobian=AutoForwardDiff(), unit="s" + p=Float64[], + xs_0 = zeros(NT, nx), + solver=RungeKutta(4), + jacobian=AutoForwardDiff(), + unit="s" ) - return NonLinModel{Float64}(f, h, Ts, nu, nx, ny, nd; p, solver, jacobian, unit) + return NonLinModel{Float64}(f, h, Ts, nu, nx, ny, nd; p, xs_0, solver, jacobian, unit) end "Get the mutating functions `f!` and `h!` from the provided functions in argument." From 239c34a41cec54ed8beb7e0066bcb9ac4777a6b7 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Mon, 16 Nov 2026 15:03:33 -0500 Subject: [PATCH 2/4] debug: default to `Float64` if not specified --- src/model/nonlinmodel.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model/nonlinmodel.jl b/src/model/nonlinmodel.jl index c64f7fd62..1c030716f 100644 --- a/src/model/nonlinmodel.jl +++ b/src/model/nonlinmodel.jl @@ -231,7 +231,7 @@ end function NonLinModel( f::Function, h::Function, Ts::Real, nu::Int, nx::Int, ny::Int, nd::Int=0; p=Float64[], - xs_0 = zeros(NT, nx), + xs_0 = zeros(nx), solver=RungeKutta(4), jacobian=AutoForwardDiff(), unit="s" From 491e4f1fc77e9900bccfdef248250dce04cf4196 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Mon, 16 Nov 2026 15:06:14 -0500 Subject: [PATCH 3/4] doc: forwarding `xs_0` to `NonLinModel` in MTK example --- docs/src/manual/mtk.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/src/manual/mtk.md b/docs/src/manual/mtk.md index a3e899f45..003186637 100644 --- a/docs/src/manual/mtk.md +++ b/docs/src/manual/mtk.md @@ -137,7 +137,8 @@ function generate_model( ) else f!, h! = f_mtk!, h_mtk! - model = NonLinModel(f!, h!, Ts, nu, nx, ny, nd; p, unit) + xs_0 = xs_mtk + model = NonLinModel(f!, h!, Ts, nu, nx, ny, nd; p, xs_0, unit) end return model # deliberately type unstable function end From fbe47c7bd61d2df54755bea7353f4a813fbb6a42 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Mon, 16 Nov 2026 15:37:15 -0500 Subject: [PATCH 4/4] test: `xs_0` test for `NonLinModel` --- test/1_test_sim_model.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/1_test_sim_model.jl b/test/1_test_sim_model.jl index 2c4f65be9..fcf1057be 100644 --- a/test/1_test_sim_model.jl +++ b/test/1_test_sim_model.jl @@ -260,6 +260,8 @@ end @test y ≈ zeros(1) nonlinmodel8 = NonLinModel(f2!, h2!, 1.0, 1, 2, 1, 1, p=p, jacobian=AutoFiniteDiff()) @test nonlinmodel8.jacobian == AutoFiniteDiff() + nonlinmodel9 = NonLinModel(f2!, h2!, 1.0, 1, 2, 1, 1; p, xs_0=[3.5, 9.5]) + @test nonlinmodel9.xs_0 ≈ [3.5, 9.5] @test_throws ErrorException NonLinModel( (x,u)->linmodel1.A*x + linmodel1.Bu*u,