Skip to content

Add unit tests for convection_terms.py#2025

Closed
irhyl wants to merge 1 commit intogoogle-deepmind:mainfrom
gsoc-2026:pr/convection-terms-test
Closed

Add unit tests for convection_terms.py#2025
irhyl wants to merge 1 commit intogoogle-deepmind:mainfrom
gsoc-2026:pr/convection-terms-test

Conversation

@irhyl
Copy link
Copy Markdown

@irhyl irhyl commented Mar 1, 2026

Summary

Addresses TODO(b/469726859) by adding the first dedicated test file for convection_terms.py, which previously had zero unit test coverage. Tests cover all boundary condition modes, edge cases, and Peclet number limiting behavior.

Changes

  • New file: torax/_src/fvm/tests/convection_terms_test.py - 17 tests in ConvectionTermsTest
    • Follows the same pattern as diffusion_terms_test.py
    • Uses _make_face_centers helper for uniform grid construction

Test coverage

  • Zero convection produces zero mat/vec
  • Output shape verification
  • Tridiagonal matrix structure
  • Single-cell grid NotImplementedError
  • All three Dirichlet modes (ghost, direct, semi-implicit)
  • Both Neumann modes (ghost, semi-implicit)
  • Invalid mode ValueError
  • Dirichlet BC contributions to vec
  • Zero-gradient Neumann behavior
  • Negative diffusion coefficient handling
  • Large/small Peclet number limits (convection/diffusion dominated)

Testing

All 17 new tests pass.

Bug: b/469726859

Create convection_terms_test.py with 17 tests for
make_convection_terms, covering:
- Zero convection produces zero mat/vec
- Output shape verification
- Tridiagonal matrix structure
- Single-cell grid NotImplementedError
- All three Dirichlet modes (ghost, direct, semi-implicit)
- Both Neumann modes (ghost, semi-implicit)
- Invalid mode ValueError
- Dirichlet BC contributions to vec
- Zero-gradient Neumann behavior
- Negative diffusion coefficient handling
- Large/small Peclet number limits (convection/diffusion dominated)

Bug: b/469726859
Copy link
Copy Markdown
Collaborator

@Nush395 Nush395 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution, I've made comments pertaining to the removal/tidying up of some of the added tests.

@@ -0,0 +1,324 @@
# Copyright 2025 DeepMind Technologies Limited
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2026



class ConvectionTermsTest(parameterized.TestCase):
"""Tests for make_convection_terms."""
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docstring here is unnecessary

"""Tests for make_convection_terms."""

def test_zero_convection_returns_zero_mat_and_vec(self):
"""When v_face is zero everywhere, mat and vec should be zero."""
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this docstring does not add any useful information, it could be removed or moved to be a comment on line 41 e.g.

np.testing.assert_allclose(mat, np.zeros((4, 4)), atol=1e-12)
np.testing.assert_allclose(vec, np.zeros(4), atol=1e-12)

def test_output_shapes(self):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shape tests are done in other tests you have added so this test can be removed.

v_face=jnp.ones(5),
d_face=jnp.ones(5),
var=cell_var,
dirichlet_mode='invalid',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can change the type of the call signature of dirichlet_mode to be a Literal of the allowed string types and then this test can be removed as any incorrect call would be picked up by static type analysis.

dirichlet_mode='invalid',
)

def test_invalid_neumann_mode_raises(self):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above.

@gsoc-2026 gsoc-2026 closed this by deleting the head repository Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants