SciML / StochasticDiffEq.jl

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

DimensionError with unitful `dt` for stochastic integrator #529

Open jebej opened 1 year ago

jebej commented 1 year ago

Currently the type for the sqdt parameter of SDEIntegrator is tType, which is the same as the type for dt. https://github.com/SciML/StochasticDiffEq.jl/blob/0cd915e95485a62e31368c90201f79d0606eebcf/src/integrators/type.jl#L33

This line thus fails with a DimensionError if dt is eg in ns during __init, since the calculated unit for sqdt will be sqrt(ns): https://github.com/SciML/StochasticDiffEq.jl/blob/0cd915e95485a62e31368c90201f79d0606eebcf/src/solve.jl#L596

ChrisRackauckas commented 1 year ago

The random values should have a sqrt(dt) factor for this to work.

jebej commented 1 year ago

I'm sorry, I can't see where the random values affect this issue. Is it not simply a question of changing the type parameter?

jebej commented 1 year ago

FWIW this is the example:

using Unitful, OrdinaryDiffEq, StochasticDiffEq
using Unitful: uconvert, GHz, J, ns
ℎ = Unitful.h # Plank constant

jj2!(ϕ′,ϕ,(M,η,Ej,i),t) = (Ej*i - Ej*sin(ϕ))/M # BAOAB version
identity_f(v,u,p,t) = v # needed to form second order dynamical ODE
g(u,p,t) = 1.0
setup_noisy_jj(M,η,Ej,i,ϕ_init=0.0,ϕ′_init=0.0/ns,tspan=(0ns,1ns)) =
    DynamicalSDEProblem{false}(jj2!,identity_f,g,ϕ′_init,ϕ_init,tspan,(M,η,Ej,i))

# underdamped
M = 4.2E-28*J*ns^2
η = 1.1E-26*J*ns
Ej = ℎ * 50GHz
i = 0.0 # unitless

prob = setup_noisy_jj(M,η,Ej,i,0.5)
res = solve(prob,BAOAB(η/M, false),dt=0.001ns)
ChrisRackauckas commented 1 year ago

without the randomness having the appropriate sqrt(t) units, you will later just get a type error that sqrt(t) + t units are combined. We can definitely expand that type signature but I don't think it's enough to get around that.