hydrobricks
Loading...
Searching...
No Matches
Reach.h
1#ifndef HYDROBRICKS_REACH_H
2#define HYDROBRICKS_REACH_H
3
4#include "Includes.h"
5
6class Subbasin;
8
25class Reach {
26 public:
30 enum class Scheme {
31 None, // instantaneous pass-through
32 Lag, // pure translation by length / celerity
33 Muskingum, // Muskingum method with K = length / celerity and the weighting factor X
34 MuskingumCunge // Muskingum with K and X derived from the channel geometry and the discharge
35 };
36
42 explicit Reach(Subbasin* subbasin);
43
44 virtual ~Reach() = default;
45
52 static Scheme SchemeFromString(const string& name);
53
60 static string SchemeToString(Scheme scheme);
61
67 void Initialize();
68
74 void SetChannelRouting(const ChannelRoutingSettings& settings);
75
84 double RouteUpstream(double inflowVolume, double timeStepInDays);
85
95 double RouteLocal(double localVolume, double timeStepInDays);
96
102 [[nodiscard]] bool RoutesLocalRunoff() const {
103 return _routeLocal && _scheme != Scheme::None;
104 }
105
109 void Reset();
110
114 void SaveAsInitialState();
115
121 [[nodiscard]] Subbasin* GetSubbasin() const {
122 return _subbasin;
123 }
124
130 [[nodiscard]] Scheme GetScheme() const {
131 return _scheme;
132 }
133
139 [[nodiscard]] double GetLength() const {
140 return _length;
141 }
142
148 [[nodiscard]] double GetSlope() const {
149 return _slope;
150 }
151
158 [[nodiscard]] double GetWidth() const;
159
166 [[nodiscard]] double GetManning() const;
167
174 [[nodiscard]] double GetCelerity() const;
175
187 [[nodiscard]] double GetCelerityForDischarge(double dischargeM3s) const;
188
195 [[nodiscard]] double GetMuskingumX() const;
196
204 [[nodiscard]] double GetTravelTimeInDays() const;
205
212 [[nodiscard]] int GetSubreachCount() const {
213 return _main.subreaches;
214 }
215
222 [[nodiscard]] double GetInflow() const {
223 return _inflow;
224 }
225
231 [[nodiscard]] double GetOutflow() const {
232 return _outflow;
233 }
234
240 [[nodiscard]] double GetStorage() const {
241 return _main.storage + _local.storage;
242 }
243
244 protected:
251 // Lag scheme: the delivery schedule (slot j: water due j time steps from now) and its ordinates.
252 vecDouble schedule;
253 vecDouble ordinates;
254 vecDouble initialSchedule;
255
256 // Muskingum schemes: the coefficients of the sub-step, the sub-step count and the previous values.
257 int substeps = 1;
258 double c0 = 1, c1 = 0, c2 = 0;
259 bool instantaneous = false; // the travel time is too short for the step: pass-through
260
261 // The sub reaches routed in series (one, unless the Muskingum-Cunge criterion asks for more), each
262 // with its own memory of the previous sub-step and its own share of the volume in transit.
263 int subreaches = 1;
264 vecDouble previousInflow; // m3 per sub-step, per sub reach
265 vecDouble previousOutflow; // m3 per sub-step, per sub reach
266 vecDouble subStorage; // m3 in transit, per sub reach
267 vecDouble initialSubStorage;
268 double subreachTimeStep = -1; // the time step the sub reach count was computed for
269
270 double storage = 0; // m3 in transit, over all the sub reaches
271 double initialStorage = 0;
272
273 // What the ordinates and coefficients were computed for, to recompute them when it changes.
274 double lastTravelTime = -1;
275 double lastX = -1;
276 double lastTimeStep = -1;
277 };
278
279 Subbasin* _subbasin; // non-owning
280 Scheme _scheme = Scheme::None;
281 bool _routeLocal = false;
282 double _length = 0; // m
283 double _slope = 0; // m/m
284
285 // Model-wide parameters (non-owning pointers into the channel routing settings).
286 const float* _celerity = nullptr;
287 const float* _celerityExponent = nullptr;
288 const float* _referenceDischarge = nullptr;
289 const float* _x = nullptr;
290 const float* _width = nullptr;
291 const float* _manning = nullptr;
292
293 // Reach-specific values from the subbasin properties (NaN when absent).
294 double _celerityOverride = NAN_D;
295 double _xOverride = NAN_D;
296 double _widthOverride = NAN_D;
297 double _manningOverride = NAN_D;
298
299 double _inflow = 0; // m3 per time step
300 double _outflow = 0;
301
302 RoutingState _main; // the upstream inflow, over the whole reach
303 RoutingState _local; // the subbasin's own runoff, over half the reach
304
305 private:
315 double RouteBranch(RoutingState& state, double volume, double length, double timeStepInDays);
316
317 double RouteLag(RoutingState& state, double volume, double travelTime, double timeStepInDays);
318
319 double RouteMuskingum(RoutingState& state, double volume, double travelTime, double x, double timeStepInDays);
320
330 void ComputeCungeParameters(double dischargeM3s, double length, double& travelTime, double& x) const;
331
342 [[nodiscard]] int ComputeSubreachCount(double length, double timeStepInDays) const;
343
349 static void SetSubreachCount(RoutingState& state, int count);
350
354 static void ComputeOrdinates(RoutingState& state, double travelTime, double timeStepInDays);
355
360 void ComputeCoefficients(RoutingState& state, double travelTime, double x, double timeStepInDays) const;
361
362 static void ResetState(RoutingState& state);
363};
364
365#endif // HYDROBRICKS_REACH_H
Definition Reach.h:25
Scheme
Definition Reach.h:30
double GetMuskingumX() const
Definition Reach.cpp:152
double RouteLocal(double localVolume, double timeStepInDays)
Definition Reach.cpp:268
double GetCelerityForDischarge(double dischargeM3s) const
Definition Reach.cpp:159
double GetWidth() const
Definition Reach.cpp:131
double GetOutflow() const
Definition Reach.h:231
double GetStorage() const
Definition Reach.h:240
double GetCelerity() const
Definition Reach.cpp:145
static Scheme SchemeFromString(const string &name)
Definition Reach.cpp:27
static string SchemeToString(Scheme scheme)
Definition Reach.cpp:44
bool RoutesLocalRunoff() const
Definition Reach.h:102
void Initialize()
Definition Reach.cpp:57
Scheme GetScheme() const
Definition Reach.h:130
double RouteUpstream(double inflowVolume, double timeStepInDays)
Definition Reach.cpp:261
double GetSlope() const
Definition Reach.h:148
void SaveAsInitialState()
Definition Reach.cpp:489
int GetSubreachCount() const
Definition Reach.h:212
double GetTravelTimeInDays() const
Definition Reach.cpp:186
double GetManning() const
Definition Reach.cpp:138
void Reset()
Definition Reach.cpp:478
Reach(Subbasin *subbasin)
Definition Reach.cpp:22
Subbasin * GetSubbasin() const
Definition Reach.h:121
void SetChannelRouting(const ChannelRoutingSettings &settings)
Definition Reach.cpp:78
double GetLength() const
Definition Reach.h:139
double GetInflow() const
Definition Reach.h:222
Definition Subbasin.h:17
Definition SettingsModel.h:23
Definition Reach.h:250