Skip to content

Commit

Permalink
Lenses and change parameter names
Browse files Browse the repository at this point in the history
Use lenses from `Accessors` for mapping standard names and for input parameters (`ncread`). Change parameter names of demand and groundwater to standard names. Writing any model output variable with the former TOML keys is possible (using `param`).
  • Loading branch information
vers-w committed Dec 17, 2024
1 parent 9288360 commit 5858906
Show file tree
Hide file tree
Showing 36 changed files with 1,089 additions and 1,411 deletions.
64 changes: 63 additions & 1 deletion Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,38 @@

julia_version = "1.10.7"
manifest_format = "2.0"
project_hash = "986b47155507036c06218d23750fe8de28eb001a"
project_hash = "0b6ba2f31af23dc8f1958221fc8ee6c1fb4f0096"

[[deps.AbstractTrees]]
git-tree-sha1 = "2d9c9a55f9c93e8887ad391fbae72f8ef55e1177"
uuid = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
version = "0.4.5"

[[deps.Accessors]]
deps = ["CompositionsBase", "ConstructionBase", "InverseFunctions", "LinearAlgebra", "MacroTools", "Markdown"]
git-tree-sha1 = "96bed9b1b57cf750cca50c311a197e306816a1cc"
uuid = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
version = "0.1.39"

[deps.Accessors.extensions]
AccessorsAxisKeysExt = "AxisKeys"
AccessorsDatesExt = "Dates"
AccessorsIntervalSetsExt = "IntervalSets"
AccessorsStaticArraysExt = "StaticArrays"
AccessorsStructArraysExt = "StructArrays"
AccessorsTestExt = "Test"
AccessorsUnitfulExt = "Unitful"

[deps.Accessors.weakdeps]
AxisKeys = "94b1ba4f-4ee9-5380-92f1-94cde586c3c5"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[[deps.Adapt]]
deps = ["LinearAlgebra", "Requires"]
git-tree-sha1 = "50c3c56a52972d78e8be9fd135bfb91c9574c140"
Expand Down Expand Up @@ -134,6 +159,30 @@ deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
version = "1.1.1+0"

[[deps.CompositionsBase]]
git-tree-sha1 = "802bb88cd69dfd1509f6670416bd4434015693ad"
uuid = "a33af91c-f02d-484b-be07-31d278c5ca2b"
version = "0.1.2"
weakdeps = ["InverseFunctions"]

[deps.CompositionsBase.extensions]
CompositionsBaseInverseFunctionsExt = "InverseFunctions"

[[deps.ConstructionBase]]
git-tree-sha1 = "76219f1ed5771adbb096743bff43fb5fdd4c1157"
uuid = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
version = "1.5.8"

[deps.ConstructionBase.extensions]
ConstructionBaseIntervalSetsExt = "IntervalSets"
ConstructionBaseLinearAlgebraExt = "LinearAlgebra"
ConstructionBaseStaticArraysExt = "StaticArrays"

[deps.ConstructionBase.weakdeps]
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[[deps.CpuId]]
deps = ["Markdown"]
git-tree-sha1 = "fcbb72b032692610bfbdb15018ac16a36cf2e406"
Expand Down Expand Up @@ -228,6 +277,19 @@ version = "0.1.5"
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[deps.InverseFunctions]]
git-tree-sha1 = "a779299d77cd080bf77b97535acecd73e1c5e5cb"
uuid = "3587e190-3f89-42d0-90ee-14403ec27112"
version = "0.1.17"

[deps.InverseFunctions.extensions]
InverseFunctionsDatesExt = "Dates"
InverseFunctionsTestExt = "Test"

[deps.InverseFunctions.weakdeps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[deps.JLLWrappers]]
deps = ["Artifacts", "Preferences"]
git-tree-sha1 = "be3dc50a92e5a386872a493a10050136d4703f9b"
Expand Down
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Deltares and contributors"]
version = "0.8.1"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
BasicModelInterface = "59605e27-edc0-445a-b93d-c09a3a50b330"
CFTime = "179af706-886a-5703-950a-314cd64e0468"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand All @@ -24,6 +25,7 @@ TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
TerminalLoggers = "5d786b92-1e48-4d6f-9151-6b4477ca9bed"

