From ec96e8e553829d80a83682e97f1c715e23dccbd1 Mon Sep 17 00:00:00 2001 From: shartgring <103518935+shartgring@users.noreply.github.com> Date: Fri, 2 Aug 2024 15:42:46 +0200 Subject: [PATCH 01/23] typo in check of crs_type in setup_channels --- hydromt_delft3dfm/dflowfm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hydromt_delft3dfm/dflowfm.py b/hydromt_delft3dfm/dflowfm.py index e6dbb80c..b5bf69a0 100644 --- a/hydromt_delft3dfm/dflowfm.py +++ b/hydromt_delft3dfm/dflowfm.py @@ -337,7 +337,7 @@ def setup_channels( if crosssections_type is None: crosssections_type = "branch" # TODO: maybe assign a specific one for river, like branch_river - assert {crosssections_type}.issubset({"xyzpoints", "branch"}) + assert {crosssections_type}.issubset({"xyz", "point", "branch"}) crosssections = self._setup_crosssections( branches=channels, region=region, From 1ea5c190ccf8e33f113e7a452fa44af2ce9bd188 Mon Sep 17 00:00:00 2001 From: shartgring <103518935+shartgring@users.noreply.github.com> Date: Fri, 2 Aug 2024 16:15:44 +0200 Subject: [PATCH 02/23] typo when writing friction type to mdu --- hydromt_delft3dfm/dflowfm.py | 1 + 1 file changed, 1 insertion(+) diff --git a/hydromt_delft3dfm/dflowfm.py b/hydromt_delft3dfm/dflowfm.py index b5bf69a0..0e38be82 100644 --- a/hydromt_delft3dfm/dflowfm.py +++ b/hydromt_delft3dfm/dflowfm.py @@ -2928,6 +2928,7 @@ def write_config(self) -> None: mdu_fn = Path(join(self.root, self._config_fn)) cwd = os.getcwd() os.chdir(dirname(mdu_fn)) + print(cf_dict) mdu = FMModel(**cf_dict) # add filepath mdu.filepath = mdu_fn From 333ff683f7f7bcbcc4ad211bb6e1b3a2d53d60e8 Mon Sep 17 00:00:00 2001 From: shartgring <103518935+shartgring@users.noreply.github.com> Date: Fri, 2 Aug 2024 16:26:37 +0200 Subject: [PATCH 03/23] fixing typo in writing friction values to mdu --- hydromt_delft3dfm/dflowfm.py | 5 ++--- hydromt_delft3dfm/workflows/mesh.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/hydromt_delft3dfm/dflowfm.py b/hydromt_delft3dfm/dflowfm.py index 0e38be82..f755d6c7 100644 --- a/hydromt_delft3dfm/dflowfm.py +++ b/hydromt_delft3dfm/dflowfm.py @@ -2928,7 +2928,6 @@ def write_config(self) -> None: mdu_fn = Path(join(self.root, self._config_fn)) cwd = os.getcwd() os.chdir(dirname(mdu_fn)) - print(cf_dict) mdu = FMModel(**cf_dict) # add filepath mdu.filepath = mdu_fn @@ -3058,7 +3057,7 @@ def _prepare_inifields(da_dict, da): # update config if friction if "frictype" in self._MAPS[name]: self.set_config( - "physics.UniFrictType", self._MAPS[name]["frictype"] + "physics.UnifFrictType", self._MAPS[name]["frictype"] ) # update config if infiltration if name == "infiltcap": @@ -3071,7 +3070,7 @@ def _prepare_inifields(da_dict, da): # update config if frcition if "frictype" in self._MAPS[name]: self.set_config( - "physics.UniFrictType", self._MAPS[name]["frictype"] + "physics.UnifFrictType", self._MAPS[name]["frictype"] ) # update config if infiltration if name == "infiltcap": diff --git a/hydromt_delft3dfm/workflows/mesh.py b/hydromt_delft3dfm/workflows/mesh.py index eb1d1604..70d210f5 100644 --- a/hydromt_delft3dfm/workflows/mesh.py +++ b/hydromt_delft3dfm/workflows/mesh.py @@ -507,7 +507,7 @@ def links1d2d_add_links_2d_to_1d_embedded( mpgl = GeometryList(*faces2d.T.copy()) idx = np.zeros(len(faces2d), dtype=bool) if isinstance(area, MultiPolygon): - area = [a for a in area] + area = list(area.geoms) else: area = [area] for subarea in area: From 9568ff875a075e4aac625965d514a654414ff734 Mon Sep 17 00:00:00 2001 From: shartgring <103518935+shartgring@users.noreply.github.com> Date: Fri, 2 Aug 2024 16:59:57 +0200 Subject: [PATCH 04/23] fixing keyword, first letter must be lowercas --- hydromt_delft3dfm/dflowfm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hydromt_delft3dfm/dflowfm.py b/hydromt_delft3dfm/dflowfm.py index f755d6c7..73b95ff2 100644 --- a/hydromt_delft3dfm/dflowfm.py +++ b/hydromt_delft3dfm/dflowfm.py @@ -3057,7 +3057,7 @@ def _prepare_inifields(da_dict, da): # update config if friction if "frictype" in self._MAPS[name]: self.set_config( - "physics.UnifFrictType", self._MAPS[name]["frictype"] + "physics.unifFrictType", self._MAPS[name]["frictype"] ) # update config if infiltration if name == "infiltcap": @@ -3070,7 +3070,7 @@ def _prepare_inifields(da_dict, da): # update config if frcition if "frictype" in self._MAPS[name]: self.set_config( - "physics.UnifFrictType", self._MAPS[name]["frictype"] + "physics.unifFrictType", self._MAPS[name]["frictype"] ) # update config if infiltration if name == "infiltcap": From 8195859e65a6797c3b5e5bfcb46c779e9b319ab0 Mon Sep 17 00:00:00 2001 From: shartgring <103518935+shartgring@users.noreply.github.com> Date: Mon, 5 Aug 2024 13:12:33 +0200 Subject: [PATCH 05/23] spacing default set to inf instead of None --- hydromt_delft3dfm/dflowfm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hydromt_delft3dfm/dflowfm.py b/hydromt_delft3dfm/dflowfm.py index 73b95ff2..a907461a 100644 --- a/hydromt_delft3dfm/dflowfm.py +++ b/hydromt_delft3dfm/dflowfm.py @@ -215,7 +215,7 @@ def setup_channels( friction_value: float = 0.023, crosssections_fn: str = None, crosssections_type: str = None, - spacing: int = None, + spacing: float = np.inf, snap_offset: float = 0.0, allow_intersection_snapping: bool = True, ): From 2dbceef50370b9ca07fd30145122179b409b4049 Mon Sep 17 00:00:00 2001 From: shartgring <103518935+shartgring@users.noreply.github.com> Date: Mon, 5 Aug 2024 15:39:06 +0200 Subject: [PATCH 06/23] debugging of 2d_to_1d links --- hydromt_delft3dfm/workflows/mesh.py | 44 ++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/hydromt_delft3dfm/workflows/mesh.py b/hydromt_delft3dfm/workflows/mesh.py index 70d210f5..f99d44c8 100644 --- a/hydromt_delft3dfm/workflows/mesh.py +++ b/hydromt_delft3dfm/workflows/mesh.py @@ -9,6 +9,7 @@ import xarray as xr import xugrid as xu from hydrolib.core.dflowfm import Branch, Network +from hydrolib.core.dflowfm.net.models import split_by from meshkernel import GeometryList from shapely.geometry import ( LineString, @@ -321,12 +322,12 @@ def round_geometry(geometry, rounding_precision: int = 6): return loads(dumps(geometry, rounding_precision=rounding_precision)) -# below function is a copy of functions from hydrolib, to avoid installation error. - - +# The function below is an altered version of the from_polygon method from hydrolib +# hydrolib > dhydamo > geometry > models.py > GeometryList > from_polygon +# hydrolib uses class methods to access the inner_outer_separator; here an object is used def polygon_to_geometrylist(polygon): # Create a list of coordinate lists - cls = GeometryList + geometrylist = GeometryList() # Add exterior x_ext, y_ext = np.array(polygon.exterior.coords[:]).T x_crds = [x_ext] @@ -334,13 +335,29 @@ def polygon_to_geometrylist(polygon): # Add interiors, seperated by inner_outer_separator for interior in polygon.interiors: x_int, y_int = np.array(interior.coords[:]).T - x_crds.append([cls.inner_outer_separator]) + x_crds.append([geometrylist.inner_outer_separator]) x_crds.append(x_int) - y_crds.append([cls.inner_outer_separator]) + y_crds.append([geometrylist.inner_outer_separator]) y_crds.append(y_int) - gl = cls(x_coordinates=np.concatenate(x_crds), y_coordinates=np.concatenate(y_crds)) + gl = GeometryList(x_coordinates=np.concatenate(x_crds), y_coordinates=np.concatenate(y_crds)) return gl +# The function below is an altered version of the from_polygon method from hydrolib +# hydrolib > dhydamo > geometry > models.py > GeometryList > _to_polygon, to_geometry +def polygon_to_geometry(geometrylist): + geometries = [ + geo + for geo in split_by(geometrylist, geometrylist.geometry_separator) + if geo.x_coordinates.size > 0 + ] + polygons = [] + for geometry in geometries: + parts = [ + np.stack([p.x_coordinates, p.y_coordinates], axis=1) + for p in split_by(geometry, geometrylist.inner_outer_separator) + ] + polygons.append(Polygon(shell=parts[0], holes=parts[1:])) + return MultiPolygon(polygons) def links1d2d_add_links_1d_to_2d( mesh: xu.UgridDataset, @@ -533,7 +550,7 @@ def links1d2d_add_links_2d_to_1d_embedded( # Generate links network._link1d2d.meshkernel.contacts_compute_with_points( - node_mask=node_mask, points=multipoint + node_mask=node_mask, polygons=multipoint ) network._link1d2d._process() @@ -586,9 +603,9 @@ def links1d2d_add_links_2d_to_1d_lateral( """ # Initialise hydrolib network object network = mutils.hydrolib_network_from_mesh(mesh) - + print(within) geometrylist = network.meshkernel.mesh2d_get_mesh_boundaries_as_polygons() - mpboundaries = GeometryList(**geometrylist.__dict__).to_geometry() + mpboundaries = polygon_to_geometry(geometrylist) if within is not None: # If a 'within' polygon was provided, get the intersection with # the meshboundaries and convert it to a geometrylist @@ -609,15 +626,14 @@ def links1d2d_add_links_2d_to_1d_lateral( network._link1d2d._link_from_2d_to_1d_lateral( node_mask, polygon=geometrylist, search_radius=max_length ) - # If the provided distance factor was None, # no further selection is needed, all links are kept. - if dist_factor is None: - return + if dist_factor is None or dist_factor == 'None': + link1d2d = mutils.links1d2d_from_hydrolib_network(network) + return link1d2d # Create multilinestring multilinestring = MultiLineString([poly.exterior for poly in mpboundaries.geoms]) - # Find the links that intersect the boundary close to the origin id1d = network._link1d2d.link1d2d[npresent:, 0] id2d = network._link1d2d.link1d2d[npresent:, 1] From ccc33dfe957b46ce3cd98722695b9785cd4c37ad Mon Sep 17 00:00:00 2001 From: shartgring <103518935+shartgring@users.noreply.github.com> Date: Tue, 6 Aug 2024 14:01:15 +0200 Subject: [PATCH 07/23] running pre-commit hooks --- hydromt_delft3dfm/workflows/mesh.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/hydromt_delft3dfm/workflows/mesh.py b/hydromt_delft3dfm/workflows/mesh.py index f99d44c8..7f3e4f84 100644 --- a/hydromt_delft3dfm/workflows/mesh.py +++ b/hydromt_delft3dfm/workflows/mesh.py @@ -339,9 +339,12 @@ def polygon_to_geometrylist(polygon): x_crds.append(x_int) y_crds.append([geometrylist.inner_outer_separator]) y_crds.append(y_int) - gl = GeometryList(x_coordinates=np.concatenate(x_crds), y_coordinates=np.concatenate(y_crds)) + gl = GeometryList( + x_coordinates=np.concatenate(x_crds), y_coordinates=np.concatenate(y_crds) + ) return gl + # The function below is an altered version of the from_polygon method from hydrolib # hydrolib > dhydamo > geometry > models.py > GeometryList > _to_polygon, to_geometry def polygon_to_geometry(geometrylist): @@ -357,7 +360,8 @@ def polygon_to_geometry(geometrylist): for p in split_by(geometry, geometrylist.inner_outer_separator) ] polygons.append(Polygon(shell=parts[0], holes=parts[1:])) - return MultiPolygon(polygons) + return MultiPolygon(polygons) + def links1d2d_add_links_1d_to_2d( mesh: xu.UgridDataset, @@ -628,7 +632,7 @@ def links1d2d_add_links_2d_to_1d_lateral( ) # If the provided distance factor was None, # no further selection is needed, all links are kept. - if dist_factor is None or dist_factor == 'None': + if dist_factor is None or dist_factor == "None": link1d2d = mutils.links1d2d_from_hydrolib_network(network) return link1d2d From ab17892c40d19381b0ae30a24f0fefde02437f2f Mon Sep 17 00:00:00 2001 From: shartgring <103518935+shartgring@users.noreply.github.com> Date: Tue, 6 Aug 2024 14:55:17 +0200 Subject: [PATCH 08/23] fix linting --- hydromt_delft3dfm/workflows/mesh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hydromt_delft3dfm/workflows/mesh.py b/hydromt_delft3dfm/workflows/mesh.py index 7f3e4f84..d234f504 100644 --- a/hydromt_delft3dfm/workflows/mesh.py +++ b/hydromt_delft3dfm/workflows/mesh.py @@ -324,7 +324,7 @@ def round_geometry(geometry, rounding_precision: int = 6): # The function below is an altered version of the from_polygon method from hydrolib # hydrolib > dhydamo > geometry > models.py > GeometryList > from_polygon -# hydrolib uses class methods to access the inner_outer_separator; here an object is used +# hydrolib uses class methods to access inner_outer_separator; here an object is used def polygon_to_geometrylist(polygon): # Create a list of coordinate lists geometrylist = GeometryList() From d52512dea0d4d06b1e801ae3dc97eb071e2a3502 Mon Sep 17 00:00:00 2001 From: Sebastian Hartgring <103518935+shartgring@users.noreply.github.com> Date: Wed, 25 Sep 2024 21:19:30 +0200 Subject: [PATCH 09/23] changing savedir of mesh in write_mesh --- hydromt_delft3dfm/dflowfm.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hydromt_delft3dfm/dflowfm.py b/hydromt_delft3dfm/dflowfm.py index 801afba9..3f218a54 100644 --- a/hydromt_delft3dfm/dflowfm.py +++ b/hydromt_delft3dfm/dflowfm.py @@ -3342,7 +3342,7 @@ def read_mesh(self): def write_mesh(self, write_gui=True): """Write 1D branches and 2D mesh at .""" self._assert_write_mode() - savedir = dirname(join(self.root, self._config_fn)) + savedir = join(self.root, "dflowfm") mesh_filename = "fm_net.nc" # write mesh @@ -3710,8 +3710,8 @@ def set_link1d2d( self._mesh = self._mesh.drop_vars( [ "link1d2d", - "link1d2d_ids", - "link1d2d_long_names", + "link1d2d_id", + "link1d2d_long_name", "link1d2d_contact_type", ] ) From 25de86f8bdd9e492e3f431ba759deabc429850a5 Mon Sep 17 00:00:00 2001 From: Sebastian Hartgring <103518935+shartgring@users.noreply.github.com> Date: Wed, 25 Sep 2024 21:23:52 +0200 Subject: [PATCH 10/23] include split_by from hydrolib core in imports --- hydromt_delft3dfm/workflows/mesh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hydromt_delft3dfm/workflows/mesh.py b/hydromt_delft3dfm/workflows/mesh.py index 677f9fcd..9c03a9ab 100644 --- a/hydromt_delft3dfm/workflows/mesh.py +++ b/hydromt_delft3dfm/workflows/mesh.py @@ -9,6 +9,7 @@ import xarray as xr import xugrid as xu from hydrolib.core.dflowfm import Branch, Network +from hydrolib.core.dflowfm.net.models import split_by from meshkernel import Contacts, GeometryList from shapely.geometry import ( LineString, @@ -426,7 +427,6 @@ def links1d2d_add_links_1d_to_2d( network._link1d2d.meshkernel.contacts_compute_single( node_mask=node_mask, polygons=geometrylist, projection_factor=1.0 ) - # Filter the links that are longer than the max distance id1d = network._link1d2d.link1d2d[npresent:, 0] id2d = network._link1d2d.link1d2d[npresent:, 1] From dafa9cb625d78518aaf27f795a8298e6aaf73c63 Mon Sep 17 00:00:00 2001 From: Sebastian Hartgring <103518935+shartgring@users.noreply.github.com> Date: Wed, 25 Sep 2024 21:46:02 +0200 Subject: [PATCH 11/23] remove dimension dropping 1d2d It is not necessary anymore to drop them as dropping the variables related to the 1d2dlinks already results in the dimensions being dropped --- hydromt_delft3dfm/dflowfm.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/hydromt_delft3dfm/dflowfm.py b/hydromt_delft3dfm/dflowfm.py index 3f218a54..e01e2c00 100644 --- a/hydromt_delft3dfm/dflowfm.py +++ b/hydromt_delft3dfm/dflowfm.py @@ -3715,8 +3715,6 @@ def set_link1d2d( "link1d2d_contact_type", ] ) - # Remove unused dims nLink1D2D_edge and Two - self._mesh = self._mesh.drop_dims(["nLink1D2D_edge", "Two"]) # Add link1d2d to mesh self._mesh = self._mesh.merge(link1d2d) From f05d8cfd658a40608f7250e721655f2f42da233e Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Wed, 25 Sep 2024 21:58:53 +0200 Subject: [PATCH 12/23] enabled mesh test --- tests/test_workflows_mesh.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/test_workflows_mesh.py b/tests/test_workflows_mesh.py index a7dd01a0..513e095b 100644 --- a/tests/test_workflows_mesh.py +++ b/tests/test_workflows_mesh.py @@ -14,16 +14,16 @@ def test_setup_inks1d2d_add_links(tmpdir): model = DFlowFMModel(root=join(EXAMPLEDIR, "dflowfm_piave"), mode="r") model.read() model.set_root(tmpdir, mode="w") - # TODO: uncomment all options below - # ValueError: These variables cannot be found in this dataset: ['link1d2d_long_names', 'link1d2d_ids'] - # model.setup_link1d2d(link_direction= "1d_to_2d", link_type="embedded") - # AttributeError: type object 'GeometryList' has no attribute 'inner_outer_separator' - # model.setup_link1d2d(link_direction= "2d_to_1d", link_type="embedded") - # ValueError: These variables cannot be found in this dataset: ['link1d2d_long_names', 'link1d2d_ids'] - # model.setup_link1d2d(link_direction= "1d_to_2d", link_type="lateral") - # AttributeError: 'GeometryList' object has no attribute 'to_geometry' + + # TODO: make fixture of model? + # TODO: uncomment last case + # TODO: add checks with assertions + + model.setup_link1d2d(link_direction= "1d_to_2d", link_type="embedded") + model.setup_link1d2d(link_direction= "2d_to_1d", link_type="embedded") + model.setup_link1d2d(link_direction= "1d_to_2d", link_type="lateral") # model.setup_link1d2d(link_direction= "2d_to_1d", link_type="lateral") - # add checks with assertions + # much of the validation is done upon writing, so maybe write the result also model.write_mesh() model.write() From 9b95d9653376204cf4954e98d43df8923c2cdc35 Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Wed, 25 Sep 2024 23:12:23 +0200 Subject: [PATCH 13/23] fixed 1d2dlinks in hydrolib-core network (and thus in written network file), including testcase --- hydromt_delft3dfm/mesh_utils.py | 11 ++++---- hydromt_delft3dfm/workflows/mesh.py | 10 +++++--- tests/test_workflows_mesh.py | 39 ++++++++++++++++++++++++----- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/hydromt_delft3dfm/mesh_utils.py b/hydromt_delft3dfm/mesh_utils.py index af56b410..0cb457e1 100644 --- a/hydromt_delft3dfm/mesh_utils.py +++ b/hydromt_delft3dfm/mesh_utils.py @@ -9,6 +9,8 @@ from hydrolib.core.dflowfm import Network from pyproj import CRS from shapely.geometry import LineString +# TODO: maybe move this function here instead of under workflows? +from hydromt_delft3dfm.workflows.mesh import _set_link1d2d logger = logging.getLogger(__name__) @@ -69,13 +71,10 @@ def hydrolib_network_from_mesh( dfm_network._mesh1d._set_mesh1d() # TODO: avoid this private function # dfm_network._mesh1d.meshkernel.mesh1d_set(grids["mesh1d"].mesh) - # add 1d2dlinks - _link1d2d_attrs = dfm_network._link1d2d.__dict__.keys() if "link1d2d" in mesh: - for var, val in mesh.variables.items(): - if var in _link1d2d_attrs: - # use hydrolib-core conventions as it does harmonization when reading. - setattr(dfm_network._link1d2d, var, val.values) + # add 1d2dlinks to network + link1d2d_arr = mesh["link1d2d"].to_numpy() + _set_link1d2d(dfm_network, link1d2d_arr) return dfm_network diff --git a/hydromt_delft3dfm/workflows/mesh.py b/hydromt_delft3dfm/workflows/mesh.py index 9c03a9ab..06cde1cc 100644 --- a/hydromt_delft3dfm/workflows/mesh.py +++ b/hydromt_delft3dfm/workflows/mesh.py @@ -450,14 +450,18 @@ def links1d2d_add_links_1d_to_2d( return link1d2d -def _filter_links_on_idx(network: Network, keep: np.ndarray) -> None: - # Select the remaining links - link1d2d_arr = network._link1d2d.link1d2d[keep] +def _set_link1d2d(network, link1d2d_arr): # set contacts on meshkernel, use .copy() to avoid strided arrays mesh1d_indices = link1d2d_arr[:, 0].copy() mesh2d_indices = link1d2d_arr[:, 1].copy() contacts = Contacts(mesh1d_indices=mesh1d_indices, mesh2d_indices=mesh2d_indices) network._link1d2d.meshkernel.contacts_set(contacts) + + +def _filter_links_on_idx(network: Network, keep: np.ndarray) -> None: + # Select the remaining links + link1d2d_arr = network._link1d2d.link1d2d[keep] + _set_link1d2d(network, link1d2d_arr) def links1d2d_add_links_2d_to_1d_embedded( diff --git a/tests/test_workflows_mesh.py b/tests/test_workflows_mesh.py index 513e095b..141590d6 100644 --- a/tests/test_workflows_mesh.py +++ b/tests/test_workflows_mesh.py @@ -1,9 +1,30 @@ from os.path import abspath, dirname, join from hydromt_delft3dfm import DFlowFMModel +from hydromt_delft3dfm.mesh_utils import hydrolib_network_from_mesh EXAMPLEDIR = join(dirname(abspath(__file__)), "..", "examples") +def test_hydrolib_network_from_mesh(tmpdir): + # from pathlib import Path; tmpdir = Path(r"c:\Users\veenstra\Downloads\hydromt_tmpdir") + model = DFlowFMModel(root=join(EXAMPLEDIR, "dflowfm_piave"), mode="r") + model.read() + model.set_root(tmpdir, mode="w") + + # check if all expected grids and link1d2 are present in mesh + assert set(model.mesh_names) == set(['mesh1d', 'network1d', 'mesh2d']) + assert "link1d2d" in model.mesh.data_vars + assert model.mesh["link1d2d"].shape == (1734,2) + + # check 1d2dlinks are properly converted to hydrolib-core network/link1d2d instance + network = hydrolib_network_from_mesh(model.mesh) + link1d2d_arr = network._link1d2d.link1d2d + assert link1d2d_arr.shape == (1734, 2) + + # call this since it does some checks + model.write_mesh() + + def test_setup_inks1d2d_add_links(tmpdir): """ to increase code coverage, we are not actually doing anything here since all options fail @@ -16,13 +37,19 @@ def test_setup_inks1d2d_add_links(tmpdir): model.set_root(tmpdir, mode="w") # TODO: make fixture of model? - # TODO: uncomment last case - # TODO: add checks with assertions + + # TODO: add checks with assertions, but at the moment nothing much seems to change compared to the initial model + # at least not with respect to 1d2dlinks shape + + # these lines below are still useful to increase code coverage + # at least the functions are executed and raise no errors + model.setup_link1d2d(link_direction="1d_to_2d", link_type="embedded") + model.setup_link1d2d(link_direction="2d_to_1d", link_type="embedded") + model.setup_link1d2d(link_direction="1d_to_2d", link_type="lateral") + # TODO: uncomment this last case, now raises `TypeError: unsupported operand type(s) for -: 'float' and 'Point'` + # (from workflows/mesh.py line 688) + # model.setup_link1d2d(link_direction="2d_to_1d", link_type="lateral") - model.setup_link1d2d(link_direction= "1d_to_2d", link_type="embedded") - model.setup_link1d2d(link_direction= "2d_to_1d", link_type="embedded") - model.setup_link1d2d(link_direction= "1d_to_2d", link_type="lateral") - # model.setup_link1d2d(link_direction= "2d_to_1d", link_type="lateral") # much of the validation is done upon writing, so maybe write the result also model.write_mesh() From 80c39c1a017bdc284fe8869856c54fe482ef76d8 Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Wed, 25 Sep 2024 23:15:42 +0200 Subject: [PATCH 14/23] linting --- hydromt_delft3dfm/mesh_utils.py | 1 + hydromt_delft3dfm/workflows/mesh.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/hydromt_delft3dfm/mesh_utils.py b/hydromt_delft3dfm/mesh_utils.py index 0cb457e1..60d6cdb2 100644 --- a/hydromt_delft3dfm/mesh_utils.py +++ b/hydromt_delft3dfm/mesh_utils.py @@ -9,6 +9,7 @@ from hydrolib.core.dflowfm import Network from pyproj import CRS from shapely.geometry import LineString + # TODO: maybe move this function here instead of under workflows? from hydromt_delft3dfm.workflows.mesh import _set_link1d2d diff --git a/hydromt_delft3dfm/workflows/mesh.py b/hydromt_delft3dfm/workflows/mesh.py index 06cde1cc..192c6d8e 100644 --- a/hydromt_delft3dfm/workflows/mesh.py +++ b/hydromt_delft3dfm/workflows/mesh.py @@ -456,7 +456,7 @@ def _set_link1d2d(network, link1d2d_arr): mesh2d_indices = link1d2d_arr[:, 1].copy() contacts = Contacts(mesh1d_indices=mesh1d_indices, mesh2d_indices=mesh2d_indices) network._link1d2d.meshkernel.contacts_set(contacts) - + def _filter_links_on_idx(network: Network, keep: np.ndarray) -> None: # Select the remaining links From 136d4e46911c9b6cb2a8a4f98a8dfd68b38fc1ae Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Wed, 25 Sep 2024 23:16:57 +0200 Subject: [PATCH 15/23] linting --- tests/test_workflows_mesh.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_workflows_mesh.py b/tests/test_workflows_mesh.py index 141590d6..f57ee57b 100644 --- a/tests/test_workflows_mesh.py +++ b/tests/test_workflows_mesh.py @@ -6,7 +6,6 @@ def test_hydrolib_network_from_mesh(tmpdir): - # from pathlib import Path; tmpdir = Path(r"c:\Users\veenstra\Downloads\hydromt_tmpdir") model = DFlowFMModel(root=join(EXAMPLEDIR, "dflowfm_piave"), mode="r") model.read() model.set_root(tmpdir, mode="w") From 25a0ac2978be9d0f9beb014f67b0b0bd5e5a2659 Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Wed, 25 Sep 2024 23:19:05 +0200 Subject: [PATCH 16/23] add durations --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 7f70015d..29c63f70 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -95,6 +95,7 @@ include = ["hydromt_delft3dfm"] exclude = ["docs", "examples", "envs", "tests", ".github"] [tool.pytest.ini_options] +addopts = "--durations=0" filterwarnings = [ "ignore:distutils Version classes are deprecated:DeprecationWarning", ] From 22131dae231fceee5998f53ae45331de088279d5 Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Wed, 25 Sep 2024 23:22:26 +0200 Subject: [PATCH 17/23] no filterwarning --- pyproject.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 29c63f70..5903eb67 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -96,6 +96,3 @@ exclude = ["docs", "examples", "envs", "tests", ".github"] [tool.pytest.ini_options] addopts = "--durations=0" -filterwarnings = [ - "ignore:distutils Version classes are deprecated:DeprecationWarning", -] From 4989c6838f4bfb2dd136e345e4f137f88067ec6b Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Wed, 25 Sep 2024 23:24:24 +0200 Subject: [PATCH 18/23] no filterwarning --- tests/test_workflows_mesh.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_workflows_mesh.py b/tests/test_workflows_mesh.py index f57ee57b..8eef8aea 100644 --- a/tests/test_workflows_mesh.py +++ b/tests/test_workflows_mesh.py @@ -36,6 +36,7 @@ def test_setup_inks1d2d_add_links(tmpdir): model.set_root(tmpdir, mode="w") # TODO: make fixture of model? + # TODO: this test takes 35 seconds, speed it up # TODO: add checks with assertions, but at the moment nothing much seems to change compared to the initial model # at least not with respect to 1d2dlinks shape From 192e2bdf22bf58337cd33b40065e147ef2817418 Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Wed, 25 Sep 2024 23:33:49 +0200 Subject: [PATCH 19/23] codecov link update --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index ee82b725..6b379826 100644 --- a/README.rst +++ b/README.rst @@ -53,7 +53,7 @@ happy to discuss how it can be implemented for your model. :target: https://github.com/Deltares/hydromt_delft3dfm/actions/workflows/ci.yml .. |codecov| image:: https://codecov.io/gh/Deltares/hydromt_delft3dfm/branch/main/graph/badge.svg?token=ss3EgmwHhH - :target: https://codecov.io/gh/Deltares/hydromt_delft3dfm + :target: https://codecov.io/gh/Deltares/hydromt_delft3dfm?displayType=list .. |black| image:: https://img.shields.io/badge/code%20style-black-000000.svg :alt: Formatter From a59b4240059952f418275b7baa39f201b04fdcaf Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Thu, 26 Sep 2024 11:26:35 +0200 Subject: [PATCH 20/23] increased test coverage --- tests/test_workflows_mesh.py | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/tests/test_workflows_mesh.py b/tests/test_workflows_mesh.py index 8eef8aea..adc3bc70 100644 --- a/tests/test_workflows_mesh.py +++ b/tests/test_workflows_mesh.py @@ -24,33 +24,26 @@ def test_hydrolib_network_from_mesh(tmpdir): model.write_mesh() -def test_setup_inks1d2d_add_links(tmpdir): - """ - to increase code coverage, we are not actually doing anything here since all options fail - nevertheless a convenient setup for a sort of unit test, - so good to keep somewhere and improve in the future - """ +def test_setup_links1d2d_add_links(tmpdir): # Instantiate an empty model - model = DFlowFMModel(root=join(EXAMPLEDIR, "dflowfm_piave"), mode="r") + model = DFlowFMModel(root=join(EXAMPLEDIR, "dflowfm_local"), mode="r") model.read() model.set_root(tmpdir, mode="w") - # TODO: make fixture of model? - # TODO: this test takes 35 seconds, speed it up + # TODO: this test takes >6 seconds, speed it up by making it more unittest-like - # TODO: add checks with assertions, but at the moment nothing much seems to change compared to the initial model - # at least not with respect to 1d2dlinks shape + # TODO: add checks with assertions, but at the moment nothing much seems to change + # compared to the initial model (eg the shape of 1d2dlinks is still (102,2)) # these lines below are still useful to increase code coverage # at least the functions are executed and raise no errors model.setup_link1d2d(link_direction="1d_to_2d", link_type="embedded") model.setup_link1d2d(link_direction="2d_to_1d", link_type="embedded") model.setup_link1d2d(link_direction="1d_to_2d", link_type="lateral") - # TODO: uncomment this last case, now raises `TypeError: unsupported operand type(s) for -: 'float' and 'Point'` - # (from workflows/mesh.py line 688) - # model.setup_link1d2d(link_direction="2d_to_1d", link_type="lateral") + # TODO: below does not work with dist_factor enabled + model.setup_link1d2d(link_direction="2d_to_1d", link_type="lateral", dist_factor="None") - # much of the validation is done upon writing, so maybe write the result also model.write_mesh() - model.write() + # TODO: writing the model fails with "pydantic.v1.error_wrappers.ValidationError: 12 validation errors for StructureModel" + # model.write() From cd95d61647148cffbce00e232c0e584658b6d6a8 Mon Sep 17 00:00:00 2001 From: Sebastian Hartgring <103518935+shartgring@users.noreply.github.com> Date: Thu, 26 Sep 2024 11:41:04 +0200 Subject: [PATCH 21/23] update friction type to be same as in PR 139 --- hydromt_delft3dfm/dflowfm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hydromt_delft3dfm/dflowfm.py b/hydromt_delft3dfm/dflowfm.py index e01e2c00..ae6a41b5 100644 --- a/hydromt_delft3dfm/dflowfm.py +++ b/hydromt_delft3dfm/dflowfm.py @@ -3068,7 +3068,7 @@ def _prepare_inifields(da_dict, da): # update config if frcition if "frictype" in self._MAPS[name]: self.set_config( - "physics.unifFrictType", self._MAPS[name]["frictype"] + "physics.uniffricttype", self._MAPS[name]["frictype"] ) # update config if infiltration if name == "infiltcap": From a6dbf8b81abea1b34872e8f45a834832240d812c Mon Sep 17 00:00:00 2001 From: shartgring <103518935+shartgring@users.noreply.github.com> Date: Thu, 26 Sep 2024 13:51:20 +0200 Subject: [PATCH 22/23] Update changelog.rst --- docs/changelog.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index d048c0fd..c29be6b4 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -14,14 +14,16 @@ This makes the code more robust and future-proof. Added ----- - Setup 1D laterals at branches from points and polygons. (PR #81) +- Adding tests for Mesh workflows. (PR #133) Changed ------- - +- Change default spacing in setup_channels from None to np.inf. (PR #133) Fixed ----- - Bugfixing of reading of frictions (global), crosssections and boundaries when update. (PR #81) - Fixing bug related to changes to pandas TimeDelta formatting, see also https://pandas.pydata.org/docs/whatsnew/v2.2.0.html#other-deprecations. (PR #129) +- Fixing setup_links1d2d for 2d to 1d direction. (PR #133) v0.2.0 (20 November 2023) ========================= From 6e1b214aa8a1c7ba23b7476e0735dab3ec458dcb Mon Sep 17 00:00:00 2001 From: shartgring <103518935+shartgring@users.noreply.github.com> Date: Thu, 26 Sep 2024 13:52:10 +0200 Subject: [PATCH 23/23] Nicer formatting in changelog.rst --- docs/changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index c29be6b4..ac121e2d 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -18,7 +18,7 @@ Added Changed ------- -- Change default spacing in setup_channels from None to np.inf. (PR #133) +- Change default spacing in setup_channels from ``None`` to ``np.inf``. (PR #133) Fixed ----- - Bugfixing of reading of frictions (global), crosssections and boundaries when update. (PR #81)