-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdike_model_function.py
293 lines (234 loc) · 12.4 KB
/
dike_model_function.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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 31 13:18:05 2017
@author: ciullo
"""
from __future__ import division
from copy import deepcopy
from ema_workbench import ema_logging
import funs_generate_network
from funs_dikes import Lookuplin, dikefailure, init_node
from funs_economy import cost_fun, discount, cost_evacuation
from funs_hydrostat import werklijn_cdf, werklijn_inv
import numpy as np
import pandas as pd
def Muskingum(C1, C2, C3, Qn0_t1, Qn0_t0, Qn1_t0):
''' Simulates hydrological routing '''
Qn1_t1 = C1 * Qn0_t1 + C2 * Qn0_t0 + C3 * Qn1_t0
return Qn1_t1
class DikeNetwork(object):
def __init__(self):
# planning steps
self.num_planning_steps = 3
self.num_events = 30
# load network
G, dike_list, dike_branch, planning_steps = funs_generate_network.get_network(
self.num_planning_steps)
# Load hydrological statistics:
self.A = pd.read_excel('./data/hydrology/werklijn_params.xlsx')
lowQ, highQ = werklijn_inv([0.992, 0.99992], self.A)
self.Qpeaks = np.unique(np.asarray(
[np.random.uniform(lowQ, highQ) / 6 for _ in range(0, self.num_events)]))[::-1]
# Probabiltiy of exceedence for the discharge @ Lobith (i.e. times 6)
self.p_exc = 1 - werklijn_cdf(self.Qpeaks * 6, self.A)
self.G = G
self.dikelist = dike_list
self.dike_branch = dike_branch
self.planning_steps = planning_steps
# Accounting for the discharge reduction due to upstream dike breaches
self.sb = True
# Planning window [y], reasonable for it to be a multiple of num_planning_steps
self.n = 200
# Years in planning step:
self.y_step = self.n//self.num_planning_steps
# Step of dike increase [m]
self.dh = 0.1
# Time step correction: Q is a mean daily value expressed in m3/s
self.timestepcorr = 24 * 60 * 60
# ema_logging.info('model initialized')
# Initialize hydrology at each node:
def _initialize_hydroloads(self, node, time, Q_0):
node['cumVol'], node['wl'], node['Qpol'], node['hbas'] = (
init_node(0, time) for _ in range(4))
node['Qin'], node['Qout'] = (init_node(Q_0, time) for _ in range(2))
node['status'] = init_node(False, time)
node['tbreach'] = np.nan
return node
def _initialize_rfr_ooi(self, G, dikenodes, steps):
for s in steps:
for n in dikenodes:
node = G.nodes[n]
# Create a copy of the rating curve that will be used in the sim:
node['rnew'] = deepcopy(node['r'])
# Initialize outcomes of interest (ooi):
node['losses {}'.format(s)] = []
node['deaths {}'.format(s)] = []
node['evacuation_costs {}'.format(s)] = []
# Initialize room for the river
G.nodes['RfR_projects {}'.format(s)]['cost'] = 0
return G
def progressive_height_and_costs(self, G, dikenodes, steps):
for dike in dikenodes:
node = G.nodes[dike]
# Rescale according to step and tranform in meters
for s in steps:
node['DikeIncrease {}'.format(s)] *= self.dh
# 1 Initialize fragility curve
# 2 Shift it to the degree of dike heigthening:
# 3 Calculate cumulative raising
node['fnew {}'.format(s)] = deepcopy(node['f'])
node['dikeh_cum {}'.format(s)] = 0
for ss in steps[steps <= s]:
node['fnew {}'.format(s)][:, 0] += node['DikeIncrease {}'.format(ss)]
node['dikeh_cum {}'.format(s)] += node['DikeIncrease {}'.format(ss)]
# Calculate dike heigheting costs:
if node['DikeIncrease {}'.format(s)] == 0:
node['dikecosts {}'.format(s)] = 0
else:
node['dikecosts {}'.format(s)] = cost_fun(
node['traj_ratio'],
node['c'],
node['b'],
node['lambda'],
node['dikeh_cum {}'.format(s)],
node['DikeIncrease {}'.format(s)])
def __call__(self, timestep=1, **kwargs):
G = self.G
Qpeaks = self.Qpeaks
dikelist = self.dikelist
# Call RfR initialization:
self._initialize_rfr_ooi(G, dikelist, self.planning_steps)
# Load all kwargs into network. Kwargs are uncertainties and levers:
for item in kwargs:
# when item is 'discount rate':
if 'discount rate' in item:
G.nodes[item]['value'] = kwargs[item]
# the rest of the times you always get a string like {}_{}:
else:
string1, string2 = item.split('_')
if 'RfR' in string2:
# string1: projectID
# string2: rfr #step
# Note: kwargs[item] in this case can be either 0
# (no project) or 1 (yes project)
temporal_step = string2.split(' ')[1]
proj_node = G.nodes['RfR_projects {}'.format(temporal_step)]
# Cost of RfR project
proj_node['cost'] += kwargs[item] * proj_node[string1][
'costs_1e6'] * 1e6
# Iterate over the location affected by the project
for key in proj_node[string1].keys():
if key != 'costs_1e6':
# Change in rating curve due to the RfR project
G.nodes[key]['rnew'][:, 1] -= kwargs[item] * proj_node[
string1][key]
else:
# string1: dikename or EWS
# string2: name of uncertainty or lever
G.nodes[string1][string2] = kwargs[item]
self.progressive_height_and_costs(G, dikelist, self.planning_steps)
# Percentage of people who can be evacuated for a given warning
# time:
G.nodes['EWS']['evacuation_percentage'] = G.nodes['EWS']['evacuees'][
G.nodes['EWS']['DaysToThreat']]
# Dictionary storing outputs:
data = {}
for s in self.planning_steps:
for Qpeak in Qpeaks:
node = G.nodes['A.0']
waveshape_id = node['ID flood wave shape']
time = np.arange(0, node['Qevents_shape'].loc[waveshape_id].shape[0],
timestep)
node['Qout'] = Qpeak * node['Qevents_shape'].loc[waveshape_id]
# Initialize hydrological event:
for key in dikelist:
node = G.nodes[key]
Q_0 = int(G.nodes['A.0']['Qout'][0])
self._initialize_hydroloads(node, time, Q_0)
# Calculate critical water level: water above which failure
# occurs
node['critWL'] = Lookuplin(node['fnew {}'.format(s)], 1, 0, node['pfail'])
# Run the simulation:
# Run over the discharge wave:
for t in range(1, len(time)):
# Run over each node of the branch:
for n in range(0, len(dikelist)):
# Select current node:
node = G.nodes[dikelist[n]]
if node['type'] == 'dike':
# Muskingum parameters:
C1 = node['C1']
C2 = node['C2']
C3 = node['C3']
prec_node = G.nodes[node['prec_node']]
# Evaluate Q coming in a given node at time t:
node['Qin'][t] = Muskingum(C1, C2, C3,
prec_node['Qout'][t],
prec_node['Qout'][t - 1],
node['Qin'][t - 1])
# Transform Q in water levels:
node['wl'][t] = Lookuplin(
node['rnew'], 0, 1, node['Qin'][t])
# Evaluate failure and, in case, Q in the floodplain and
# Q left in the river:
res = dikefailure(self.sb,
node['Qin'][t], node['wl'][t],
node['hbas'][t], node['hground'],
node['status'][t - 1], node['Bmax'],
node['Brate'], time[t],
node['tbreach'], node['critWL'])
node['Qout'][t] = res[0]
node['Qpol'][t] = res[1]
node['status'][t] = res[2]
node['tbreach'] = res[3]
# Evaluate the volume inside the floodplain as the integral
# of Q in time up to time t.
node['cumVol'][t] = np.trapz(
node['Qpol']) * self.timestepcorr
Area = Lookuplin(node['table'], 4, 0, node['wl'][t])
node['hbas'][t] = node['cumVol'][t] / float(Area)
elif node['type'] == 'downstream':
node['Qin'] = G.nodes[dikelist[n - 1]]['Qout']
# Iterate over the network and store outcomes of interest for a
# given event
for dike in self.dikelist:
node = G.nodes[dike]
# If breaches occured:
if node['status'][-1] == True:
# Losses per event:
node['losses {}'.format(s)].append(Lookuplin(node['table'],
6, 4, np.max(node['wl'])))
node['deaths {}'.format(s)].append(Lookuplin(node['table'],
6, 3, np.max(node['wl'])) * (
1 - G.nodes['EWS']['evacuation_percentage']))
node['evacuation_costs {}'.format(s)].append(
cost_evacuation(Lookuplin(
node['table'], 6, 5, np.max(node['wl'])
) * G.nodes['EWS']['evacuation_percentage'],
G.nodes['EWS']['DaysToThreat']))
else:
node['losses {}'.format(s)].append(0)
node['deaths {}'.format(s)].append(0)
node['evacuation_costs {}'.format(s)].append(0)
EECosts = []
# Iterate over the network,compute and store ooi over all events
for dike in dikelist:
node = G.nodes[dike]
# Expected Annual Damage:
EAD = np.trapz(node['losses {}'.format(s)], self.p_exc)
# Discounted annual risk per dike ring:
disc_EAD = np.sum(discount(EAD, rate=G.nodes[
'discount rate {}'.format(s)]['value'], n=self.y_step))
# Expected Annual number of deaths:
END = np.trapz(node['deaths {}'.format(s)], self.p_exc)
# Expected Evacuation costs: depend on the event, the higher
# the event, the more people you have got to evacuate:
EECosts.append(np.trapz(node['evacuation_costs {}'.format(s)], self.p_exc))
data.update({'{}_Expected Annual Damage {}'.format(dike,s): disc_EAD,
'{}_Expected Number of Deaths {}'.format(dike,s): END,
'{}_Dike Investment Costs {}'.format(dike,s
): node['dikecosts {}'.format(s)]})
data.update({'RfR Total Costs {}'.format(s): G.nodes[
'RfR_projects {}'.format(s)]['cost'.format(s)]})
data.update({'Expected Evacuation Costs {}'.format(s): np.sum(EECosts)})
return data