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.85k stars 226 forks source link

PeriodicCallback needs to match types #313

Closed ChrisRackauckas closed 5 years ago

ChrisRackauckas commented 6 years ago
using DifferentialEquations
using Plots
gr()

g = @ode_def LorenzExample begin
  dx = σ*(y-x)
  dy = x*(ρ-z) - y
  dz = x*y - β*z
end σ ρ β

u0 = [1.0,0.0,0.0]
tspan = (0.0,10.0)
p = [10.0,28.0,8/3]
prob = ODEProblem(g,u0,tspan,p)

function periodic(integrator)
    integrator.u[2] += 10
end

cb = PeriodicCallback(periodic, 2::Number)

sol = solve(prob, callback = cb)
plot(sol)

fails, but

cb = PeriodicCallback(periodic, 2.0)

sol = solve(prob, Tsit5(), callback = cb)
plot(sol)

works. It should know to upconvert but it fails in the push! for the binary heap.

> sol = solve(prob, Tsit5(), callback = cb)
ERROR: MethodError: no method matching push!(::DataStructures.BinaryHeap{Float64,DataStructures.LessThan}, ::Int64)
Closest candidates are:
  push!(::Any, ::Any, ::Any) at abstractarray.jl:1940
  push!(::Any, ::Any, ::Any, ::Any...) at abstractarray.jl:1941
  push!(::Array{Any,1}, ::ANY) at array.jl:653
  ...
