Skip to content

Commit

Permalink
Merge branch 'master' into margriet_375_schema_300_2d_1d2d
Browse files Browse the repository at this point in the history
  • Loading branch information
margrietpalm committed Sep 9, 2024
2 parents eb2d7d7 + ee78533 commit ecebf54
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 26 deletions.
8 changes: 7 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ Changelog of threedigrid-builder
================================


1.17.2 (unreleased)
1.18.1 (unreleased)
-------------------

- Adapt for changes in schema upgrade for 2d and 1d2d


1.18.0 (2024-09-09)
-------------------

- Support 225 schema migration (boundary conditions and laterals).


1.17.1 (2024-09-02)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion threedigrid_builder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
from .exceptions import * # NOQA

# fmt: off
__version__ = '1.17.2.dev0'
__version__ = '1.18.1.dev0'
# fmt: on
20 changes: 10 additions & 10 deletions threedigrid_builder/grid/boundary_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

class BoundaryCondition1D:
id: int
boundary_type: BoundaryType
type: BoundaryType
connection_node_id: int


Expand All @@ -35,7 +35,7 @@ def apply(self, grid):
Fields on nodes/lines that have a boundary condition be adjusted:
- nodes.calculation_type: set (overridden) to BOUNDARY_NODE
- nodes.boundary_id: from BoundaryConditions1D.id
- nodes.boundary_type: from BoundaryConditions1D.boundary_type
- nodes.boundary_type: from BoundaryConditions1D.type
- nodes.node_type: set to NODE_1D_BOUNDARIES
- lines.boundary_id: from BoundaryConditions1D.id
Expand Down Expand Up @@ -89,23 +89,23 @@ def apply(self, grid):
# set node attributes
grid.nodes.calculation_type[idx] = CalculationType.BOUNDARY_NODE
grid.nodes.boundary_id[idx] = self.id
grid.nodes.boundary_type[idx] = self.boundary_type
grid.nodes.boundary_type[idx] = self.type
grid.nodes.node_type[idx] = NodeType.NODE_1D_BOUNDARIES
# set line attributes
grid.lines.is_1d_boundary[line_idx] = 1


class BoundaryCondition2D:
id: int
boundary_type: BoundaryType
the_geom: shapely.Geometry
type: BoundaryType
geom: shapely.Geometry


class BoundaryConditions2D(Array[BoundaryCondition2D]):
def get_intersecting_node_idx(
self, idx: int, cell_tree: shapely.STRtree
) -> np.ndarray:
bc_geom = self.the_geom[idx]
bc_geom = self.geom[idx]

x1, y1, x2, y2 = shapely.bounds(bc_geom)
is_horizontal = (x2 - x1) > (y2 - y1)
Expand Down Expand Up @@ -187,7 +187,7 @@ def get_neighoring_node_idx(
return node_idx[before if is_before else after], is_before

def adapt_node_idx_groundwater(self, idx: int, nodes: Nodes, node_idx):
is_groundwater = self.boundary_type_is_groundwater(self.boundary_type[idx])
is_groundwater = self.boundary_type_is_groundwater(self.type[idx])
if is_groundwater:
n_groundwater_cells = nodes.n_groundwater_cells
if n_groundwater_cells == 0:
Expand Down Expand Up @@ -266,8 +266,8 @@ def is_groundwater(kcu: LineType) -> bool:
}

