loonatick-src / SolarHeatingSim

1 stars 0 forks source link

Make the system code automatic differentiation compatible. #2

Open loonatick-src opened 1 year ago

loonatick-src commented 1 year ago

On trying to solve the system using an implicit solver from SciML such as RadauIIA3, we get the following error message.

  Got exception outside of a @test
  First call to automatic differentiation for the Jacobian
  failed. This means that the user `f` function is not compatible
  with automatic differentiation. Methods to fix this include:

  1. Turn off automatic differentiation (e.g. Rosenbrock23() becomes
     Rosenbrock23(autodiff=false)). More details can befound at
     https://docs.sciml.ai/DiffEqDocs/stable/features/performance_overloads/
  2. Improving the compatibility of `f` with ForwardDiff.jl automatic 
     differentiation (using tools like PreallocationTools.jl). More details
     can be found at https://docs.sciml.ai/DiffEqDocs/stable/basics/faq/#Autodifferentiation-and-Dual-Numbers
  3. Defining analytical Jacobians. More details can be
     found at https://docs.sciml.ai/DiffEqDocs/stable/types/ode_types/#SciMLBase.ODEFunction

Followed by

  MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}, Float64, 1})

And the stack trace is entirely functions from SciML packages.

So, make the system AD compatible. Alternatives include using a finite difference approx Jacobian or analytical Jacobian by hand.

loonatick-src commented 1 year ago

This is most likely because of incompatible caches used for storing the matrix-vector product results -- we need the cache to be polymorphic over floats and dual numbers. The fix is to use get_tmp and dualcache from PreallocationTools.jl to initialize the temp buffers in SWHSCache.