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

Quality of life improvements #488

Merged
merged 7 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.10.5"
manifest_format = "2.0"
project_hash = "acf9296ffbaffd3f335bdb6005badef66b3e10fe"
project_hash = "986b47155507036c06218d23750fe8de28eb001a"

[[deps.AbstractTrees]]
git-tree-sha1 = "2d9c9a55f9c93e8887ad391fbae72f8ef55e1177"
Expand All @@ -11,9 +11,9 @@ version = "0.4.5"

[[deps.Adapt]]
deps = ["LinearAlgebra", "Requires"]
git-tree-sha1 = "6a55b747d1812e699320963ffde36f1ebdda4099"
git-tree-sha1 = "d80af0733c99ea80575f612813fa6aa71022d33a"
uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
version = "4.0.4"
version = "4.1.0"
weakdeps = ["StaticArrays"]

[deps.Adapt.extensions]
Expand Down Expand Up @@ -78,15 +78,15 @@ version = "0.1.6"

[[deps.Blosc_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Lz4_jll", "Zlib_jll", "Zstd_jll"]
git-tree-sha1 = "19b98ee7e3db3b4eff74c5c9c72bf32144e24f10"
git-tree-sha1 = "ef12cdd1c7fb7e1dfd6fa8fd60d4db6bc61d2f23"
uuid = "0b7ba130-8d10-5ba8-a3d6-c5182647fed9"
version = "1.21.5+0"
version = "1.21.6+0"

[[deps.Bzip2_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "9e2a6b69137e6969bab0152632dcb3bc108c8bdd"
git-tree-sha1 = "8873e196c2eb87962a2048b3b8e08946535864a1"
uuid = "6e34b625-4abd-537c-b88f-471c36dfa7a0"
version = "1.0.8+1"
version = "1.0.8+2"

[[deps.CFTime]]
deps = ["Dates", "Printf"]
Expand Down Expand Up @@ -239,9 +239,9 @@ uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[deps.JLLWrappers]]
deps = ["Artifacts", "Preferences"]
git-tree-sha1 = "f389674c99bfcde17dc57454011aa44d5a260a40"
git-tree-sha1 = "be3dc50a92e5a386872a493a10050136d4703f9b"
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
version = "1.6.0"
version = "1.6.1"

[[deps.LRUCache]]
git-tree-sha1 = "b3cc6698599b10e652832c2f23db3cab99d51b59"
Expand Down Expand Up @@ -337,9 +337,9 @@ version = "1.10.0+1"

[[deps.MPICH_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Hwloc_jll", "JLLWrappers", "LazyArtifacts", "Libdl", "MPIPreferences", "TOML"]
git-tree-sha1 = "19d4bd098928a3263693991500d05d74dbdc2004"
git-tree-sha1 = "7715e65c47ba3941c502bffb7f266a41a7f54423"
uuid = "7cb0a576-ebde-5e09-9194-50597f1243b4"
version = "4.2.2+0"
version = "4.2.3+0"

[[deps.MPIPreferences]]
deps = ["Libdl", "Preferences"]
Expand All @@ -349,9 +349,9 @@ version = "0.1.11"

[[deps.MPItrampoline_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "LazyArtifacts", "Libdl", "MPIPreferences", "TOML"]
git-tree-sha1 = "fde81c9f9c94fe5fbeaed7b3f1330305cf9a327c"
git-tree-sha1 = "70e830dab5d0775183c99fc75e4c24c614ed7142"
uuid = "f1f71cc9-e9ae-5b93-9b94-4fe0e1ad3748"
version = "5.5.0+0"
version = "5.5.1+0"

[[deps.MacroTools]]
deps = ["Markdown", "Random"]
Expand Down Expand Up @@ -559,9 +559,9 @@ weakdeps = ["OffsetArrays", "StaticArrays"]

[[deps.StaticArrays]]
deps = ["LinearAlgebra", "PrecompileTools", "Random", "StaticArraysCore"]
git-tree-sha1 = "eeafab08ae20c62c44c8399ccb9354a04b80db50"
git-tree-sha1 = "777657803913ffc7e8cc20f0fd04b634f871af8f"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.9.7"
version = "1.9.8"

[deps.StaticArrays.extensions]
StaticArraysChainRulesCoreExt = "ChainRulesCore"
Expand Down Expand Up @@ -668,9 +668,9 @@ version = "5.11.0+0"

[[deps.libzip_jll]]
deps = ["Artifacts", "Bzip2_jll", "GnuTLS_jll", "JLLWrappers", "Libdl", "XZ_jll", "Zlib_jll", "Zstd_jll"]
git-tree-sha1 = "3282b7d16ae7ac3e57ec2f3fa8fafb564d8f9f7f"
git-tree-sha1 = "668ac0297e6bd8f4d53dfdcd3ace71f2e00f4a35"
uuid = "337d8026-41b4-5cde-a456-74a10e5b31d1"
version = "1.10.1+0"
version = "1.11.1+0"

[[deps.nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
Expand Down
6 changes: 2 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
Polyester = "f517fe37-dbe3-4b94-8317-1923a5111588"
ProgressLogging = "33c8b6b6-d38a-422a-b730-caa89a2f386c"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
TerminalLoggers = "5d786b92-1e48-4d6f-9151-6b4477ca9bed"
UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"

[compat]
Aqua = "0.8"
Expand All @@ -50,15 +48,15 @@ Statistics = "1"
TOML = "1"
TerminalLoggers = "0.1.5"
Test = "1"
UnPack = "1"
julia = "1.6"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Aqua", "Downloads", "Polynomials", "QuadGK", "Test"]
test = ["Aqua", "Downloads", "Polynomials", "QuadGK", "Test", "Random"]
40 changes: 19 additions & 21 deletions src/Wflow.jl
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
module Wflow

using Dates
using TOML
using Graphs
using NCDatasets
using StaticArrays
using Statistics
using UnPack
using Random
using BasicModelInterface
using FieldMetadata
using Parameters
using DelimitedFiles
using ProgressLogging
import BasicModelInterface as BMI

using Base.Threads: nthreads
using CFTime: CFTime, monthday, dayofyear
using Dates: Dates, Second, Minute, Hour, Day, Month, year, TimeType, DatePeriod, TimePeriod, Date, DateTime, now, isleapyear, datetime2unix
using DelimitedFiles: readdlm
using FieldMetadata: @metadata
using Glob: glob
using Graphs: Graphs, Graph, DiGraph, add_edge!, is_cyclic, inneighbors, outneighbors, edges, topological_sort_by_dfs, src, dst, vertices, nv, ne, induced_subgraph, add_vertex!
using IfElse: IfElse
using LoggingExtras
using LoopVectorization: @tturbo
using NCDatasets: NCDatasets, NCDataset, dimnames, dimsize, nomissing, defDim, defVar
using Parameters: @with_kw
using Polyester: @batch
using ProgressLogging: @progress
using StaticArrays: SVector, pushfirst, setindex
using Statistics: mean, median, quantile!
using TerminalLoggers
using CFTime
using Base.Threads
using Glob
using Polyester
using LoopVectorization
using IfElse
using TOML: TOML

@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
const CFDataset = Union{NCDataset,NCDatasets.MFDataset}
const CFVariable_MF = Union{NCDatasets.CFVariable,NCDatasets.MFCFVariable}
Expand Down Expand Up @@ -213,7 +211,7 @@ function run_timestep(model::Model; update_func = update, write_model_output = t
end

function run(model::Model; close_files = true)
@unpack network, config, writer, clock = model
(; config, writer, clock) = model

model_type = config.model.type::String

Expand Down
33 changes: 15 additions & 18 deletions src/bmi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ Update the model for a single timestep.
- `run = nothing`: to update a model partially.
"""
function BMI.update(model::Model; run = nothing)
@unpack clock, network, config = model
if isnothing(run)
model = run_timestep(model)
elseif run == "sbm_until_recharge"
Expand All @@ -64,7 +63,6 @@ function BMI.update(model::Model; run = nothing)
end

function BMI.update_until(model::Model, time::Float64)
@unpack clock, network, config = model
t = BMI.get_current_time(model)
_div, _rem = divrem(time - t, model.clock.dt.value)
steps = Int(_div)
Expand All @@ -85,7 +83,7 @@ end

"Write state output to netCDF and close files."
function BMI.finalize(model::Model)
@unpack config, writer, clock = model
(; config, writer) = model
# it is possible that the state dataset has been closed by `save_state`
if !isnothing(writer.state_dataset) && isopen(writer.state_dataset)
write_netcdf_timestep(model, writer.state_dataset, writer.state_parameters)
Expand All @@ -95,8 +93,7 @@ function BMI.finalize(model::Model)
end

function BMI.get_component_name(model::Model)
@unpack config = model
return config.model.type
return model.config.model.type
end

function BMI.get_input_item_count(model::Model)
Expand All @@ -115,7 +112,7 @@ This `API` sections contains a list of `Model` components for which variables ca
exchanged.
"""
function BMI.get_input_var_names(model::Model)
@unpack config = model
(; config) = model
if haskey(config, "API")
var_names = Vector{String}()
for c in config.API.components
Expand Down Expand Up @@ -211,7 +208,7 @@ function BMI.get_var_location(model::Model, name::String)
end

function BMI.get_current_time(model::Model)
@unpack config = model
(; config) = model
calendar = get(config, "calendar", "standard")::String
starttime = cftime(config.starttime, calendar)
return 0.001 * Dates.value(model.clock.time - starttime)
Expand All @@ -222,7 +219,7 @@ function BMI.get_start_time(model::Model)
end

function BMI.get_end_time(model::Model)
@unpack config = model
(; config) = model
calendar = get(config, "calendar", "standard")::String
starttime = cftime(config.starttime, calendar)
endtime = cftime(config.endtime, calendar)
Expand All @@ -243,7 +240,7 @@ function BMI.get_value(model::Model, name::String, dest::Vector{T}) where {T<:Ab
end

function BMI.get_value_ptr(model::Model, name::String)
@unpack network = model
(; network) = model
s = split(name, "[")
key = symbols(first(s))
if exchange(param(model, key[1:end-1]), key[end]) == 1
Expand Down Expand Up @@ -322,19 +319,19 @@ function BMI.get_grid_rank(model::Model, grid::Int)
end

function BMI.get_grid_x(model::Model, grid::Int, x::Vector{T}) where {T<:AbstractFloat}
@unpack reader, config = model
@unpack dataset = reader
sel = active_indices(model.network, grids[grid])
(; reader, network) = model
(; dataset) = reader
sel = active_indices(network, grids[grid])
inds = [sel[i][1] for i in eachindex(sel)]
x_nc = read_x_axis(dataset)
x .= x_nc[inds]
return x
end

function BMI.get_grid_y(model::Model, grid::Int, y::Vector{T}) where {T<:AbstractFloat}
@unpack reader, config = model
@unpack dataset = reader
sel = active_indices(model.network, grids[grid])
(; reader, network) = model
(; dataset) = reader
sel = active_indices(network, grids[grid])
inds = [sel[i][2] for i in eachindex(sel)]
y_nc = read_y_axis(dataset)
y .= y_nc[inds]
Expand All @@ -350,7 +347,7 @@ function BMI.get_grid_size(model::Model, grid::Int)
end

function BMI.get_grid_edge_count(model::Model, grid::Int)
@unpack network = model
(; network) = model
if grid == 3
return ne(network.river.graph)
elseif grid == 4
Expand All @@ -365,7 +362,7 @@ function BMI.get_grid_edge_count(model::Model, grid::Int)
end

function BMI.get_grid_edge_nodes(model::Model, grid::Int, edge_nodes::Vector{Int})
@unpack network = model
(; network) = model
n = length(edge_nodes)
m = div(n, 2)
# inactive nodes (boundary/ghost points) are set at -999
Expand Down Expand Up @@ -402,7 +399,7 @@ function load_state(model::Model)
end

function save_state(model::Model)
@unpack config, writer, clock = model
(; config, writer) = model
if haskey(config, "state") && haskey(config.state, "path_output")
@info "Write output states to netCDF file `$(model.writer.state_nc_path)`."
end
Expand Down
4 changes: 2 additions & 2 deletions src/flextopo_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ function initialize_flextopo_model(config::Config)
end

function update(model::Model{N,L,V,R,W,T}) where {N,L,V,R,W,T<:FlextopoModel}
@unpack lateral, vertical, network, clock, config = model
(; lateral, vertical, network, config) = model

inds_riv = network.index_river

Expand Down Expand Up @@ -716,7 +716,7 @@ function update(model::Model{N,L,V,R,W,T}) where {N,L,V,R,W,T<:FlextopoModel}
end

function set_states(model::Model{N,L,V,R,W,T}) where {N,L,V,R,W,T<:FlextopoModel}
@unpack lateral, config = model
(; lateral, config) = model
reinit = get(config.model, "reinit", true)::Bool
# read and set states in model object if reinit=true
if reinit == false
Expand Down
Loading
Loading