Skip to content

Commit d8c91a2

Browse files
removed much of the nesting, 7 cased fail but no empty initial network anymore
1 parent bf51790 commit d8c91a2

File tree

2 files changed

+197
-174
lines changed

2 files changed

+197
-174
lines changed

hydrolib/core/dflowfm/net/models.py

Lines changed: 148 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -84,49 +84,50 @@ class Mesh2d(BaseModel): #TODO: this is an inconvenient name since meshkernel al
8484

8585
meshkernel: mk.MeshKernel = Field(default_factory=mk.MeshKernel)
8686

87-
mesh2d_node_x: np.ndarray = Field(
88-
default_factory=lambda: np.empty(0, dtype=np.double)
89-
)
90-
mesh2d_node_y: np.ndarray = Field(
91-
default_factory=lambda: np.empty(0, dtype=np.double)
92-
)
93-
mesh2d_node_z: np.ndarray = Field(
94-
default_factory=lambda: np.empty(0, dtype=np.double)
95-
)
96-
97-
mesh2d_edge_x: np.ndarray = Field(
98-
default_factory=lambda: np.empty(0, dtype=np.double)
99-
)
100-
mesh2d_edge_y: np.ndarray = Field(
101-
default_factory=lambda: np.empty(0, dtype=np.double)
102-
)
103-
mesh2d_edge_z: np.ndarray = Field(
104-
default_factory=lambda: np.empty(0, dtype=np.double)
105-
)
106-
mesh2d_edge_nodes: np.ndarray = Field(
107-
default_factory=lambda: np.empty((0, 2), dtype=np.int32)
108-
)
109-
110-
mesh2d_face_x: np.ndarray = Field(
111-
default_factory=lambda: np.empty(0, dtype=np.double)
112-
)
113-
mesh2d_face_y: np.ndarray = Field(
114-
default_factory=lambda: np.empty(0, dtype=np.double)
115-
)
116-
mesh2d_face_z: np.ndarray = Field(
117-
default_factory=lambda: np.empty(0, dtype=np.double)
118-
)
119-
mesh2d_face_nodes: np.ndarray = Field(
120-
default_factory=lambda: np.empty((0, 0), dtype=np.int32)
121-
)
87+
# mesh2d_node_x: np.ndarray = Field(
88+
# default_factory=lambda: np.empty(0, dtype=np.double)
89+
# )
90+
# mesh2d_node_y: np.ndarray = Field(
91+
# default_factory=lambda: np.empty(0, dtype=np.double)
92+
# )
93+
# mesh2d_node_z: np.ndarray = Field(
94+
# default_factory=lambda: np.empty(0, dtype=np.double)
95+
# )
96+
97+
# mesh2d_edge_x: np.ndarray = Field(
98+
# default_factory=lambda: np.empty(0, dtype=np.double)
99+
# )
100+
# mesh2d_edge_y: np.ndarray = Field(
101+
# default_factory=lambda: np.empty(0, dtype=np.double)
102+
# )
103+
# mesh2d_edge_z: np.ndarray = Field(
104+
# default_factory=lambda: np.empty(0, dtype=np.double)
105+
# )
106+
# mesh2d_edge_nodes: np.ndarray = Field(
107+
# default_factory=lambda: np.empty((0, 2), dtype=np.int32)
108+
# )
109+
110+
# mesh2d_face_x: np.ndarray = Field(
111+
# default_factory=lambda: np.empty(0, dtype=np.double)
112+
# )
113+
# mesh2d_face_y: np.ndarray = Field(
114+
# default_factory=lambda: np.empty(0, dtype=np.double)
115+
# )
116+
# mesh2d_face_z: np.ndarray = Field(
117+
# default_factory=lambda: np.empty(0, dtype=np.double)
118+
# )
119+
# mesh2d_face_nodes: np.ndarray = Field(
120+
# default_factory=lambda: np.empty((0, 0), dtype=np.int32)
121+
# )
122122

123123
def is_empty(self) -> bool:
124124
"""Determine whether this Mesh2d is empty.
125125
126126
Returns:
127127
(bool): Whether this Mesh2d is empty.
128128
"""
129-
return self.mesh2d_node_x.size == 0
129+
# return self.mesh2d_node_x.size == 0
130+
return self.get_mesh2d().node_x.size == 0
130131