@staticmethod
def boundary_type_is_groundwater(boundary_type: BoundaryType) -> bool:
return boundary_type in {
def boundary_type_is_groundwater(type: BoundaryType) -> bool:
return type in {
BoundaryType.GROUNDWATERLEVEL,
BoundaryType.GROUNDWATERDISCHARGE,
}
Expand Down Expand Up @@ -296,7 +296,7 @@ def create_boundary_cells(
itertools.islice(node_id_counter, len(boundary_cells))
)
boundary_cells.boundary_id[:] = self.id[idx]
boundary_cells.boundary_type[:] = self.boundary_type[idx]
boundary_cells.boundary_type[:] = self.type[idx]
boundary_cells.node_type[:] = (
NodeType.NODE_2D_GROUNDWATER_BOUNDARIES
if self.is_groundwater(kcu)
Expand Down
8 changes: 4 additions & 4 deletions threedigrid_builder/interface/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def get_boundary_conditions_1d(self) -> BoundaryConditions1D:
arr = (
session.query(
models.BoundaryCondition1D.id,
models.BoundaryCondition1D.boundary_type,
models.BoundaryCondition1D.type,
models.BoundaryCondition1D.connection_node_id,
)
.order_by(models.BoundaryCondition1D.id)
Expand All @@ -377,13 +377,13 @@ def get_boundary_conditions_2d(self) -> BoundaryConditions2D:
arr = (
session.query(
models.BoundaryConditions2D.id,
models.BoundaryConditions2D.boundary_type,
models.BoundaryConditions2D.the_geom,
models.BoundaryConditions2D.type,
models.BoundaryConditions2D.geom,
)
.order_by(models.BoundaryConditions2D.id)
.as_structarray()
)
arr["the_geom"] = self.reproject(arr["the_geom"])
arr["geom"] = self.reproject(arr["geom"])

# transform to a BoundaryConditions1D object
return BoundaryConditions2D(**{name: arr[name] for name in arr.dtype.names})
Expand Down
18 changes: 9 additions & 9 deletions threedigrid_builder/tests/test_boundary_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def grid1d():
def test_1d_apply(grid1d):
boundary_conditions_1d = BoundaryConditions1D(
id=[10, 22],
boundary_type=[2, 3],
type=[2, 3],
connection_node_id=[9, 5],
)
boundary_conditions_1d.apply(grid1d)
Expand Down Expand Up @@ -278,8 +278,8 @@ def test_2d_boundary_condition(
):
boundary_conditions_2d = BoundaryConditions2D(
id=range(len(bc_coords)),
boundary_type=[3] * len(bc_coords),
the_geom=shapely.linestrings(bc_coords),
type=[3] * len(bc_coords),
geom=shapely.linestrings(bc_coords),
)

nodes, lines = boundary_conditions_2d.get_nodes_and_lines(
Expand Down Expand Up @@ -319,8 +319,8 @@ def test_2d_boundary_condition(
def test_2d_boundary_condition_err(grid2d, bc_coords, expected_message):
boundary_conditions_2d = BoundaryConditions2D(
id=range(len(bc_coords)),
boundary_type=[3] * len(bc_coords),
the_geom=shapely.linestrings(bc_coords),
type=[3] * len(bc_coords),
geom=shapely.linestrings(bc_coords),
)

with pytest.raises(SchematisationError, match=expected_message):
Expand Down Expand Up @@ -383,8 +383,8 @@ def grid2d_gw():
def test_2d_boundary_condition_types(grid2d_gw, boundary_type, node_type, kcu, line):
boundary_conditions_2d = BoundaryConditions2D(
id=[1],
boundary_type=[boundary_type],
the_geom=[shapely.linestrings([(12.0, 3.0), (12.0, 8.0)])],
type=[boundary_type],
geom=[shapely.linestrings([(12.0, 3.0), (12.0, 8.0)])],
)
nodes, lines = boundary_conditions_2d.get_nodes_and_lines(
grid2d_gw.nodes,
Expand All @@ -411,8 +411,8 @@ def test_2d_boundary_condition_types(grid2d_gw, boundary_type, node_type, kcu, l
def test_2d_boundary_condition_combined(grid2d_gw):
boundary_conditions_2d = BoundaryConditions2D(
id=[1, 2],
boundary_type=[BoundaryType.WATERLEVEL, BoundaryType.GROUNDWATERLEVEL],
the_geom=[shapely.linestrings([(12.0, 3.0), (12.0, 8.0)])] * 2,
type=[BoundaryType.WATERLEVEL, BoundaryType.GROUNDWATERLEVEL],
geom=[shapely.linestrings([(12.0, 3.0), (12.0, 8.0)])] * 2,
)

nodes, lines = boundary_conditions_2d.get_nodes_and_lines(
Expand Down
2 changes: 1 addition & 1 deletion threedigrid_builder/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_get_boundary_conditions_1d(db):
# some test samples
assert len(boundary_conditions_1d) == 4
assert boundary_conditions_1d.id[1] == 2
assert boundary_conditions_1d.boundary_type[2] == BoundaryType.DISCHARGE
assert boundary_conditions_1d.type[2] == BoundaryType.DISCHARGE
assert boundary_conditions_1d.connection_node_id[3] == 59


Expand Down

0 comments on commit ecebf54

Please sign in to comment.