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

Fix wrong number of mesh levels when grid is multiple of refinement factor #26

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
)
Loading