-
Notifications
You must be signed in to change notification settings - Fork 0
/
lisflood.py
594 lines (498 loc) · 22 KB
/
lisflood.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# REMOVE TIMESTEP5 ??
# ADD k AS MODEL PARAMETER
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.axes import Axes
from tqdm.auto import tqdm
from typing import Union, List, Tuple, Dict
from .basemodel import Reservoir
class Lisflood(Reservoir):
"""Representation of a reservoir in the LISFLOOD-OS hydrological model."""
def __init__(self,
Vmin: float,
Vn: float,
Vn_adj: float,
Vf: float,
Vtot: float,
Qmin: float,
Qn: float,
Qf: float,
k: float = 1.2,
At: int = 86400):
"""
Parameters:
-----------
Vmin: float
Volume (m3) associated to the conservative storage
Vn: float
Volume (m3) associated to the normal storage
Vn_adj: float
Volume (m3) associated to the adjusted (calibrated) normal storage
Vf: float
Volume (m3) associated to the flood storage
Vtot: float
Total reservoir storage capacity (m3)
Qmin: float
Minimum outflow (m3/s)
Qn: float
Normal outflow (m3/s)
Qf: float
Non-damaging outflow (m3/s)
At: int
Simulation time step in seconds.
"""
super().__init__(Vmin, Vtot, Qmin, Qf, At)
# storage limits
self.Vn = Vn
self.Vn_adj = Vn_adj
self.Vf = Vf
# outflow limits
self.Qn = Qn
self.k = k
def timestep(self,
I: float,
V: float,
limit_Q: bool = True,
# k: float = 1.2
) -> List[float]:
"""Given an inflow and an initial storage values, it computes the corresponding outflow
Parameters:
-----------
I: float
Inflow (m3/s)
V: float
Volume stored in the reservoir (m3)
limit_Q: bool
Whether to limit the outflow in the flood zone when it exceeds inflow by more than 'k' times
k: float
Release coefficient. If the reservoir is in the flood zone, the outflow is limited to k times the inflow
Returns:
--------
Q, V: List[float]
Outflow (m3/s) and updated storage (m3)
"""
# update reservoir storage with the inflow volume
V += I * self.At
# ouflow depending on the storage level
if V < 2 * self.Vmin:
Q = self.Qmin
elif V < self.Vn:
Q = self.Qmin + (self.Qn - self.Qmin) * (V - 2 * self.Vmin) / (self.Vn - 2 * self.Vmin)
elif V < self.Vn_adj:
Q = self.Qn
elif V < self.Vf:
Q = self.Qn + (self.Qf - self.Qn) * (V - self.Vn_adj) / (self.Vf - self.Vn_adj)
if limit_Q:
if Q > self.k * I:
Q = np.max([self.k * I, self.Qn])
# # Q <= Qf at this storage zone, so this second approach (from the documentation) makes no sense
# Q = np.min([self.Qf, np.max([self.k * I, self.Qn])])
elif V > self.Vf:
Q = np.max([(V - self.Vf) / self.At, np.min([self.Qf, np.max([self.k * I, self.Qn])])])
# limit outflow so the final storage is between 0 and 1
Q = np.max([np.min([Q, (V - self.Vmin) / self.At]), (V - self.Vtot) / self.At])
# update reservoir storage with the outflow volume
V -= Q * self.At
assert 0 <= V, 'The volume at the end of the timestep is negative.'
assert V <= self.Vtot, 'The volume at the end of the timestep is larger than the total reservoir capacity.'
return Q, V
def timestep2(self,
I: float,
V: float,
# k: float = 1
) -> List[float]:
"""Given an inflow and an initial storage values, it computes the corresponding outflow
Parameters:
-----------
I: float
Inflow (m3/s)
V: float
Volume stored in the reservoir (m3)
limit_Q: bool
Whether to limit the outflow in the flood zone when it exceeds inflow by more than 1.2 times
k: float
Release coefficient. If the reservoir is in the flood zone, the outflow is limited to k times the inflow
verbose: bool
Whether to show on screen the evolution
Returns:
--------
Q, V: List[float]
Outflow (m3/s) and updated storage (m3)
"""
# update reservoir storage with the inflow volume
V += I * self.At
# ouflow depending on the storage level
if V < 2 * self.Vmin:
Q = self.Qmin
elif V < self.Vn:
Q = self.Qmin + (self.Qn - self.Qmin) * (V - 2 * self.Vmin) / (self.Vn - 2 * self.Vmin)
elif V < self.Vn_adj:
Q = self.Qn
elif V < self.Vf:
Q = self.Qn + (self.Qf - self.Qn) * (V - self.Vn_adj) / (self.Vf - self.Vn_adj)
elif V > self.Vf:
Q = np.min([(V - self.Vf) / self.At, np.max([self.Qf, self.k * I])])
# limit outflow so the final storage is between 0 and 1
Q = np.max([np.min([Q, (V - self.Vmin) / self.At]), (V - self.Vtot) / self.At])
# update reservoir storage with the outflow volume
V -= Q * self.At
assert 0 <= V, 'The volume at the end of the timestep is negative.'
assert V <= self.Vtot, 'The volume at the end of the timestep is larger than the total reservoir capacity.'
return Q, V
def timestep3(self,
I: float,
V: float,
limit_Q: bool = True,
# k: float = 1.2
) -> List[float]:
"""Given an inflow and an initial storage values, it computes the corresponding outflow
Parameters:
-----------
I: float
Inflow (m3/s)
V: float
Volume stored in the reservoir (m3)
limit_Q: bool
Whether to limit the outflow in the flood zone when it exceeds inflow by more than 'k' times
k: float
Release coefficient. If the reservoir is in the flood zone, the outflow is limited to k times the inflow
Returns:
--------
Q, V: List[float]
Outflow (m3/s) and updated storage (m3)
"""
# update reservoir storage with the inflow volume
V += I * self.At
# ouflow depending on the storage level
if V < 2 * self.Vmin:
Q = self.Qmin
elif V < self.Vn:
Q = self.Qmin + (self.Qn - self.Qmin) * (V - 2 * self.Vmin) / (self.Vn - 2 * self.Vmin)
elif V < self.Vn_adj:
Q = self.Qn
elif V < self.Vf:
Q = self.Qn + (self.Qf - self.Qn) * (V - self.Vn_adj) / (self.Vf - self.Vn_adj)
if limit_Q:
if Q > self.k * I:
Q = np.max([self.k * I, self.Qn])
# # Q <= Qf at this storage zone, so this second approach (from the documentation) makes no sense
# Q = np.min([self.Qf, np.max([self.k * I, self.Qn])])
elif V > self.Vf:
Q = np.min([self.Qf, np.max([self.k * I, self.Qn])])
# limit outflow so the final storage is between 0 and 1
Q = np.max([np.min([Q, (V - self.Vmin) / self.At]), (V - self.Vtot) / self.At])
# update reservoir storage with the outflow volume
V -= Q * self.At
assert 0 <= V, 'The volume at the end of the timestep is negative.'
assert V <= self.Vtot, 'The volume at the end of the timestep is larger than the total reservoir capacity.'
return Q, V
def timestep4(self,
I: float,
V: float,
limit_Q: bool = True,
# k: float = 1.2,
p: float = 3.333
) -> List[float]:
"""Given an inflow and an initial storage values, it computes the corresponding outflow
Parameters:
-----------
I: float
Inflow (m3/s)
V: float
Volume stored in the reservoir (m3)
limit_Q: bool
Whether to limit the outflow in the flood zone when it exceeds inflow by more than 'k' times
k: float
Release coefficient. If the reservoir is in the flood zone, the outflow is limited to k times the inflow
p: float
Factor of Qf that limits the maximum allowed release in case of flooding
Returns:
--------
Q, V: List[float]
Outflow (m3/s) and updated storage (m3)
"""
# update reservoir storage with the inflow volume
V += I * self.At
# ouflow depending on the storage level
if V < 2 * self.Vmin:
Q = self.Qmin
elif V < self.Vn:
Q = self.Qmin + (self.Qn - self.Qmin) * (V - 2 * self.Vmin) / (self.Vn - 2 * self.Vmin)
elif V < self.Vn_adj:
Q = self.Qn
elif V < self.Vf:
Q = self.Qn + (self.Qf - self.Qn) * (V - self.Vn_adj) / (self.Vf - self.Vn_adj)
if limit_Q:
if Q > self.k * I:
Q = np.max([self.k * I, self.Qn])
# # Q <= Qf at this storage zone, so this second approach (from the documentation) makes no sense
# Q = np.min([self.Qf, np.max([self.k * I, self.Qn])])
elif V > self.Vf:
Q = np.max([np.min([(V - self.Vf) / self.At, p * self.Qf]), np.min([self.Qf, np.max([self.k * I, self.Qn])])])
# limit outflow so the final storage is between 0 and 1
Q = np.max([np.min([Q, (V - self.Vmin) / self.At]), (V - self.Vtot) / self.At])
# update reservoir storage with the outflow volume
V -= Q * self.At
assert 0 <= V, 'The volume at the end of the timestep is negative.'
assert V <= self.Vtot, 'The volume at the end of the timestep is larger than the total reservoir capacity.'
return Q, V
def timestep5(self,
I: float,
V: float,
limit_Q: bool = True,
# k: float = 1.2,
tol: float = 1e-6
) -> List[float]:
"""Given an inflow and an initial storage values, it computes the corresponding outflow
Parameters:
-----------
I: float
Inflow (m3/s)
V: float
Volume stored in the reservoir (m3)
limit_Q: bool
Whether to limit the outflow in the flood zone when it exceeds inflow by more than 1.2 times
k: float
Release coefficient. If the reservoir is in the flood zone, the outflow is limited to k times the inflow
tol: float
Returns:
--------
Q, V: List[float]
Outflow (m3/s) and updated storage (m3)
"""
# update reservoir storage with the inflow volume
V += I * self.At
# ouflow depending on the storage level
if V < 2 * self.Vmin:
Q = np.min([self.Qmin, (V - self.Vmin) / self.At])
elif V < self.Vn:
Q = self.Qmin + (self.Qn - self.Qmin) * (V - 2 * self.Vmin) / (self.Vn - 2 * self.Vmin)
elif V < self.Vn_adj:
Q = self.Qn
elif V < self.Vf:
Q = self.Qn + (self.Qf - self.Qn) * (V - self.Vn_adj) / (self.Vf - self.Vn_adj)
if limit_Q:
if Q > self.k * I:
Q = np.max([self.k * I, self.Qn])
elif V > self.Vf:
# Q = np.max([(V - self.Vf - tol * self.Vtot) / self.At, np.min([self.Qf, np.max([1.2 * I, self.Qn])])])
Q = np.max([self.Qf, I])
# limit outflow so the final storage is between 0 and 1
Q = np.max([np.min([Q, (V - self.Vmin) / self.At]), (V - self.Vtot) / self.At])
# Q = np.max([np.min([Q, (V - self.Vmin) / self.At]), I])
# update reservoir storage with the outflow volume
# AV = np.min([Q * self.At, V])
# AV = np.max([AV, V - self.Vtot])
V -= Q * self.At
assert 0 <= V, 'The volume at the end of the timestep is negative.'
assert V <= self.Vtot, 'The volume at the end of the timestep is larger than the total reservoir capacity.'
return Q, V
def timestep6(self,
I: float,
Io: float,
V: float,
limit_Q: bool = True,
# k: float = 1.2
) -> List[float]:
"""Given an inflow and an initial storage values, it computes the corresponding outflow
Parameters:
-----------
I: float
Inflow (m3/s)
Io: float
Inflow threshold (m3/s) that defines the release in case of flooding.
- If I > Io, the flood release is the maximum value between Qf and I / k
- If I <= Io, the flood release is the maximum value between Qf and I * k
V: float
Volume stored in the reservoir (m3)
limit_Q: bool
Whether to limit the outflow in the flood zone when it exceeds inflow by more than 1.2 times
k: float
Release coefficient. If the reservoir is in the flood zone, the outflow is limited to k times the inflow
Returns:
--------
Q, V: List[float]
Outflow (m3/s) and updated storage (m3)
"""
# update reservoir storage with the inflow volume
V += I * self.At
# ouflow depending on the storage level
if V < 2 * self.Vmin:
Q = self.Qmin
elif V < self.Vn:
Q = self.Qmin + (self.Qn - self.Qmin) * (V - 2 * self.Vmin) / (self.Vn - 2 * self.Vmin)
elif V < self.Vn_adj:
Q = self.Qn
elif V < self.Vf:
Q = self.Qn + (self.Qf - self.Qn) * (V - self.Vn_adj) / (self.Vf - self.Vn_adj)
if limit_Q:
if Q > self.k * I:
Q = np.max([self.k * I, self.Qn])
elif V > self.Vf:
if I > Io:
Q = np.max([self.Qf, I / self.k])
else:
Q = np.max([self.Qf, self.k * I])
# limit outflow so the final storage is between 0 and 1
Q = np.max([np.min([Q, (V - self.Vmin) / self.At]), (V - self.Vtot) / self.At])
# update reservoir storage with the outflow volume
V -= Q * self.At
assert 0 <= V, 'The volume at the end of the timestep is negative.'
assert V <= self.Vtot, 'The volume at the end of the timestep is larger than the total reservoir capacity.'
return Q, V
def simulate(self,
inflow: pd.Series,
Vo: float = None,
limit_Q: bool = True,
routine: int = 1,
# k: float = 1
) -> pd.DataFrame:
"""Given a inflow time series (m3/s) and an initial storage (m3), it computes the time series of outflow (m3/s) and storage (m3)
Parameters:
-----------
inflow: pd.Series
Time series of flow coming into the reservoir (m3/s)
Vo: float
Initial value of reservoir storage (m3). If not provided, it is assumed that the normal storage is the initial condition
limit_Q: bool
Whether to limit the outflow in the flood zone when it exceeds inflow by more than 1.2 times
routine: integer
Value from 1 to 6 that defines the version of the LISFLOOD reservoir routine to be used
k: float
Release coefficient. If the reservoir is in the flood zone, the outflow is limited to k times the inflow
Returns:
--------
pd.DataFrame
A table that concatenates the storage, inflow and outflow time series.
"""
if Vo is None:
Vo = self.Qn
routines = {1: self.timestep,
2: self.timestep2,
3: self.timestep3,
4: self.timestep4,
5: self.timestep5,
6: self.timestep6}
storage = pd.Series(index=inflow.index, dtype=float, name='storage')
outflow = pd.Series(index=inflow.index, dtype=float, name='outflow')
# for ts in tqdm(inflow.index):
for ts in inflow.index:
try:
# compute outflow and new storage
if routine == 2:
Q, V = routines[routine](inflow[ts], Vo)#, k=k)
elif routine == 6:
try:
Q, V = routines[routine](inflow[ts], inflow[ts - timedelta(seconds=self.At)], Vo, limit_Q=limit_Q)#, k=k)
except:
Q, V = routines[routine](inflow[ts], inflow[ts], Vo, limit_Q=limit_Q)#, k=k)
else:
Q, V = routines[routine](inflow[ts], Vo, limit_Q=limit_Q)#, k=k)
except Exception as e:
print(ts)
print(e)
return pd.concat((storage, inflow, outflow), axis=1)
storage[ts] = V
outflow[ts] = Q
# update current storage
Vo = V
return pd.concat((storage, inflow, outflow), axis=1)
def routine(self,
V: pd.Series,
I: Union[float, pd.Series]
) -> pd.Series:
"""Given a time series of reservoir storage (m3) and a value or a time series of inflow (m3/s), it computes the ouflow (m3/s). This function is only meant for explanatory purposes; since the volume time series is given, the computed outflow does not update the reservoir storage. If the intention is to simulate the behaviour of the reservoir, refer to the function "simulate"
Parameters:
-----------
V: pd.Series
Time series of reservoir storage (m3)
I: Union[float, pd.Series]
Reservor inflow (m3/s)
Returns:
--------
O: pd.Series
Time series of reservoir outflow (m3/s)
"""
if isinstance(I, float) or isinstance(I, int):
assert I >= 0, '"I" must be a positive value'
I = pd.Series(I, index=V.index)
O1 = V / self.At
O1[O1 > self.Qmin] = self.Qmin
O = O1.copy()
O2 = self.Qmin + (self.Qn - self.Qmin) * (V - 2 * self.Vmin) / (self.Vn - 2 * self.Vmin)
maskV2 = (2 * self.Vmin <= V) & (V < self.Vn)
O[maskV2] = O2[maskV2]
O3 = pd.Series(self.Qn, index=V.index)
maskV3 = (self.Vn <= V) & (V < self.Vn_adj)
O[maskV3] = O3[maskV3]
O4 = self.Qn + (self.Qf - self.Qn) * (V - self.Vn_adj) / (self.Vf - self.Vn_adj)
maskV4 = (self.Vn_adj <= V) & (V < self.Vf)
O[maskV4] = O4[maskV4]
Omax = 1.2 * I
Omax[Omax < self.Qn] = self.Qn
Omax[Omax > self.Qf] = self.Qf
O5 = pd.concat(((V - self.Vf - .01 * self.Vtot) / self.At, Omax), axis=1).max(axis=1)
maskV5 = self.Vf <= V
O[maskV5] = O5[maskV5]
Oreg = I
Oreg[Oreg < self.Qn] = self.Qn
Oreg = pd.concat((O, Oreg), axis=1).min(axis=1)
maskO = (O > 1.2 * I) & (O > self.Qn) & (V < self.Vf)
O[maskO] = Oreg[maskO]
temp = pd.concat((O1, O2, O3, O4, O5, Omax, Oreg), axis=1)
temp.columns = ['O1', 'O2', 'O3', 'O4', 'O5', 'Omax', 'Oreg']
self.O = temp
return O
def plot_routine(self, ax: Axes = None, **kwargs):
"""It creates a plot that explains the reservoir routine.
Parameters:
-----------
ax: Axes
If provided, the plot will be added to the given axes
"""
# dummy storage time series
V = pd.Series(np.linspace(0, self.Vtot + .01, 1000))
# create scatter plot
if ax is None:
fig, ax = plt.subplots(figsize=kwargs.get('figsize', (5, 5)))
# outflow
outflow = self.routine(V, I=self.Qf)
ax.plot(V, outflow, lw=1, c='C0')
# reference storages and outflows
vs = [self.Vmin, 2 * self.Vmin, self.Vn, self.Vn_adj, self.Vf]
qs = [self.Qmin, self.Qmin, self.Qn, self.Qn, self.Qf]
for v, q in zip(vs, qs):
ax.vlines(v, 0, q, color='k', ls=':', lw=.5, zorder=0)
ax.hlines(q, 0, v, color='k', ls=':', lw=.5, zorder=0)
# labels
ax.text(0, self.Qmin, r'$Q_{min}$', ha='left', va='bottom')
ax.text(0, self.Qn, r'$Q_{n,adj}$', ha='left', va='bottom')
ax.text(0, self.Qf, r'$Q_nd$', ha='left', va='bottom')
ax.text(self.Vn, 0, r'$V_n$', rotation=90, ha='right', va='bottom')
ax.text(self.Vn_adj, 0, r'$V_{n,adj}$', rotation=90, ha='right', va='bottom')
ax.text(self.Vf, 0, r'$V_f$', rotation=90, ha='right', va='bottom')
# setup
ax.set(xlim=(0, self.Vtot),
xlabel='storage (hm3)',
ylim=(0, None),
ylabel='outflow (m3/s)')
ax.set_title('LISFLOOD reservoir routine')
def get_params(self) -> Dict:
"""It generates a dictionary with the reservoir parameters
Returns:
--------
params: Dict
A dictionary with the name and value of the reservoir parameters
"""
params = {'Vmin': self.Vmin,
'Vn': self.Vn,
'Vn_adj': self.Vn_adj,
'Vf': self.Vf,
'Vtot': self.Vtot,
'Qmin': self.Qmin,
'Qn': self.Qn,
'Qf': self.Qf,
'k': self.k}
params = {key: float(value) for key, value in params.items()}
return params