-
-
Notifications
You must be signed in to change notification settings - Fork 231
Closed
Description
Hello,
If I try to modelingtoolkitize
a DifferentialEquations.jl SDE that takes a parameters array p
that contains both Float64
s and distributions, I get an error.
So I reproduced the behaviour in the following MWE:
using DifferentialEquations, Distributions
# Model parameters
β = 0.01# infection rate
λ_R = 0.05 # inverse of transition time from infected to recovered
λ_D = 0.83 # inverse of transition time from infected to dead
σ_β = 0.01
σ_R = 0.01
σ_D = 0.01
𝒫 = [β, λ_R, λ_D, Normal(), Normal(), Normal()]
# regional contact matrix and regional population
## regional contact matrix
const C = [3.45536 0.485314 0.506389 0.123002 ; 0.597721 2.11738 0.911374 0.323385 ; 0.906231 1.35041 1.60756 0.67411 ; 0.237902 0.432631 0.726488 0.979258] # 4x4 contact matrix
## regional population stratified by age
N= [723208 , 874150, 1330993, 1411928] # array of 4 elements, each of which representing the absolute amount of population in the corresponding age class.
# Initial conditions
i₀ = 0.075 # fraction of initial infected people in every age class
I₀ = repeat([i₀],4)
S₀ = N.-I₀
R₀ = [0.0 for n in 1:length(N)]
D₀ = [0.0 for n in 1:length(N)]
D_tot₀ = [0.0]
ℬ = vcat([S₀, I₀, R₀, D₀, D_tot₀]...)
# Time
final_time = 20
𝒯 = (1.0,final_time);
# initialize this parameter (death probability stratified by age, taken from literature)
#const δ₁, δ₂, δ₃, δ₄ = [0.003/100, 0.004/100, (0.015+0.030+0.064+0.213+0.718)/(5*100), (2.384+8.466+12.497+1.117)/(4*100)]
const δ = vcat(repeat([0.003/100],1),repeat([0.004/100],1),repeat([(0.015+0.030+0.064+0.213+0.718)/(5*100)],1),repeat([(2.384+8.466+12.497+1.117)/(4*100)],4-1-1-1))
function SIRD!(du,u,p,t)
# extract parameters
β, λ_R, λ_D, _,_,_= p
# State variables
S = @view u[4*0+1:4*1]
I = @view u[4*1+1:4*2]
R = @view u[4*2+1:4*3]
D = @view u[4*3+1:4*4]
D_tot = @view u[4*4+1]
# Differentials
dS = @view du[4*0+1:4*1]
dI = @view du[4*1+1:4*2]
dR = @view du[4*2+1:4*3]
dD = @view du[4*3+1:4*4]
dD_tot = @view du[4*4+1]
# Force of infection
Λ = β*[sum([C[i,j]*I[j]/N[j] for j in 1:size(C)[1]]) for i in 1:size(C)[2]]
# System of equations
@. dS = -Λ*S
@. dI = Λ*S - ((1-δ)*λ_R + δ*λ_D)*I
@. dR = λ_R*(1-δ)*I
@. dD = λ_D*δ*I
@. dD_tot = dD[1]+dD[2]+dD[3]+dD[4]
end;
# define noise
function SIRD_noise!(du,u,p,t)
# extract the distributions
posteriors = p[(length(p) ÷ 2)+1:end]
# State variables
S = @view u[4*0+1:4*1]
I = @view u[4*1+1:4*2]
R = @view u[4*2+1:4*3]
D = @view u[4*3+1:4*4]
D_tot = @view u[4*4+1]
# Differentials
dS = @view du[4*0+1:4*1]
dI = @view du[4*1+1:4*2]
dR = @view du[4*2+1:4*3]
dD = @view du[4*3+1:4*4]
dD_tot = @view du[4*4+1]
n_β, n_R, n_D = [rand(posterior) for posterior in posteriors]
# Force of infection
Λ = n_β*[sum([C[i,j]*I[j]/N[j] for j in 1:size(C)[1]]) for i in 1:size(C)[2]]
# System of equations
@. dS = -Λ*S
@. dI = Λ*S - ((1-δ)*n_R + δ*n_D)*I
@. dR = n_R*(1-δ)*I
@. dD = n_D*δ*I
@. dD_tot = dD[1]+dD[2]+dD[3]+dD[4]
end;
# create problem and check it works
sde_problem = SDEProblem(SIRD!,SIRD_noise!,ℬ, 𝒯, 𝒫 )
solution = @time solve(sde_problem, SImplicitMidpoint(), saveat = 1:final_time);
1.601126 seconds (4.81 M allocations: 216.508 MiB, 2.78% gc time)
If I try to modelingtoolkitize
, I can obtain the sys
:
sys = modelingtoolkitize(sde_problem)
SDESystem(Equation[Equation(Differential(x₁(t)), (-(α₁ * (((((3.45536 * x₅(t)) / 723208) + ((0.485314 * x₆(t)) / 874150)) + ((0.506389 * x₇(t)) / 1330993)) + ((0.123002 * x₈(t)) / 1411928)))) * x₁(t)), Equation(Differential(x₂(t)), (-(α₁ * (((((0.597721 * x₅(t)) / 723208) + ((2.11738 * x₆(t)) / 874150)) + ((0.911374 * x₇(t)) / 1330993)) + ((0.323385 * x₈(t)) / 1411928)))) * x₂(t)), Equation(Differential(x₃(t)), (-(α₁ * (((((0.906231 * x₅(t)) / 723208) + ((1.35041 * x₆(t)) / 874150)) + ((1.60756 * x₇(t)) / 1330993)) + ((0.67411 * x₈(t)) / 1411928)))) * x₃(t)), Equation(Differential(x₄(t)), (-(α₁ * (((((0.237902 * x₅(t)) / 723208) + ((0.432631 * x₆(t)) / 874150)) + ((0.726488 * x₇(t)) / 1330993)) + ((0.979258 * x₈(t)) / 1411928)))) * x₄(t)), Equation(Differential(x₅(t)), ((α₁ * (((((3.45536 * x₅(t)) / 723208) + ((0.485314 * x₆(t)) / 874150)) + ((0.506389 * x₇(t)) / 1330993)) + ((0.123002 * x₈(t)) / 1411928))) * x₁(t)) - (((0.99997 * α₂) + (3.0e-5 * α₃)) * x₅(t))), Equation(Differential(x₆(t)), ((α₁ * (((((0.597721 * x₅(t)) / 723208) + ((2.11738 * x₆(t)) / 874150)) + ((0.911374 * x₇(t)) / 1330993)) + ((0.323385 * x₈(t)) / 1411928))) * x₂(t)) - (((0.99996 * α₂) + (4.0e-5 * α₃)) * x₆(t))), Equation(Differential(x₇(t)), ((α₁ * (((((0.906231 * x₅(t)) / 723208) + ((1.35041 * x₆(t)) / 874150)) + ((1.60756 * x₇(t)) / 1330993)) + ((0.67411 * x₈(t)) / 1411928))) * x₃(t)) - (((0.99792 * α₂) + (0.0020800000000000003 * α₃)) * x₇(t))), Equation(Differential(x₈(t)), ((α₁ * (((((0.237902 * x₅(t)) / 723208) + ((0.432631 * x₆(t)) / 874150)) + ((0.726488 * x₇(t)) / 1330993)) + ((0.979258 * x₈(t)) / 1411928))) * x₄(t)) - (((0.93884 * α₂) + (0.061160000000000006 * α₃)) * x₈(t))), Equation(Differential(x₉(t)), (α₂ * 0.99997) * x₅(t)), Equation(Differential(x₁₀(t)), (α₂ * 0.99996) * x₆(t)), Equation(Differential(x₁₁(t)), (α₂ * 0.99792) * x₇(t)), Equation(Differential(x₁₂(t)), (α₂ * 0.93884) * x₈(t)), Equation(Differential(x₁₃(t)), (α₃ * 3.0e-5) * x₅(t)), Equation(Differential(x₁₄(t)), (α₃ * 4.0e-5) * x₆(t)), Equation(Differential(x₁₅(t)), (α₃ * 0.0020800000000000003) * x₇(t)), Equation(Differential(x₁₆(t)), (α₃ * 0.061160000000000006) * x₈(t)), Equation(Differential(x₁₇(t)), ((((α₃ * 3.0e-5) * x₅(t)) + ((α₃ * 4.0e-5) * x₆(t))) + ((α₃ * 0.0020800000000000003) * x₇(t))) + ((α₃ * 0.061160000000000006) * x₈(t)))], Any[(-(rand(α₄) * (((((3.45536 * x₅(t)) / 723208) + ((0.485314 * x₆(t)) / 874150)) + ((0.506389 * x₇(t)) / 1330993)) + ((0.123002 * x₈(t)) / 1411928)))) * x₁(t), (-(rand(α₄) * (((((0.597721 * x₅(t)) / 723208) + ((2.11738 * x₆(t)) / 874150)) + ((0.911374 * x₇(t)) / 1330993)) + ((0.323385 * x₈(t)) / 1411928)))) * x₂(t), (-(rand(α₄) * (((((0.906231 * x₅(t)) / 723208) + ((1.35041 * x₆(t)) / 874150)) + ((1.60756 * x₇(t)) / 1330993)) + ((0.67411 * x₈(t)) / 1411928)))) * x₃(t), (-(rand(α₄) * (((((0.237902 * x₅(t)) / 723208) + ((0.432631 * x₆(t)) / 874150)) + ((0.726488 * x₇(t)) / 1330993)) + ((0.979258 * x₈(t)) / 1411928)))) * x₄(t), ((rand(α₄) * (((((3.45536 * x₅(t)) / 723208) + ((0.485314 * x₆(t)) / 874150)) + ((0.506389 * x₇(t)) / 1330993)) + ((0.123002 * x₈(t)) / 1411928))) * x₁(t)) - (((0.99997 * rand(α₅)) + (3.0e-5 * rand(α₆))) * x₅(t)), ((rand(α₄) * (((((0.597721 * x₅(t)) / 723208) + ((2.11738 * x₆(t)) / 874150)) + ((0.911374 * x₇(t)) / 1330993)) + ((0.323385 * x₈(t)) / 1411928))) * x₂(t)) - (((0.99996 * rand(α₅)) + (4.0e-5 * rand(α₆))) * x₆(t)), ((rand(α₄) * (((((0.906231 * x₅(t)) / 723208) + ((1.35041 * x₆(t)) / 874150)) + ((1.60756 * x₇(t)) / 1330993)) + ((0.67411 * x₈(t)) / 1411928))) * x₃(t)) - (((0.99792 * rand(α₅)) + (0.0020800000000000003 * rand(α₆))) * x₇(t)), ((rand(α₄) * (((((0.237902 * x₅(t)) / 723208) + ((0.432631 * x₆(t)) / 874150)) + ((0.726488 * x₇(t)) / 1330993)) + ((0.979258 * x₈(t)) / 1411928))) * x₄(t)) - (((0.93884 * rand(α₅)) + (0.061160000000000006 * rand(α₆))) * x₈(t)), (rand(α₅) * 0.99997) * x₅(t), (rand(α₅) * 0.99996) * x₆(t), (rand(α₅) * 0.99792) * x₇(t), (rand(α₅) * 0.93884) * x₈(t), (rand(α₆) * 3.0e-5) * x₅(t), (rand(α₆) * 4.0e-5) * x₆(t), (rand(α₆) * 0.0020800000000000003) * x₇(t), (rand(α₆) * 0.061160000000000006) * x₈(t), ((((rand(α₆) * 3.0e-5) * x₅(t)) + ((rand(α₆) * 4.0e-5) * x₆(t))) + ((rand(α₆) * 0.0020800000000000003) * x₇(t))) + ((rand(α₆) * 0.061160000000000006) * x₈(t))], t, Term{Real}[x₁(t), x₂(t), x₃(t), x₄(t), x₅(t), x₆(t), x₇(t), x₈(t), x₉(t), x₁₀(t), x₁₁(t), x₁₂(t), x₁₃(t), x₁₄(t), x₁₅(t), x₁₆(t), x₁₇(t)], Sym{Real}[α₁, α₂, α₃, α₄, α₅, α₆], Any[], Any[], Base.RefValue{Array{Num,1}}(Num[]), Base.RefValue{Any}(Array{Num}(undef,0,0)), Base.RefValue{Array{Num,2}}(Array{Num}(undef,0,0)), Base.RefValue{Array{Num,2}}(Array{Num}(undef,0,0)), Symbol("##SDESystem#307"), SDESystem[])
But then I get the following error:
fast_sde_problem = SDEProblem(sys,ℬ, 𝒯, 𝒫 )
MethodError: no method matching lower_mapnames(::Array{Any,1})
Closest candidates are:
lower_mapnames(!Matched::AbstractArray{T,N} where N) where T<:Pair at C:\Users\claud\.julia\packages\ModelingToolkit\0G1y7\src\utils.jl:274
lower_mapnames(!Matched::AbstractArray{T,N} where N, !Matched::Any) where T<:Pair at C:\Users\claud\.julia\packages\ModelingToolkit\0G1y7\src\utils.jl:277
lower_mapnames(!Matched::Tuple{Vararg{T,N}}) where {N, T<:Pair} at C:\Users\claud\.julia\packages\ModelingToolkit\0G1y7\src\utils.jl:280
...
Stacktrace:
[1] SDEProblem{true,tType,isinplace,P,NP,F,G,K,ND} where ND where K where G where F where NP where P where isinplace where tType(::SDESystem, ::Array{Float64,1}, ::Tuple{Float64,Int64}, ::Array{Any,1}; version::Nothing, tgrad::Bool, jac::Bool, Wfact::Bool, checkbounds::Bool, sparse::Bool, sparsenoise::Bool, linenumbers::Bool, parallel::ModelingToolkit.SerialForm, eval_expression::Bool, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\claud\.julia\packages\ModelingToolkit\0G1y7\src\systems\diffeqs\sdesystem.jl:317
[2] SDEProblem{true,tType,isinplace,P,NP,F,G,K,ND} where ND where K where G where F where NP where P where isinplace where tType(::SDESystem, ::Array{Float64,1}, ::Tuple{Float64,Int64}, ::Array{Any,1}) at C:\Users\claud\.julia\packages\ModelingToolkit\0G1y7\src\systems\diffeqs\sdesystem.jl:310
[3] SDEProblem(::SDESystem, ::Array{Float64,1}, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\claud\.julia\packages\ModelingToolkit\0G1y7\src\systems\diffeqs\sdesystem.jl:340
[4] SDEProblem(::SDESystem, ::Array{Float64,1}, ::Vararg{Any,N} where N) at C:\Users\claud\.julia\packages\ModelingToolkit\0G1y7\src\systems\diffeqs\sdesystem.jl:340
[5] top-level scope at In[5]:1
[6] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
I tried to circumvent this in many ways, with no success.
I need to have such a composite p
in order to be later able to change the distributions during the solve
of an EnsembleProblem
built upon that SDEProblem
, using DiscreteCallback
.
My status:
(computationalEpi) pkg> st
Status `E:\IlMIoDrive\magistrale\2anno\primo_periodo\computationalEpi\Project.toml`
[ff3c4d4f] AutoOptimize v0.1.0 `https://github.com/SciML/AutoOptimize.jl#master`
[6e4b80f9] BenchmarkTools v0.5.0
[76274a88] Bijectors v0.8.12
[5d742f6a] CSVFiles v1.0.0
[35d6a980] ColorSchemes v3.10.2
[8f4d0f93] Conda v1.5.0
[667455a9] Cubature v1.5.1
[2445eb08] DataDrivenDiffEq v0.5.2
[071ae1c0] DiffEqGPU v1.9.1
[41bf760c] DiffEqSensitivity v6.40.0
[0c46a032] DifferentialEquations v6.16.0
[bbc10e6e] DynamicHMC v2.2.0
[7073ff75] IJulia v1.23.1
[5ab0869b] KernelDensity v0.6.2
[2472808a] KernelDensityEstimate v0.5.3
[c7f686f2] MCMCChains v4.4.0
[6fafb56a] Memoization v0.1.5
[961ee093] ModelingToolkit v4.5.0
[d330b81b] PyPlot v2.9.0
[1fd47b50] QuadGK v2.4.1
[37e2e3b7] ReverseDiff v1.5.0
[295af30f] Revise v3.1.11
[2913bbd2] StatsBase v0.33.2
[f3b207a7] StatsPlots v0.14.17
[84d833dd] TransformVariables v0.3.10
[fce5fe82] Turing v0.15.8
[e88e6eb3] Zygote v0.6.0
Thanks in advance
pitmonticone, daorse and InterdisciplinaryPhysicsTeam
Metadata
Metadata
Assignees
Labels
No labels