Skip to content

Commit

Permalink
Add more tests for (extended) BMI
Browse files Browse the repository at this point in the history
  • Loading branch information
vers-w committed Oct 25, 2023
1 parent 26464f6 commit 5dc79b1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/bmi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function BMI.get_var_grid(model::Model, name::String)
key = symbols(first(s))
if exchange(param(model, key[1:end-1]), key[end]) == 1
gridtype = grid_type(param(model, key))
type = typeof(param(model, key))
type = typeof(param(model, key[1:end-1]))
if gridtype == "scalar"
0
elseif :reservoir in key
Expand Down Expand Up @@ -358,19 +358,19 @@ function BMI.get_grid_edge_nodes(model::Model, grid::Int, edge_nodes::Vector{Int
edge_nodes[range(2, n, step = 2)] = nodes_at_edge.dst
return edge_nodes
elseif grid == 5
xu = network.staggered_indices.xu
xu = network.land.staggered_indices.xu
edge_nodes[range(1, n, step = 2)] = range(1, m)
xu[xu.==m+1] .= -999
edge_nodes[range(2, n, step = 2)] = xu
return edge_nodes
elseif grid == 6
yu = network.staggered_indices.yu
yu = network.land.staggered_indices.yu
edge_nodes[range(1, n, step = 2)] = range(1, m)
yu[yu.==m+1] .= -999
edge_nodes[range(2, n, step = 2)] = yu
return edge_nodes
elseif grid in range(0, 3) || grid == 7
warn("edges are not provided for grid type $grid (variables are located at nodes)")
@warn("edges are not provided for grid type $grid (variables are located at nodes)")
else
error("unknown grid type $grid")
end
Expand Down
38 changes: 38 additions & 0 deletions test/bmi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ tomlpath = joinpath(@__DIR__, "sbm_config.toml")

@testset "update and get and set functions" begin
@test BMI.get_current_time(model) == 86400.0
@test_throws ErrorException BMI.get_value_ptr(model, "vertical.")
dest = zeros(Float, size(model.vertical.zi))
BMI.get_value(model, "vertical.zi", dest)
@test mean(dest) 276.3767651555451
Expand Down Expand Up @@ -77,7 +78,12 @@ tomlpath = joinpath(@__DIR__, "sbm_config.toml")

@testset "model grid functions" begin
@test BMI.get_grid_type(model, 0) == "scalar"
@test BMI.get_grid_type(model, 2) == "points"
@test BMI.get_grid_type(model, 7) == "unstructured"
@test_throws ErrorException BMI.get_grid_rank(model, 8)
@test BMI.get_grid_rank(model, 0) == 0
@test BMI.get_grid_rank(model, 7) == 2
@test_throws ErrorException BMI.get_grid_rank(model, 8)
@test BMI.get_grid_node_count(model, 1) == 2
@test BMI.get_grid_node_count(model, 4) == 5809
@test BMI.get_grid_node_count(model, 5) == 50063
Expand Down Expand Up @@ -105,6 +111,23 @@ tomlpath = joinpath(@__DIR__, "sbm_config.toml")

end

@testset "BMI grid edges" begin
tomlpath = joinpath(@__DIR__, "sbm_swf_config.toml")
model = BMI.initialize(Wflow.Model, tomlpath)
@test BMI.get_var_grid(model, "lateral.land.qx") == 5
@test BMI.get_var_grid(model, "lateral.land.qy") == 6
@test BMI.get_grid_edge_count(model, 5) == 50063
@test BMI.get_grid_edge_count(model, 6) == 50063
@test BMI.get_grid_edge_nodes(model, 5, fill(0, 2 * 50063))[1:4] == [1, -999, 2, 3]
@test BMI.get_grid_edge_nodes(model, 6, fill(0, 2 * 50063))[1:4] == [1, 4, 2, 10]
@test_logs (
:warn,
"edges are not provided for grid type 3 (variables are located at nodes)",
) BMI.get_grid_edge_nodes(model, 3, fill(0, 2 * 50063))
@test_throws ErrorException BMI.get_grid_edge_nodes(model, 8, fill(0, 2 * 50063))
BMI.finalize(model)
end

@testset "BMI run SBM in parts" begin
tomlpath = joinpath(@__DIR__, "sbm_gw.toml")
model = BMI.initialize(Wflow.Model, tomlpath)
Expand Down Expand Up @@ -150,4 +173,19 @@ tomlpath = joinpath(@__DIR__, "sbm_config.toml")

BMI.finalize(model)
end

end

@testset "BMI extension functions" begin

model = BMI.initialize(Wflow.Model, tomlpath)
@test Wflow.get_start_unix_time(model) == 9.466848e8
satwaterdepth = mean(model.vertical.satwaterdepth)
model.config.model.reinit = false
model = Wflow.load_state(model)
@test satwaterdepth mean(model.vertical.satwaterdepth)
@test_logs (
:info,
"Write output states to NetCDF file `$(model.writer.state_nc_path)`.",
) Wflow.save_state(model)
end

0 comments on commit 5dc79b1

Please sign in to comment.