Skip to content

Commit 2a9979a

Browse files
Merge pull request #713 from SciML/tuple
allow tuples in varmaps
2 parents 0ed3b4a + 704ac47 commit 2a9979a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/variables.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,22 @@ function varmap_to_vars(varmap::AbstractArray{Pair{T,S}},varlist) where {T,S}
227227
# Does things like MArray->SArray
228228
ArrayInterface.restructure(varmap,out)
229229
end
230+
231+
function varmap_to_vars(varmap::NTuple{N,Pair{T,S}},varlist) where {N,T,S}
232+
out = MArray{Tuple{N},S}(undef)
233+
for (ivar, ival) in varmap
234+
j = findfirst(isequal(ivar),varlist)
235+
if isnothing(j)
236+
throw(ArgumentError("Value $(ivar) provided in map not found in $(varlist)"))
237+
elseif j > length(varmap)
238+
throw(ArgumentError("Missing value in $(varmap), need $(varlist)"))
239+
end
240+
out[j] = ival
241+
end
242+
out.data
243+
end
244+
230245
varmap_to_vars(varmap::AbstractArray,varlist) = varmap
246+
varmap_to_vars(varmap::Tuple,varlist) = varmap
231247
varmap_to_vars(varmap::DiffEqBase.NullParameters,varlist) = varmap
232248
varmap_to_vars(varmap::Nothing,varlist) = varmap

test/odesystem.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,14 @@ u0 = [y₁ => 1.0,
192192
p = [k₁ => 0.04,
193193
k₂ => 3e7,
194194
k₃ => 1e4]
195+
p2 = (k₁ => 0.04,
196+
k₂ => 3e7,
197+
k₃ => 1e4)
195198
tspan = (0.0,100000.0)
196199
prob1 = ODEProblem(sys,u0,tspan,p)
200+
prob12 = ODEProblem(sys,u0,tspan,[0.04,3e7,1e4])
201+
prob13 = ODEProblem(sys,u0,tspan,(0.04,3e7,1e4))
202+
prob14 = ODEProblem(sys,u0,tspan,p2)
197203
prob2 = ODEProblem(sys,u0,tspan,p,jac=true)
198204
prob3 = ODEProblem(sys,u0,tspan,p,jac=true,sparse=true)
199205
for (prob, atol) in [(prob1, 1e-12), (prob2, 1e-12), (prob3, 1e-12)]

0 commit comments

Comments
 (0)