40auto compute_parent_facets(std::span<const std::int32_t> simplex_set)
42 static_assert(tdim == 2 or tdim == 3);
43 assert(simplex_set.size() % (tdim + 1) == 0);
45 = std::conditional_t<tdim == 2, std::array<std::int8_t, 12>,
46 std::array<std::int8_t, 32>>;
54 constexpr std::array<std::array<int, 3>, 3> facet_table_2d{
55 {{1, 2, 3}, {0, 2, 4}, {0, 1, 5}}};
57 constexpr std::array<std::array<int, 6>, 4> facet_table_3d{
63 const int ncells = simplex_set.size() / (tdim + 1);
64 for (
int fpi = 0; fpi < (tdim + 1); ++fpi)
67 for (
int cc = 0; cc < ncells; ++cc)
69 for (
int fci = 0; fci < (tdim + 1); ++fci)
72 std::array<int, tdim> cf, set_output;
74 int num_common_vertices;
75 if constexpr (tdim == 2)
77 for (
int j = 0; j < tdim; ++j)
78 cf[j] = simplex_set[cc * 3 + facet_table_2d[fci][j]];
80 std::ranges::sort(cf);
81 auto [last1, last2, it_last] = std::ranges::set_intersection(
82 facet_table_2d[fpi], cf, set_output.begin());
84 = std::ranges::distance(set_output.begin(), it_last);
88 for (
int j = 0; j < tdim; ++j)
89 cf[j] = simplex_set[cc * 4 + facet_table_3d[fci][j]];
91 std::ranges::sort(cf);
92 auto [last1, last2, it_last] = std::ranges::set_intersection(
93 facet_table_3d[fpi], cf, set_output.begin());
95 = std::ranges::distance(set_output.begin(), it_last);
98 if (num_common_vertices == tdim)
129std::pair<std::array<std::int32_t, 32>, std::size_t>
130get_simplices(std::span<const std::int64_t> indices,
131 std::span<const std::int32_t> longest_edge,
int tdim,
137 std::span<std::int8_t> marked_edges,
139 std::span<const std::int32_t> long_edge);
150template <std::
floating_po
int T>
151std::pair<std::vector<std::int32_t>, std::vector<std::int8_t>>
154 const int tdim =
mesh.topology()->dim();
156 mesh.topology_mutable()->create_entities(1);
157 mesh.topology_mutable()->create_entities(2);
158 mesh.topology_mutable()->create_connectivity(2, 1);
159 mesh.topology_mutable()->create_connectivity(1, tdim);
160 mesh.topology_mutable()->create_connectivity(tdim, 2);
162 std::int64_t num_faces =
mesh.topology()->index_map(2)->size_local()
163 +
mesh.topology()->index_map(2)->num_ghosts();
166 std::vector<std::int32_t> long_edge(num_faces);
167 std::vector<std::int8_t> edge_ratio_ok;
171 const T min_ratio = std::sqrt(2.0) / 2.0;
173 edge_ratio_ok.resize(num_faces);
175 auto x_dofmap =
mesh.geometry().dofmaps().front();
177 auto c_to_v =
mesh.topology()->connectivity(tdim, 0);
179 auto e_to_c =
mesh.topology()->connectivity(1, tdim);
181 auto e_to_v =
mesh.topology()->connectivity(1, 0);
185 auto map_e =
mesh.topology()->index_map(1);
187 std::vector<T> edge_length(map_e->size_local() + map_e->num_ghosts());
188 for (std::size_t e = 0; e < edge_length.size(); ++e)
191 auto cells = e_to_c->links(e);
192 assert(!cells.empty());
193 auto cell_vertices = c_to_v->links(cells.front());
194 auto edge_vertices = e_to_v->links(e);
197 auto it0 = std::find(cell_vertices.begin(), cell_vertices.end(),
199 assert(it0 != cell_vertices.end());
200 const std::size_t local0
201 = std::ranges::distance(cell_vertices.begin(), it0);
202 auto it1 = std::find(cell_vertices.begin(), cell_vertices.end(),
204 assert(it1 != cell_vertices.end());
205 const std::size_t local1
206 = std::ranges::distance(cell_vertices.begin(), it1);
208 auto x_dofs = md::submdspan(x_dofmap, cells.front(), md::full_extent);
209 std::span<const T, 3> x0(
mesh.geometry().x().data() + 3 * x_dofs[local0],
211 std::span<const T, 3> x1(
mesh.geometry().x().data() + 3 * x_dofs[local1],
215 edge_length[e] = std::sqrt(std::transform_reduce(
216 x0.begin(), x0.end(), x1.begin(), 0.0, std::plus<>(),
217 [](
auto x0,
auto x1) { return (x0 - x1) * (x0 - x1); }));
221 auto f_to_v =
mesh.topology()->connectivity(2, 0);
223 auto f_to_e =
mesh.topology()->connectivity(2, 1);
225 const std::vector global_indices
226 =
mesh.topology()->index_map(0)->global_indices();
227 for (
int f = 0; f < f_to_v->num_nodes(); ++f)
229 auto face_edges = f_to_e->links(f);
231 std::int32_t imax = 0;
233 T min_len = std::numeric_limits<T>::max();
235 for (
int i = 0; i < 3; ++i)
237 const T e_len = edge_length[face_edges[i]];
238 min_len = std::min(e_len, min_len);
244 else if (tdim == 3 and e_len == max_len)
249 auto vertices = f_to_v->links(f);
250 const int vmax = vertices[imax];
251 const int vi = vertices[i];
252 if (global_indices[vi] > global_indices[vmax])
259 edge_ratio_ok[f] = (min_len / max_len >= min_ratio);
261 long_edge[f] = face_edges[imax];
264 return std::pair(std::move(long_edge), std::move(edge_ratio_ok));
291template <std::
floating_po
int T>
292std::tuple<graph::AdjacencyList<std::int64_t>, std::vector<T>,
293 std::array<std::size_t, 2>, std::optional<std::vector<std::int32_t>>,
294 std::optional<std::vector<std::int8_t>>>
295compute_refinement(MPI_Comm neighbor_comm,
296 std::span<const std::int8_t> marked_edges,
299 std::span<const std::int32_t> long_edge,
300 std::span<const std::int8_t> edge_ratio_ok,
Option option)
302 int tdim =
mesh.topology()->dim();
303 int num_cell_edges = tdim * 3 - 3;
304 int num_cell_vertices = tdim + 1;
310 const auto [new_vertex_map, new_vertex_coords, xshape]
313 std::optional<std::vector<std::int32_t>>
parent_cell(std::nullopt);
314 if (compute_parent_cell)
317 std::optional<std::vector<std::int8_t>>
parent_facet(std::nullopt);
321 std::vector<std::int64_t> indices(num_cell_vertices + num_cell_edges);
323 auto map_c =
mesh.topology()->index_map(tdim);
325 auto c_to_v =
mesh.topology()->connectivity(tdim, 0);
327 auto c_to_e =
mesh.topology()->connectivity(tdim, 1);
329 auto c_to_f =
mesh.topology()->connectivity(tdim, 2);
332 std::int32_t num_new_vertices_local = std::count(
333 marked_edges.begin(),
334 marked_edges.begin() +
mesh.topology()->index_map(1)->size_local(),
true);
336 std::vector<std::int64_t> global_indices
339 const std::int32_t num_cells = map_c->size_local();
342 const std::size_t max_children = std::size_t(1) << tdim;
343 std::vector<std::int64_t> cell_topology;
344 cell_topology.reserve(
static_cast<std::size_t
>(num_cells) * max_children
345 * num_cell_vertices);
346 if (compute_parent_cell)
347 parent_cell->reserve(
static_cast<std::size_t
>(num_cells) * max_children);
350 parent_facet->reserve(
static_cast<std::size_t
>(num_cells) * max_children
351 * num_cell_vertices);
355 std::vector<std::int32_t> longest_edge;
356 for (
int c = 0; c < num_cells; ++c)
362 auto vertices = c_to_v->links(c);
363 for (std::size_t v = 0; v < vertices.size(); ++v)
364 indices[v] = global_indices[vertices[v]];
367 auto edges = c_to_e->links(c);
368 bool no_edge_marked =
true;
369 for (std::size_t ei = 0; ei < edges.size(); ++ei)
371 if (marked_edges[edges[ei]])
373 no_edge_marked =
false;
374 auto nv = new_vertex_map.links(edges[ei]);
375 assert(nv.size() == 1);
376 indices[num_cell_vertices + ei] = nv[0];
379 indices[num_cell_vertices + ei] = -1;
385 for (
auto v : vertices)
386 cell_topology.push_back(global_indices[v]);
388 if (compute_parent_cell)
403 longest_edge.clear();
404 for (
auto f : c_to_f->links(c))
405 longest_edge.push_back(long_edge[f]);
408 for (std::int32_t& p : longest_edge)
410 for (std::size_t ej = 0; ej < edges.size(); ++ej)
420 const bool uniform = (tdim == 2) ? edge_ratio_ok[c] :
false;
421 const auto [simplex_set_b, simplex_set_size]
422 = get_simplices(indices, longest_edge, tdim, uniform);
423 std::span<const std::int32_t> simplex_set(simplex_set_b.data(),
427 const std::int32_t ncells = simplex_set.size() / num_cell_vertices;
428 if (compute_parent_cell)
430 for (std::int32_t i = 0; i < ncells; ++i)
438 auto npf = compute_parent_facets<3>(simplex_set);
440 std::next(npf.begin(), simplex_set.size()));
444 auto npf = compute_parent_facets<2>(simplex_set);
446 std::next(npf.begin(), simplex_set.size()));
451 for (std::int32_t v : simplex_set)
452 cell_topology.push_back(indices[v]);
456 assert(cell_topology.size() % num_cell_vertices == 0);
457 std::vector<std::int32_t> offsets(
458 cell_topology.size() / num_cell_vertices + 1, 0);
459 for (std::size_t i = 0; i < offsets.size() - 1; ++i)
460 offsets[i + 1] = offsets[i] + num_cell_vertices;
463 return {std::move(cell_adj), std::move(new_vertex_coords), xshape,
477template <std::
floating_po
int T>
478std::tuple<graph::AdjacencyList<std::int64_t>, std::vector<T>,
479 std::array<std::size_t, 2>, std::optional<std::vector<std::int32_t>>,
480 std::optional<std::vector<std::int8_t>>>
482 std::optional<std::span<const std::int32_t>> edges,
486 auto topology =
mesh.topology();
489 if (topology->cell_type() != mesh::CellType::triangle
490 and topology->cell_type() != mesh::CellType::tetrahedron)
492 throw std::invalid_argument(
"Cell type not supported");
495 auto map_e = topology->index_map(1);
497 throw std::runtime_error(
"Edges must be initialised");
505 std::vector<std::int8_t> marked_edges(
506 map_e->size_local() + map_e->num_ghosts(), !edges.has_value());
507 std::vector<std::vector<std::int32_t>> marked_for_update(ranks.size());
511 for (
auto edge : edges.value())
513 if (!marked_edges[edge])
515 marked_edges[edge] =
true;
518 for (
int rank : edge_ranks.
links(edge))
519 marked_for_update[rank].push_back(edge);
525 MPI_Dist_graph_create_adjacent(
mesh.comm(), ranks.size(), ranks.data(),
526 MPI_UNWEIGHTED, ranks.size(), ranks.data(),
527 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm);
534 const auto [long_edge, edge_ratio_ok] = impl::face_long_edge(
mesh);
535 impl::enforce_rules(comm, edge_ranks, marked_edges, *topology, long_edge);
538 = impl::compute_refinement(comm, marked_edges, edge_ranks,
mesh,
539 long_edge, edge_ratio_ok, option);
540 MPI_Comm_free(&comm);
542 return {std::move(cell_adj), std::move(new_vertex_coords), xshape,