The 2.14 release introduces the support of NonLinModelDAE for the MovingHorizonEstimator. I added a nice tutorial on a pH neutralization process. See it here: Nonlinear Design (DAE) · ModelPredictiveControl.jl
Next step: supporting NonLinModelDAE in NonLinMPC. Should be drastically easier than MHE. So, expect a new release in the next weeks!
Many thanks ! I think we all have to acknowledge by the way the enormous work of Donald Knuth and Till Tantau for the iconic Computer Modern font, LaTeX and TikZ. They are not easy-to-use tools, but the results is clearly unmatched by anything else (although Typst seems like a strong contender!). They are aesthetes among scientists. It doesn’t grow on trees!
The 2.14.1, 2.14.2, 2.14.3 and 2.14.4 releases introduced some bugfixes related to DAE systems. The v2.15.0 will be released in a few minutes, and now the NonLinMPC fully supports NonLinModelDAE. I completed the pH neutralization tutorial with an adapative NMPC strategy with tight constraints on the pH. See it in action here: Nonlinear Design (DAE) · ModelPredictiveControl.jl
The latest release introduces the support of first-order hold (FOH, or piecewise linear functions) on the manipulated inputs \mathbf{u} and measured disturbances \mathbf{d}, for the RungeKutta solver. It was already supported for all the CollocationMethods, but not in SingleShooting and MultipleShooting transcriptions for NonLinMPC and MovingHorizonEstimator. I personally never encountered an MPC package that natively support FOH discretization in the explicit Runge-Kutta solver, so this is feature is quite exclusive AFAIK.
The default is still zero-order hold (ZOH) for \mathbf{u}, since this is usually how the I/O electronic stack behave. But measured disturbances \mathbf{d} are now always discretized with FOH. This is more adequate for sampled continuous signals, and it can slightly improve the performances of the feedforward compensation. Lastly, this change makes the codebase more consistent. FOH is used for \mathbf{d} everywhere: in LinModel, NonLinModel and NonLinModelDAE.
In the same vein, it is also possible to simulate nonlinear plant model using FOH discretization in open-loop, with the new unext and dnext keyword arguments. Note that this is non-causal since it’s information about the future, but it is still useful in practice because “smooth” disturbances without any discontinuities in the signals are generally more realistic than the ZOH approach.
The release will be registered in the following hours. My next task will be adding bound constraints on the algebraic variable \mathbf{a} for the MovingHorizonEstimator. Here’s the change log since my last post:
added: first-order hold (FOH) discretization for RungeKutta in MPC and MHE
added: FOH discretization of NonLinModel and NonLinModelDAE open-loop simulations
added: InternalModel now supports NonLinModelDAE
changed: measured disturbanaces d are discretized with FOH with SingleShooting and MultipleShooting
debug: do not use mhe.buffer.x̂ in getinfo(mhe)
changed: verbose=false for getinfo with MPC and MHE (redundant information)
changed: small cleanup in UnscentedKalmanFilter implementation
doc: various improvements in the pH neutralization example of the manual
bench: create 3 bench categories with matrix (improve parallelism)
I will soon register a new patch release with a small change, but with HUGE improvements on the user experience:
~2.5x faster precompilation of the package (it was one of the worse package in the Julia ecosystem on this aspect, this is now way better)
~2.5x faster TTFX when constructing NonLinMPC and MovingHorizonEstimator objects
the allocated memory more than halved both when constructing and solving NonLinMPC and MovingHorizonEstimator
minor improvements for the solve time of NonLinMPC and MovingHorizonEstimator (~1.1 faster), likely because of the reduced allocations.
For me, it also means that all the CI jobs are about 2x faster, a nice side bonus.
The culprit: I’m using the MOI.VectorNonlinearOracle for the constraints, but I’m still using the splatting syntax and JuMP.add_nonlinear_operator for the objective function. I need 3 complex closures for the f, ∇f and ∇²f arguments, in order to call DI.value_gradient_and_hessian! once and cache the 3 results. Julia compiled these 3 complex closures with many captured variables, which is a complete waste of resources since MOI.jl internally call them with dynamic dispatch in the end. The trick was creating a function-like object that hide the concrete type of these 3 closures, to force dynamic dispatch everywhere.
(Another workaround would be to do an epigraph reformulation, \min_{\mathbf{\tilde{Z}}} t subject to t \ge J({\mathbf{\tilde{Z}}}), and define the objective with the MOI.VectorNonlinearOracle, but it will change the shape of the optimization problem)
There is nothing under the last red set bar, and there are no external C calls involved and such, it must means that this is Julia compilation (the bar should be gray for compilation, but these colors are heuristics)
I already knew that MOI.jl calls the registered nonlinear operators with a dynamic dispatch internally (by looking at their code), so my instinct was to disable compilation altogether on these 3 closures defined by me.
I was not sure how to do this so I asked an LLM. He suggested two solutions:
a. inserting @nospecialize in the signatures, did not liked it, I tend to avoid macro If I can.
b. a new function-like object that hides the concrete type of the original function:
struct NoTypeFunction <: Function
f::Function # the field type is deliberately abstract here
end
(obj::NoTypeFunction)(args...) = obj.f(args...)
I found the b. solution a bit weird but simple enough so I opt for this one. It worked, and it drastically reduced the precompilation time by the way. Here’s the new flame graph, the set bar is so small that it is no longer visible:
And was it successful in the past on problem like this (long TTFX or long precompilation)? I mean, can we deduce the same information from JETLS output that I deduced from the profiling? That is, the majority of the time in add_nonlinear_operator is compilation?
It is not exactly the same information, but related: JETLS will always flag closures that capture variables, and so far AI was always able (with my code base) to fix that. JETLS finds type instabilities, and type instabilities cause a bad performance. Not sure if they cause long precompilation times, though. Invalidations cause long precompilation times.