@@ -56,33 +56,31 @@ Base.@kwdef mutable struct DamNode{P, A<:AbstractFloat} <: NetworkNode
5656 level:: Array{A} = []
5757 discharge:: Array{A} = []
5858 outflow:: Array{A} = []
59-
6059end
6160
6261
6362function DamNode (
6463 name:: String ,
65- area:: Float64 ,
66- max_storage:: Float64 ,
67- storage_coef:: Float64 ,
68- initial_storage:: Float64 ,
64+ area:: F ,
65+ max_storage:: F ,
66+ storage_coef:: F ,
67+ initial_storage:: F ,
6968 calc_dam_level:: Function ,
7069 calc_dam_area:: Function ,
7170 calc_dam_discharge:: Function ,
72- calc_dam_outflow:: Function )
71+ calc_dam_outflow:: Function ) where {F <: Float64 }
7372 return DamNode (name, area, max_storage, storage_coef,
7473 calc_dam_level, calc_dam_area, calc_dam_discharge, calc_dam_outflow,
75- [initial_storage], [], [], [], [], [], [], [])
74+ F [initial_storage], F [], F [], F [], F [], F [], F [], F [])
7675end
7776
78-
7977"""
8078 DamNode(name::String, spec::Dict)
8179
8280Create DamNode from a given specification.
8381"""
84- function DamNode (name:: String , spec:: Dict )
85- n = DamNode {Param} (; name= name, area= spec[" area" ],
82+ function DamNode (name:: String , spec:: Union{ Dict,OrderedDict} )
83+ n = DamNode {Param,Float64 } (; name= name, area= spec[" area" ],
8684 max_storage= spec[" max_storage" ])
8785
8886 node_params = spec[" parameters" ]
@@ -127,8 +125,22 @@ function storage(node::DamNode)
127125 return last (node. storage)
128126end
129127
128+ function prep_state! (node:: DamNode , timesteps:: Int64 )
129+ resize! (node. storage, timesteps+ 1 )
130+ node. storage[2 : end ] .= 0.0
131+
132+ node. effective_rainfall = zeros (timesteps)
133+ node. et = zeros (timesteps)
134+ node. inflow = zeros (timesteps)
135+ node. dam_area = zeros (timesteps)
136+
137+ node. level = zeros (timesteps)
138+ node. discharge = zeros (timesteps)
139+ node. outflow = zeros (timesteps)
140+ end
141+
130142
131- function update_state (node:: DamNode , storage, rainfall, et, area, discharge, outflow)
143+ function update_state! (node:: DamNode , storage, rainfall, et, area, discharge, outflow)
132144 push! (node. storage, storage)
133145 push! (node. effective_rainfall, rainfall)
134146 push! (node. et, et)
@@ -139,6 +151,18 @@ function update_state(node::DamNode, storage, rainfall, et, area, discharge, out
139151
140152 return nothing
141153end
154+ function update_state! (node:: DamNode , ts:: Int64 , storage, rainfall, et, area, discharge, outflow)
155+ node. storage[ts+ 1 ] = storage
156+
157+ node. effective_rainfall[ts] = rainfall
158+ node. et[ts] = et
159+ node. level[ts] = node. calc_dam_level (storage)
160+ node. dam_area[ts] = area
161+ node. discharge[ts] = discharge
162+ node. outflow[ts] = outflow
163+
164+ return nothing
165+ end
142166
143167
144168"""
@@ -172,10 +196,14 @@ end
172196function run_node! (node:: DamNode , climate:: Climate ;
173197 inflow= nothing , extraction= nothing , exchange= nothing )
174198 timesteps = sim_length (climate)
199+ prep_state! (node, timesteps)
200+
175201 for ts in 1 : timesteps
176202 run_node! (node, climate, ts;
177203 inflow= inflow, extraction= extraction, exchange= exchange)
178204 end
205+
206+ return nothing
179207end
180208
181209
@@ -194,23 +222,25 @@ Run a specific node for a specified time step.
194222- `exchange::DataFrame` : Time series of groundwater flux
195223"""
196224function run_node! (node:: DamNode , climate:: Climate , timestep:: Int ;
197- inflow= nothing , extraction= nothing , exchange= nothing )
225+ inflow= nothing , extraction= nothing , exchange= nothing ):: Nothing
198226 ts = timestep
199- if checkbounds (Bool, node. outflow, ts)
200- if node. outflow[ts] != undef
201- # already ran for this time step so no need to run
202- return node. outflow[ts], node. level[ts]
203- end
204- end
227+ # if checkbounds(Bool, node.outflow, ts)
228+ # if node.outflow[ts] != undef
229+ # # already ran for this time step so no need to run
230+ # return node.outflow[ts], node.level[ts]
231+ # end
232+ # end
205233
206234 node_name = node. name
207235 rain, et = climate_values (node, climate, ts)
208236 wo = timestep_value (ts, node_name, " releases" , extraction)
209237 ex = timestep_value (ts, node_name, " exchange" , exchange)
210238 in_flow = timestep_value (ts, node_name, " inflow" , inflow)
211- vol = node. storage[ts]
239+ current_vol = node. storage[ts]
240+
241+ run_node! (node, ts, rain, et, current_vol, in_flow, wo, ex)
212242
213- return run_node! (node, rain, et, vol, in_flow, wo, ex)
243+ return nothing
214244end
215245
216246
@@ -229,23 +259,23 @@ Calculate outflow for the dam node for a single time step.
229259- outflow from dam
230260"""
231261function run_node! (node:: DamNode ,
262+ ts:: Int64 ,
232263 rain:: Float64 ,
233264 et:: Float64 ,
234- vol :: Float64 ,
265+ volume :: Float64 ,
235266 inflow:: Float64 ,
236267 extractions:: Float64 ,
237268 gw_flux:: Float64 )
238- volume = vol
239269 dam_area = node. calc_dam_area (volume)
240270 discharge = node. calc_dam_discharge (volume, node. max_storage)
241271
242272 updated_store = update_volume (volume, inflow, gw_flux, rain, et,
243273 dam_area, extractions, discharge, node. max_storage)
244274 outflow = node. calc_dam_outflow (discharge, extractions)
245275
246- update_state (node, updated_store, rain, et, dam_area, discharge, outflow)
276+ update_state! (node, ts , updated_store, rain, et, dam_area, discharge, outflow)
247277
248- return outflow, level (node)
278+ return nothing
249279end
250280
251281
0 commit comments