[compat]
Accessors = "0.1"
Aqua = "0.8"
BasicModelInterface = "0.1"
CFTime = "0.1"
Expand Down
114 changes: 49 additions & 65 deletions server/test/sbm_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,27 @@ path_output = "outstates-moselle.nc"
# if listed, the variable must be present in the NetCDF or error
# if not listed, the variable can get a default value if it has one

[state.vertical.interception.variables]
canopy_storage = "canopystorage"
[state.variables]
vegetation_canopy_water__storage = "canopystorage"

[state.vertical.soil.variables]
satwaterdepth = "satwaterdepth"
tsoil = "tsoil"
ustorelayerdepth = "ustorelayerdepth"
soil_water_sat-zone__depth = "satwaterdepth"
soil_surface__temperature = "tsoil"
soil_water_unsat-zone__depth-per-soil_layer = "ustorelayerdepth"

[state.vertical.snow.variables]
snow_storage = "snow"
snow_water = "snowwater"
"snowpack~dry__leq-depth" = "snow"
"snowpack~liquid__depth" = "snowwater"

[state.lateral.river.variables]
h = "h_river"
h_av = "h_av_river"
q = "q_river"
river_water__depth = "h_river"
river_water__time_average_of_depth = "h_av_river"
river_water__volume_flow_rate = "q_river"

[state.lateral.river.boundary_conditions.reservoir.variables]
volume = "volume_reservoir"
reservoir_water__volume = "volume_reservoir"

[state.lateral.subsurface.variables]
ssf = "ssf"
subsurface_water__volume_flow_rate = "ssf"

[state.lateral.land.variables]
h = "h_land"
h_av = "h_av_land"
q = "q_land"
land_surface_water__volume_flow_rate = "q_land"
land_surface_water__depth = "h_land"
land_surface_water__time_average_of_depth = "h_av_land"

[input]
path_forcing = "forcing-moselle.nc"
Expand All @@ -57,6 +51,12 @@ ldd = "wflow_ldd"
river_location = "wflow_river"
subcatchment = "wflow_subcatch"

[input.forcing]
atmosphere_water__precipitation_volume_flux = "precip"
land_surface_water__potential_evaporation_volume_flux = "pet"
atmosphere_air__temperature = "temp"

[input.parameters]
atmosphere_air__snowfall_temperature_threshold = "TT"
atmosphere_air__snowfall_temperature_interval = "TTI"

Expand Down Expand Up @@ -95,8 +95,8 @@ river_bank_water__elevation = "RiverZ"
land_surface_water_flow__manning_n_parameter = "N"
land_surface__slope = "Slope"

reservoir_areas__number = "wflow_reservoirareas"
reservoir_locations__number = "wflow_reservoirlocs"
reservoir_area__number = "wflow_reservoirareas"
reservoir_location__number = "wflow_reservoirlocs"
reservoir_surface__area = "ResSimpleArea"
"reservoir_water_demand~required~downstream__volume_flow_rate" = "ResDemand"
reservoir_water_release-below-spillway__max_volume_flow_rate = "ResMaxRelease"
Expand All @@ -108,20 +108,16 @@ subsurface_water__horizontal-to-vertical_saturated_hydraulic_conductivity_ratio

# specify the internal IDs of the parameters which vary over time
# the external name mapping needs to be below together with the other mappings
[input.dynamic]
forcing = [
"vertical.atmospheric_forcing.precipitation",
"vertical.atmospheric_forcing.temperature",
"vertical.atmospheric_forcing.potential_evaporation",
"atmosphere_water__precipitation_volume_flux",
"atmosphere_air__temperature",
"land_surface_water__potential_evaporation_volume_flux",
]

cyclic = ["vegetation__leaf-area_index"]

[input.vertical.atmospheric_forcing]
potential_evaporation = "pet"
precipitation = "precip"
temperature = "temp"

[input.soil_surface_water__vertical_saturated_hydraulic_conductivity]
[input.parameters.soil_surface_water__vertical_saturated_hydraulic_conductivity]
netcdf.variable.name = "KsatVer"
scale = 1.0
offset = 0.0
Expand All @@ -140,31 +136,19 @@ min_streamorder_land = 5
[output]
path = "output_moselle.nc"

[output.vertical.interception.variables]
canopy_storage = "canopystorage"

[output.vertical.soil.variables]
satwaterdepth = "satwaterdepth"
tsoil = "tsoil"
ustorelayerdepth = "ustorelayerdepth"

