Skip to content

Commit 3d9d1ef

Browse files
Fix crash when trying to create flat multiscale graphs with >= 3 levels (#41)
* Fix computation of number of nodex in x- and y-directions * Add test testing graph creation with more than 2 levels * Add changelog entry
1 parent d909a1c commit 3d9d1ef

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
[2, Ny, Nx]), to allow for non-regularly gridded coordinates
2121
[\#32](https://github.com/mllam/weather-model-graphs/pull/32), @joeloskarsson
2222

23+
### Fixed
24+
25+
- Fix crash when trying to create flat multiscale graphs with >= 3 levels
26+
[\#41](https://github.com/mllam/weather-model-graphs/pull/41), @joeloskarsson
27+
2328
## [v0.2.0](https://github.com/mllam/weather-model-graphs/releases/tag/v0.2.0)
2429

2530
### Added

src/weather_model_graphs/create/mesh/kinds/flat.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ def create_flat_multiscale_mesh_graph(
5252
G_tot = G_all_levels[0]
5353
# First node at level l+1 share position with node (offset, offset) at level l
5454
level_offset = level_refinement_factor // 2
55+
56+
first_level_nodes = list(G_all_levels[0].nodes)
57+
# Last nodes in first layer has pos (nx-1, ny-1)
58+
num_nodes_x = first_level_nodes[-1][0] + 1
59+
num_nodes_y = first_level_nodes[-1][1] + 1
60+
5561
for lev in range(1, len(G_all_levels)):
5662
nodes = list(G_all_levels[lev - 1].nodes)
57-
# Last nodes always has pos (nx-1, ny-1)
58-
num_nodes_x = nodes[-1][0] + 1
59-
num_nodes_y = nodes[-1][1] + 1
6063
ij = (
6164
np.array(nodes)
6265
.reshape((num_nodes_x, num_nodes_y, 2))[
@@ -72,6 +75,10 @@ def create_flat_multiscale_mesh_graph(
7275
)
7376
G_tot = networkx.compose(G_tot, G_all_levels[lev])
7477

78+
# Update number of nodes in x- and y-direction for next iteraion
79+
num_nodes_x //= level_refinement_factor
80+
num_nodes_y //= level_refinement_factor
81+
7582
# Relabel mesh nodes to start with 0
7683
G_tot = prepend_node_index(G_tot, 0)
7784

tests/test_graph_creation.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,24 @@ def test_create_lat_lon(kind):
192192
coords_crs=coords_crs,
193193
graph_crs=graph_crs,
194194
)
195+
196+
197+
@pytest.mark.parametrize("kind", ["graphcast", "oskarsson_hierarchical"])
198+
def test_create_many_levels(kind):
199+
"""Test that mesh graph creation methods that work with many levels
200+
can handle more than 2 levels
201+
"""
202+
# Test 4 levels at lrf=3
203+
grid_coord_range = 10
204+
level_refinement_factor = 3
205+
mesh_node_distance = grid_coord_range / 3**4
206+
207+
xy = test_utils.create_fake_xy(N=grid_coord_range)
208+
fn_name = f"create_{kind}_graph"
209+
fn = getattr(wmg.create.archetype, fn_name)
210+
211+
fn(
212+
coords=xy,
213+
mesh_node_distance=mesh_node_distance,
214+
level_refinement_factor=level_refinement_factor,
215+
)

0 commit comments

Comments
 (0)