There are definitely a lot of NaN’s, long before the program crashes. So the problem probably lies in the forwarddiff.
The error message pasted below. I don’t call log at any point in my model.
┌ Error: MPC terminated without solution: returning last solution shifted
│ status = INVALID_MODEL::TerminationStatusCode = 21
â”” @ ModelPredictiveControl ~/.julia/packages/ModelPredictiveControl/OoBo3/src/controller/execute.jl:456
ERROR: LoadError: DomainError with -1.0171433030139918e-5:
log was called with a negative real argument but will only return a complex result if called with a complex argument. Try log(Complex(x)).
DomainError detected in the user `f` function. This occurs when the domain of a function is violated.
For example, `log(-1.0)` is undefined because `log` of a real number is defined to only output real
numbers, but `log` of a negative number is complex valued and therefore Julia throws a DomainError
by default. Cases to be aware of include:
* `log(x)`, `sqrt(x)`, `cbrt(x)`, etc. where `x<0`
* `x^y` for `x<0` floating point `y` (example: `(-1.0)^(1/2) == im`)
Within the context of SciML, this error can occur within the solver process even if the domain constraint
would not be violated in the solution due to adaptivity. For example, an ODE solver or optimization
routine may check a step at `new_u` which violates the domain constraint, and if violated reject the
step and use a smaller `dt`. However, the throwing of this error will have halted the solving process.
Thus the recommended fix is to replace this function with the equivalent ones from NaNMath.jl
(https://github.com/JuliaMath/NaNMath.jl) which returns a NaN instead of an error. The solver will then
effectively use the NaN within the error control routines to reject the out of bounds step. Additionally,
one could perform a domain transformation on the variables so that such an issue does not occur in the
definition of `f`.
For more information, check out the following FAQ page:
https://docs.sciml.ai/Optimization/stable/API/FAQ/#The-Solver-Seems-to-Violate-Constraints-During-the-Optimization,-Causing-DomainErrors,-What-Can-I-Do-About-That?
Stacktrace:
[1] throw_complex_domainerror(f::Symbol, x::Float64)
@ Base.Math ./math.jl:33
[2] _log(x::Float64, base::Val{:â„Ż}, func::Symbol)
@ Base.Math ./special/log.jl:301
[3] log
@ ./special/log.jl:267 [inlined]
[4] macro expansion
@ ~/.julia/packages/JuliaSimCompiler/y10jZ/src/backends/julia.jl:214 [inlined]
[5] macro expansion
@ ~/.julia/packages/RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:163 [inlined]
[6] macro expansion
@ ./none:0 [inlined]
[7] generated_callfunc
@ ./none:0 [inlined]
[8] (::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{…})(::Vector{…}, ::SubArray{…}, ::Vector{…}, ::Vector{…}, ::Float64)
@ RuntimeGeneratedFunctions ~/.julia/packages/RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:150
[9] f!
@ ~/Code/KitePredictiveControl.jl/src/mtk_interface.jl:19 [inlined]
[10] (::ModelPredictiveControl.var"#inner_solver_f!#19"{…})(xnext::SubArray{…}, x::SubArray{…}, u::Vector{…}, d::Vector{…}, p::Vector{…})
@ ModelPredictiveControl ~/.julia/packages/ModelPredictiveControl/OoBo3/src/model/solver.jl:61
[11] f!
@ ~/.julia/packages/ModelPredictiveControl/OoBo3/src/model/nonlinmodel.jl:208 [inlined]
[12] f̂!(x̂next0::SubArray{…}, û0::Vector{…}, estim::UnscentedKalmanFilter{…}, model::NonLinModel{…}, x̂0::Vector{…}, u0::Vector{…}, d0::Vector{…})
@ ModelPredictiveControl ~/.julia/packages/ModelPredictiveControl/OoBo3/src/estimator/execute.jl:44
[13] update_estimate!(estim::UnscentedKalmanFilter{Float64, NonLinModel{…}}, y0m::Vector{Float64}, d0::Vector{Float64}, u0::Vector{Float64})
@ ModelPredictiveControl ~/.julia/packages/ModelPredictiveControl/OoBo3/src/estimator/kalman.jl:850
[14] updatestate!(estim::UnscentedKalmanFilter{Float64, NonLinModel{…}}, u::Vector{Float64}, ym::Vector{Float64}, d::Vector{Float64})
@ ModelPredictiveControl ~/.julia/packages/ModelPredictiveControl/OoBo3/src/estimator/execute.jl:277
[15] updatestate!
@ ~/.julia/packages/ModelPredictiveControl/OoBo3/src/controller/execute.jl:508 [inlined]
[16] macro expansion
@ ~/.julia/packages/ModelPredictiveControl/OoBo3/src/plot_sim.jl:303 [inlined]
[17] macro expansion
@ ~/.julia/packages/ProgressLogging/6KXlp/src/ProgressLogging.jl:470 [inlined]
[18] sim_closedloop!(est_mpc::NonLinMPC{…}, estim::UnscentedKalmanFilter{…}, N::Int64, u_ry::Vector{…}, d::Vector{…}, ru::Vector{…}; plant::NonLinModel{…}, u_step::Vector{…}, u_noise::Vector{…}, y_step::Vector{…}, y_noise::Vector{…}, d_step::Vector{…}, d_noise::Vector{…}, x_noise::Vector{…}, x_0::Vector{…}, xhat_0::Nothing, lastu::Vector{…}, x̂_0::Nothing)
@ ModelPredictiveControl ~/.julia/packages/ModelPredictiveControl/OoBo3/src/plot_sim.jl:285
[19] #sim!#157
@ ~/.julia/packages/ModelPredictiveControl/OoBo3/src/plot_sim.jl:244 [inlined]
[20] sim!
@ ~/.julia/packages/ModelPredictiveControl/OoBo3/src/plot_sim.jl:236 [inlined]
[21] top-level scope
@ ~/Code/KitePredictiveControl.jl/src/KitePredictiveControl.jl:50
[22] include(fname::String)
@ Base.MainInclude ./client.jl:489
[23] top-level scope
@ REPL[46]:1
in expression starting at /home/bart/Code/KitePredictiveControl.jl/src/KitePredictiveControl.jl:50
Some type information was truncated. Use `show(err)` to see complete types.
When using MadNLP I get the following message:
EXIT: Invalid number in NLP objective gradient detected.
So apparantly the gradients of my model are invalid / NaN. Is there any way to debug what parts of my model are causing this? I will try to run Forwarddiff manually.