Skip to content

Linearize is failing when using AutoFiniteDiff() #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1-Bart-1 opened this issue Apr 7, 2025 · 2 comments · Fixed by #186
Closed

Linearize is failing when using AutoFiniteDiff() #185

1-Bart-1 opened this issue Apr 7, 2025 · 2 comments · Fixed by #186

Comments

@1-Bart-1
Copy link

1-Bart-1 commented Apr 7, 2025

This MWE fails:

using ModelPredictiveControl, FiniteDiff, DifferentiationInterface

ad_type = AutoFiniteDiff()

function f(x, u, _ , p)
    g, L, K, m = p          # [m/s²], [m], [kg/s], [kg]
    θ, ω = x[1], x[2]       # [rad], [rad/s]
    τ  = u[1]               # [Nm]= ω
    dω = -g/L*sin(θ) - K/m*ω + τ/m/L^2
    return [dθ, dω]
end
h(x, _ , _ ) = [180/π*x[1]] # [°]
p_model = [9.8, 0.4, 1.2, 0.3]
nu, nx, ny, Ts = 1, 2, 1, 0.1
vu, vx, vy = ["\$τ\$ (Nm)"], ["\$θ\$ (rad)", "\$ω\$ (rad/s)"], ["\$θ\$ (°)"]
model = setname!(NonLinModel(f, h, Ts, nu, nx, ny; p=p_model, jacobian=ad_type); u=vu, x=vx, y=vy)

linmodel = ModelPredictiveControl.linearize(model, x=[π, 0], u=[0])

While it runs succesfully when using ad_type = AutoForwardDiff()

Stacktrace:

ERROR: LoadError: ArgumentError: range must be non-empty
Stacktrace:
  [1] maximum
    @ ./range.jl:851 [inlined]
  [2] finite_difference_jacobian!(J::Matrix{…}, f::DifferentiationInterface.FixTail{…}, x::Vector{…}, cache::FiniteDiff.JacobianCache{…}, f_in::Nothing; relstep::Float64, absstep::Float64, colorvec::UnitRange{…}, sparsity::Nothing, dir::Bool)
    @ FiniteDiff ~/.julia/packages/FiniteDiff/EBPBu/src/jacobians.jl:450
  [3] finite_difference_jacobian! (repeats 2 times)
    @ ~/.julia/packages/FiniteDiff/EBPBu/src/jacobians.jl:406 [inlined]
  [4] jacobian!
    @ ~/.julia/packages/DifferentiationInterface/7eD1K/ext/DifferentiationInterfaceFiniteDiffExt/twoarg.jl:391 [inlined]
  [5] linfunc!
    @ ~/.julia/packages/ModelPredictiveControl/NFmDi/src/model/linearization.jl:42 [inlined]
  [6] linearize_core!
    @ ~/.julia/packages/ModelPredictiveControl/NFmDi/src/model/linearization.jl:195 [inlined]
  [7] linearize!(linmodel::LinModel{…}, model::NonLinModel{…}; x::Vector{…}, u::Vector{…}, d::Vector{…})
    @ ModelPredictiveControl ~/.julia/packages/ModelPredictiveControl/NFmDi/src/model/linearization.jl:166
  [8] linearize!
    @ ~/.julia/packages/ModelPredictiveControl/NFmDi/src/model/linearization.jl:155 [inlined]
  [9] linearize(model::NonLinModel{…}; kwargs::@Kwargs{})
    @ ModelPredictiveControl ~/.julia/packages/ModelPredictiveControl/NFmDi/src/model/linearization.jl:131
 [10] top-level scope
    @ ~/Code/KitePredictiveControl.jl/mwe/finitediff.jl:37
 [11] include(fname::String)
    @ Main ./sysimg.jl:38
 [12] top-level scope
    @ REPL[1]:1
in expression starting at /home/bart/Code/KitePredictiveControl.jl/mwe/finitediff.jl:37
Some type information was truncated. Use `show(err)` to see complete types.
@franckgaga
Copy link
Member

Thanks for the report! I can reproduce, will investigate on this.

@franckgaga
Copy link
Member

Should be solved by #186. I will release an update just after merging the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants