Skip to content

Commit be3bfe6

Browse files
committed
Add equidistribution marking (py)
1 parent 2ca2271 commit be3bfe6

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

python/dolfinx/mesh.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from dolfinx.cpp.refinement import (
3636
IdentityPartitionerPlaceholder,
3737
RefinementOption,
38+
mark_equidistribution,
3839
mark_maximum,
3940
)
4041
from dolfinx.cpp.refinement import (
@@ -73,6 +74,7 @@
7374
"exterior_facet_indices",
7475
"locate_entities",
7576
"locate_entities_boundary",
77+
"mark_equidistribution",
7678
"mark_maximum",
7779
"meshtags",
7880
"meshtags_from_entities",

python/dolfinx/wrappers/dolfinx_wrappers/refinement.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ void declare_refinement(nanobind::module_& m)
134134
std::span(marker.data(), marker.size()), theta, comm.get()));
135135
},
136136
nb::arg("marker"), nb::arg("theta"), nb::arg("comm"));
137+
138+
m.def(
139+
"mark_equidistribution",
140+
[](nb::ndarray<const T, nb::ndim<1>, nb::c_contig> marker, T theta,
141+
MPICommWrapper comm)
142+
{
143+
return dolfinx_wrappers::as_nbarray(
144+
dolfinx::refinement::mark_equidistribution(
145+
std::span(marker.data(), marker.size()), theta, comm.get()));
146+
},
147+
nb::arg("marker"), nb::arg("theta"), nb::arg("comm"));
137148
}
138149

139150
} // namespace dolfinx_wrappers

python/test/unit/refinement/test_mark.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,26 @@ def test_mark_maximum(theta: float, dtype: np.dtype) -> None:
3131
msh.topology.create_entities(1)
3232
marked_edges = mesh.compute_incident_entities(msh.topology, marked_cells, tdim, 1)
3333
mesh.refine(msh, marked_edges)
34+
35+
36+
@pytest.mark.parametrize("theta", [0.2, 0.4, 0.6, 0.8])
37+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
38+
def test_mark_equidistribution(theta: float, dtype: np.dtype) -> None:
39+
msh = mesh.create_unit_square(comm := MPI.COMM_WORLD, n := 10, n, dtype=dtype)
40+
41+
tdim = msh.topology.dim
42+
cell_count = (cell_im := msh.topology.index_map(tdim)).size_local + cell_im.num_ghosts
43+
marker = np.random.default_rng(0).random(cell_count)
44+
45+
marked_cells = mesh.mark_equidistribution(marker, theta, comm)
46+
47+
norm = np.sqrt(comm.allreduce(np.sum(marker**2), MPI.SUM))
48+
count = comm.allreduce(marker.size)
49+
assert np.allclose(
50+
marked_cells,
51+
np.argwhere(marker > theta * norm / np.sqrt(count)).flatten(),
52+
)
53+
54+
msh.topology.create_entities(1)
55+
marked_edges = mesh.compute_incident_entities(msh.topology, marked_cells, tdim, 1)
56+
mesh.refine(msh, marked_edges)

0 commit comments

Comments
 (0)