An error occurred while loading the file. Please try again.
-
Pierre-Antoine Rouby authored
Stricklers and friction.
89b9fd21
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
# River.py -- Pamhyr river model
# Copyright (C) 2023 INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# -*- coding: utf-8 -*-
from tools import flatten
from Model.DB import SQLSubModel
from Model.Network.Node import Node
from Model.Network.Edge import Edge
from Model.Network.Graph import Graph
from Model.Geometry.Profile import Profile
from Model.Geometry.Reach import Reach
from Model.BoundaryCondition.BoundaryConditionList import BoundaryConditionList
from Model.LateralContribution.LateralContributionList import LateralContributionList
from Model.InitialConditions.InitialConditionsDict import InitialConditionsDict
from Model.Stricklers.StricklersList import StricklersList
from Model.Friction.FrictionList import FrictionList
from Model.SolverParameters.SolverParametersList import SolverParametersList
from Model.SedimentLayer.SedimentLayerList import SedimentLayerList
from Solver.Solvers import solver_type_list
class RiverNode(Node, SQLSubModel):
_sub_classes = []
def __init__(self, id:str, name:str,
x:float, y:float,
status = None):
super(RiverNode, self).__init__(
id, name,
x, y,
status = status
)
self._locker = None
@classmethod
def _sql_create(cls, execute):
execute("""
CREATE TABLE river_node(
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
x REAL NOT NULL,
y REAL NOT NULL
)
""")
cls._create_submodel(execute)
return True
@classmethod
def _sql_update(cls, execute, version):
return True
@classmethod
def _sql_load(cls, execute, data = None):
nodes = []
table = execute("SELECT id, name, x, y FROM river_node")
for row in table:
# Update id counter
cls._id_cnt = max(cls._id_cnt, row[0])
# Create new node
nodes.append(cls(*row, status=data["status"]))
return nodes
def _sql_save(self, execute, data = None):
sql = (
"INSERT OR REPLACE INTO river_node(id, name, x, y) VALUES (" +
f"{self.id}, '{self._sql_format(self.name)}', " +
f"{self.x}, {self.y}"+
")"
)
execute(sql)
return True
@property
def locker(self):
return self._locker
@locker.setter
def locker(self, locker):
self._locker = locker
class RiverReach(Edge, SQLSubModel):
_sub_classes = [
Reach,
FrictionList,
]
def __init__(self, id:str, name:str,
node1:RiverNode = None,
node2:RiverNode = None,
status = None):
super(RiverReach, self).__init__(
id, name,
node1, node2,
status = status
)
self._reach = Reach(status=self._status, parent=self)
self._frictions = FrictionList(status = self._status)
@classmethod
def _sql_create(cls, execute):
execute("""
CREATE TABLE river_reach(
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
enable BOOLEAN NOT NULL,
node1 INTEGER,
node2 INTEGER,
FOREIGN KEY(node1) REFERENCES river_node(id),
FOREIGN KEY(node2) REFERENCES river_node(id)
)
""")
cls._create_submodel(execute)
return True
@classmethod
def _sql_update(cls, execute, version):
return cls._update_submodel(execute, version)
@classmethod
def _sql_load(cls, execute, data = None):
reachs = []
if data is None:
data = {}
table = execute("SELECT id, name, enable, node1, node2 FROM river_reach")
for row in table:
# Update id counter
cls._id_cnt = max(cls._id_cnt, row[0])
# Create new reach
id = row[0]
name = row[1]
enable = (row[2] == 1)
# Get nodes corresponding to db foreign key id
node1 = next(filter(lambda n: n.id == row[3], data["nodes"]))
node2 = next(filter(lambda n: n.id == row[4], data["nodes"]))
new = cls(id, name, node1, node2, status = data["status"])
new.enable(enable = enable)
data["reach"] = id
data["parent"] = new
new._reach = Reach._sql_load(execute, data)
new._frictions = FrictionList._sql_load(execute, data)
reachs.append(new)
return reachs
def _sql_save(self, execute, data = None):
sql = (
"INSERT OR REPLACE INTO " +
"river_reach(id, name, enable, node1, node2) "+
"VALUES (" +
f"{self.id}, '{self._sql_format(self._name)}', " +
f"{self._sql_format(self.is_enable())},"
f"{self.node1.id}, {self.node2.id}"+
")"
)
execute(sql)
if data is None:
data = {}
data["reach"] = self
objs = [self._reach, self._frictions]
return self._save_submodel(execute, objs, data)
@property
def reach(self):
return self._reach
@property
def frictions(self):
return self._frictions
class River(Graph, SQLSubModel):
_sub_classes = [
RiverNode,
RiverReach,
BoundaryConditionList,
LateralContributionList,
InitialConditionsDict,
StricklersList,
SolverParametersList,
SedimentLayerList,
]
def __init__(self, status=None):
super(River, self).__init__(status=status)
# Replace Node and Edge ctor by custom ctor
self._node_ctor = RiverNode
self._edge_ctor = RiverReach
self._current_reach = None
self._boundary_condition = BoundaryConditionList(status=self._status)
self._lateral_contribution = LateralContributionList(status=self._status)
self._initial_conditions = InitialConditionsDict(status=self._status)
self._stricklers = StricklersList(status=self._status)
self._parameters = {}
self._sediment_layers = SedimentLayerList(status=self._status)
@classmethod
def _sql_create(cls, execute):
cls._create_submodel(execute)
return True
@classmethod
def _sql_update(cls, execute, version):
cls._update_submodel(execute, version)
return True
@classmethod
def _sql_load(cls, execute, data = None):
new = cls(data["status"])
# Stricklers (Stricklers is load in first because it's needed
# for reachs)
new._stricklers = StricklersList._sql_load(
execute,
data
)
data["stricklers"] = new._stricklers
# Initial conditions
new._sediment_layers = SedimentLayerList._sql_load(
execute,
data
)
data["sediment_layers_list"] = new._sediment_layers
# Network
new._nodes = RiverNode._sql_load(
execute,
data
)
data["nodes"] = new.nodes()
new._edges = RiverReach._sql_load(
execute,
data
)
data["edges"] = new.edges()
# Boundary Condition
new._boundary_condition = BoundaryConditionList._sql_load(
execute,
data
)
# Lateral Contribution
new._lateral_contribution = LateralContributionList._sql_load(
execute,
data
)
# Initial conditions
new._initial_conditions = InitialConditionsDict._sql_load(
execute,
data
)
# Parameters
new._parameters = SolverParametersList._sql_load(
execute,
data
)
return new
def _sql_save(self, execute, data = None):
objs = (self._nodes + self._edges)
objs.append(self._boundary_condition)
objs.append(self._initial_conditions)
objs.append(self._lateral_contribution)
objs.append(self._sediment_layers)
objs.append(self._stricklers)
for solver in self._parameters:
objs.append(self._parameters[solver])
self._save_submodel(execute, objs, data)
return True
@property
def boundary_condition(self):
return self._boundary_condition
@property
def lateral_contribution(self):
return self._lateral_contribution
@property
def initial_conditions(self):
return self._initial_conditions
@property
def sediment_layers(self):
return self._sediment_layers
@property
def stricklers(self):
return self._stricklers
def strickler(self, name):
ret = list(
filter(
lambda s: s.name == name or str(s) == name,
self._stricklers.stricklers
)
)
if len(ret) == 0:
return None
return ret[0]
@property
def parameters(self):
return self._parameters
def get_params(self, solver):
if solver in self._parameters:
return self._parameters[solver]
new = SolverParametersList(
solver_type = solver_type_list[solver],
status = self._status
)
self._parameters[solver] = new
self._status.modified()
return self._parameters[solver]
def has_current_reach(self):
if self.enable_edges_counts() == 1:
return True
return self._current_reach is not None
def current_reach(self):
ee = self.enable_edges()
if len(ee) == 1:
return ee[0]
return self._current_reach
def set_current_reach(self, reach):
self._current_reach = reach
def has_sediment(self):
has = len(self._sediment_layers) != 0
has &= any(
filter(
lambda p: p.sl != None,
flatten(
map(lambda e: e.reach.profiles, self.edges())
)
)
)
return has