Skip to content

Commit

Permalink
Fix incorrect cross sections for CLOSED_RECTANGLE shapes (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
margrietpalm authored Dec 17, 2024
1 parent 5c61dce commit c0f8206
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
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.21.3 (unreleased)
-------------------

- Nothing changed yet.
- Fix creating cross sections with schema 228


1.21.2 (2024-12-17)
Expand Down
5 changes: 4 additions & 1 deletion threedigrid_builder/grid/cross_section_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ def get_unique(self):
[getattr(self, attr)[mask] for attr in cross_section_attributes[shape]]
)
# Find unique rows and add these to the new_csd_dict with new id's
u_arr, u_idx = np.unique(attr_arr, return_index=True)
if len(cross_section_attributes[shape]) > 1:
u_arr, u_idx = np.unique(attr_arr, axis=0, return_index=True)
else:
u_arr, u_idx = np.unique(attr_arr, return_index=True)
new_id = np.arange(len(u_idx)) + len(new_csd_dict["id"])
for key in new_csd_dict:
if key == "id":
Expand Down
48 changes: 22 additions & 26 deletions threedigrid_builder/tests/test_cross_section_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,27 +515,23 @@ def test_tabulate_yz_err(width, height, match):
class TestCrossSectionDefinitionGetUnique:
def test_for_closed_rectangle(self):
csd_in = CrossSectionDefinitions(
id=[100, 200],
id=[100, 200, 300, 400],
shape=[
constants.CrossSectionShape.CLOSED_RECTANGLE.value,
constants.CrossSectionShape.CLOSED_RECTANGLE.value,
constants.CrossSectionShape.CLOSED_RECTANGLE.value,
constants.CrossSectionShape.CLOSED_RECTANGLE.value,
],
width=[1, 1],
height=[1, 1],
cross_section_table=["foo", "bar"],
origin_table=["pipe", "weir"],
origin_id=[10, 20],
width=[1, 3, 1, 3],
height=[1, 2, 1, 2],
cross_section_table=["foo", "bar", "foo", "bar"],
origin_table=["pipe", "weir", "pipe", "pipe"],
origin_id=[10, 20, 30, 40],
)
unique_definition, _ = csd_in.get_unique()
assert unique_definition.id == [
0,
]
assert unique_definition.width == [
1,
]
assert unique_definition.height == [
1,
]
np.testing.assert_array_equal(unique_definition.id, [0, 1])
np.testing.assert_array_equal(unique_definition.width, [1, 3])
np.testing.assert_array_equal(unique_definition.height, [1, 2])

@pytest.mark.parametrize(
"shape",
Expand Down Expand Up @@ -574,19 +570,19 @@ def test_for_tabulated_shape(self, shape):
)
def test_for_other_shapes(self, shape):
csd_in = CrossSectionDefinitions(
id=[100, 200],
shape=[shape, shape],
width=[1, 1],
height=[10, 21],
cross_section_table=["foo", "foo"],
origin_table=["pipe", "weir"],
origin_id=[10, 20],
id=[100, 200, 300],
shape=[shape, shape, shape],
width=[1, 1, 100],
height=[10, 21, 100],
cross_section_table=["foo", "foo", "bar"],
origin_table=["pipe", "weir", "weir"],
origin_id=[10, 20, 30],
)
unique_definition, _ = csd_in.get_unique()
assert unique_definition.id == [
0,
]
assert unique_definition.cross_section_table == ["foo"]
np.testing.assert_array_equal(unique_definition.id, [0, 1])
np.testing.assert_array_equal(
sorted(unique_definition.cross_section_table), sorted(["foo", "bar"])
)

def test_mapping(self):
csd_in = CrossSectionDefinitions(
Expand Down

0 comments on commit c0f8206

Please sign in to comment.