Skip to content

Commit

Permalink
BMI.get_value_ptr and SVector type
Browse files Browse the repository at this point in the history
Replace getindex (values are retrieved from the array (copy)), with reinterpret (a copy is not created).
  • Loading branch information
vers-w committed Oct 23, 2023
1 parent 8fb571c commit 3c8e815
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/bmi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,11 @@ function BMI.get_value_ptr(model::Model, name::String)
if occursin("[", name)
ind = tryparse(Int, split(s[end], "]")[1])
if eltype(param(model, key)) <: SVector
value = @view getindex.(param(model, key), ind)[1:n]
return value
model_vals = param(model, key)
el_type = eltype(first(model_vals))
dim = length(first(model_vals))
value = reshape(reinterpret(el_type, model_vals), dim, :)
return @view value[ind, 1:n]
else
value = @view param(model, key)[ind, 1:n]
return value
Expand Down
6 changes: 6 additions & 0 deletions test/bmi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ tomlpath = joinpath(@__DIR__, "sbm_config.toml")
zeros(Float, 3),
[1, 2, 3],
) getindex.(model.vertical.vwc, 1)[1:3]
BMI.set_value_at_indices(
model,
"vertical.vwc[2]",
[1, 2, 3],
[0.10, 0.15, 0.20],
) getindex.(model.vertical.vwc, 2)[1:3]
@test BMI.get_value_at_indices(
model,
"lateral.river.q",
Expand Down

0 comments on commit 3c8e815

Please sign in to comment.