SciML / StochasticDiffEq.jl

Solvers for stochastic differential equations which connect with the scientific machine learning (SciML) ecosystem
Other
245 stars 66 forks source link

import AbstractController from OrdinaryDiffEq.jl #522

Open frankschae opened 1 year ago

frankschae commented 1 year ago

If Controllers should subtype StochasticDiffEq.AbstractController this will allow us to implement custom adaptive schemes easily.

struct CustomController <: StochasticDiffEq.AbstractController
end

function StochasticDiffEq.stepsize_controller!(integrator::StochasticDiffEq.SDEIntegrator, controller::CustomController, alg)
    u = integrator.u
    integrator.q = max(...)
    nothing
end

function StochasticDiffEq.step_accept_controller!(integrator::StochasticDiffEq.SDEIntegrator, controller::CustomController, alg)
    integrator.dtnew = 1 / integrator.q * oneunit(integrator.dt)
    nothing
end

sol = solve(prob, EM(), dt=dt, adaptive=true, controller=CustomController())
ChrisRackauckas commented 1 year ago

Maybe this should be implemented in OrdinaryDiffEq and then just used with any solver?

frankschae commented 1 year ago

Maybe this should be implemented in OrdinaryDiffEq and then just used with any solver?

Can you elaborate? I thought that it's fine to let a user define their own controller type as a subtype of AbstractController and then dispatch on it to change the adaptive scheme. AbstractController is defined in OrdinaryDiffEq and only imported here. I don't know how to implement a generically valid integrator.q = max(...).

ChrisRackauckas commented 1 year ago

I mean, why not implement CustomController in OrdinaryDiffEq and then document it as a feature for ODEs, SDEs, DDEs, DAEs, and SDDEs?

frankschae commented 1 year ago

What would that look like? Would we only add

struct CustomController <: StochasticDiffEq.AbstractController
end

to OrdinaryDiffEq? Wouldn't that be worse than leaving the type "free"? I have in mind that users might want to do something sophisticated and it would be good, e.g., if CustomController could hold additional fields (but adding it to the docs would be great of course).

ChrisRackauckas commented 1 year ago

Yeah, I think some documented type where a user gives some functions like that.

frankschae commented 1 year ago

Isn't AbstractController basically already that type (so you can subtype it with a concrete type with its own fields and do all the fancy things you want to do)?

ChrisRackauckas commented 1 year ago

Documenting it might be all that's necessary.

frankschae commented 1 year ago

Maybe on this page https://docs.sciml.ai/DiffEqDocs/stable/extras/timestepping/?

ChrisRackauckas commented 1 year ago

That seems right.