131132
def read_file(self, file_path: Path) -> None:
132133
"""Read the Mesh2d from the file at file_path.
@@ -137,14 +138,14 @@ def read_file(self, file_path: Path) -> None:
137138
reader = UgridReader(file_path)
138139
reader.read_mesh2d(self)
139140

140-
def _set_mesh2d(self) -> None:
141-
mesh2d = mk.Mesh2d(
142-
node_x=self.mesh2d_node_x,
143-
node_y=self.mesh2d_node_y,
144-
edge_nodes=self.mesh2d_edge_nodes.ravel(),
145-
)
141+
# def _set_mesh2d(self) -> None:
142+
# mesh2d = mk.Mesh2d(
143+
# node_x=self.mesh2d_node_x,
144+
# node_y=self.mesh2d_node_y,
145+
# edge_nodes=self.mesh2d_edge_nodes.ravel(),
146+
# )
146147

147-
self.meshkernel.mesh2d_set(mesh2d)
148+
# self.meshkernel.mesh2d_set(mesh2d)
148149

149150
def get_mesh2d(self) -> mk.Mesh2d:
150151
"""Get the mesh2d as represented in the MeshKernel
@@ -178,13 +179,13 @@ def create_rectilinear(self, extent: tuple, dx: float, dy: float) -> None:
178179
block_size_x=dx,
179180
block_size_y=dy)
180181

181-
mesh2d_input = mk.MeshKernel()
182+
mesh2d_input = self.meshkernel #mk.MeshKernel()
182183
mesh2d_input.curvilinear_compute_rectangular_grid(params)
183184
mesh2d_input.curvilinear_convert_to_mesh2d() #convert to ugrid/mesh2d
184185
mesh2d_input_m2d = mesh2d_input.mesh2d_get() #get Mesh2d object
185186

186187
# mesh2d_input_raw = mk.Mesh2d(mesh2d_input_m2d.node_x, mesh2d_input_m2d.node_y, mesh2d_input_m2d.edge_nodes)
187-
188+
188189
# Process
189190
self._process(mesh2d_input_m2d)
190191

@@ -201,27 +202,29 @@ def create_triangular(self, geometry_list: mk.GeometryList) -> None:
201202
self._process(self.get_mesh2d())
202203

