Skip to content

Commit

Permalink
use numpy.testing for array asserts (#103)
Browse files Browse the repository at this point in the history
* use numpy.testing for array asserts

yields better error messages when tests fail

* assert_allclose has different default tolerances than allclose
  • Loading branch information
minrk authored Jan 24, 2024
1 parent d5e3232 commit 3fa6274
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion python/tests/test_cube_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dolfinx.fem as fem
import gmsh
import numpy as np
import numpy.testing as nt
import pytest
import scipy.sparse.linalg
import ufl
Expand Down Expand Up @@ -301,7 +302,7 @@ def sigma(v):
# Back substitution to full solution vector
uh_numpy = K @ d
atol = 1000 * np.finfo(default_scalar_type).resolution
assert np.allclose(uh_numpy, u_mpc, atol=atol)
nt.assert_allclose(uh_numpy, u_mpc, atol=atol)
L_org.destroy()

list_timings(comm, [TimingType.wall])
3 changes: 2 additions & 1 deletion python/tests/test_integration_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from petsc4py import PETSc

import numpy as np
import numpy.testing as nt
import pytest
import scipy.sparse.linalg
import ufl
Expand Down Expand Up @@ -117,7 +118,7 @@ def l2b(li):
d = scipy.sparse.linalg.spsolve(KTAK.astype(scipy_dtype), reduced_L.astype(scipy_dtype))
# Back substitution to full solution vector
uh_numpy = K.astype(scipy_dtype) @ d.astype(scipy_dtype)
assert np.allclose(uh_numpy.astype(u_mpc.dtype), u_mpc, rtol=500
nt.assert_allclose(uh_numpy.astype(u_mpc.dtype), u_mpc, rtol=500
* np.finfo(default_scalar_type).resolution)
solver.destroy()
b.destroy()
Expand Down
5 changes: 3 additions & 2 deletions python/tests/test_lifting.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from petsc4py import PETSc

import numpy as np
import numpy.testing as nt
import pytest
import scipy.sparse.linalg
import ufl
Expand Down Expand Up @@ -113,8 +114,8 @@ def l2b(li):
reduced_L = K.T @ (L_np) # - constants)
# Solve linear system
d = scipy.sparse.linalg.spsolve(KTAK, reduced_L)
# Back substitution to full solution vecto
# Back substitution to full solution vector
uh_numpy = K @ (d) # + constants)
assert np.allclose(uh_numpy, u_mpc)
nt.assert_allclose(uh_numpy, u_mpc, rtol=1e-5, atol=1e-8)

list_timings(comm, [TimingType.wall])
3 changes: 2 additions & 1 deletion python/tests/test_linear_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from petsc4py import PETSc

import numpy as np
import numpy.testing as nt
import pytest
import scipy.sparse.linalg
import ufl
Expand Down Expand Up @@ -85,7 +86,7 @@ def PeriodicBoundary(x):
d = scipy.sparse.linalg.spsolve(KTAK, reduced_L)
# Back substitution to full solution vector
uh_numpy = K.astype(scipy_dtype) @ d
assert np.allclose(uh_numpy.astype(u_mpc.dtype), u_mpc,
nt.assert_allclose(uh_numpy.astype(u_mpc.dtype), u_mpc,
rtol=500
* np.finfo(default_scalar_type).resolution,
atol=500
Expand Down
5 changes: 3 additions & 2 deletions python/tests/test_mpc_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from petsc4py import PETSc

import numpy as np
import numpy.testing as nt
import pytest
import scipy.sparse.linalg
import ufl
Expand Down Expand Up @@ -101,7 +102,7 @@ def l2b(li):
d = scipy.sparse.linalg.spsolve(KTAK, reduced_L)
# Back substitution to full solution vector
uh_numpy = K.astype(scipy_dtype) @ d
assert np.allclose(uh_numpy.astype(u_mpc.dtype), u_mpc, rtol=500
nt.assert_allclose(uh_numpy.astype(u_mpc.dtype), u_mpc, rtol=500
* np.finfo(default_scalar_type).resolution)

list_timings(comm, [TimingType.wall])
Expand Down Expand Up @@ -169,7 +170,7 @@ def l2b(li):
d = scipy.sparse.linalg.spsolve(KTAK, reduced_L)
# Back substitution to full solution vector
uh_numpy = K.astype(scipy_dtype) @ d
assert np.allclose(uh_numpy.astype(u_mpc.dtype), u_mpc, rtol=500
nt.assert_allclose(uh_numpy.astype(u_mpc.dtype), u_mpc, rtol=500
* np.finfo(default_scalar_type).resolution)

list_timings(comm, [TimingType.wall])
3 changes: 2 additions & 1 deletion python/tests/test_surface_integral.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import dolfinx.fem as fem
import numpy as np
import numpy.testing as nt
import pytest
import scipy.sparse.linalg
import ufl
Expand Down Expand Up @@ -132,7 +133,7 @@ def l2b(li):
d = scipy.sparse.linalg.spsolve(KTAK, reduced_L)
# Back substitution to full solution vector
uh_numpy = K.astype(scipy_dtype) @ d
assert np.allclose(uh_numpy.astype(u_mpc.dtype), u_mpc, rtol=500
nt.assert_allclose(uh_numpy.astype(u_mpc.dtype), u_mpc, rtol=500
* np.finfo(default_scalar_type).resolution)

L_org.destroy()
Expand Down
3 changes: 2 additions & 1 deletion python/tests/test_vector_poisson.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import dolfinx.fem as fem
import numpy as np
import numpy.testing as nt
import pytest
import scipy.sparse.linalg
import ufl
Expand Down Expand Up @@ -121,7 +122,7 @@ def l2b(li):
d = scipy.sparse.linalg.spsolve(KTAK, reduced_L)
# Back substitution to full solution vector
uh_numpy = K.astype(scipy_dtype) @ d
assert np.allclose(uh_numpy.astype(u_mpc.dtype), u_mpc, rtol=500
nt.assert_allclose(uh_numpy.astype(u_mpc.dtype), u_mpc, rtol=500
* np.finfo(default_scalar_type).resolution)

b.destroy()
Expand Down

0 comments on commit 3fa6274

Please sign in to comment.