Recursive use of DifferentialEquations.jl solutions

Hi, I am currently in the process of implementing a two pass trajectory optimization algorithm where the solution (sol) of one IVP problem produced in the first pass is a parameter (part of p) in the IVP problem of the second pass, its solution then serving as a parameter for the first pass of the next iteration. The reason I am passing the whole solution is that I need to evaluate the dense output each time dynamics (f) are called, which isn’t fixed with adaptive time-stepping.

When using solve the main bottleneck seemed to be compilation time (99.9%) which occurred at every call, so instead I tried to first initialize an integrator with init and then just update p before every pass. Unfortunately, this produces an ERROR: LoadError: MethodError: Cannot convert an object of type.... The problem seems to be that even though I am using a method with “free” interpolation, a lot of information about the previous pass, such as the signature of f and the type of p are stored in sol, even though they shouldn’t be needed for interpolation (there might be some other reason for including them). This also explains why solve had to be compiled every time.

Is there some crafty way I could resolve this or should I bite the bullet and roll my own implementation of explicit RK integration? And perhaps, is something worth addressing in DifferentialEquations.jl itself?

I do have an open issue for making this simpler:

Though your case doesn’t need pretty much any of that. You could just put your p in a FunctionWrapper (FunctionWrappers.jl) in your case and that would eliminate all extra information and allow for caching the compilation in a non-f-specific way. It’s one line of code to put the wrapper on it, but on my phone so ask if you have an issue with that.

That should never be required, since at the end of the day SimpleDiffEq.jl has the straightforward loops if that’s what you need. Of course, that’s going to not have all of the error checking and adaptivity features, but it trades for being simpler and cheaper compile time.

Thanks for the suggestions. How FunctionWrappers.jl works isn’t clear to me at first glance but I’ll have a closer look tomorrow. Using SimpleDiffEq.jl is certainly also a viable option, I’d just have add a function for interpolating f.

@ChrisRackauckas using FunctionWrappers.jl solved the performance issues, thank you for the recommendation.

In the future I will most likely want use DifferentialEquations.jl in more unusual ways, but before then I will hopefully publish a paper describing the algorithm.

No problem, glad it worked. Yes, we might want to create some nice tutorials with FunctionWrappers.jl to make it more accessible, it’s a good tool but underdocumented right now.