Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade laterals and boundary conditions #376

Merged
merged 13 commits into from
Sep 9, 2024
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Changelog of threedigrid-builder
1.17.1 (unreleased)
-------------------

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


1.17.0 (2024-08-16)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_version():

install_requires = [
"numpy>=1.15,<3.0",
"threedi-schema==0.224.*",
"threedi-schema @ https://github.com/nens/threedi-schema/archive/eli-laterals-and-boundary-conditions.zip",
"shapely>=2",
"pyproj>=3",
"condenser[geo]>=0.1.1",
Expand Down
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
10 changes: 5 additions & 5 deletions threedigrid_builder/interface/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# hardcoded source projection
SOURCE_EPSG = 4326

MIN_SQLITE_VERSION = 224
MIN_SQLITE_VERSION = 225

DAY_IN_SECONDS = 24.0 * 3600.0

Expand Down Expand Up @@ -343,7 +343,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 @@ -359,13 +359,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
6 changes: 3 additions & 3 deletions threedigrid_builder/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_init(tmp_path):
with mock.patch(
"threedigrid_builder.interface.db.ThreediDatabase"
) as db, mock.patch.object(SQLite, "get_version") as get_version:
get_version.return_value = 224
get_version.return_value = 225
sqlite = SQLite(path)

db.assert_called_with(path)
Expand Down Expand Up @@ -65,7 +65,7 @@ def test_init_bad_version(tmp_path):


def test_get_version(db):
assert db.get_version() == 224
assert db.get_version() == 225


def test_get_boundary_conditions_1d(db):
Expand All @@ -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
Loading