Skip to content

Commit 494b978

Browse files
committed
Add unit test for patch_volume
1 parent 7d840fe commit 494b978

1 file changed

Lines changed: 80 additions & 6 deletions

File tree

‎test/test_mesh.py‎

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import numpy as np
55
import pytest
66

7-
from adapt_common.mesh import cell_volume
7+
from adapt_common.mesh import cell_volume, patch_volume
88

99

1010
@pytest.fixture
1111
def n():
12-
"""Set number of elements in each direction."""
13-
return 5
12+
"""Set number of cells in each direction."""
13+
return 2
1414

1515

1616
@pytest.fixture(params=[1, 2, 3], ids=["1D", "2D", "3D"])
@@ -21,16 +21,90 @@ def topological_dimension(request):
2121

2222
@pytest.fixture
2323
def uniform_mesh(n, topological_dimension):
24-
"""Create a uniform unit simplex mesh with n elements in each direction."""
24+
"""Create a uniform unit hypercube mesh with n cells in each direction."""
2525
return {
2626
1: fd.UnitIntervalMesh(n),
27-
2: fd.UnitSquareMesh(n, n),
28-
3: fd.UnitCubeMesh(n, n, n),
27+
2: fd.UnitSquareMesh(n, n, reorder=False),
28+
3: fd.UnitCubeMesh(n, n, n, reorder=False),
2929
}[topological_dimension]
3030

3131

32+
expected_coords_1d = np.array([0.0, 0.5, 1.0])
33+
34+
expected_coords_2d = np.array(
35+
[
36+
[0.0, 0.0],
37+
[0.0, 0.5],
38+
[0.5, 0.0],
39+
[0.5, 0.5],
40+
[0.0, 1.0],
41+
[0.5, 1.0],
42+
[1.0, 0.0],
43+
[1.0, 0.5],
44+
[1.0, 1.0],
45+
],
46+
)
47+
48+
expected_coords_3d = np.array(
49+
[
50+
[0.0, 0.0, 0.0],
51+
[0.5, 0.0, 0.0],
52+
[0.5, 0.5, 0.0],
53+
[0.5, 0.5, 0.5],
54+
[0.5, 0.0, 0.5],
55+
[0.0, 0.0, 0.5],
56+
[0.0, 0.5, 0.0],
57+
[0.0, 0.5, 0.5],
58+
[1.0, 0.0, 0.0],
59+
[1.0, 0.5, 0.0],
60+
[1.0, 0.5, 0.5],
61+
[1.0, 0.0, 0.5],
62+
[0.5, 1.0, 0.0],
63+
[0.5, 1.0, 0.5],
64+
[0.0, 1.0, 0.0],
65+
[0.0, 1.0, 0.5],
66+
[1.0, 1.0, 0.0],
67+
[1.0, 1.0, 0.5],
68+
[0.5, 0.5, 1.0],
69+
[0.5, 0.0, 1.0],
70+
[0.0, 0.0, 1.0],
71+
[0.0, 0.5, 1.0],
72+
[1.0, 0.5, 1.0],
73+
[1.0, 0.0, 1.0],
74+
[0.5, 1.0, 1.0],
75+
[0.0, 1.0, 1.0],
76+
[1.0, 1.0, 1.0],
77+
]
78+
)
79+
80+
81+
def test_mesh_coordinates(topological_dimension, uniform_mesh):
82+
"""Test that the mesh coordinates take expected values under reorder=False."""
83+
expected_coords = {
84+
1: expected_coords_1d,
85+
2: expected_coords_2d,
86+
3: expected_coords_3d,
87+
}[topological_dimension]
88+
np.testing.assert_almost_equal(uniform_mesh.coordinates.dat.data, expected_coords)
89+
90+
3291
def test_cell_volume_uniform_mesh(uniform_mesh):
3392
"""Test that the cell volume of a uniform mesh is indeed uniform."""
3493
volume = cell_volume(uniform_mesh)
3594
expected = 1.0 / uniform_mesh.num_cells()
3695
np.testing.assert_almost_equal(volume.dat.data, expected)
96+
97+
98+
def test_patch_volume_uniform_mesh(topological_dimension, uniform_mesh):
99+
"""Test that the patch volumes of a uniform mesh takes expected values."""
100+
patch_vol = patch_volume(uniform_mesh)
101+
cell_vol = 1.0 / uniform_mesh.num_cells()
102+
# fmt: off
103+
num_neighbouring_cells = {
104+
1: [1, 2, 1],
105+
2: [1, 3, 3, 6, 2, 3, 2, 3, 1],
106+
3: [6, 8, 12, 24, 12, 8, 8, 12, 2, 4, 12, 4, 4, 12, 2, 4, 2, 8, 12, 4, 2, 4, 8, 2, 8, 2, 6], # noqa
107+
}[topological_dimension]
108+
# fmt: on
109+
expected = np.array(num_neighbouring_cells) * cell_vol
110+
np.testing.assert_almost_equal(patch_vol.dat.data, expected)

0 commit comments

Comments
 (0)