203204
def _process(self, mesh2d_input) -> None:
204-
205-
# Add input
205+
return
206+
207+
# # Add input
206208
# self.meshkernel.mesh2d_set(mesh2d_input) #TODO: in this meshkernel function duplicates the amount of nodes. Seems not desireable, but more testbanks fail if commented.
207-
# Get output
208-
mesh2d_output = mesh2d_input#self.meshkernel.mesh2d_get() #better results for some testcases, comment above and: mesh2d_output = mesh2d_input
209-
210-
# Add to mesh2d variables
211-
self.mesh2d_node_x = mesh2d_output.node_x
212-
self.mesh2d_node_y = mesh2d_output.node_y
213-
214-
self.mesh2d_edge_x = mesh2d_output.edge_x
215-
self.mesh2d_edge_y = mesh2d_output.edge_y
216-
self.mesh2d_edge_nodes = mesh2d_output.edge_nodes.reshape((-1, 2))
217-
218-
self.mesh2d_face_x = mesh2d_output.face_x
219-
self.mesh2d_face_y = mesh2d_output.face_y
220-
npf = mesh2d_output.nodes_per_face
221-
self.mesh2d_face_nodes = np.full(
222-
(len(self.mesh2d_face_x), max(npf)), np.iinfo(np.int32).min
223-
)
224-
#TODO: commented since caused errors in hydromt_delft3dfm
209+
# # Get output
210+
# mesh2d_output = self.meshkernel.mesh2d_get() #better results for some testcases, comment above and: mesh2d_output = mesh2d_input
211+
# # mesh2d_output = mesh2d_input
212+
213+
# # Add to mesh2d variables
214+
# self.mesh2d_node_x = mesh2d_output.node_x
215+
# self.mesh2d_node_y = mesh2d_output.node_y
216+
217+
# self.mesh2d_edge_x = mesh2d_output.edge_x
218+
# self.mesh2d_edge_y = mesh2d_output.edge_y
219+
# self.mesh2d_edge_nodes = mesh2d_output.edge_nodes.reshape((-1, 2))
220+
221+
# self.mesh2d_face_x = mesh2d_output.face_x
222+
# self.mesh2d_face_y = mesh2d_output.face_y
223+
# npf = mesh2d_output.nodes_per_face
224+
# self.mesh2d_face_nodes = np.full(
225+
# (len(self.mesh2d_face_x), max(npf)), np.iinfo(np.int32).min
226+
# )
227+
# #TODO: below causes errors in hydromt_delft3dfm
225228
# idx = (
226229
# np.ones_like(self.mesh2d_face_nodes) * np.arange(max(npf))[None, :]
227230
# ) < npf[:, None]
@@ -241,7 +244,7 @@ def clip(
241244
"""
242245

243246
# Add current mesh to Mesh2d instance
244-
self._set_mesh2d()
247+
# self._set_mesh2d()
245248

246249
# For clipping outside
247250
if not inside:
@@ -308,12 +311,12 @@ def refine(self, polygon: mk.GeometryList, level: int, min_edge_size=10.0):
308311
level (int): Number of refinement steps
309312
"""
310313
# Add current mesh to Mesh2d instance
311-
mesh2d_input = mk.Mesh2d(
312-
node_x=self.mesh2d_node_x,
313-
node_y=self.mesh2d_node_y,
314-
edge_nodes=self.mesh2d_edge_nodes.ravel(),
315-
)
316-
self.meshkernel.mesh2d_set(mesh2d_input)
314+
# mesh2d_input = mk.Mesh2d(
315+
# node_x=self.mesh2d_node_x,
316+
# node_y=self.mesh2d_node_y,
317+
# edge_nodes=self.mesh2d_edge_nodes.ravel(),
318+
# )
319+
# self.meshkernel.mesh2d_set(mesh2d_input)
317320

318321
# Check if parts are closed
319322
# if not (polygon.x_coordinates[0], polygon.y_coordinates[0]) == (
@@ -645,24 +648,25 @@ def _process(self) -> None:
645648
"""
646649
Get links from meshkernel and add to the array with link administration
647650
"""
648-
contacts = self.meshkernel.contacts_get()
649-
650-
self.link1d2d = np.append(
651-
self.link1d2d,
652-
np.stack([contacts.mesh1d_indices, contacts.mesh2d_indices], axis=1),
653-
axis=0,
654-
)
655-
self.link1d2d_contact_type = np.append(
656-
self.link1d2d_contact_type, np.full(contacts.mesh1d_indices.size, 3)
657-
)
658-
self.link1d2d_id = np.append(
659-
self.link1d2d_id,
660-
np.array([f"{n1d:d}_{f2d:d}" for n1d, f2d in self.link1d2d]),
661-
)
662-
self.link1d2d_long_name = np.append(
663-
self.link1d2d_long_name,
664-
np.array([f"{n1d:d}_{f2d:d}" for n1d, f2d in self.link1d2d]),
665-
)
651+
return
652+
# contacts = self.meshkernel.contacts_get()
653+
654+
# self.link1d2d = np.append(
655+
# self.link1d2d,
656+
# np.stack([contacts.mesh1d_indices, contacts.mesh2d_indices], axis=1),
657+
# axis=0,
658+
# )
659+
# self.link1d2d_contact_type = np.append(
660+
# self.link1d2d_contact_type, np.full(contacts.mesh1d_indices.size, 3)
661+
# )
662+
# self.link1d2d_id = np.append(
663+
# self.link1d2d_id,
664+
# np.array([f"{n1d:d}_{f2d:d}" for n1d, f2d in self.link1d2d]),
665+
# )
666+
# self.link1d2d_long_name = np.append(
667+
# self.link1d2d_long_name,
668+
# np.array([f"{n1d:d}_{f2d:d}" for n1d, f2d in self.link1d2d]),
669+
# )
666670

667671
def _link_from_1d_to_2d(
668672
self, node_mask: np.ndarray, polygon: mk.GeometryList = None
@@ -680,6 +684,7 @@ def _link_from_1d_to_2d(
680684

681685
# Computes Mesh1d-Mesh2d contacts, where each single Mesh1d node is connected to one Mesh2d face circumcenter.
682686
# The boundary nodes of Mesh1d (those sharing only one Mesh1d edge) are not connected to any Mesh2d face.
687+
# TODO: new projection_factor seems not to have effect
683688
self.meshkernel.contacts_compute_single(node_mask=node_mask, polygons=polygon, projection_factor=1.0)
684689
self._process()
685690

@@ -788,59 +793,60 @@ def _process_network1d(self) -> None:
788793
Determine x, y locations of mesh1d nodes based on the network1d
789794
"""
790795
# Create a list of coordinates to create the branches from
791-
ngeom = list(zip(self.network1d_geom_x, self.network1d_geom_y))
792-
793-
self.branches.clear()
794-
795-
for i, (name, nnodes) in enumerate(
796-
zip(self.network1d_branch_id, self.network1d_part_node_count)
797-
):
796+
return
797+
# ngeom = list(zip(self.network1d_geom_x, self.network1d_geom_y))
798798

799-
# Create network branch
800-
# Get geometry of branch from network geometry
801-
geometry = np.array([ngeom.pop(0) for _ in range(nnodes)])
802-
# Get branch offsets
803-
idx = self.mesh1d_node_branch_id == i
804-
branch_offsets = self.mesh1d_node_branch_offset[idx]
805-
mask = np.full(branch_offsets.shape, False)
806-
807-
# Determine if a start or end coordinate needs to be added for constructing a complete branch
808-
# As nodes are re-used, the last and first branch_offsets are often missing. However, they are still used
809-
# for determining the length along the discretized branch.
810-
if branch_offsets.size == 0 or not np.isclose(branch_offsets[0], 0.0):
811-
branch_offsets = np.concatenate([[0], branch_offsets])
812-
mask = np.concatenate([[True], mask])
813-
length = np.hypot(*np.diff(geometry, axis=0).T).sum()
814-
if not np.isclose(branch_offsets[-1], length):
815-
branch_offsets = np.concatenate([branch_offsets, [length]])
816-
mask = np.concatenate([mask, [True]])
817-
818-
# Create instance of branch object and add to dictionary
819-
geo_branch = Branch(geometry, branch_offsets=branch_offsets, mask=mask)
820-
self.branches[name.strip()] = geo_branch
821-
822-
# Convert list with all coordinates (except the appended ones for the schematized branches) to arrays
823-
node_x, node_y = np.vstack(
824-
[branch.node_xy[~branch.mask] for branch in self.branches.values()]
825-
).T
799+
# self.branches.clear()
826800

827-
# Add to variables
828-
self.mesh1d_node_x = node_x
829-
self.mesh1d_node_y = node_y
830-
831-
# Calculate edge coordinates
832-
edge_x, edge_y = np.vstack(
833-
[
834-
branch.interpolate(
835-
self.mesh1d_edge_branch_offset[self.mesh1d_edge_branch_id == i]
836-
)
837-
for i, branch in enumerate(self.branches.values())
838-
]
839-
).T
801+
# for i, (name, nnodes) in enumerate(
802+
# zip(self.network1d_branch_id, self.network1d_part_node_count)
803+
# ):
840804

841-
# Add to variables
842-
self.mesh1d_edge_x = edge_x
843-
self.mesh1d_edge_y = edge_y
805+
# # Create network branch
806+
# # Get geometry of branch from network geometry
807+
# geometry = np.array([ngeom.pop(0) for _ in range(nnodes)])
808+
# # Get branch offsets
809+
# idx = self.mesh1d_node_branch_id == i
810+
# branch_offsets = self.mesh1d_node_branch_offset[idx]
811+
# mask = np.full(branch_offsets.shape, False)
812+
813+
# # Determine if a start or end coordinate needs to be added for constructing a complete branch
814+
# # As nodes are re-used, the last and first branch_offsets are often missing. However, they are still used
815+
# # for determining the length along the discretized branch.
816+
# if branch_offsets.size == 0 or not np.isclose(branch_offsets[0], 0.0):
817+
# branch_offsets = np.concatenate([[0], branch_offsets])
818+
# mask = np.concatenate([[True], mask])
819+
# length = np.hypot(*np.diff(geometry, axis=0).T).sum()
820+
# if not np.isclose(branch_offsets[-1], length):
821+
# branch_offsets = np.concatenate([branch_offsets, [length]])
822+
# mask = np.concatenate([mask, [True]])
823+
824+
# # Create instance of branch object and add to dictionary
825+
# geo_branch = Branch(geometry, branch_offsets=branch_offsets, mask=mask)
826+
# self.branches[name.strip()] = geo_branch
827+
828+
# # Convert list with all coordinates (except the appended ones for the schematized branches) to arrays
829+
# node_x, node_y = np.vstack(
830+
# [branch.node_xy[~branch.mask] for branch in self.branches.values()]
831+
# ).T
832+
833+
# # Add to variables
834+
# self.mesh1d_node_x = node_x
835+
# self.mesh1d_node_y = node_y
836+
837+
# # Calculate edge coordinates
838+
# edge_x, edge_y = np.vstack(
839+
# [
840+
# branch.interpolate(
841+
# self.mesh1d_edge_branch_offset[self.mesh1d_edge_branch_id == i]
842+
# )
843+
# for i, branch in enumerate(self.branches.values())
844+
# ]
845+
# ).T
846+
847+
# # Add to variables
848+
# self.mesh1d_edge_x = edge_x
849+
# self.mesh1d_edge_y = edge_y
844850

845851
def _network1d_node_position(self, x: float, y: float) -> Union[np.int32, None]:
846852
"""Determine the position (index) of a x, y coordinate in the network nodes
@@ -1222,6 +1228,8 @@ def mesh1d_add_branch(
12221228
long_name=long_name,
12231229
force_midpoint=force_midpoint,
12241230
)
1231+
self._mesh1d._set_mesh1d()
1232+
12251233
return name
12261234

12271235

0 commit comments

Comments
 (0)