Skip to content

Commit 112d0a3

Browse files
Merge pull request #16 from ConnectedSystems/migration
Performance changes and updates for Julia v1.11
2 parents 9fe812b + 536f323 commit 112d0a3

File tree

14 files changed

+442
-390
lines changed

14 files changed

+442
-390
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ docs/site/
2424
Manifest.toml
2525

2626
.vscode/
27+
28+
sandbox

Project.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Streamfall"
22
uuid = "09d4d480-e905-4803-959d-af438f1c2811"
33
authors = ["ConnectedSystems"]
4-
version = "0.1.9"
4+
version = "0.2.0"
55

66
[deps]
77
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
@@ -23,7 +23,6 @@ GR = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71"
2323
GR_jll = "d2c73de3-f751-5644-a686-071e5b155ba9"
2424
GraphPlot = "a2cc645c-3eea-5389-862e-a155d0052231"
2525
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
26-
Infiltrator = "5903a43b-9cc3-4c30-8d17-598619ec4e9b"
2726
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
2827
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
2928
MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
@@ -34,7 +33,6 @@ OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
3433
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
3534
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
3635
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
37-
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
3836
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
3937
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
4038
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
@@ -43,9 +41,8 @@ StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
4341
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
4442
Tar = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
4543
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
46-
Traceur = "37b6cedf-1f77-55f8-9503-c64b63398394"
4744
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
4845
ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"
4946

5047
[compat]
51-
ModelParameters = "0.3.0"
48+
ModelParameters = "0.4.4"

src/Climate.jl

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,50 @@ struct Climate
88
# t_id::Union{String, Nothing} = nothing
99
end
1010

11-
1211
"""
13-
subcatchment_data(node::NetworkNode, climate::Climate)
12+
Extract streamflow data from file.
1413
15-
Extract all data for a given node from climate object.
14+
Streamflow (Q) column is identified the Gauge ID.
15+
16+
Flow data is identified with the suffix `_Q` by default
17+
e.g., ("000001_Q")
18+
19+
# Arguments
20+
- `data` : Observation data
21+
- `gauge_id` : Gauge/Node ID
22+
- `suffix` : Suffix used to indicate flow data (default: "_Q")
23+
24+
# Returns
25+
DataFrame of observations for selected gauge.
1626
"""
17-
function subcatchment_data(node::NetworkNode, climate::Climate)::DataFrame
18-
data = climate.climate_data
19-
cols = filter(x -> occursin(node.name, string(x)), names(data))
27+
@inline function extract_flow(
28+
data::DataFrame, gauge_id::String, suffix::String="_Q"
29+
)::DataFrame
30+
target = data[:, ["Date", gauge_id * suffix]]
31+
rename!(target, gauge_id * suffix => gauge_id)
2032

21-
return data[:, vcat(["Date"], cols)]
33+
return target
2234
end
2335

36+
"""
37+
Create a climate dataset of Precipitation (P) and Potential Evapotranspiration (PET).
38+
Data for multiple gauges may be defined in a single dataset.
39+
40+
P and PET columns are identified by `_P` and `_PET` suffixes by default.
41+
42+
# Arguments
43+
- `data` : Observation data
44+
- `P_suffix` : Suffix used to indicate precipitation (default: "_P")
45+
- `PET_suffix` : Suffix used to indicate Potential Evapotranspiration (default: "_PET")
46+
47+
# Returns
48+
Climate
49+
"""
50+
@inline function extract_climate(
51+
data::DataFrame; P_suffix::String="_P", PET_suffix::String="_PET"
52+
)::Climate
53+
return Climate(data, P_suffix, PET_suffix)
54+
end
2455

