Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor types and model initialization #517

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
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 = "7340a1e1d084fa2d694e3291e3df55fd710ca408"

[[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 = "b392ede862e506d451fc1616e79aa6f4c673dab8"
uuid = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
version = "0.1.38"

[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
23 changes: 14 additions & 9 deletions docs/developments/julia_structs.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ Below the composite type that represents all different aspects of a `Wflow.Model
network, parameters, clock, model type, configuration and input and output.

```julia
struct Model{N,L,V,R,W,T}
config::Config # all configuration options
network::N # connectivity information, directed graph
lateral::L # lateral model that holds lateral state, moves along network
vertical::V # vertical model that holds vertical state, independent of each other
clock::Clock # to keep track of simulation time
reader::R # provides the model with dynamic input
writer::W # writes model output
type::T # model type
struct Model{
L <: Lateral,
V <: AbstractLandSurface,
R <: NCReader,
T <: AbstractModelType,
} <: AbstractModel{T}
config::Config # all configuration options
network::Network # connectivity information, directed graph
lateral::L # lateral model that holds lateral state, moves along network
vertical::V # vertical model that holds vertical state, independent of each other
clock::Clock # to keep track of simulation time
reader::R # provides the model with dynamic input
writer::Writer # writes model output
type::T # model type
end
```

Expand Down
67 changes: 32 additions & 35 deletions server/test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ end
end

@testset "Reading and writing NaN values allowed" begin
msg =
(fn = "get_value", name = "vertical.soil.variables.vwc[1]", dest = fill(0.0, 50063))
msg = (fn = "get_value", name = "land.soil.variables.vwc[1]", dest = fill(0.0, 50063))
@test isnan(mean(request(msg)["value"]))
end

Expand All @@ -41,10 +40,10 @@ end
@test request((fn = "get_input_item_count",)) == Dict("input_item_count" => 202)
@test request((fn = "get_output_item_count",)) == Dict("output_item_count" => 202)
to_check = [
"vertical.soil.parameters.nlayers",
"vertical.soil.parameters.theta_r",
"lateral.river.variables.q",
"lateral.river.boundary_conditions.reservoir.variables.outflow",
"land.soil.parameters.nlayers",
"land.soil.parameters.theta_r",
"routing.river_flow.variables.q",
"routing.river_flow.boundary_conditions.reservoir.variables.outflow",
]
retrieved_vars = request((fn = "get_input_var_names",))["input_var_names"]
@test all(x -> x in retrieved_vars, to_check)
Expand All @@ -55,101 +54,99 @@ end
zi_size = 0
vwc_1_size = 0
@testset "variable information and get and set functions" begin
@test request((fn = "get_var_itemsize", name = "lateral.subsurface.variables.ssf")) ==
Dict("var_itemsize" => sizeof(Wflow.Float))
@test request((fn = "get_var_type", name = "vertical.n"))["status"] == "ERROR"
@test request((fn = "get_var_units", name = "vertical.soil.parameters.theta_s")) ==
@test request((
fn = "get_var_itemsize",
name = "routing.subsurface_flow.variables.ssf",
)) == Dict("var_itemsize" => sizeof(Wflow.Float))
@test request((fn = "get_var_type", name = "land.n"))["status"] == "ERROR"
@test request((fn = "get_var_units", name = "land.soil.parameters.theta_s")) ==
Dict("var_units" => "-")
@test request((fn = "get_var_location", name = "lateral.river.variables.q")) ==
@test request((fn = "get_var_location", name = "routing.river_flow.variables.q")) ==
Dict("var_location" => "node")
zi_nbytes =
request((fn = "get_var_nbytes", name = "vertical.soil.variables.zi"))["var_nbytes"]
request((fn = "get_var_nbytes", name = "land.soil.variables.zi"))["var_nbytes"]
@test zi_nbytes == 400504
zi_itemsize =
request((fn = "get_var_itemsize", name = "vertical.soil.variables.zi"))["var_itemsize"]
request((fn = "get_var_itemsize", name = "land.soil.variables.zi"))["var_itemsize"]
zi_size = Int(zi_nbytes / zi_itemsize)
vwc_1_nbytes =
request((fn = "get_var_nbytes", name = "vertical.soil.variables.vwc[1]"))["var_nbytes"]
request((fn = "get_var_nbytes", name = "land.soil.variables.vwc[1]"))["var_nbytes"]
@test vwc_1_nbytes == 400504
vwc_1_itemsize =
request((fn = "get_var_itemsize", name = "vertical.soil.variables.vwc[1]"))["var_itemsize"]
request((fn = "get_var_itemsize", name = "land.soil.variables.vwc[1]"))["var_itemsize"]
vwc_1_size = Int(vwc_1_nbytes / vwc_1_itemsize)
@test request((fn = "get_var_grid", name = "lateral.river.variables.h")) ==
@test request((fn = "get_var_grid", name = "routing.river_flow.variables.h")) ==
Dict("var_grid" => 3)
msg = (fn = "get_value", name = "vertical.soil.variables.zi", dest = fill(0.0, zi_size))
msg = (fn = "get_value", name = "land.soil.variables.zi", dest = fill(0.0, zi_size))
@test mean(request(msg)["value"]) ≈ 277.3620724821974
msg = (fn = "get_value_ptr", name = "vertical.soil.parameters.theta_s")
msg = (fn = "get_value_ptr", name = "land.soil.parameters.theta_s")
@test mean(request(msg)["value_ptr"]) ≈ 0.4409211971535584
msg = (
fn = "get_value_at_indices",
name = "lateral.river.variables.q",
name = "routing.river_flow.variables.q",
dest = [0.0, 0.0, 0.0],
inds = [1, 5, 10],
)
@test request(msg)["value_at_indices"] ≈
[2.1007361866518766, 2.5702292750107687, 3.2904803551115727]
msg =
(fn = "set_value", name = "vertical.soil.variables.zi", src = fill(300.0, zi_size))
msg = (fn = "set_value", name = "land.soil.variables.zi", src = fill(300.0, zi_size))
@test request(msg) == Dict("status" => "OK")
msg = (fn = "get_value", name = "vertical.soil.variables.zi", dest = fill(0.0, zi_size))
msg = (fn = "get_value", name = "land.soil.variables.zi", dest = fill(0.0, zi_size))
@test mean(request(msg)["value"]) == 300.0
msg = (
fn = "set_value_at_indices",
name = "vertical.soil.variables.zi",
name = "land.soil.variables.zi",
src = [250.0, 350.0],
inds = [1, 2],
)
@test request(msg) == Dict("status" => "OK")
msg = (
fn = "get_value_at_indices",
name = "vertical.soil.variables.zi",
name = "land.soil.variables.zi",
dest = [0.0, 0.0, 0.0],
inds = [1, 2, 3],
)
@test request(msg)["value_at_indices"] == [250.0, 350.0, 300.0]
msg = (
fn = "get_value",
name = "vertical.soil.variables.vwc[1]",
name = "land.soil.variables.vwc[1]",
dest = fill(0.0, vwc_1_size),
)
@test mean(request(msg)["value"]) ≈ 0.18600013563085036f0
msg = (
fn = "get_value_at_indices",
name = "vertical.soil.variables.vwc[1]",
name = "land.soil.variables.vwc[1]",
dest = [0.0, 0.0, 0.0],
inds = [1, 2, 3],
)
@test request(msg)["value_at_indices"] ≈
[0.12089607119560242f0, 0.11968416924304527f0, 0.14602328618707333f0]
msg = (
fn = "set_value",
name = "vertical.soil.variables.vwc[1]",
src = fill(0.3, vwc_1_size),
)
msg =
(fn = "set_value", name = "land.soil.variables.vwc[1]", src = fill(0.3, vwc_1_size))
@test request(msg) == Dict("status" => "OK")
msg = (
fn = "get_value",
name = "vertical.soil.variables.vwc[1]",
name = "land.soil.variables.vwc[1]",
dest = fill(0.0, vwc_1_size),
)
@test mean(request(msg)["value"]) ≈ 0.3f0
msg = (
fn = "get_value_at_indices",
name = "vertical.soil.variables.vwc[1]",
name = "land.soil.variables.vwc[1]",
dest = [0.0, 0.0, 0.0],
inds = [1, 2, 3],
)
@test request(msg)["value_at_indices"] == [0.3, 0.3, 0.3]
msg = (
fn = "set_value_at_indices",
name = "vertical.soil.variables.vwc[1]",
name = "land.soil.variables.vwc[1]",
src = [0.1, 0.25],
inds = [1, 2],
)
@test request(msg) == Dict("status" => "OK")
msg = (
fn = "get_value_at_indices",
name = "vertical.soil.variables.vwc[1]",
name = "land.soil.variables.vwc[1]",
dest = [0.0, 0.0],
inds = [1, 2],
)
Expand Down
Loading
Loading