48std::vector<std::int8_t>
49compute_parent_facets(std::span<const std::int32_t> simplex_set)
52 assert(simplex_set.size() % (tdim + 1) == 0);
53 std::vector<std::int8_t>
parent_facet(simplex_set.size(), -1);
58 constexpr std::array<std::array<int, 3>, 3> facet_table_2d{
59 {{1, 2, 3}, {0, 2, 4}, {0, 1, 5}}};
61 constexpr std::array<std::array<int, 6>, 4> facet_table_3d{
67 const int ncells = simplex_set.size() / (tdim + 1);
68 for (
int fpi = 0; fpi < (tdim + 1); ++fpi)
71 for (
int cc = 0; cc < ncells; ++cc)
73 for (
int fci = 0; fci < (tdim + 1); ++fci)
76 std::array<int, tdim> cf, set_output;
78 int num_common_vertices;
79 if constexpr (tdim == 2)
81 for (
int j = 0; j < tdim; ++j)
82 cf[j] = simplex_set[cc * 3 + facet_table_2d[fci][j]];
83 std::sort(cf.begin(), cf.end());
84 auto it = std::set_intersection(
85 facet_table_2d[fpi].begin(), facet_table_2d[fpi].end(),
86 cf.begin(), cf.end(), set_output.begin());
87 num_common_vertices = std::distance(set_output.begin(), it);
91 for (
int j = 0; j < tdim; ++j)
92 cf[j] = simplex_set[cc * 4 + facet_table_3d[fci][j]];
93 std::sort(cf.begin(), cf.end());
94 auto it = std::set_intersection(
95 facet_table_3d[fpi].begin(), facet_table_3d[fpi].end(),
96 cf.begin(), cf.end(), set_output.begin());
97 num_common_vertices = std::distance(set_output.begin(), it);
100 if (num_common_vertices == tdim)
130std::vector<std::int32_t>
131get_simplices(std::span<const std::int64_t> indices,
132 std::span<const std::int32_t> longest_edge,
int tdim,
137void enforce_rules(MPI_Comm comm,
const graph::AdjacencyList<int>& shared_edges,
138 std::vector<std::int8_t>& marked_edges,
139 const mesh::Topology& topology,
140 std::span<const std::int32_t> long_edge);
151template <std::
floating_po
int T>
152std::pair<std::vector<std::int32_t>, std::vector<std::int8_t>>
153face_long_edge(
const mesh::Mesh<T>& mesh)
155 const int tdim = mesh.topology()->dim();
157 mesh.topology_mutable()->create_entities(1);
158 mesh.topology_mutable()->create_entities(2);
159 mesh.topology_mutable()->create_connectivity(2, 1);
160 mesh.topology_mutable()->create_connectivity(1, tdim);
161 mesh.topology_mutable()->create_connectivity(tdim, 2);
163 std::int64_t num_faces = mesh.topology()->index_map(2)->size_local()
164 + mesh.topology()->index_map(2)->num_ghosts();
167 std::vector<std::int32_t> long_edge(num_faces);
168 std::vector<std::int8_t> edge_ratio_ok;
172 const T min_ratio = sqrt(2.0) / 2.0;
174 edge_ratio_ok.resize(num_faces);
176 auto x_dofmap = mesh.geometry().dofmap();
178 auto c_to_v = mesh.topology()->connectivity(tdim, 0);
180 auto e_to_c = mesh.topology()->connectivity(1, tdim);
182 auto e_to_v = mesh.topology()->connectivity(1, 0);
186 auto map_e = mesh.topology()->index_map(1);
188 std::vector<T> edge_length(map_e->size_local() + map_e->num_ghosts());
189 for (std::size_t e = 0; e < edge_length.size(); ++e)
192 auto cells = e_to_c->links(e);
193 assert(!
cells.empty());
194 auto cell_vertices = c_to_v->links(
cells.front());
195 auto edge_vertices = e_to_v->links(e);
198 auto it0 = std::find(cell_vertices.begin(), cell_vertices.end(),
200 assert(it0 != cell_vertices.end());
201 const std::size_t local0 = std::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 = std::distance(cell_vertices.begin(), it1);
207 auto x_dofs = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
208 x_dofmap,
cells.front(), MDSPAN_IMPL_STANDARD_NAMESPACE::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::vector<std::int32_t>,
294 std::vector<std::int8_t>>
295compute_refinement(MPI_Comm neighbor_comm,
296 const std::vector<std::int8_t>& marked_edges,
297 const graph::AdjacencyList<int>& shared_edges,
298 const mesh::Mesh<T>& mesh,
299 const std::vector<std::int32_t>& long_edge,
300 const std::vector<std::int8_t>& edge_ratio_ok,
303 int tdim = mesh.topology()->dim();
304 int num_cell_edges = tdim * 3 - 3;
308 bool compute_parent_cell
313 const auto [new_vertex_map, new_vertex_coords, xshape]
318 std::vector<std::int64_t> indices(num_cell_vertices + num_cell_edges);
319 std::vector<std::int32_t> simplex_set;
321 auto map_c = mesh.topology()->index_map(tdim);
323 auto c_to_v = mesh.topology()->connectivity(tdim, 0);
325 auto c_to_e = mesh.topology()->connectivity(tdim, 1);
327 auto c_to_f = mesh.topology()->connectivity(tdim, 2);
330 std::int32_t num_new_vertices_local = std::count(
331 marked_edges.begin(),
332 marked_edges.begin() + mesh.topology()->index_map(1)->size_local(),
true);
334 std::vector<std::int64_t> global_indices
335 =
adjust_indices(*mesh.topology()->index_map(0), num_new_vertices_local);
337 const int num_cells = map_c->size_local();
340 std::vector<std::int64_t> cell_topology;
341 for (
int c = 0; c < num_cells; ++c)
347 auto vertices = c_to_v->links(c);
348 for (std::size_t v = 0; v < vertices.size(); ++v)
349 indices[v] = global_indices[vertices[v]];
352 auto edges = c_to_e->links(c);
353 bool no_edge_marked =
true;
354 for (std::size_t ei = 0; ei < edges.size(); ++ei)
356 if (marked_edges[edges[ei]])
358 no_edge_marked =
false;
359 auto it = new_vertex_map.find(edges[ei]);
360 assert(it != new_vertex_map.end());
370 for (
auto v : vertices)
371 cell_topology.push_back(global_indices[v]);
373 if (compute_parent_cell)
388 std::vector<std::int32_t> longest_edge;
389 for (
auto f : c_to_f->links(c))
390 longest_edge.push_back(long_edge[f]);
393 for (std::int32_t& p : longest_edge)
395 for (std::size_t ej = 0; ej < edges.size(); ++ej)
405 const bool uniform = (tdim == 2) ? edge_ratio_ok[c] : false;
408 simplex_set = get_simplices(indices, longest_edge, tdim, uniform);
413 if (compute_parent_cell)
415 for (std::int32_t i = 0; i < ncells; ++i)
421 std::vector<std::int8_t> npf;
423 npf = compute_parent_facets<3>(simplex_set);
425 npf = compute_parent_facets<2>(simplex_set);
430 for (std::int32_t v : simplex_set)
431 cell_topology.push_back(indices[v]);
435 assert(cell_topology.size() % num_cell_vertices == 0);
436 std::vector<std::int32_t> offsets(
437 cell_topology.size() / num_cell_vertices + 1, 0);
438 for (std::size_t i = 0; i < offsets.size() - 1; ++i)
439 offsets[i + 1] = offsets[i] + num_cell_vertices;
440 graph::AdjacencyList cell_adj(std::move(cell_topology), std::move(offsets));
442 return {std::move(cell_adj), std::move(new_vertex_coords), xshape,
457template <std::
floating_po
int T>
458std::tuple<mesh::Mesh<T>, std::vector<std::int32_t>, std::vector<std::int8_t>>
467 mesh.geometry().cmap(), new_coords, xshape,
468 mesh::GhostMode::none),
473 std::shared_ptr<const common::IndexMap> map_c
474 = mesh.topology()->index_map(mesh.topology()->dim());
475 const int num_ghost_cells = map_c->num_ghosts();
478 int max_ghost_cells = 0;
479 MPI_Allreduce(&num_ghost_cells, &max_ghost_cells, 1, MPI_INT, MPI_MAX,
484 ? mesh::GhostMode::none
485 : mesh::GhostMode::shared_facet;
486 return {partition<T>(mesh, cell_adj, std::span(new_coords), xshape,
487 redistribute, ghost_mode),
503template <std::
floating_po
int T>
504std::tuple<mesh::Mesh<T>, std::vector<std::int32_t>, std::vector<std::int8_t>>
506 bool redistribute,
Option option)
514 mesh.geometry().cmap(), new_vertex_coords, xshape,
515 mesh::GhostMode::none),
520 std::shared_ptr<const common::IndexMap> map_c
521 = mesh.topology()->index_map(mesh.topology()->dim());
522 const int num_ghost_cells = map_c->num_ghosts();
525 int max_ghost_cells = 0;
526 MPI_Allreduce(&num_ghost_cells, &max_ghost_cells, 1, MPI_INT, MPI_MAX,
531 ? mesh::GhostMode::none
532 : mesh::GhostMode::shared_facet;
534 return {partition<T>(mesh, cell_adj, new_vertex_coords, xshape,
535 redistribute, ghost_mode),
548template <std::
floating_po
int T>
549std::tuple<graph::AdjacencyList<std::int64_t>, std::vector<T>,
550 std::array<std::size_t, 2>, std::vector<std::int32_t>,
551 std::vector<std::int8_t>>
555 auto topology = mesh.topology();
558 if (topology->cell_type() != mesh::CellType::triangle
559 and topology->cell_type() != mesh::CellType::tetrahedron)
561 throw std::runtime_error(
"Cell type not supported");
564 auto map_e = topology->index_map(1);
566 throw std::runtime_error(
"Edges must be initialised");
573 std::vector<int> ranks(edge_ranks.
array().begin(), edge_ranks.
array().end());
574 std::sort(ranks.begin(), ranks.end());
575 ranks.erase(std::unique(ranks.begin(), ranks.end()), ranks.end());
578 std::transform(edge_ranks.
array().begin(), edge_ranks.
array().end(),
579 edge_ranks.
array().begin(),
582 auto it = std::lower_bound(ranks.begin(), ranks.end(), r);
583 assert(it != ranks.end() and *it == r);
584 return std::distance(ranks.begin(), it);
588 MPI_Dist_graph_create_adjacent(mesh.comm(), ranks.size(), ranks.data(),
589 MPI_UNWEIGHTED, ranks.size(), ranks.data(),
590 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm);
592 const auto [long_edge, edge_ratio_ok] = impl::face_long_edge(mesh);
594 = impl::compute_refinement(
596 std::vector<std::int8_t>(map_e->size_local() + map_e->num_ghosts(),
598 edge_ranks, mesh, long_edge, edge_ratio_ok, option);
599 MPI_Comm_free(&comm);
601 return {std::move(cell_adj), std::move(new_vertex_coords), xshape,
614template <std::
floating_po
int T>
615std::tuple<graph::AdjacencyList<std::int64_t>, std::vector<T>,
616 std::array<std::size_t, 2>, std::vector<std::int32_t>,
617 std::vector<std::int8_t>>
619 std::span<const std::int32_t> edges,
Option option)
622 auto topology = mesh.topology();
625 if (topology->cell_type() != mesh::CellType::triangle
626 and topology->cell_type() != mesh::CellType::tetrahedron)
628 throw std::runtime_error(
"Cell type not supported");
631 auto map_e = topology->index_map(1);
633 throw std::runtime_error(
"Edges must be initialised");
640 std::vector<int> ranks(edge_ranks.
array().begin(), edge_ranks.
array().end());
641 std::sort(ranks.begin(), ranks.end());
642 ranks.erase(std::unique(ranks.begin(), ranks.end()), ranks.end());
645 std::transform(edge_ranks.
array().begin(), edge_ranks.
array().end(),
646 edge_ranks.
array().begin(),
649 auto it = std::lower_bound(ranks.begin(), ranks.end(), r);
650 assert(it != ranks.end() and *it == r);
651 return std::distance(ranks.begin(), it);
655 std::vector<std::int8_t> marked_edges(
656 map_e->size_local() + map_e->num_ghosts(),
false);
657 std::vector<std::vector<std::int32_t>> marked_for_update(ranks.size());
658 for (
auto edge : edges)
660 if (!marked_edges[edge])
662 marked_edges[edge] =
true;
665 for (
int rank : edge_ranks.
links(edge))
666 marked_for_update[rank].push_back(edge);
671 MPI_Dist_graph_create_adjacent(mesh.comm(), ranks.size(), ranks.data(),
672 MPI_UNWEIGHTED, ranks.size(), ranks.data(),
673 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm);
680 const auto [long_edge, edge_ratio_ok] = impl::face_long_edge(mesh);
681 impl::enforce_rules(comm, edge_ranks, marked_edges, *topology, long_edge);
684 = impl::compute_refinement(comm, marked_edges, edge_ranks, mesh,
685 long_edge, edge_ratio_ok, option);
686 MPI_Comm_free(&comm);
688 return {std::move(cell_adj), std::move(new_vertex_coords), xshape,