Skip to content

Commit

Permalink
Cleanup metadata macros (#434)
Browse files Browse the repository at this point in the history
* Cleanup metadata structs
Remove `exchange`, `grid_type` and `grid_location` from metadata structs.  `grid_type` is not required, it is already implemented as part of BMI. `exhange` and `grid_location` are now implemented without the use of the FieldMetadata package.

* Update changelog

* Fix `exchange` function for `ShallowWaterLand`

* Add tests

* Move `exchange` and `grid_location` functions
To file bmi.jl as these functions are quite specific to BMI functionality.

* Add `v1` branch to CI
  • Loading branch information
vers-w authored Jul 2, 2024
1 parent de91ed9 commit 5107824
Show file tree
Hide file tree
Showing 15 changed files with 492 additions and 267 deletions.
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
- v1
tags: '*'
jobs:
test:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/CIWflowServer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
- v1
tags: '*'
jobs:
test:
Expand Down
3 changes: 3 additions & 0 deletions docs/src/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- Removed vertical concepts `HBV` and `FLEXTopo`.
- Removed metadata macros `exchange`, `grid_type` and `grid_location`. The macro `grid_type`
is not required because this functionality is already part of `BMI`. The macros `exchange`
and `grid_location` are replaced by functions resulting in cleaner code.

### Added

Expand Down
4 changes: 0 additions & 4 deletions src/Wflow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ using LoopVectorization
using IfElse

@metadata get_units "mm dt-1" String
# metadata for BMI grid
@metadata exchange 1 Integer
@metadata grid_type "unstructured" String
@metadata grid_location "node" String

const BMI = BasicModelInterface
const Float = Float64
Expand Down
155 changes: 139 additions & 16 deletions src/bmi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,23 @@ function BMI.get_input_var_names(model::Model)
var_names = Vector{String}()
for c in config.API.components
type = typeof(param(model, c))
inds = findall(x -> x != 0, exchange(type))
field_names = fieldnames(type)[inds]
field_names = fieldnames(type)

for name in field_names
var = string(c, ".", name)
model_var = param(model, var)
if eltype(model_var) <: SVector
for i = 1:length(first(model_var))
push!(var_names, string(var, "[", i, "]"))
if exchange(param(model, c), name) == 1
var = string(c, ".", name)
model_var = param(model, var)
if eltype(model_var) <: SVector
for i = 1:length(first(model_var))
push!(var_names, string(var, "[", i, "]"))
end
elseif ndims(model_var) > 1
for i = 1:length(first(model_var))
push!(var_names, string(var, "[", i, "]"))
end
else
push!(var_names, var)
end
elseif ndims(model_var) > 1
for i = 1:length(first(model_var))
push!(var_names, string(var, "[", i, "]"))
end
else
push!(var_names, var)
end
end
end
Expand All @@ -152,7 +153,6 @@ function BMI.get_var_grid(model::Model, name::String)
s = split(name, "[")
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[1:end-1]))
return if :reservoir in key
0
Expand Down Expand Up @@ -199,8 +199,9 @@ end

function BMI.get_var_location(model::Model, name::String)
key = symbols(first(split(name, "[")))
if exchange(param(model, key[1:end-1]), key[end]) == 1
return grid_location(model, key)
type = param(model, key[1:end-1])
if exchange(type, key[end]) == 1
return grid_location(type, key[end])
else
error("$name not listed as variable for BMI exchange")
end
Expand Down Expand Up @@ -409,3 +410,125 @@ end
function get_start_unix_time(model::Model)
datetime2unix(DateTime(model.config.starttime))
end

# Exchange and grid location functions.
exchange(::Nothing, var) = 0
grid_location(::Nothing, var) = "none"

function exchange(::SurfaceFlow, var)
if var in (:dt, :beta, :its, :alpha_pow, :reservoir, :lake, :kinwave_it)
0
else
1
end
end

function grid_location(::SurfaceFlow, var)
if var in (:dt, :its, :kinwave_it)
"none"
else
"node"
end
end

exchange(::Union{LateralSSF,GroundwaterExchange}, var) = var == :dt ? 0 : 1
grid_location(::Union{LateralSSF,GroundwaterExchange}, var) = var == :dt ? "none" : "node"

function exchange(::ShallowWaterRiver, var)
if var in (
:dt,
:n,
:ne,
:g,
:alpha,
:h_thresh,
:froude_limit,
:reservoir_index,
:lake_index,
:reservoir,
:lake,
:floodplain,
)
0
else
1
end
end

function grid_location(::ShallowWaterRiver, var)
if var in (:n, :ne, :dt, :froude_limit)
"none"
elseif var in (
:active_e,
:q,
:q0,
:q_av,
:mannings_n_sq,
:zs_max,
:hf,
:dl_at_link,
:width_at_link,
:a,
:r,
)
"edge"
else
"node"
end
end

function exchange(::ShallowWaterLand, var)
if var in (:n, :g, :theta, :alpha, :h_thresh, :dt, :froude_limit)
0
else
1
end
end

function grid_location(::ShallowWaterLand, var)
if var in (:n, :dt, :froude_limit)
"none"
elseif var in (:xwidth, :ywidth, :qy0, :qx0, :qx, :qy, :zx_max, :zy_max, :mannings_n_sq)
"edge"
else
"node"
end
end

exchange(::FloodPlainProfile, var) = 1
grid_location(::FloodPlainProfile, var) = "node"

exchange(::FloodPlain, var) = var == :profile ? 0 : 1
function grid_location(::FloodPlain, var)
if var in (:mannings_n_sq, :a, :r, :hf, :zb_max, :q0, :q, :q_av, :hf_index)
"edge"
else
"node"
end

end

exchange(::SimpleReservoir, var) = var == :dt ? 0 : 1
grid_location(::SimpleReservoir, var) = var == :dt ? "none" : "node"

exchange(::Lake, var) = var == :dt ? 0 : 1
grid_location(::Lake, var) = var == :dt ? "none" : "node"

exchange(::SBM, var) = var in (:n, :dt, :maxlayers) ? 0 : 1
grid_location(::SBM, var) = var in (:n, :dt, :maxlayers) ? "none" : "node"

exchange(::Union{LandSediment,OverlandFlowSediment}, var) = var == :n ? 0 : 1
grid_location(::Union{LandSediment,OverlandFlowSediment}, var) = var == :n ? "none" : "node"

exchange(::RiverSediment, var) = var in (:n, :dt) ? 0 : 1
grid_location(::RiverSediment, var) = var in (:n, :dt) ? "none" : "node"


exchange(::Aquifer, var) = 1
grid_location(::Aquifer, var) = "node"

exchange(::ConstantHead, var) = 0
grid_location(::ConstantHead, var) = "node"

exchange(::AquiferBoundaryCondition, var) = 1
grid_location(::AquiferBoundaryCondition, var) = "node"
Loading

0 comments on commit 5107824

Please sign in to comment.