2556
"""
2657
rainfall_data(node::NetworkNode, climate::Climate)::DataFrame
@@ -29,27 +60,38 @@ Extract rainfall data for a given node.
2960
"""
3061
function rainfall_data(node::NetworkNode, climate::Climate)::DataFrame
3162
data = climate.climate_data
32-
rain_col = filter(x -> occursin(node.name, x)
33-
& occursin(climate.rainfall_id, x),
63+
rain_col = filter(x -> occursin(node.name, x)
64+
& occursin(climate.rainfall_id, x),
3465
names(data))[1]
3566

3667
return data[:, rain_col]
3768
end
3869

70+
"""
71+
subcatchment_data(node::NetworkNode, climate::Climate)
72+
73+
Extract all data for a given node from climate object.
74+
"""
75+
function subcatchment_data(node::NetworkNode, climate::Climate)::DataFrame
76+
data = climate.climate_data
77+
cols = filter(x -> occursin(node.name, string(x)), names(data))
78+
79+
return data[:, vcat(["Date"], cols)]
80+
end
3981

4082
"""
4183
climate_values(node::NetworkNode, climate::Climate, timestep::Int)
4284
4385
Extract climate related data for a given time step.
4486
"""
45-
function climate_values(node::NetworkNode, climate::Climate,
87+
function climate_values(node::NetworkNode, climate::Climate,
4688
timestep::Int)
4789
node_name::String = node.name
4890
data::DataFrame = climate.climate_data
4991

5092
# TODO : Catch instances where data is not found (raises BoundsError)
51-
rain_col = filter(x -> occursin(node_name, x)
52-
& occursin(climate.rainfall_id, x),
93+
rain_col = filter(x -> occursin(node_name, x)
94+
& occursin(climate.rainfall_id, x),
5395
names(data))[1]
5496
et_col = filter(x -> occursin(node_name, x)
5597
& occursin(climate.et_id, x),
@@ -66,14 +108,14 @@ end
66108
"""
67109
climate_values(node::NetworkNode, climate::Climate)
68110
69-
Extract climate related data for a given time step.
111+
Extract climate related data.
70112
"""
71113
function climate_values(node::NetworkNode, climate::Climate)
72114
data::DataFrame = climate.climate_data
73115

74116
# TODO : Catch instances where data is not found (raises BoundsError)
75-
rain_col = filter(x -> occursin(node.name, x)
76-
& occursin(climate.rainfall_id, x),
117+
rain_col = filter(x -> occursin(node.name, x)
118+
& occursin(climate.rainfall_id, x),
77119
names(data))[1]
78120
et_col = filter(x -> occursin(node.name, x)
79121
& occursin(climate.et_id, x),

src/DamNode.jl

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ function c_dam_outflow(discharge, irrigation_extraction)
2626
end
2727

2828

29-
Base.@kwdef mutable struct DamNode{P} <: NetworkNode
30-
@network_node
29+
Base.@kwdef mutable struct DamNode{P, A<:AbstractFloat} <: NetworkNode
30+
name::String
31+
area::A
3132

32-
max_storage::Float64
33+
max_storage::A
3334
storage_coef::P = Param(0.5, bounds=(0.00001, 10.0))
3435

3536

@@ -45,16 +46,16 @@ Base.@kwdef mutable struct DamNode{P} <: NetworkNode
4546
# Function to calculate outflow from dam
4647
calc_dam_outflow::Function = c_dam_outflow
4748

48-
storage::Array{Float64} = [0.0]
49+
storage::Array{A} = [0.0]
4950

50-
effective_rainfall::Array{Float64} = []
51-
et::Array{Float64} = []
52-
inflow::Array{Float64} = []
53-
dam_area::Array{Float64} = []
51+
effective_rainfall::Array{A} = []
52+
et::Array{A} = []
53+
inflow::Array{A} = []
54+
dam_area::Array{A} = []
5455

55-
level::Array{Float64} = []
56-
discharge::Array{Float64} = []
57-
outflow::Array{Float64} = []
56+
level::Array{A} = []
57+
discharge::Array{A} = []
58+
outflow::Array{A} = []
5859

5960
end
6061

@@ -161,25 +162,25 @@ Update dam volume for timestep.
161162
volume of water stored in dam
162163
"""
163164
function update_volume(volume, node_inflow, gamma, rain, evap, area, extractions, discharge, max_store)::Float64
164-
165+
165166
vol = volume + (node_inflow + gamma) + (rain - evap) * area - extractions - discharge
166167

167168
return max(0.0, min(max_store, vol))
168169
end
169170

170171

171-
function run_node!(node::DamNode, climate::Climate;
172+
function run_node!(node::DamNode, climate::Climate;
172173
inflow=nothing, extraction=nothing, exchange=nothing)
173174
timesteps = sim_length(climate)
174175
for ts in 1:timesteps
175-
run_node!(node, climate, ts;
176+
run_node!(node, climate, ts;
176177
inflow=inflow, extraction=extraction, exchange=exchange)
177178
end
178179
end
179180

180181

181182
"""
182-
run_node!(node::DamNode, climate::Climate, timestep::Int;
183+
run_node!(node::DamNode, climate::Climate, timestep::Int;
183184
inflow=nothing, extraction=nothing, exchange=nothing)
184185
185186
Run a specific node for a specified time step.
@@ -192,7 +193,7 @@ Run a specific node for a specified time step.
192193
- `extraction::DataFrame` : Time series of water orders (expects column of `_releases`)
193194
- `exchange::DataFrame` : Time series of groundwater flux
194195
"""
195-
function run_node!(node::DamNode, climate::Climate, timestep::Int;
196+
function run_node!(node::DamNode, climate::Climate, timestep::Int;
196197
inflow=nothing, extraction=nothing, exchange=nothing)
197198
ts = timestep
198199
if checkbounds(Bool, node.outflow, ts)
@@ -220,14 +221,14 @@ Calculate outflow for the dam node for a single time step.
220221
- node : DamNode
221222
- rain : rainfall in mm
222223
- et : evapotranspiration data in mm
223-
- irrig_ext : irrigation extractions
224+
- irrig_ext : irrigation extractions
224225
- extractions : extraction data in ML
225226
- gw_flux : groundwater interaction
226227
227228
# Returns
228229
- outflow from dam
229230
"""
230-
function run_node!(node::DamNode,
231+
function run_node!(node::DamNode,
231232
rain::Float64,
232233
et::Float64,
233234
vol::Float64,

src/EnsembleNode.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ using Statistics
66
abstract type EnsembleNode <: NetworkNode end
77

88

9-
Base.@kwdef mutable struct BaseEnsemble{N<:NetworkNode, P, R<:Real} <: EnsembleNode
10-
@network_node
9+
Base.@kwdef mutable struct BaseEnsemble{N<:NetworkNode, P, A<:Real} <: EnsembleNode
10+
name::String
11+
area::A
1112

1213
instances::Array{N} = []
1314
weights::Array{P} = [
@@ -17,7 +18,7 @@ Base.@kwdef mutable struct BaseEnsemble{N<:NetworkNode, P, R<:Real} <: EnsembleN
1718
# Default to weighted sum
1819
comb_method::Function = (X, weights) -> sum([x * w for (x, w) in zip(X, weights)])
1920

20-
outflow::Array{R} = []
21+
outflow::Array{A} = []
2122
end
2223

2324

0 commit comments

Comments
 (0)