SciML / DiffEqCallbacks.jl

A library of useful callbacks for hybrid scientific machine learning (SciML) with augmented differential equation solvers
https://docs.sciml.ai/DiffEqCallbacks/stable/
Other
95 stars 47 forks source link

Numerical Integration Callbacks purpose and usage example #208

Closed a6a3uh closed 7 months ago

a6a3uh commented 7 months ago

Hi!

My question is about Numerical Integration Callback. In particular IntegratingCallback. Unfortunately there is no explicit example in the documentation that would demonstrate intended purpose of that feature. Also there is IntegratingSumCallback spotted in the source code which is not documented at all.

My intention is to calculate some integral of running cost function for my control system simulation which is described as an ODE. And it looks like Numerical Integration Callbacks should do the job.

But trying it I'm getting weird errors and sometimes even stack overflow.

I've written a question on the forum already with not much attention yet unfortunately.

I've boiled down my example to simplest form possible as follows:

      prob = ODEProblem((u,p,t)->[1.0], [0.0], (0.0,1.0), ())
      integrated = IntegrandValues(Float64, Vector{Float64})
      sol = solve(prob, Euler(),
                  callback = IntegratingCallback(
                      (u, t, integrator) -> 1.0, integrated),
                  dt=0.1)

and with IntegratingSumCallback:

      prob = ODEProblem((u,p,t)->[1.0], [0.0], (0.0,1.0), ())
      integrated = IntegrandValuesSum(0.)
      sol = solve(prob, Euler(),
                  callback = IntegratingSumCallback(
                      (u, t, integrator) -> 1.0, integrated),
                  dt=0.1)

Both produce errors complain wrong Fix1 usage. Interestingly if I provide nonempty scalar parameter to ODEProblem I get StackOverflow. And if the parameter is Vector then error is again about Fix1 usage. I'm not sure how parameters are used and why they affect calculation this way while I'm not using parameters specifically in ODE or integral calculation.

Another question is about integrating function implementation. Brief look at the source reveals there is some Gauss quadrature used there. So I expect it breaks the time interval into specifically spaced points and calculates the quadrature accordingly multiplying by weights. The questions arises when I'm using Callback to terminate simulation on steady state condition. It looks like that will prevent precise calculation of the quadrature because time interval will be changed and it is unknown in advance.

Thank you.

ChrisRackauckas commented 7 months ago

There were some issues when it got generalized. I fixed those and added your example.