-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem.jl
64 lines (48 loc) · 2.21 KB
/
problem.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
struct Problem{T,S<:Spatialness,R<:Stochasticity,D<:Discreteness}
model::Model
prob::T
tspan
initial_condition
spatialgraph::Union{SpatialGraph, Missing}
end
is_spatial(p::Problem) = !ismissing(p.spatialgraph)
model(p::Problem) = p.model
function problem(m::Model; kwargs...)
problem(m, Deterministic; kwargs...)
end
function problem(m::Model, ::Type{Deterministic}; tspan=(0,100), u0=nothing)
prob = discreteness(m) == MetacommunityDynamics.Continuous ? ODEProblem : DiscreteProblem
# this should dispatch on whether a spatialgraph was provided
f = factory(m)
u0 = isnothing(u0) ? initial(m) : u0
θ = parameters(m)
pr = prob(f, u0, tspan, θ)
Problem{typeof(pr),Local,Deterministic,discreteness(m) }(m, pr, tspan, u0, Missing())
end
function problem(m::SpatialModel,::Type{Deterministic}; tspan=(0,100), u0=nothing, )
f = factory(m.model, m.diffusion)
s = numsites(m.spatialgraph)
u0 = isnothing(u0) ? hcat([initial(m.model) for _ in 1:s]...) : hcat([u0 for _ in 1:s]...)
θ = parameters(m.model)
pr = ODEProblem(f, u0, tspan, θ)
Problem{typeof(pr),Spatial,Deterministic,discreteness(m.model) }(m.model, pr, tspan, u0, m.spatialgraph)
end
function problem(m::SpatialModel, gd::GaussianDrift; tspan=(0,100), u0=nothing)
f,g = factory(m.model, m.diffusion, gd)
s = numsites(m.spatialgraph)
u0 = isnothing(u0) ? hcat([initial(m.model) for _ in 1:s]...) : hcat([u0 for _ in 1:s]...)
θ = parameters(m.model)
pr = SDEProblem(f, g, u0, tspan, θ)
Problem{typeof(pr),Spatial,Stochastic,discreteness(m.model) }(m.model, pr, tspan, u0, m.spatialgraph)
end
function problem(m::T, gd::GaussianDrift; tspan=(0,100), u0=nothing) where T<:Model
prob = discreteness(m) == MetacommunityDynamics.Continuous ? SDEProblem : DiscreteProblem
# this should dispatch on whether a spatialgraph was provided
f, g = factory(m,gd)
u0 = isnothing(u0) ? initial(m) : u0
θ = parameters(m)
# This will also enable injection of environment dependent variables here
# for the spatial version of this method
pr = prob(f, g, u0, tspan, θ)
Problem{typeof(pr),Local,Stochastic,discreteness(m)}(m, pr, tspan, u0, Missing())
end