Skip to content

Commit

Permalink
Merge pull request #79 from SciML/as/remake-buffer-arraysym
Browse files Browse the repository at this point in the history
fix: make `remake_buffer` work for array symbolics
  • Loading branch information
ChrisRackauckas authored May 28, 2024
2 parents 71408bb + f5e96c6 commit d0f4433
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/remake.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,24 @@ function remake_buffer(sys, oldbuffer::AbstractArray, vals::Dict)
if ArrayInterface.ismutable(oldbuffer) && !isa(oldbuffer, MArray)
elT = Union{}
for val in values(vals)
elT = promote_type(elT, typeof(val))
if val isa AbstractArray
valT = eltype(val)
else
valT = typeof(val)
end
elT = promote_type(elT, valT)
end

newbuffer = similar(oldbuffer, elT)
copyto!(newbuffer, oldbuffer)
setu(sys, collect(keys(vals)))(newbuffer, elT.(values(vals)))
for (k, v) in vals
if v isa AbstractArray
v = elT.(v)
else
v = elT(v)
end
setu(sys, k)(newbuffer, v)
end
else
mutbuffer = remake_buffer(sys, collect(oldbuffer), vals)
newbuffer = similar_type(oldbuffer, eltype(mutbuffer))(mutbuffer)
Expand Down
1 change: 1 addition & 0 deletions test/downstream/Project.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[deps]
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
11 changes: 11 additions & 0 deletions test/downstream/remake_arrayvars.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using ModelingToolkit
using ModelingToolkit: t_nounits as t
using SymbolicIndexingInterface

@variables x(t)[1:2] y(t)
@named sys = ODESystem(Equation[], t, [x, y], [])
sys = complete(sys)

u0 = [1.0, 2.0, 3.0]
newu0 = remake_buffer(sys, u0, Dict(x => [5.0, 6.0], y => 7.0))
@test newu0 == [5.0, 6.0, 7.0]

0 comments on commit d0f4433

Please sign in to comment.