Stacktrace:
 [1] add_tstop!(::OrdinaryDiffEq.ODEIntegrator{OrdinaryDiffEq.Tsit5,Array{Float64,1},Float64,Array{Float64,1},Float64,Float64,Float64,Array{Array{Float64,1},1},DiffEqBase.ODESolution{Float64,2,Array{Array{Float64,1},1},Void,Void,Array{Float64,1},Array{Array{Array{Float64,1},1},1},DiffEqBase.ODEProblem{Array{Float64,1},Float64,true,Array{Float64,1},LorenzExample,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem},OrdinaryDiffEq.Tsit5,OrdinaryDiffEq.InterpolationData{LorenzExample,Array{Array{Float64,1},1},Array{Float64,1},Array{Array{Array{Float64,1},1},1},OrdinaryDiffEq.Tsit5Cache{Array{Float64,1},Array{Float64,1},Array{Float64,1},Array{Float64,1},OrdinaryDiffEq.Tsit5ConstantCache{Float64,Float64}}}},LorenzExample,Void,OrdinaryDiffEq.Tsit5Cache{Array{Float64,1},Array{Float64,1},Array{Float64,1},Array{Float64,1},OrdinaryDiffEq.Tsit5ConstantCache{Float64,Float64}},OrdinaryDiffEq.DEOptions{Float64,Float64,Float64,Float64,DiffEqBase.#ODE_DEFAULT_NORM,DiffEqBase.CallbackSet{Tuple{},Tuple{DiffEqBase.DiscreteCallback{DiffEqCallbacks.##40#43{Base.RefValue{Int64}},DiffEqCallbacks.##41#44{#periodic,Int64,Base.RefValue{Int64}},DiffEqCallbacks.##42#45{DiffEqBase.#INITIALIZE_DEFAULT,Bool,Int64,Base.RefValue{Int64},DiffEqCallbacks.##41#44{#periodic,Int64,Base.RefValue{Int64}}}}}},DiffEqBase.#ODE_DEFAULT_ISOUTOFDOMAIN,DiffEqBase.#ODE_DEFAULT_PROG_MESSAGE,DiffEqBase.#ODE_DEFAULT_UNSTABLE_CHECK,DataStructures.BinaryHeap{Float64,DataStructures.LessThan},DataStructures.BinaryHeap{Float64,DataStructures.LessThan},Void,Void,Int64,Array{Float64,1},Array{Float64,1},Array{Float64,1}},Array{Float64,1}}, ::Int64) at C:\Users\Chris\.julia\v0.6\OrdinaryDiffEq\src\integrators\integrator_interface.jl:73
bhgomes commented 5 years ago

This code seems to work now:

retcode: Success
Interpolation: Automatic order switching interpolation
t: 147-element Array{Float64,1}:
  0.0
  0.0001413379452045338
  0.0015547173972498716
  0.006140501838384454
  0.01312258935349105
  0.02224468968914082
  0.03472725114873801
  0.05089052454305308
  0.07178363101153133
  0.09825023816788206
  0.13123968667077915
  0.16804720410962184
  0.2084044242598322
  0.252091059104267
  0.29872880414275005
  0.3476870327614323
  0.3985071904736671
  0.4425751031410838
  0.49193271681727374
  0.5351884199271842
  0.5913071673808932
  0.6453523277102994
  ⋮
  8.663519363450483
  8.722553349525109
  8.788042365949227
  8.858898757577474
  8.914440272401379
  8.982564484075816
  9.051425436411101
  9.134066363302088
  9.219912972499623
  9.29490651373013
  9.365235296935996
  9.430181223043789
  9.493750574858643
  9.55783942891183
  9.631828805730093
  9.688309742885659
  9.743066591310896
  9.805906000087049
  9.873577091867496
  9.951356740189112
 10.0
u: 147-element Array{Array{Float64,1},1}:
 [1.0, 0.0, 0.0]
 [0.99859, 0.00395439, 2.79227e-7]
 [0.984908, 0.0431666, 3.32627e-5]
 [0.945504, 0.166537, 0.000494582]
 [0.89911, 0.344792, 0.00211665]
 [0.860606, 0.564835, 0.00566877]
 [0.843224, 0.852529, 0.012879]
 [0.872816, 1.22176, 0.0263646]
 [0.987347, 1.73037, 0.0527011]
 [1.24939, 2.49315, 0.109098]
 [1.77668, 3.76144, 0.2482]
 [2.70396, 5.82719, 0.598475]
 [4.31156, 9.27733, 1.54279]
 [7.07643, 14.9173, 4.19956]
 [11.5537, 22.7797, 11.4717]
 [17.2176, 27.662, 27.4712]
 [19.7437, 17.2906, 45.5225]
 [15.6973, 1.14909, 47.3689]
 [7.7458, -7.94379, 39.3889]
 [1.87752, -9.33182, 33.4495]
 [-2.83158, -8.78984, 29.1594]
 [-5.20299, -8.35381, 27.0314]
 ⋮
 [8.42847, -0.788229, 35.8673]
 [3.76559, -2.52214, 30.0451]
 [0.765664, -2.35582, 24.9216]
 [-0.769193, -2.27021, 20.6476]
 [-1.49106, -2.69882, 17.9522]
 [-2.40216, -3.98612, 15.3726]
 [-3.82269, -6.48686, 13.8081]
 [-6.89207, -11.8008, 14.6934]
 [-11.9725, -18.1861, 23.0139]
 [-14.937, -15.1739, 35.5159]
 [-12.0998, -4.68167, 38.356]
 [-6.93192, 0.519247, 33.2148]
 [-3.13286, 1.30062, 27.7353]
 [-1.11966, 0.943413, 23.2424]
 [-0.148541, 0.627735, 19.0517]
 [0.176523, 0.614947, 16.3889]
 [0.393204, 0.780182, 14.1726]
 [0.673968, 1.21556, 12.0166]
 [1.1678, 2.14033, 10.1267]
 [2.28518, 4.32263, 8.61306]
 [3.54816, 6.79329, 8.29889]
Screen Shot 2019-06-26 at 09 39 52

with the following environment:

]status
(v1.1) pkg> status
    Status `~/.julia/environments/v1.1/Project.toml`
  [c3fe647b] AbstractAlgebra v0.5.0
  [621f4979] AbstractFFTs v0.4.1
  [9e5c662f] AbstractInstances v0.1.0
  [0bf59076] AdvancedHMC v0.1.8
  [c7e460c6] ArgParse v0.6.2
  [65a8f2f4] ArraysOfArrays v0.3.0
  [c52e3926] Atom v0.8.5
  [6710c13c] AutoGrad v1.1.4
  [fbb218c0] BSON v0.2.3
  [ecbce9bc] BenchmarkProfiles v0.2.0
  [6e4b80f9] BenchmarkTools v0.4.2
  [76274a88] Bijectors v0.3.0
  [ad839575] Blink v0.10.1
  [8e7c35d0] BlockArrays v0.9.1
  [764a87c0] BoundaryValueDiffEq v2.2.3
  [3391f64e] CDDLib v0.5.2
  [aaaa29a8] Clustering v0.13.1
  [593b3428] CmdStan v5.1.0
  [861a8166] Combinatorics v0.7.0
  [ed09eef8] ComputationalResources v0.3.0
  [a6e380b2] ControlSystems v0.5.3
  [150eb455] CoordinateTransformations v0.5.0
  [8a292aeb] Cuba v2.0.0
  [667455a9] Cubature v1.3.1
  [d58978e5] Dagger v0.8.0
  [124859b0] DataDeps v0.6.2
  [a93c6f00] DataFrames v0.18.3
  [864edb3b] DataStructures v0.15.0
  [ebbdde9d] DiffEqBayes v1.1.0
  [0c46a032] DifferentialEquations v6.4.0
  [22fd7b30] DirectSum v0.2.4
  [b4f34e82] Distances v0.8.0
  [aaf54ef3] DistributedArrays v0.6.2
  [31c24e10] Distributions v0.20.0
  [968ba79b] DocOpt v0.4.0
  [e30172f5] Documenter v0.22.4
  [fa6b7ba4] DualNumbers v0.6.2
  [bbc10e6e] DynamicHMC v1.0.5
  [fdbdab4c] ElasticArrays v0.4.0
  [01fcc997] FTPClient v1.0.1
  [5789e2e9] FileIO v1.0.7
  [8fc22ac5] FilePaths v0.7.0
  [1a297f60] FillArrays v0.6.3
  [587475ba] Flux v0.8.3
  [9dda63f9] ForceImport v0.0.3
  [f6369f11] ForwardDiff v0.10.3
  [c91e804a] Gadfly v1.0.1
  [bc5e4493] GitHub v5.1.1
  [aa1b3936] GraphIO v0.4.0
  [a2cc645c] GraphPlot v0.3.1
  [4df31cd9] Grassmann v0.1.7
  [19dc6840] HCubature v1.4.0
  [3e1990a7] Hecke v0.6.2
  [50ceba7f] HyperDualNumbers v4.0.0
  [7073ff75] IJulia v1.18.1
  [916415d5] Images v0.17.3
  [2a4eff87] Immerse v0.1.1
  [4858937d] InfiniteArrays v0.1.1
  [c601a237] Interact v0.10.2
  [8197267c] IntervalSets v0.3.1
  [d8418881] Intervals v0.5.1
  [4138dd39] JLD v0.9.1
  [033835bb] JLD2 v0.1.2
  [682c06a0] JSON v0.20.0
  [4076af6c] JuMP v0.19.2
  [a93385a2] JuliaDB v0.12.0
  [2c06ca41] JuliaDBMeta v0.4.2
  [e5e0dc1b] Juno v0.7.0
  [5888135b] KeywordDispatch v0.3.0
  [1902f260] Knet v1.2.2
  [23fbe1c1] Latexify v0.8.2
  [50d2b5c4] Lazy v0.13.2
  [5078a376] LazyArrays v0.9.0
  [b4f0291d] LazySets v1.13.0
  [b27032c2] LibCURL v0.5.1
  [093fc24a] LightGraphs v1.2.0
  [89bd72ed] LightGraphsExtras v0.3.0
  [2f5eb75a] LightGraphsFlows v0.3.0
  [6fdf6af0] LogDensityProblems v0.8.2
  [c7f686f2] MCMCChains v0.3.10
  [add582a8] MLJ v0.2.5
  [d491faf4] MLJModels v0.2.5
  [1914dd2f] MacroTools v0.5.0
  [ee78f7c6] Makie v0.9.4
  [5424a776] Mamba v0.12.1
  [b51810bb] MatrixDepot v0.7.0
  [eff96d63] Measurements v2.0.0
  [283c5d60] MeshCat v0.6.0
  [e6723b4c] Meshing v0.4.1
  [626554b9] MetaGraphs v0.6.1
  [dbeba491] Metalhead v0.3.0
  [e1d29d7a] Missings v0.4.1
  [961ee093] ModelingToolkit v0.2.0
  [0987c9cc] MonteCarloMeasurements v0.3.4
  [872c559c] NNlib v0.6.0
  [dd2c4c9e] NearestNeighborDescent v0.2.0
  [b8a86587] NearestNeighbors v0.4.3
  [2edaba10] Nemo v0.14.0
  [46757867] NetworkLayout v0.2.0
  [d0dd6a25] ONNX v0.1.1
  [6fe1bfb0] OffsetArrays v0.11.1
  [5fb14364] OhMyREPL v0.5.1
  [a15396b6] OnlineStats v0.23.0
  [1dea7af3] OrdinaryDiffEq v5.8.1
  [3b7a836e] PGFPlots v3.1.1
  [8314cec4] PGFPlotsX v1.0.0
  [65888b18] ParameterizedFunctions v4.1.1
  [ccf2f8ad] PlotThemes v0.3.0
  [91a5bcdd] Plots v0.25.1
  [67491407] Polyhedra v0.5.1
  [3a141323] PolynomialRoots v0.2.0
  [c46f51b8] ProfileView v0.4.1
  [92933f4c] ProgressMeter v0.9.0
  [438e738f] PyCall v1.91.2
  [d330b81b] PyPlot v2.8.1
  [a8468747] QHull v0.1.0
  [94ee1d12] Quaternions v0.4.0
  [ce6b1742] RDatasets v0.6.2
  [0d4725de] Readables v0.3.3
  [93e0c654] Reduce v1.1.1
  [bfcbc1eb] ReduceLinAlg v0.1.0
  [189a3867] Reexport v0.2.0
  [ae029012] Requires v0.5.2
  [6038ab10] Rotations v0.11.1
  [b3480ff6] ShapesOfVariables v0.2.0
  [55797a34] SimpleGraphs v0.3.0
  [47aef6b3] SimpleWeightedGraphs v1.1.0
  [276daf66] SpecialFunctions v0.7.2
  [90137ffa] StaticArrays v0.11.0
  [4c8beaf5] StaticGraphs v0.1.0
  [2913bbd2] StatsBase v0.30.0
  [4c63d2b9] StatsFuns v0.8.0
  [f3b207a7] StatsPlots v0.11.0
  [09ab397b] StructArrays v0.3.4
  [123dc426] SymEngine v0.5.0
  [24249f21] SymPy v1.0.3
  [a906b1d5] Symata v0.4.5
  [bd369af6] Tables v0.2.6
  [6aa5eb33] TaylorSeries v0.10.0
  [48a634ad] Tensors v1.1.0
  [a759f4b9] TimerOutputs v0.5.0
  [fce5fe82] Turing v0.6.17
  [9d95f2ec] TypedTables v1.2.0
  [dcd9ba68] UncertainData v0.1.8
  [b8865327] UnicodePlots v1.1.0
  [1986cc42] Unitful v0.15.0
  [6fb2a4bd] UnitfulAngles v0.5.0
  [98cad3c8] ValueHistories v0.5.1
  [72f80fcb] VoronoiDelaunay v0.3.1
  [5872b779] Yao v0.5.0
  [e88e6eb3] Zygote v0.3.2
  [8ba89e20] Distributed
  [37e2e46d] LinearAlgebra
julia> versioninfo()
Julia Version 1.1.1
Platform Info:
  OS: macOS (x86_64-apple-darwin13.4.0)
  CPU: Intel(R) Core(TM) i7-3540M CPU @ 3.00GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, ivybridge)
ChrisRackauckas commented 5 years ago

Fix just forgot to close the issue. Thanks!