SciML / DifferentialEquations.jl

Multi-language suite for high-performance solvers of differential equations and scientific machine learning (SciML) components. Ordinary differential equations (ODEs), stochastic differential equations (SDEs), delay differential equations (DDEs), differential-algebraic equations (DAEs), and more in Julia.
https://docs.sciml.ai/DiffEqDocs/stable/
Other
2.86k stars 228 forks source link

Documentation: NoiseProcess example wrong signatures for dist! & bridge! #825

Open vitusbenson opened 2 years ago

vitusbenson commented 2 years ago

Edited. I am trying to reproduce this example from the Documentation with the 3D Lorenz example: https://diffeq.sciml.ai/stable/features/noise_process/#Direct-Construction-of-a-NoiseProcess

using DifferentialEquations, Plots, Random
function INPLACE_WHITE_NOISE_DIST(rand_vec,W,dt,rng)#(rand_vec,W,dt,u,p,c,rng)
  randn!(rng,rand_vec)
  sqrtabsdt = @fastmath sqrt(abs(dt))
  @. rand_vec *= sqrtabsdt
end
function INPLACE_WHITE_NOISE_BRIDGE(rand_vec,W,W0,Wh,q,h,rng)#(rand_vec,W,W0,Wh,q,h,u,p,c,rng)
  randn!(rng,rand_vec)
  #rand_vec .= sqrt((1.-q).*q.*abs(h)).*rand_vec.+q.*Wh
  sqrtcoeff = @fastmath sqrt((1-q)*q*abs(h))
  @. rand_vec = sqrtcoeff*rand_vec+q*Wh
end
OurWienerProcess!(t0,W0,Z0=nothing) = NoiseProcess(t0,W0,Z0,INPLACE_WHITE_NOISE_DIST,INPLACE_WHITE_NOISE_BRIDGE)
W = OurWienerProcess!(0.0, [0.0, 0.0, 0.0])

function lorenz(du,u,p,t)
  du[1] = 10.0(u[2]-u[1])
  du[2] = u[1]*(28.0-u[3]) - u[2]
  du[3] = u[1]*u[2] - (8/3)*u[3]
end

function σ_lorenz(du,u,p,t)
  du[1] = 3.0
  du[2] = 3.0
  du[3] = 3.0
end

prob_sde_lorenz = SDEProblem(lorenz,σ_lorenz,[1.0,0.0,0.0],(0.0,10.0), noise = W)
sol = solve(prob_sde_lorenz)
plot(sol,vars=(1,2,3))

This gives me MethodErrors:

  1. MethodError: no method matching INPLACE_WHITE_NOISE_DIST(::Vector{Float64}, ::NoiseProcess{Float64, 2, Float64, Vector{Float64}, Nothing, Nothing, typeof(INPLACE_WHITE_NOISE_DIST), typeof(INPLACE_WHITE_NOISE_BRIDGE), false, ResettableStacks.ResettableStack{Tuple{Float64, Vector{Float64}, Nothing}, false}, ResettableStacks.ResettableStack{Tuple{Float64, Vector{Float64}, Nothing}, false}, RSWM{Float64}, Nothing, RandomNumbers.Xorshifts.Xoroshiro128Plus}, ::Float64, ::Vector{Float64}, ::SciMLBase.NullParameters, ::Float64, ::RandomNumbers.Xorshifts.Xoroshiro128Plus), which is because in https://github.com/SciML/DiffEqNoiseProcess.jl/blob/b2467a0639a13e15938b80583e3fd830e10dfbd5/src/noise_interfaces/noise_process_interface.jl#L145 and onwards, a different signature for the dist! function is used than described in https://github.com/SciML/DiffEqDocs.jl/blob/e432ad33c723776868bf8a39bcafcd3e3b893a60/docs/src/features/noise_process.md?plain=1#L244 . This can be fixed by defining two identical methods for dist!, one with 3 extra arguments before the rng.
  2. MethodError: no method matching INPLACE_WHITE_NOISE_BRIDGE(::Vector{Float64}, ::NoiseProcess{Float64, 2, Float64, Vector{Float64}, Nothing, Nothing, typeof(INPLACE_WHITE_NOISE_DIST), typeof(INPLACE_WHITE_NOISE_BRIDGE), true, ResettableStacks.ResettableStack{Tuple{Float64, Vector{Float64}, Nothing}, true}, ResettableStacks.ResettableStack{Tuple{Float64, Vector{Float64}, Nothing}, true}, RSWM{Float64}, Nothing, RandomNumbers.Xorshifts.Xoroshiro128Plus}, ::Int64, ::Vector{Float64}, ::Float64, ::Float64, ::Vector{Float64}, ::SciMLBase.NullParameters, ::Float64, ::RandomNumbers.Xorshifts.Xoroshiro128Plus), the same as before, e.g. here https://github.com/SciML/DiffEqNoiseProcess.jl/blob/b2467a0639a13e15938b80583e3fd830e10dfbd5/src/noise_interfaces/noise_process_interface.jl#L278 signature is different than given in documentation here https://github.com/SciML/DiffEqDocs.jl/blob/e432ad33c723776868bf8a39bcafcd3e3b893a60/docs/src/features/noise_process.md?plain=1#L256. Again can be fixed by giving bridge! 3 extra arguments before the rng.

Help is much appreciated! :-)

Versions: julia v1.6.3 DifferentialEquations.jl v6.20.0

ChrisRackauckas commented 2 years ago

Don't use an in-place noise process with scalars. You need to use an out-of-place noise process with immutable u0 objects.

vitusbenson commented 2 years ago

Don't use an in-place noise process with scalars. You need to use an out-of-place noise process with immutable u0 objects.

Thank you, this solved the fill! issues, still another error persists, which might just be a missmatch between the documentation and implementation, I've updated the issue.

ChrisRackauckas commented 2 years ago

I still see an in-place one. See how WienerProcess is implemented, the non ! one.

https://github.com/SciML/DiffEqNoiseProcess.jl/blob/v5.9.0/src/wiener.jl#L1-L49

ChrisRackauckas commented 2 years ago

Oh yes, the signatures look like they are missing u,p,t, things.