[output.vertical.snow.variables]
snow_storage = "snow"
snow_water = "snowwater"

[output.lateral.river.variables]
h = "h_river"
q = "q_river"

[output.lateral.river.boundary_conditions.reservoir.variables]
volume = "volume_reservoir"

[output.lateral.subsurface.variables]
ssf = "ssf"

[output.lateral.land.variables]
h = "h_land"
q = "q_land"
[output.variables]
soil_water_sat-zone__depth = "satwaterdepth"
soil_surface__temperature = "tsoil"
soil_water_unsat-zone__depth-per-soil_layer = "ustorelayerdepth"
"snowpack~dry__leq-depth" = "snow"
"snowpack~liquid__depth" = "snowwater"
river_water__depth = "h_river"
river_water__volume_flow_rate = "q_river"
reservoir_water__volume = "volume_reservoir"
subsurface_water__volume_flow_rate = "ssf"
land_surface_water__volume_flow_rate = "q_land"
land_surface_water__depth = "h_land"
vertical.interception.variables.canopy_storage = "canopystorage"

[netcdf]
path = "output_scalar_moselle.nc"
Expand All @@ -179,33 +163,33 @@ coordinate.x = 6.255
coordinate.y = 50.012
name = "temp_coord"
location = "temp_bycoord"
parameter = "vertical.atmospheric_forcing.temperature"
parameter = "atmosphere_air__temperature"

[[netcdf.variable]]
location = "temp_byindex"
name = "temp_index"
index.x = 100
index.y = 264
parameter = "vertical.atmospheric_forcing.temperature"
parameter = "atmosphere_air__temperature"

[csv]
path = "output_moselle.csv"

[[csv.column]]
header = "Q"
parameter = "lateral.river.variables.q"
parameter = "river_water__volume_flow_rate"
reducer = "maximum"

[[csv.column]]
header = "volume"
index = 1
parameter = "lateral.river.boundary_conditions.reservoir.variables.volume"
parameter = "reservoir_water__volume"

[[csv.column]]
coordinate.x = 6.255
coordinate.y = 50.012
header = "temp_bycoord"
parameter = "vertical.atmospheric_forcing.temperature"
parameter = "atmosphere_air__temperature"

[[csv.column]]
coordinate.x = 6.255
Expand All @@ -218,17 +202,17 @@ layer = 2
header = "temp_byindex"
index.x = 100
index.y = 264
parameter = "vertical.atmospheric_forcing.temperature"
parameter = "atmosphere_air__temperature"

[[csv.column]]
header = "Q"
map = "gauges"
parameter = "lateral.river.variables.q"
parameter = "river_water__volume_flow_rate"

[[csv.column]]
header = "recharge"
map = "subcatchment"
parameter = "vertical.soil.variables.recharge"
parameter = "soil_water_sat-zone_top__recharge_volume_flux"
reducer = "mean"

[API]
Expand Down
1 change: 1 addition & 0 deletions src/Wflow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Wflow

import BasicModelInterface as BMI

using Accessors: @optic
using Base.Threads: nthreads
using CFTime: CFTime, monthday, dayofyear
using Dates:
Expand Down
20 changes: 10 additions & 10 deletions src/bmi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# https://github.com/Deltares/BasicModelInterface.jl

# Mapping of grid identifier to a key, to get the active indices of the model domain.
# See also function active_indices(network, key::Tuple).
const grids = Dict{Int, Tuple{Symbol}}(
0 => (:reservoir,),
1 => (:lake,),
2 => (:drain,),
3 => (:river,),
4 => (:land,),
5 => (:land,),
6 => (:land,),
# See also function active_indices(network, key::AbstractString).
const grids = Dict{Int, String}(
0 => "reservoir",
1 => "lake",
2 => "drain",
3 => "river",
4 => "land",
5 => "land",
6 => "land",
)

"""
Expand Down Expand Up @@ -247,7 +247,7 @@ function BMI.get_value_ptr(model::Model, name::String)
s = split(name, "[")
key = symbols(first(s))
if exchange(param(model, key))
n = length(active_indices(network, key))
n = length(active_indices(network, first(s)))
if occursin("[", name)
ind = tryparse(Int, split(s[end], "]")[1])
if eltype(param(model, key)) <: SVector
Expand Down
Loading

0 comments on commit 5858906

Please sign in to comment.