Skip to content

Commit bd09d73

Browse files
authored
Add getindex/setindex! methods for MTKParameters with ParameterIndex
1 parent 1b858f6 commit bd09d73

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/systems/parameter_buffer.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,41 @@ function Base.setindex!(p::MTKParameters, val, i)
361361
end
362362
end
363363

364+
function Base.getindex(p::MTKParameters, pind::ParameterIndex)
365+
(;portion, idx) = pind, (i,j) = idx
366+
if portion isa SciMLStructures.Tunable
367+
p.tunable[i][j]
368+
elseif portion isa SciMLStructures.Discrete
369+
p.discrete[i][j]
370+
elseif portion isa SciMLStructures.Constants
371+
p.constant[i][j]
372+
elseif portion === DEPENDENT_PORTION
373+
p.dependent[i][j]
374+
elseif portion === NONNUMERIC_PORTION
375+
p.nonnumeric[i][j]
376+
else
377+
error("Unhandled portion $portion")
378+
end
379+
end
380+
381+
function Base.setindex!(p::MTKParameters, val, pind::ParameterIndex)
382+
(;portion, idx) = pind
383+
(i,j) = idx
384+
if portion isa SciMLStructures.Tunable
385+
p.tunable[i][j] = val
386+
elseif portion isa SciMLStructures.Discrete
387+
p.discrete[i][j] = val
388+
elseif portion isa SciMLStructures.Constants
389+
p.constant[i][j] = val
390+
elseif portion === DEPENDENT_PORTION
391+
p.dependent[i][j] = val
392+
elseif portion === NONNUMERIC_PORTION
393+
p.nonnumeric[i][j] = val
394+
else
395+
error("Unhandled portion $portion")
396+
end
397+
end
398+
364399
function Base.iterate(buf::MTKParameters, state = 1)
365400
total_len = 0
366401
total_len += _num_subarrays(buf.tunable)

0 commit comments

Comments
 (0)