SciML / DelayDiffEq.jl

Delay differential equation (DDE) solvers in Julia for the SciML scientific machine learning ecosystem. Covers neutral and retarded delay differential equations, and differential-algebraic equations.
Other
58 stars 26 forks source link

DDE does not provide any solution (example from docs) #266

Closed pclus closed 1 year ago

pclus commented 1 year ago

Hi all, this is a question I posted some weeks ago in Julia Discourse (see here).

Basically, I am trying to reproduce the basic example from the DifferentialEquations documentation, and it does not work. I wonder if this might be due to some package conflicts or compiler errors?

Here goes the original issue:

I am trying to use DifferentialEquations.jl to solve a delay differential equation. However, I fail to reproduce the example provided in the documentation. I copy here the code of the example:

using DifferentialEquations
function bc_model(du,u,h,p,t)
  p0,q0,v0,d0,p1,q1,v1,d1,d2,beta0,beta1,tau = p
  hist3 = h(p, t-tau)[3]
  du[1] = (v0/(1+beta0*(hist3^2))) * (p0 - q0)*u[1] - d0*u[1]
  du[2] = (v0/(1+beta0*(hist3^2))) * (1 - p0 + q0)*u[1] +
          (v1/(1+beta1*(hist3^2))) * (p1 - q1)*u[2] - d1*u[2]
  du[3] = (v1/(1+beta1*(hist3^2))) * (1 - p1 + q1)*u[2] - d2*u[3]
end

h(p, t) = ones(3)
tau = 1
lags = [tau]
p0 = 0.2; q0 = 0.3; v0 = 1; d0 = 5
p1 = 0.2; q1 = 0.3; v1 = 1; d1 = 1
d2 = 1; beta0 = 1; beta1 = 1
p = (p0,q0,v0,d0,p1,q1,v1,d1,d2,beta0,beta1,tau)
tspan = (0.0,10.0)
u0 = [1.0,1.0,1.0]

prob = DDEProblem(bc_model,u0,h,tspan,p; constant_lags=lags)
alg = MethodOfSteps(Tsit5())
sol = solve(prob,alg)

This is supposed to give the solution integrated over tspan, but instead I get:

retcode: Default
Interpolation: specialized 4th order "free" interpolation
t: 1-element Vector{Float64}:
 0.0
u: 1-element Vector{Vector{Float64}}:
 [1.0, 1.0, 1.0]

I do not know how to solve the issue, it looks system specific, since the code is the same from the example. I tried to update the DifferentialEquations package with pkg> update DifferentialEquations, but I got the same result.

Any help will be appreciated, thanks!

devmotion commented 1 year ago

Unfortunately, I can't reproduce your issue, the example works fine on my computer (tested in a new temporary environment). I assume there might be a problem with your package environment and the versions that you use. Did you try just installing DelayDiffEq (DifferentialEquations is not needed for the example) in a new, empty environment?

ChrisRackauckas commented 1 year ago

Share ]st

pclus commented 1 year ago

Answering to @devmotion: I tried with an empty environtment with DelayDiffEq and it works, also I try with DifferentialEquations. So you're right, there might be another package causing the problem.

Answer to @ChrisRackauckas:

(@v1.8) pkg> st
Status `~/.julia/environments/v1.8/Project.toml`
⌃ [0f109fa4] BifurcationKit v0.2.5
  [35d6a980] ColorSchemes v3.20.0
  [d38c429a] Contour v0.6.2
  [717857b8] DSP v0.7.8
⌃ [bcd4f6db] DelayDiffEq v5.37.0
⌃ [0c46a032] DifferentialEquations v7.1.0
  [7a1cc6ca] FFTW v1.6.0
  [f6369f11] ForwardDiff v0.10.35
⌃ [0337cf30] GRUtils v0.8.2
  [4b11ee91] Gaston v1.1.0
  [dc211083] Gnuplot v1.4.1
  [09f84164] HypothesisTests v0.10.11
  [b964fa9f] LaTeXStrings v1.3.0
  [a81958ac] Multitaper v0.2.0
  [e7bfaba1] NumericalIntegration v0.3.3
  [8314cec4] PGFPlotsX v1.5.3
  [d96e819e] Parameters v0.12.3
  [18e31ff7] Peaks v0.4.3
  [995b91a9] PlotUtils v1.3.4
  [f0f68f2c] PlotlyJS v0.18.10
⌃ [91a5bcdd] Plots v1.38.9
  [295af30f] Revise v3.5.2
⌃ [f2b01f46] Roots v2.0.10
⌅ [efcf1570] Setfield v0.8.2
⌅ [276daf66] SpecialFunctions v0.10.3
  [37f6aa50] TikzPictures v3.4.2
  [3a884ed6] UnPack v1.0.2
Info Packages marked with ⌃ and ⌅ have new versions available, but those with ⌅ are restricted by compatibility constraints from upgrading. To see why use `status --outdated`
pclus commented 1 year ago

I found the incompatibility, it is the Multitaper package. I do not know why nor how to fix it, but I can probably work around this since I do not need both packages in the same project.

However, would be nice to have an explanation or solution, to make sure does not happen with other packages as well...

Hopefully you can reproduce the error by following this:

From Julia in a new empty folder I create an empty environment:

empty!(LOAD_PATH)
push!(LOAD_PATH, "@")
] activate .

Then install the two conflicting packages:

] add DelayDiffEq
] add Multitaper

and repeat the example from the docs:

using DelayDiffEq
function bc_model(du,u,h,p,t)
  p0,q0,v0,d0,p1,q1,v1,d1,d2,beta0,beta1,tau = p
  hist3 = h(p, t-tau)[3]
  du[1] = (v0/(1+beta0*(hist3^2))) * (p0 - q0)*u[1] - d0*u[1]
  du[2] = (v0/(1+beta0*(hist3^2))) * (1 - p0 + q0)*u[1] +
          (v1/(1+beta1*(hist3^2))) * (p1 - q1)*u[2] - d1*u[2]
  du[3] = (v1/(1+beta1*(hist3^2))) * (1 - p1 + q1)*u[2] - d2*u[3]
end

h(p, t) = ones(3)
tau = 1
lags = [tau] 
p0 = 0.2; q0 = 0.3; v0 = 1; d0 = 5
p1 = 0.2; q1 = 0.3; v1 = 1; d1 = 1
d2 = 1; beta0 = 1; beta1 = 1
p = (p0,q0,v0,d0,p1,q1,v1,d1,d2,beta0,beta1,tau)
tspan = (0.0,10.0)
u0 = [1.0,1.0,1.0]

prob = DDEProblem(bc_model,u0,h,tspan,p; constant_lags=lags)
alg = MethodOfSteps(Tsit5())
sol = solve(prob,alg)

If Multitaper is not added I get the expected solution.

ChrisRackauckas commented 1 year ago

https://github.com/lootie/Multitaper.jl/pull/10 is likely the cause. Everything uses Special functions v2 since forever.

pclus commented 1 year ago

Thanks, with the latest version of Multitaper I don't have the problem anymore.