Skip to content

Commit

Permalink
feat: Use new contacts_set function from MeshKernel 4.0.2 (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
veenstrajelmer authored Jan 31, 2024
1 parent 825a1b4 commit 3d5fa34
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
18 changes: 8 additions & 10 deletions hydrolib/core/dflowfm/net/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import netCDF4 as nc
import numpy as np
from meshkernel import Contacts

from hydrolib.core.basemodel import BaseModel

Expand Down Expand Up @@ -123,16 +124,13 @@ def read_link1d2d(self, link1d2d: Link1d2d) -> None:
for meshkey, nckey in self._explorer.link1d2d_var_name_mapping.items():
setattr(link1d2d, meshkey, self._read_nc_attribute(ds[nckey]))

# TODO: setting contacts is not possible yet in meshkernel
# https://github.com/Deltares/MeshKernelPy/issues/107
# https://github.com/Deltares/HYDROLIB-core/issues/575
# so misalignment between link1d2d.link1d2d and
# empty _link1d2d.meshkernel.contacts_get().mesh2d_indices
# mesh1d_indices = link1d2d.link1d2d[:,0]
# mesh2d_indices = link1d2d.link1d2d[:,1]
# import meshkernel as mk
# contacts = mk.Contacts(mesh1d_indices=mesh1d_indices, mesh2d_indices=mesh2d_indices)
# link1d2d.meshkernel.contacts_set(contacts)
# set contacts on meshkernel, use .copy() to avoid strided arrays
mesh1d_indices = link1d2d.link1d2d[:, 0].copy()
mesh2d_indices = link1d2d.link1d2d[:, 1].copy()
contacts = Contacts(
mesh1d_indices=mesh1d_indices, mesh2d_indices=mesh2d_indices
)
link1d2d.meshkernel.contacts_set(contacts)

ds.close()

Expand Down
6 changes: 2 additions & 4 deletions tests/dflowfm/test_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,8 @@ def test_create_1d_2d_1d2d():
network2_con_m1d = network2._link1d2d.meshkernel.contacts_get().mesh1d_indices
network2_con_m2d = network2._link1d2d.meshkernel.contacts_get().mesh2d_indices
assert network2_link1d2d.shape == (21, 2)
# TODO: below asserts fail, since the meshkernel contacts are not set upon reading
# https://github.com/Deltares/HYDROLIB-core/issues/575
# assert network2_con_m1d.size == 21
# assert network2_con_m2d.size == 21
assert network2_con_m1d.size == 21
assert network2_con_m2d.size == 21

# plot both networks
import matplotlib.pyplot as plt
Expand Down

0 comments on commit 3d5fa34

Please sign in to comment.