Skip to content

Commit

Permalink
Merge PR mllam#26 into branch
Browse files Browse the repository at this point in the history
  • Loading branch information
joeloskarsson committed Oct 13, 2024
1 parent 6f03908 commit d2a081a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Fix wrong number of mesh levels when grid is multiple of refinement factor
[\#26](https://github.com/mllam/weather-model-graphs/pull/26)
@joeloskarsson

- Create different number of mesh nodes in x- and y-direction.
[\#21](https://github.com/mllam/weather-model-graphs/pull/21)
@joeloskarsson
Expand Down
14 changes: 8 additions & 6 deletions src/weather_model_graphs/create/mesh/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,14 @@ def create_multirange_2d_mesh_graphs(
# Find the number of mesh levels possible in x- and y-direction,
# and the number of leaf nodes that would correspond to
# max_coord/(grid_refinement_factor*level_refinement_factor^mesh_levels) = 1
max_mesh_levels = (
(np.log(coord_extent) - np.log(grid_refinement_factor))
/ np.log(level_refinement_factor)
).astype(
int
) # (2,)
max_mesh_levels_float = (
np.log(coord_extent) - np.log(grid_refinement_factor)
) / np.log(level_refinement_factor)

# Need to add a small epsilon before flooring to int, due to numerical
# issues with the computation above
eps = 1e-8
max_mesh_levels = (max_mesh_levels_float + eps).astype(int) # (2,)
nleaf = grid_refinement_factor * (
level_refinement_factor**max_mesh_levels
) # leaves at the bottom in each direction, if using max_mesh_levels
Expand Down
20 changes: 20 additions & 0 deletions tests/test_graph_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,23 @@ def test_create_rectangular_graph(kind):
fn_name = f"create_{kind}_graph"
fn = getattr(wmg.create.archetype, fn_name)
fn(xy_grid=xy, grid_refinement_factor=2)


@pytest.mark.parametrize("grid_refinement_factor", (2, 3))
@pytest.mark.parametrize("level_refinement_factor", (2, 3, 5))
def test_create_exact_refinement(grid_refinement_factor, level_refinement_factor):
"""
This test is to check that it is possible to create graph hierarchies when
the refinement factors are an exact multiple of the number of nodes. In these
situations it should be possible to create multi-level graphs, but it was not
earlier due to numerical issues.
"""
N = grid_refinement_factor * (level_refinement_factor**2)
xy = _create_rectangular_fake_xy(Nx=N, Ny=N)

# Build hierarchical graph, should have 2 levels and not give error
wmg.create.archetype.create_oskarsson_hierarchical_graph(
xy,
grid_refinement_factor=grid_refinement_factor,
level_refinement_factor=level_refinement_factor,
)

0 comments on commit d2a081a

Please sign in to comment.