Skip to content

Commit 5dd6826

Browse files
Give (non-type) global constants upper case names (#534)
* Give (non-type) global constants upper case names and fixes issue #535 * Remove parametric type `T` from structs This is always set at Float64. Supporting parametric precision could be part of a separate PR (if we want to support this), including which structs would need type parameters. --------- Co-authored-by: Willem van Verseveld <[email protected]>
1 parent 2dd2ba7 commit 5dd6826

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2901
-2456
lines changed

server/src/bmi_service.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ end
8888
struct GetValue
8989
fn::String
9090
name::String
91-
dest::Vector{Wflow.Float}
91+
dest::Vector{Float64}
9292
end
9393

9494
struct GetValuePtr
@@ -99,21 +99,21 @@ end
9999
struct GetValueAtIndices
100100
fn::String
101101
inds::Vector{Int}
102-
dest::Vector{Wflow.Float}
102+
dest::Vector{Float64}
103103
name::String
104104
end
105105

106106
struct SetValue
107107
fn::String
108108
name::String
109-
src::Vector{Wflow.Float}
109+
src::Vector{Float64}
110110
end
111111

112112
struct SetValueAtIndices
113113
fn::String
114114
inds::Vector{Int}
115115
name::String
116-
src::Vector{Wflow.Float}
116+
src::Vector{Float64}
117117
end
118118

119119
struct GetGridType
@@ -134,13 +134,13 @@ end
134134
struct GetGridX
135135
fn::String
136136
grid::Int
137-
x::Vector{Wflow.Float}
137+
x::Vector{Float64}
138138
end
139139

140140
struct GetGridY
141141
fn::String
142142
grid::Int
143-
y::Vector{Wflow.Float}
143+
y::Vector{Float64}
144144
end
145145

146146
struct GetGridNodeCount

server/src/server.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# map JSON function name to Struct (bmi_service.jl)
2-
const map_structs = Dict(
2+
const MAP_STRUCTS = Dict(
33
"initialize" => Initialize,
44
"get_component_name" => GetComponentName,
55
"get_input_item_count" => GetInputItemCount,
@@ -65,7 +65,7 @@ end
6565

6666
"Validate JSON request against mapped Struct"
6767
function valid_request(json)
68-
for f in fieldnames(map_structs[json.fn])
68+
for f in fieldnames(MAP_STRUCTS[json.fn])
6969
if f keys(json)
7070
return f
7171
break
@@ -154,10 +154,10 @@ function start(port::Int)
154154
json = JSON3.read(req; allow_inf = true)
155155
@info "Received request to run function `$(json.fn)`..."
156156

157-
if haskey(map_structs, json.fn)
157+
if haskey(MAP_STRUCTS, json.fn)
158158
v = valid_request(json)
159159
if isnothing(v)
160-
f = StructTypes.constructfrom(map_structs[json.fn], json)
160+
f = StructTypes.constructfrom(MAP_STRUCTS[json.fn], json)
161161
wflow_bmi(socket, handler, f)
162162
else
163163
err = (

server/test/client.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ zi_size = 0
5959
vwc_1_size = 0
6060
@testset "variable information and get and set functions" begin
6161
@test request((fn = "get_var_itemsize", name = "subsurface_water__volume_flow_rate")) ==
62-
Dict("var_itemsize" => sizeof(Wflow.Float))
62+
Dict("var_itemsize" => sizeof(Float64))
6363
@test request((fn = "get_var_units", name = "river_water__volume_flow_rate")) ==
6464
Dict("var_units" => "m3 s-1")
6565
@test request((fn = "get_var_location", name = "river_water__volume_flow_rate")) ==
@@ -125,15 +125,15 @@ vwc_1_size = 0
125125
name = "soil_layer~1_water__volume_fraction",
126126
dest = fill(0.0, vwc_1_size),
127127
)
128-
@test mean(request(msg)["value"]) 0.18600013563085036f0
128+
@test mean(request(msg)["value"]) 0.18602528294012882
129129
msg = (
130130
fn = "get_value_at_indices",
131131
name = "soil_layer~1_water__volume_fraction",
132132
dest = [0.0, 0.0, 0.0],
133133
inds = [1, 2, 3],
134134
)
135135
@test request(msg)["value_at_indices"]
136-
[0.12089607119560242f0, 0.11968416924304527f0, 0.14602328618707333f0]
136+
[0.12089607119560242, 0.11968416924304527, 0.14602328618707333]
137137
msg = (
138138
fn = "set_value",
139139
name = "soil_layer~1_water__volume_fraction",
@@ -145,7 +145,7 @@ vwc_1_size = 0
145145
name = "soil_layer~1_water__volume_fraction",
146146
dest = fill(0.0, vwc_1_size),
147147
)
148-
@test mean(request(msg)["value"]) 0.3f0
148+
@test mean(request(msg)["value"]) 0.3
149149
msg = (
150150
fn = "get_value_at_indices",
151151
name = "soil_layer~1_water__volume_fraction",

src/Wflow.jl

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ using Statistics: mean, median, quantile!, quantile
5454
using TerminalLoggers
5555
using TOML: TOML
5656

57-
const Float = Float64
5857
const CFDataset = Union{NCDataset, NCDatasets.MFDataset}
5958
const CFVariable_MF = Union{NCDatasets.CFVariable, NCDatasets.MFCFVariable}
60-
const version =
59+
const VERSION =
6160
VersionNumber(TOML.parsefile(joinpath(@__DIR__, "..", "Project.toml"))["version"])
6261

6362
mutable struct Clock{T}
@@ -145,6 +144,16 @@ Base.show(io::IO, ::AbstractModel{T}) where {T} = print(io, "model of type ", T)
145144

146145
include("forcing.jl")
147146
include("parameters.jl")
147+
include("vegetation/rainfall_interception.jl")
148+
include("vegetation/canopy.jl")
149+
include("snow/snow_process.jl")
150+
include("snow/snow.jl")
151+
include("glacier/glacier_process.jl")
152+
include("glacier/glacier.jl")
153+
include("surfacewater/runoff.jl")
154+
include("soil/soil.jl")
155+
include("soil/soil_process.jl")
156+
include("sbm.jl")
148157
include("groundwater/connectivity.jl")
149158
include("groundwater/aquifer.jl")
150159
include("groundwater/boundary_conditions.jl")
@@ -156,16 +165,6 @@ include("routing/surface_kinwave.jl")
156165
include("routing/surface_local_inertial.jl")
157166
include("routing/surface_routing.jl")
158167
include("routing/routing_process.jl")
159-
include("vegetation/rainfall_interception.jl")
160-
include("vegetation/canopy.jl")
161-
include("snow/snow_process.jl")
162-
include("snow/snow.jl")
163-
include("glacier/glacier_process.jl")
164-
include("glacier/glacier.jl")
165-
include("surfacewater/runoff.jl")
166-
include("soil/soil.jl")
167-
include("soil/soil_process.jl")
168-
include("sbm.jl")
169168
include("demand/water_demand.jl")
170169
include("sbm_model.jl")
171170
include("sediment/erosion/erosion_process.jl")
@@ -216,7 +215,7 @@ function run(tomlpath::AbstractString; silent = nothing)
216215
fews_run = get(config, "fews_run", false)::Bool
217216
logger, logfile = init_logger(config; silent)
218217
with_logger(logger) do
219-
@info "Wflow version `v$version`"
218+
@info "Wflow version `v$VERSION`"
220219
# to catch stacktraces in the log file a try-catch is required
221220
try
222221
run(config)

src/bmi.jl

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# Mapping of grid identifier to a key, to get the active indices of the model domain.
55
# See also function active_indices(network, key::AbstractString).
6-
const grids = Dict{Int, String}(
6+
const GRIDS = Dict{Int, String}(
77
0 => "reservoir",
88
1 => "lake",
99
2 => "drain",
@@ -211,11 +211,7 @@ function BMI.get_time_step(model::Model)
211211
return Float64(model.config.time.timestepsecs)
212212
end
213213

214-
function BMI.get_value(
215-
model::Model,
216-
name::String,
217-
dest::Vector{T},
218-
) where {T <: AbstractFloat}
214+
function BMI.get_value(model::Model, name::String, dest::Vector{Float64})
219215
dest .= copy(BMI.get_value_ptr(model, name))
220216
return dest
221217
end
@@ -241,39 +237,34 @@ end
241237
function BMI.get_value_at_indices(
242238
model::Model,
243239
name::String,
244-
dest::Vector{T},
240+
dest::Vector{Float64},
245241
inds::Vector{Int},
246-
) where {T <: AbstractFloat}
242+
)
247243
dest .= BMI.get_value_ptr(model, name)[inds]
248244
return dest
249245
end
250246

251247
"""
252-
BMI.set_value(model::Model, name::String, src::Vector{T}) where T<:AbstractFloat
248+
BMI.set_value(model::Model, name::String, src::Vector{Float64})
253249
254250
Set a model variable `name` to the values in vector `src`, overwriting the current contents.
255251
The type and size of `src` must match the model's internal array.
256252
"""
257-
function BMI.set_value(
258-
model::Model,
259-
name::String,
260-
src::Vector{T},
261-
) where {T <: AbstractFloat}
253+
function BMI.set_value(model::Model, name::String, src::Vector{Float64})
262254
return BMI.get_value_ptr(model, name) .= src
263255
end
264256

265257
"""
266-
BMI.set_value_at_indices(model::Model, name::String, inds::Vector{Int}, src::Vector{T})
267-
where T<:AbstractFloat
258+
BMI.set_value_at_indices(model::Model, name::String, inds::Vector{Int}, src::Vector{Float64})
268259
269260
Set a model variable `name` to the values in vector `src`, at indices `inds`.
270261
"""
271262
function BMI.set_value_at_indices(
272263
model::Model,
273264
name::String,
274265
inds::Vector{Int},
275-
src::Vector{T},
276-
) where {T <: AbstractFloat}
266+
src::Vector{Float64},
267+
)
277268
return BMI.get_value_ptr(model, name)[inds] .= src
278269
end
279270

@@ -295,32 +286,32 @@ function BMI.get_grid_rank(model::Model, grid::Int)
295286
end
296287
end
297288

298-
function BMI.get_grid_x(model::Model, grid::Int, x::Vector{T}) where {T <: AbstractFloat}
289+
function BMI.get_grid_x(model::Model, grid::Int, x::Vector{Float64})
299290
(; reader, network) = model
300291
(; dataset) = reader
301-
sel = active_indices(network, grids[grid])
292+
sel = active_indices(network, GRIDS[grid])
302293
inds = [sel[i][1] for i in eachindex(sel)]
303294
x_nc = read_x_axis(dataset)
304295
x .= x_nc[inds]
305296
return x
306297
end
307298

308-
function BMI.get_grid_y(model::Model, grid::Int, y::Vector{T}) where {T <: AbstractFloat}
299+
function BMI.get_grid_y(model::Model, grid::Int, y::Vector{Float64})
309300
(; reader, network) = model
310301
(; dataset) = reader
311-
sel = active_indices(network, grids[grid])
302+
sel = active_indices(network, GRIDS[grid])
312303
inds = [sel[i][2] for i in eachindex(sel)]
313304
y_nc = read_y_axis(dataset)
314305
y .= y_nc[inds]
315306
return y
316307
end
317308

318309
function BMI.get_grid_node_count(model::Model, grid::Int)
319-
return length(active_indices(model.network, grids[grid]))
310+
return length(active_indices(model.network, GRIDS[grid]))
320311
end
321312

322313
function BMI.get_grid_size(model::Model, grid::Int)
323-
return length(active_indices(model.network, grids[grid]))
314+
return length(active_indices(model.network, GRIDS[grid]))
324315
end
325316

326317
function BMI.get_grid_edge_count(model::Model, grid::Int)

0 commit comments

Comments
 (0)