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.