Skip to content

Commit

Permalink
Increase test coverage for POD default arguments, by testing the case…
Browse files Browse the repository at this point in the history
… in which neither N nor tol are provided, only N is provided, and both N and tol are provided
  • Loading branch information
francesco-ballarin committed Aug 29, 2023
1 parent b4e6440 commit 2a454c2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,37 @@ def tensors_list_mat(mesh: dolfinx.mesh.Mesh) -> rbnicsx.backends.TensorsList:
def test_backends_proper_orthogonal_decomposition_functions( # type: ignore[no-any-unimported]
functions_list: rbnicsx.backends.FunctionsList, inner_product: ufl.Form, normalize: bool
) -> None:
"""Check rbnicsx.backends.proper_orthogonal_decomposition for the case of dolfinx.fem.Function snapshots."""
"""
Check rbnicsx.backends.proper_orthogonal_decomposition for the case of dolfinx.fem.Function snapshots.
The case of default N and tolerance is tested here.
"""
compute_inner_product = rbnicsx.backends.bilinear_form_action(inner_product)
eigenvalues, modes, eigenvectors = rbnicsx.backends.proper_orthogonal_decomposition(
functions_list[:2], compute_inner_product, N=2, tol=0.0, normalize=normalize)
functions_list[:2], compute_inner_product, normalize=normalize)
assert len(eigenvalues) == 2
assert np.isclose(eigenvalues[0], 5)
assert np.isclose(eigenvalues[1], 0)
assert len(modes) == 2
assert np.isclose(compute_inner_product(modes[0])(modes[0]), 1 if normalize else 5)
if normalize:
assert np.allclose(modes[0].vector.array, 1)
# np.allclose(modes[2], 0) may not be true in arithmetic precision when scaling with a very small eigenvalue
assert len(eigenvectors) == 2


@pytest.mark.parametrize("normalize", [True, False])
def test_backends_proper_orthogonal_decomposition_functions_N( # type: ignore[no-any-unimported]
functions_list: rbnicsx.backends.FunctionsList, inner_product: ufl.Form, normalize: bool
) -> None:
"""
Check rbnicsx.backends.proper_orthogonal_decomposition for the case of dolfinx.fem.Function snapshots.
The case of non default N, but default zero tolerance, is tested here.
"""
compute_inner_product = rbnicsx.backends.bilinear_form_action(inner_product)
eigenvalues, modes, eigenvectors = rbnicsx.backends.proper_orthogonal_decomposition(
functions_list[:2], compute_inner_product, N=2, normalize=normalize)
assert len(eigenvalues) == 2
assert np.isclose(eigenvalues[0], 5)
assert np.isclose(eigenvalues[1], 0)
Expand All @@ -102,13 +129,13 @@ def test_backends_proper_orthogonal_decomposition_functions( # type: ignore[no-


@pytest.mark.parametrize("normalize", [True, False])
def test_backends_proper_orthogonal_decomposition_functions_tol( # type: ignore[no-any-unimported]
def test_backends_proper_orthogonal_decomposition_functions_N_tol( # type: ignore[no-any-unimported]
functions_list: rbnicsx.backends.FunctionsList, inner_product: ufl.Form, normalize: bool
) -> None:
"""
Check rbnicsx.backends.proper_orthogonal_decomposition for the case of dolfinx.fem.Function snapshots.
The case of non zero tolerance is tested here.
The case of non default N and non zero tolerance is tested here.
"""
compute_inner_product = rbnicsx.backends.bilinear_form_action(inner_product)
eigenvalues, modes, eigenvectors = rbnicsx.backends.proper_orthogonal_decomposition(
Expand Down
40 changes: 36 additions & 4 deletions tests/unit/online/test_online_proper_orthogonal_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,43 @@ def test_online_proper_orthogonal_decomposition_functions( # type: ignore[no-an
functions_list: rbnicsx.online.FunctionsList, inner_product: typing.Callable[[int], petsc4py.PETSc.Mat],
normalize: bool
) -> None:
"""Check rbnicsx.online.proper_orthogonal_decomposition for the case of snapshots stored in a FunctionsList."""
"""
Check rbnicsx.online.proper_orthogonal_decomposition for the case of snapshots stored in a FunctionsList.
The case of default N and tolerance is tested here.
"""
size = functions_list[0].size
inner_product_matrix = inner_product(size)
eigenvalues, modes, eigenvectors = rbnicsx.online.proper_orthogonal_decomposition(
functions_list[:2], inner_product_matrix, N=2, tol=0.0, normalize=normalize)
functions_list[:2], inner_product_matrix, normalize=normalize)
assert len(eigenvalues) == 2
sum_squares_first_size_numbers = size * (size + 1) * (2 * size + 1) / 6
assert np.isclose(eigenvalues[0], 5 * sum_squares_first_size_numbers)
assert np.isclose(eigenvalues[1], 0)
assert len(modes) == 2
assert np.isclose(
compute_inner_product(inner_product_matrix, modes[0], modes[0]),
1 if normalize else 5 * sum_squares_first_size_numbers)
if normalize:
assert np.allclose(modes[0].array, 1 / np.sqrt(sum_squares_first_size_numbers) * np.arange(1, size + 1))
# np.allclose(modes[2], 0) may not be true in arithmetic precision when scaling with a very small eigenvalue
assert len(eigenvectors) == 2


@pytest.mark.parametrize("normalize", [True, False])
def test_online_proper_orthogonal_decomposition_functions_N( # type: ignore[no-any-unimported]
functions_list: rbnicsx.online.FunctionsList, inner_product: typing.Callable[[int], petsc4py.PETSc.Mat],
normalize: bool
) -> None:
"""
Check rbnicsx.online.proper_orthogonal_decomposition for the case of snapshots stored in a FunctionsList.
The case of non default N, but default zero tolerance, is tested here.
"""
size = functions_list[0].size
inner_product_matrix = inner_product(size)
eigenvalues, modes, eigenvectors = rbnicsx.online.proper_orthogonal_decomposition(
functions_list[:2], inner_product_matrix, N=2, normalize=normalize)
assert len(eigenvalues) == 2
sum_squares_first_size_numbers = size * (size + 1) * (2 * size + 1) / 6
assert np.isclose(eigenvalues[0], 5 * sum_squares_first_size_numbers)
Expand All @@ -164,14 +196,14 @@ def test_online_proper_orthogonal_decomposition_functions( # type: ignore[no-an


@pytest.mark.parametrize("normalize", [True, False])
def test_online_proper_orthogonal_decomposition_functions_tol( # type: ignore[no-any-unimported]
def test_online_proper_orthogonal_decomposition_functions_N_tol( # type: ignore[no-any-unimported]
functions_list: rbnicsx.online.FunctionsList, inner_product: typing.Callable[[int], petsc4py.PETSc.Mat],
normalize: bool
) -> None:
"""
Check rbnicsx.online.proper_orthogonal_decomposition for the case of snapshots stored in a FunctionsList.
The case of non zero tolerance is tested here.
The case of non default N and non zero tolerance is tested here.
"""
size = functions_list[0].size
inner_product_matrix = inner_product(size)
Expand Down

0 comments on commit 2a454c2

Please sign in to comment.