SciML / SciMLBase.jl

The Base interface of the SciML ecosystem
https://docs.sciml.ai/SciMLBase/stable
MIT License
119 stars 91 forks source link

Leaner serialization for solution types #629

Open ChrisRackauckas opened 4 months ago

ChrisRackauckas commented 4 months ago

The solution types contain functions, and things like JLD2 don't like functions. So it would be nice to have a "strip_solution` function that generates a much leaner form of the solution for simply doing easier serialization. Here's how that should look:

  1. [ ] we should make a new trait for has_lazy_interpolation in https://github.com/SciML/SciMLBase.jl/blob/master/src/alg_traits.jl which is then set to true on BS5 and VernX solvers. strip_solution should then give an informative error message that these problems are not supported by this function. Just some proper error handling, and with that out of the way the other solvers should be free to go.
  2. [ ] https://github.com/SciML/SciMLBase.jl/blob/master/src/solutions/ode_solutions.jl#L102-L118 we can strip out any of the function information from this object. I.e. make a new problem where prob and alg are nothing.
  3. [ ] Downstream, sol.interp holds some extra information as well. So in SciMLBase we need strip_interp(::AbstractInterpolation). The SciMLBase ones are just the identity function, so that's easy https://github.com/SciML/SciMLBase.jl/blob/master/src/interpolation.jl .
  4. [ ] At this point any non-OrdinaryDiffEq algorithm will strip well. The only downstream interp to handle is the OrdinaryDiffEq one. https://github.com/SciML/OrdinaryDiffEq.jl/blob/master/src/interp_func.jl#L4-L15 . f is only in there for the lazy interpolations, so set it to nothing. You need to keep the caches because that's used for dispatch, but at this point you're done with explicit methods.
  5. [ ] For implicit methods, you need to strip https://github.com/SciML/OrdinaryDiffEq.jl/blob/master/src/caches/rosenbrock_caches.jl#L30-L33 jac_config and grad_config, as those contain function information. As a simple thing, you can just create a new cache with everything nothing though since this is only used for dispatch (and addsteps!, but since you know these caches are non-lazy the interpolation addsteps! post solution is trivial so it won't use anything in the cache). We need to make good functionality for this anyways for better default solvers (CC @oscardssmith), so it might be a good time to do this now.

With that done strip_solution(sol) should return a lean solution with no function information in it (or rather, any function information in it is in SciMLBase, which also defines the structure so that's a required import) and so it should work just fine with any BSON, JLD2, etc. package.