13#include "graphbuild.h"
15#include <basix/mdspan.hpp>
18#include <dolfinx/graph/AdjacencyList.h>
19#include <dolfinx/graph/ordering.h>
20#include <dolfinx/graph/partition.h>
57void reorder_list(std::span<T> list, std::span<const std::int32_t> nodemap)
62 assert(list.size() % nodemap.size() == 0);
63 std::size_t degree = list.size() / nodemap.size();
64 const std::vector<T> orig(list.begin(), list.end());
65 for (std::size_t n = 0; n < nodemap.size(); ++n)
67 std::span links_old(orig.data() + n * degree, degree);
68 auto links_new = list.subspan(nodemap[n] * degree, degree);
69 std::ranges::copy(links_old, links_new.begin());
86template <std::
floating_po
int T>
87std::tuple<std::vector<std::int32_t>, std::vector<T>, std::vector<std::int32_t>>
89 std::span<const std::int32_t> facets)
91 auto topology =
mesh.topology();
93 const int tdim = topology->dim();
96 throw std::runtime_error(
97 "Cannot use mesh::locate_entities_boundary (boundary) for cells.");
101 mesh.topology_mutable()->create_connectivity(tdim - 1, 0);
102 mesh.topology_mutable()->create_connectivity(tdim - 1, dim);
103 std::vector<std::int32_t> vertices, entities;
105 auto f_to_v = topology->connectivity(tdim - 1, 0);
107 auto f_to_e = topology->connectivity(tdim - 1, dim);
109 for (
auto f : facets)
111 auto v = f_to_v->links(f);
112 vertices.insert(vertices.end(), v.begin(), v.end());
113 auto e = f_to_e->links(f);
114 entities.insert(entities.end(), e.begin(), e.end());
119 std::ranges::sort(vertices);
120 auto [unique_end, range_end] = std::ranges::unique(vertices);
121 vertices.erase(unique_end, range_end);
125 std::ranges::sort(entities);
126 auto [unique_end, range_end] = std::ranges::unique(entities);
127 entities.erase(unique_end, range_end);
132 auto x_dofmap =
mesh.geometry().dofmaps().front();
133 std::span<const T> x_nodes =
mesh.geometry().x();
136 mesh.topology_mutable()->create_connectivity(0, tdim);
137 mesh.topology_mutable()->create_connectivity(tdim, 0);
138 auto v_to_c = topology->connectivity(0, tdim);
140 auto c_to_v = topology->connectivity(tdim, 0);
142 std::vector<T> x_vertices(3 * vertices.size(), -1.0);
143 std::vector<std::int32_t> vertex_to_pos(v_to_c->num_nodes(), -1);
144 for (std::size_t i = 0; i < vertices.size(); ++i)
146 const std::int32_t v = vertices[i];
149 const std::int32_t c = v_to_c->links(v).front();
150 auto cell_vertices = c_to_v->links(c);
151 auto it = std::ranges::find(cell_vertices, v);
152 assert(it != cell_vertices.end());
153 const std::size_t local_pos
154 = std::ranges::distance(cell_vertices.begin(), it);
156 auto dofs = md::submdspan(x_dofmap, c, md::full_extent);
157 for (std::size_t j = 0; j < 3; ++j)
158 x_vertices[j * vertices.size() + i] = x_nodes[3 * dofs[local_pos] + j];
159 vertex_to_pos[v] = i;
162 return {std::move(entities), std::move(x_vertices), std::move(vertex_to_pos)};
214 MPI_Comm comm,
int nparts,
const std::vector<CellType>& cell_types,
215 const std::vector<std::span<const std::int64_t>>& cells)>;
234 std::optional<std::int32_t> max_facet_to_cell_links)
251 return [&, max_facet_to_cell_links](
252 const std::vector<CellType>& celltypes,
253 const std::vector<fem::ElementDofLayout>& doflayouts,
254 const std::vector<std::vector<int>>& ghost_owners,
255 std::vector<std::vector<std::int64_t>>& cells,
256 std::vector<std::vector<std::int64_t>>& cells_v,
257 std::vector<std::vector<std::int64_t>>& original_idx,
258 int num_threads) -> std::vector<std::int64_t>
264 spdlog::info(
"Build local dual graphs, re-order cells, and compute process "
265 "boundary vertices.");
267 std::vector<std::pair<std::vector<std::int64_t>,
int>> facets;
270 std::vector<std::span<const std::int64_t>> cells1_v_local;
271 for (std::size_t i = 0; i < celltypes.size(); ++i)
274 std::size_t num_owned_cells
276 cells1_v_local.emplace_back(cells_v[i].data(),
280 auto [
graph, unmatched_facets, max_v, _facet_attached_cells]
282 std::vector{cells1_v_local.back()},
283 max_facet_to_cell_links, num_threads);
286 facets.emplace_back(std::move(unmatched_facets), max_v);
289 const std::vector<std::int32_t> remap = reorder_fn(
graph);
292 const std::vector<std::int64_t>& orig_idx = original_idx[i];
293 std::vector<std::int64_t> _original_idx(orig_idx.size());
294 std::copy_n(orig_idx.rbegin(), ghost_owners[i].size(),
295 _original_idx.rbegin());
297 for (std::size_t j = 0; j < remap.size(); ++j)
298 _original_idx[remap[j]] = orig_idx[j];
300 original_idx[i] = _original_idx;
307 std::span(cells[i].data(), remap.size() * doflayouts[i].num_dofs()),
311 if (facets.size() == 1)
313 std::vector<std::int64_t>& vertices = facets.front().first;
316 std::ranges::sort(vertices);
317 auto [unique_end, range_end] = std::ranges::unique(vertices);
318 vertices.erase(unique_end, range_end);
323 if (!vertices.empty() and vertices.front() == -1)
324 vertices.erase(vertices.begin());
334 std::size_t num_facets = std::accumulate(
335 facets.begin(), facets.end(), std::size_t(0),
336 [](std::size_t x,
auto& y)
337 { return x + (y.second > 0 ? y.first.size() / y.second : 0); });
338 int max_v = std::ranges::max_element(facets, [](
auto& a,
auto& b)
339 {
return a.second < b.second; })
342 std::vector<std::int64_t> facets0_b(max_v * num_facets, -1);
343 std::vector<std::span<std::int64_t>> facets0(max_v);
344 for (
int j = 0; j < max_v; ++j)
345 facets0[j] = std::span(facets0_b.data() + j * num_facets, num_facets);
349 for (
const auto& [v_data, num_v] : facets)
351 for (
auto it = v_data.begin(); it != v_data.end(); it += num_v, ++row)
352 for (
int j = 0; j < num_v; ++j)
353 facets0[j][row] = *std::next(it, j);
358 std::vector<std::span<const std::int64_t>> facets0_view(facets0.begin(),
361 std::span<std::span<const std::int64_t>>(facets0_view));
365 std::vector<std::int64_t> vertices;
369 auto trim_len = [&facets0, max_v](std::int32_t row)
372 while (n > 0 and facets0[n - 1][row] < 0)
377 auto it = perm.begin();
378 while (it != perm.end())
380 std::int32_t row0 = *it;
381 int n = trim_len(row0);
385 auto it1 = std::find_if_not(it, perm.end(),
386 [&facets0, row0, n](std::int32_t row)
388 for (int j = 0; j < n; ++j)
389 if (facets0[j][row] != facets0[j][row0])
395 if (std::ranges::distance(it, it1) == 1)
397 for (
int j = 0; j < n; ++j)
398 vertices.push_back(facets0[j][row0]);
400 else if (std::ranges::distance(it, it1) > 2)
401 throw std::runtime_error(
"More than two matching facets found.");
408 std::ranges::sort(vertices);
409 auto [unique_end, range_end] = std::ranges::unique(vertices);
410 vertices.erase(unique_end, range_end);
428 std::span<const std::int64_t> cells);
438template <std::
floating_po
int T>
439std::vector<T>
h(
const Mesh<T>&
mesh, std::span<const std::int32_t> entities,
442 if (entities.empty())
443 return std::vector<T>();
445 return std::vector<T>(entities.size(), 0);
448 const auto [vertex_xdofs, xdof_shape]
452 std::span<const T> x =
mesh.geometry().x();
455 auto delta_norm = [](
auto&& p0,
auto&& p1)
458 for (std::size_t i = 0; i < 3; ++i)
459 norm += (p0[i] - p1[i]) * (p0[i] - p1[i]);
460 return std::sqrt(norm);
465 std::vector<T>
h(entities.size(), 0);
466 for (std::size_t e = 0; e < entities.size(); ++e)
469 std::span<const std::int32_t> e_vertices(
470 vertex_xdofs.data() + e * xdof_shape[1], xdof_shape[1]);
473 for (std::size_t i = 0; i < e_vertices.size(); ++i)
475 std::span<const T, 3> p0(x.data() + 3 * e_vertices[i], 3);
476 for (std::size_t j = i + 1; j < e_vertices.size(); ++j)
478 std::span<const T, 3> p1(x.data() + 3 * e_vertices[j], 3);
479 h[e] = std::max(
h[e], delta_norm(p0, p1));
490template <std::
floating_po
int T>
492 std::span<const std::int32_t> entities)
494 if (entities.empty())
495 return std::vector<T>();
497 auto topology =
mesh.topology();
499 if (topology->cell_type() == CellType::prism and dim == 2)
501 throw std::runtime_error(
502 "Cell normal computation for prism cells not yet supported.");
505 const int gdim =
mesh.geometry().dim();
509 std::span<const T> x =
mesh.geometry().x();
510 const auto [geometry_entities, eshape]
513 std::vector<T> n(entities.size() * 3);
516 case CellType::interval:
519 throw std::invalid_argument(
"Interval cell normal undefined in 3D.");
520 for (std::size_t i = 0; i < entities.size(); ++i)
523 std::array vertices{geometry_entities[i * eshape[1]],
524 geometry_entities[i * eshape[1] + 1]};
525 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
526 std::span<const T, 3>(x.data() + 3 * vertices[1], 3)};
530 std::ranges::transform(p[1], p[0], t.begin(),
531 [](
auto x,
auto y) { return x - y; });
533 T norm = std::sqrt(t[0] * t[0] + t[1] * t[1]);
534 std::span<T, 3> ni(n.data() + 3 * i, 3);
535 ni[0] = -t[1] / norm;
541 case CellType::triangle:
543 for (std::size_t i = 0; i < entities.size(); ++i)
546 std::array vertices = {geometry_entities[i * eshape[1] + 0],
547 geometry_entities[i * eshape[1] + 1],
548 geometry_entities[i * eshape[1] + 2]};
549 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
550 std::span<const T, 3>(x.data() + 3 * vertices[1], 3),
551 std::span<const T, 3>(x.data() + 3 * vertices[2], 3)};
554 std::array<T, 3> dp1, dp2;
555 std::ranges::transform(p[1], p[0], dp1.begin(),
556 [](
auto x,
auto y) { return x - y; });
557 std::ranges::transform(p[2], p[0], dp2.begin(),
558 [](
auto x,
auto y) { return x - y; });
561 std::array<T, 3> ni = math::cross(dp1, dp2);
562 T norm = std::sqrt(ni[0] * ni[0] + ni[1] * ni[1] + ni[2] * ni[2]);
563 std::ranges::transform(ni, std::next(n.begin(), 3 * i),
564 [norm](
auto x) { return x / norm; });
569 case CellType::quadrilateral:
572 for (std::size_t i = 0; i < entities.size(); ++i)
575 std::array vertices = {geometry_entities[i * eshape[1] + 0],
576 geometry_entities[i * eshape[1] + 1],
577 geometry_entities[i * eshape[1] + 2]};
578 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
579 std::span<const T, 3>(x.data() + 3 * vertices[1], 3),
580 std::span<const T, 3>(x.data() + 3 * vertices[2], 3)};
583 std::array<T, 3> dp1, dp2;
584 std::ranges::transform(p[1], p[0], dp1.begin(),
585 [](
auto x,
auto y) { return x - y; });
586 std::ranges::transform(p[2], p[0], dp2.begin(),
587 [](
auto x,
auto y) { return x - y; });
590 std::array<T, 3> ni = math::cross(dp1, dp2);
591 T norm = std::sqrt(ni[0] * ni[0] + ni[1] * ni[1] + ni[2] * ni[2]);
592 std::ranges::transform(ni, std::next(n.begin(), 3 * i),
593 [norm](
auto x) { return x / norm; });
599 throw std::invalid_argument(
600 "cell_normal not supported for this cell type.");
607template <std::
floating_po
int T>
609 std::span<const std::int32_t> entities)
611 if (entities.empty())
612 return std::vector<T>();
614 std::span<const T> x =
mesh.geometry().x();
617 const auto [e_to_g, eshape]
620 std::vector<T> x_mid(entities.size() * 3, 0);
621 for (std::size_t e = 0; e < entities.size(); ++e)
623 std::span<T, 3> p(x_mid.data() + 3 * e, 3);
624 std::span<const std::int32_t> rows(e_to_g.data() + e * eshape[1],
626 for (
auto row : rows)
628 std::span<const T, 3> xg(x.data() + 3 * row, 3);
629 std::ranges::transform(p, xg, p.begin(),
630 [size = rows.size()](
auto x,
auto y)
631 { return x + y / size; });
644template <std::
floating_po
int T>
645std::pair<std::vector<T>, std::array<std::size_t, 2>>
648 auto topology =
mesh.topology();
650 const int tdim = topology->dim();
655 const std::int32_t num_vertices = topology->index_map(0)->size_local()
656 + topology->index_map(0)->num_ghosts();
658 std::vector<std::int32_t> vertex_to_node(num_vertices);
659 for (
int cell_type_idx = 0,
660 num_cell_types = topology->entity_types(tdim).size();
661 cell_type_idx < num_cell_types; ++cell_type_idx)
663 auto x_dofmap =
mesh.geometry().dofmaps().at(cell_type_idx);
664 auto c_to_v = topology->connectivity({tdim, cell_type_idx}, {0, 0});
666 for (
int c = 0; c < c_to_v->num_nodes(); ++c)
668 auto x_dofs = md::submdspan(x_dofmap, c, md::full_extent);
669 auto vertices = c_to_v->links(c);
670 for (std::size_t i = 0; i < vertices.size(); ++i)
671 vertex_to_node[vertices[i]] = x_dofs[i];
676 std::span<const T> x_nodes =
mesh.geometry().x();
677 std::vector<T> x_vertices(3 * vertex_to_node.size(), 0.0);
678 for (std::size_t i = 0; i < vertex_to_node.size(); ++i)
680 std::int32_t pos = 3 * vertex_to_node[i];
681 for (std::size_t j = 0; j < 3; ++j)
682 x_vertices[j * vertex_to_node.size() + i] = x_nodes[pos + j];
685 return {std::move(x_vertices), {3, vertex_to_node.size()}};
691template <
typename Fn,
typename T>
693 std::vector<std::int8_t>, Fn,
695 md::extents<std::size_t, 3, md::dynamic_extent>>>::value;
712template <std::
floating_po
int T, MarkerFn<T> U>
714 U marker,
int entity_type_idx)
718 = md::mdspan<const T, md::extents<std::size_t, 3, md::dynamic_extent>>;
721 const auto [xdata, xshape] = impl::compute_vertex_coords(
mesh);
723 cmdspan3x_t x(xdata.data(), xshape);
724 const std::vector<std::int8_t> marked = marker(x);
725 if (marked.size() != x.extent(1))
726 throw std::runtime_error(
"Length of array of markers is wrong.");
728 auto topology =
mesh.topology();
730 const int tdim = topology->dim();
732 mesh.topology_mutable()->create_entities(dim);
734 mesh.topology_mutable()->create_connectivity(dim, 0);
738 auto e_to_v = topology->connectivity({dim, entity_type_idx}, {0, 0});
740 std::vector<std::int32_t> entities;
741 for (
int e = 0; e < e_to_v->num_nodes(); ++e)
744 bool all_vertices_marked =
true;
745 for (std::int32_t v : e_to_v->links(e))
749 all_vertices_marked =
false;
754 if (all_vertices_marked)
755 entities.push_back(e);
774template <std::
floating_po
int T, MarkerFn<T> U>
778 const int num_entity_types =
mesh.topology()->entity_types(dim).size();
779 if (num_entity_types > 1)
781 throw std::runtime_error(
782 "Multiple entity types of this dimension. Specify entity type index");
810template <std::
floating_po
int T, MarkerFn<T> U>
815 auto topology =
mesh.topology();
817 int tdim = topology->dim();
820 throw std::runtime_error(
821 "Cannot use mesh::locate_entities_boundary (boundary) for cells.");
825 mesh.topology_mutable()->create_entities(tdim - 1);
826 mesh.topology_mutable()->create_connectivity(tdim - 1, tdim);
830 = md::mdspan<const T, md::extents<std::size_t, 3, md::dynamic_extent>>;
833 auto [facet_entities, xdata, vertex_to_pos]
834 = impl::compute_vertex_coords_boundary(
mesh, dim, boundary_facets);
835 cmdspan3x_t x(xdata.data(), 3, xdata.size() / 3);
836 std::vector<std::int8_t> marked = marker(x);
837 if (marked.size() != x.extent(1))
838 throw std::runtime_error(
"Length of array of markers is wrong.");
841 mesh.topology_mutable()->create_entities(dim);
842 auto e_to_v = topology->connectivity(dim, 0);
844 std::vector<std::int32_t> entities;
845 for (
auto e : facet_entities)
848 bool all_vertices_marked =
true;
849 for (
auto v : e_to_v->links(e))
851 const std::int32_t pos = vertex_to_pos[v];
854 all_vertices_marked =
false;
860 if (all_vertices_marked)
861 entities.push_back(e);
885template <std::
floating_po
int T>
886std::pair<std::vector<std::int32_t>, std::array<std::size_t, 2>>
888 std::span<const std::int32_t> entities,
889 bool permute =
false)
891 auto topology =
mesh.topology();
893 CellType cell_type = topology->cell_type();
894 if ((cell_type == CellType::prism or cell_type == CellType::pyramid)
897 throw std::runtime_error(
"mesh::entities_to_geometry for prism/pyramid "
898 "cell facets not yet supported.");
901 const int tdim = topology->dim();
903 auto xdofs =
geometry.dofmaps().front();
909 std::vector<std::int32_t> entity_xdofs;
910 entity_xdofs.reserve(entities.size() * num_entity_dofs);
911 std::array<std::size_t, 2> eshape{entities.size(), num_entity_dofs};
914 const std::vector<std::vector<std::vector<int>>>& closure_dofs_all
920 for (std::int32_t c : entities)
923 auto x_c = md::submdspan(xdofs, c, md::full_extent);
924 for (std::int32_t entity_dof : closure_dofs_all[tdim][0])
925 entity_xdofs.push_back(x_c[entity_dof]);
928 return {std::move(entity_xdofs), eshape};
933 auto e_to_c = topology->connectivity(dim, tdim);
936 throw std::runtime_error(std::format(
937 "Entity-to-cell connectivity has not been computed. Missing dims "
942 auto c_to_e = topology->connectivity(tdim, dim);
945 throw std::runtime_error(std::format(
946 "Cell-to-entity connectivity has not been computed. Missing dims "
952 std::span<const std::uint32_t> cell_info;
954 cell_info = std::span(
mesh.topology()->get_cell_permutation_info());
956 for (std::int32_t e : entities)
959 assert(!e_to_c->links(e).empty());
960 std::int32_t c = e_to_c->links(e).front();
963 std::span<const std::int32_t> cell_entities = c_to_e->links(c);
964 auto it = std::find(cell_entities.begin(), cell_entities.end(), e);
965 assert(it != cell_entities.end());
966 std::size_t local_entity = std::ranges::distance(cell_entities.begin(), it);
970 std::vector<std::int32_t> closure_dofs(closure_dofs_all[dim][local_entity]);
976 entity_type, local_entity);
980 auto x_c = md::submdspan(xdofs, c, md::full_extent);
981 for (std::int32_t entity_dof : closure_dofs)
982 entity_xdofs.push_back(x_c[entity_dof]);
985 return {std::move(entity_xdofs), eshape};
1001 std::optional<std::int32_t> max_facet_to_cell_links);
1014 std::optional<std::int32_t> max_facet_to_cell_links);
1023std::vector<std::int32_t>
1025 std::span<const std::int32_t> entities,
int d0,
1073template <
typename U>
1075 MPI_Comm comm, MPI_Comm commt,
1076 std::vector<std::span<const std::int64_t>> cells,
1078 typename std::remove_reference_t<typename U::value_type>>>& elements,
1079 MPI_Comm commg,
const U& x, std::array<std::size_t, 2> xshape,
1081 std::optional<std::int32_t> max_facet_to_cell_links,
int num_threads,
1084 if (cells.size() != elements.size())
1085 throw std::runtime_error(
"Number of cell arrays and elements must match.");
1086 std::vector<CellType> celltypes;
1087 std::ranges::transform(elements, std::back_inserter(celltypes),
1088 [](
auto& e) {
return e.cell_shape(); });
1089 std::vector<fem::ElementDofLayout> doflayouts;
1090 std::ranges::transform(elements, std::back_inserter(doflayouts),
1091 [](
auto& e) {
return e.create_dof_layout(); });
1100 std::int32_t num_cell_types = cells.size();
1103 std::vector<std::vector<std::int64_t>> cells1(num_cell_types);
1104 std::vector<std::vector<std::int64_t>> original_idx1(num_cell_types);
1105 std::vector<std::vector<int>> ghost_owners(num_cell_types);
1108 spdlog::info(
"Using partitioner with cell data ({} cell types)",
1111 if (commt != MPI_COMM_NULL)
1114 std::vector<std::vector<std::int64_t>> t(num_cell_types);
1115 std::vector<std::span<const std::int64_t>> tspan(num_cell_types);
1116 for (std::int32_t i = 0; i < num_cell_types; ++i)
1119 tspan[i] = std::span(t[i]);
1121 dest = partitioner(commt, size, celltypes, tspan);
1124 std::int32_t cell_offset = 0;
1125 for (std::int32_t i = 0; i < num_cell_types; ++i)
1127 std::size_t num_cell_nodes = doflayouts[i].num_dofs();
1128 if (cells[i].size() % num_cell_nodes != 0)
1130 throw std::runtime_error(
"Cell array size is not a multiple of the "
1131 "number of nodes per cell.");
1133 std::size_t num_cells = cells[i].size() / num_cell_nodes;
1136 std::vector<std::int32_t> offsets_i(
1137 std::next(dest.
offsets().begin(), cell_offset),
1138 std::next(dest.
offsets().begin(), cell_offset + num_cells + 1));
1139 std::vector<std::int32_t> data_i(
1140 std::next(dest.
array().begin(), offsets_i.front()),
1141 std::next(dest.
array().begin(), offsets_i.back()));
1142 std::int32_t offset_0 = offsets_i.front();
1143 std::ranges::for_each(offsets_i,
1144 [&offset_0](std::int32_t& j) { j -= offset_0; });
1146 cell_offset += num_cells;
1150 std::vector<int> src_ranks;
1151 std::tie(cells1[i], src_ranks, original_idx1[i], ghost_owners[i])
1153 {num_cells, num_cell_nodes}, dest_i);
1154 spdlog::debug(
"Got {} cells from distribution", cells1[i].size());
1160 std::int64_t num_owned = 0;
1161 for (std::int32_t i = 0; i < num_cell_types; ++i)
1163 cells1[i] = std::vector<std::int64_t>(cells[i].begin(), cells[i].end());
1164 std::int32_t num_cell_nodes = doflayouts[i].num_dofs();
1165 if (cells1[i].size() % num_cell_nodes != 0)
1167 throw std::runtime_error(
"Cell array size is not a multiple of the "
1168 "number of nodes per cell.");
1170 original_idx1[i].resize(cells1[i].size() / num_cell_nodes);
1171 num_owned += original_idx1[i].size();
1175 std::int64_t global_offset = 0;
1176 MPI_Exscan(&num_owned, &global_offset, 1, MPI_INT64_T, MPI_SUM, comm);
1177 for (std::int32_t i = 0; i < num_cell_types; ++i)
1179 std::iota(original_idx1[i].begin(), original_idx1[i].end(),
1181 global_offset += original_idx1[i].size();
1187 std::vector<std::vector<std::int64_t>> cells1_v(num_cell_types);
1188 for (std::int32_t i = 0; i < num_cell_types; ++i)
1191 spdlog::info(
"Extract basic topology: {}->{}", cells1[i].size(),
1192 cells1_v[i].size());
1197 const std::vector<std::int64_t> boundary_v
1198 = boundary_v_fn(celltypes, doflayouts, ghost_owners, cells1, cells1_v,
1199 original_idx1, num_threads);
1201 spdlog::debug(
"Got {} boundary vertices", boundary_v.size());
1204 std::vector<std::span<const std::int64_t>> cells1_v_span;
1205 std::ranges::transform(cells1_v, std::back_inserter(cells1_v_span),
1206 [](
auto& c) {
return std::span(c); });
1207 std::vector<std::span<const std::int64_t>> original_idx1_span;
1208 std::ranges::transform(original_idx1, std::back_inserter(original_idx1_span),
1209 [](
auto& c) {
return std::span(c); });
1210 std::vector<std::span<const int>> ghost_owners_span;
1211 std::ranges::transform(ghost_owners, std::back_inserter(ghost_owners_span),
1212 [](
auto& c) {
return std::span(c); });
1215 ghost_owners_span, boundary_v, num_threads);
1219 for (
int i = 0; i < num_cell_types; ++i)
1221 const auto& entity_dofs = doflayouts[i].entity_dofs_all();
1222 for (
int dim = 1; dim < topology.dim(); ++dim)
1226 = std::accumulate(entity_dofs[dim].begin(), entity_dofs[dim].end(), 0,
1227 [](
int c,
auto v) {
return c + v.size(); });
1229 spdlog::debug(
"Counting entity dofs, dim={}: {}", dim, dim_sum);
1231 topology.create_entities(dim);
1234 if (elements[i].needs_dof_permutations())
1235 topology.create_entity_permutations();
1240 std::vector<std::int64_t> nodes1, nodes2;
1241 for (std::vector<std::int64_t>& c : cells1)
1242 nodes1.insert(nodes1.end(), c.begin(), c.end());
1243 for (std::vector<std::int64_t>& c : cells1)
1244 nodes2.insert(nodes2.end(), c.begin(), c.end());
1247 auto [unique_end, range_end] = std::ranges::unique(nodes1);
1248 nodes1.erase(unique_end, range_end);
1255 =
create_geometry(topology, elements, nodes1, nodes2, coords, xshape[1]);
1257 return Mesh(comm, std::make_shared<Topology>(std::move(topology)),
1300template <
typename U>
1302 MPI_Comm comm, MPI_Comm commt, std::span<const std::int64_t> cells,
1304 typename std::remove_reference_t<typename U::value_type>>& element,
1305 MPI_Comm commg,
const U& x, std::array<std::size_t, 2> xshape,
1307 std::optional<std::int32_t> max_facet_to_cell_links,
int num_threads,
1310 return create_mesh(comm, commt, std::vector{cells}, std::vector{element},
1311 commg, x, xshape, partitioner, max_facet_to_cell_links,
1312 num_threads, reorder_fn);
1335template <
typename U>
1336Mesh<typename std::remove_reference_t<typename U::value_type>>
1339 std::remove_reference_t<typename U::value_type>>& elements,
1340 const U& x, std::array<std::size_t, 2> xshape,
GhostMode ghost_mode,
1341 std::optional<std::int32_t> max_facet_to_cell_links = 2)
1345 return create_mesh(comm, comm, std::vector{cells}, std::vector{elements},
1346 comm, x, xshape,
nullptr, max_facet_to_cell_links, 1);
1351 comm, comm, std::vector{cells}, std::vector{elements}, comm, x, xshape,
1353 max_facet_to_cell_links, 1);
1370template <std::
floating_po
int T>
1371std::pair<Geometry<T>, std::vector<int32_t>>
1373 std::span<const std::int32_t> subentity_to_entity)
1380 =
geometry.cmaps().front().create_dof_layout();
1382 const std::vector<std::int32_t> x_indices
1385 std::vector<std::int32_t> sub_x_dofs = x_indices;
1386 std::ranges::sort(sub_x_dofs);
1387 auto [unique_end, range_end] = std::ranges::unique(sub_x_dofs);
1388 sub_x_dofs.erase(unique_end, range_end);
1391 auto x_index_map =
geometry.index_map();
1392 assert(x_index_map);
1394 std::shared_ptr<common::IndexMap> sub_x_dof_index_map;
1395 std::vector<std::int32_t> subx_to_x_dofmap;
1399 sub_x_dof_index_map = std::make_shared<common::IndexMap>(std::move(map));
1400 subx_to_x_dofmap = std::move(new_to_old);
1404 std::span<const T> x =
geometry.x();
1405 std::int32_t sub_num_x_dofs = subx_to_x_dofmap.size();
1406 std::vector<T> sub_x(3 * sub_num_x_dofs);
1407 for (std::int32_t i = 0; i < sub_num_x_dofs; ++i)
1409 std::copy_n(std::next(x.begin(), 3 * subx_to_x_dofmap[i]), 3,
1410 std::next(sub_x.begin(), 3 * i));
1414 std::vector<std::int32_t> x_to_subx_dof_map(
1415 x_index_map->size_local() + x_index_map->num_ghosts(), -1);
1416 for (std::size_t i = 0; i < subx_to_x_dofmap.size(); ++i)
1417 x_to_subx_dof_map[subx_to_x_dofmap[i]] = i;
1420 std::vector<std::int32_t> sub_x_dofmap;
1421 sub_x_dofmap.reserve(x_indices.size());
1422 std::ranges::transform(x_indices, std::back_inserter(sub_x_dofmap),
1423 [&x_to_subx_dof_map](
auto x_dof)
1425 assert(x_to_subx_dof_map[x_dof] != -1);
1426 return x_to_subx_dof_map[x_dof];
1436 = (sub_xcell == CellType::point) ? 0 :
geometry.cmaps().front().degree();
1438 geometry.cmaps().front().variant());
1441 const std::vector<std::int64_t>& igi =
geometry.input_global_indices();
1442 std::vector<std::int64_t> sub_igi;
1443 sub_igi.reserve(subx_to_x_dofmap.size());
1444 std::ranges::transform(subx_to_x_dofmap, std::back_inserter(sub_igi),
1445 [&igi](
auto sub_x_dof) {
return igi[sub_x_dof]; });
1449 sub_x_dof_index_map,
1450 std::vector<std::vector<std::int32_t>>{std::move(sub_x_dofmap)},
1451 {sub_cmap}, std::move(sub_x),
geometry.dim(), std::move(sub_igi)),
1452 std::move(subx_to_x_dofmap)};
1464template <std::
floating_po
int T>
1465std::tuple<Mesh<T>, EntityMap, EntityMap, std::vector<std::int32_t>>
1467 std::span<const std::int32_t> entities)
1470 mesh.topology_mutable()->create_connectivity(dim, 0);
1471 auto [topology, subentity_to_entity, subvertex_to_vertex]
1475 const int tdim =
mesh.topology()->dim();
1476 mesh.topology_mutable()->create_entities(dim);
1477 mesh.topology_mutable()->create_connectivity(dim, tdim);
1478 mesh.topology_mutable()->create_connectivity(tdim, dim);
1479 mesh.topology_mutable()->create_entity_permutations();
1484 =
Mesh(
mesh.comm(), std::make_shared<Topology>(std::move(topology)),
1487 subentity_to_entity);
1489 subvertex_to_vertex);
1490 return {std::move(submesh), std::move(entity_map), std::move(vertex_map),
1491 std::move(subx_to_x_dofmap)};
1501template <
typename T>
1504 std::shared_ptr<const dolfinx::mesh::Topology> submesh_topology,
1507 int tag_dim = tags.
dim();
1508 int submesh_tdim = submesh_topology->dim();
1510 if (tag_dim > submesh_tdim)
1512 throw std::runtime_error(
"Tag dimension must be less than or equal to "
1513 "submesh dimension");
1515 std::shared_ptr<const dolfinx::common::IndexMap> sub_cell_imap
1516 = submesh_topology->index_map(submesh_tdim);
1519 throw std::runtime_error(
1520 std::format(
"Entities of dimension {} does not exist in mesh topology.",
1525 std::int32_t submesh_num_cells
1526 = sub_cell_imap->size_local() + sub_cell_imap->num_ghosts();
1527 auto sub_cells = std::ranges::views::iota(0, submesh_num_cells);
1528 std::vector<std::int32_t> sub_cell_to_parent_entity
1533 auto parent_entity_imap = topology->index_map(submesh_tdim);
1534 if (!parent_entity_imap)
1536 throw std::runtime_error(std::format(
1537 "Entities of dimension {} does not exist in parent mesh topology.",
1540 std::size_t num_parent_entities
1541 = parent_entity_imap->size_local() + parent_entity_imap->num_ghosts();
1542 std::vector<std::int32_t> parent_entity_to_sub_cell(num_parent_entities, -1);
1543 for (std::size_t i = 0; i < sub_cell_to_parent_entity.size(); ++i)
1544 parent_entity_to_sub_cell[sub_cell_to_parent_entity[i]]
1545 =
static_cast<std::int32_t
>(i);
1548 std::vector<std::int32_t> sub_to_parent_vertex;
1550 auto sub_vertex_map = submesh_topology->index_map(0);
1551 std::int32_t num_sub_vertices
1552 = sub_vertex_map->size_local() + sub_vertex_map->num_ghosts();
1553 auto sub_vertices = std::ranges::views::iota(0, num_sub_vertices);
1555 sub_to_parent_vertex
1559 auto sub_e_to_v = submesh_topology->connectivity(tag_dim, 0);
1560 auto sub_c_to_e = submesh_topology->connectivity(submesh_tdim, tag_dim);
1561 auto sub_entity_imap = submesh_topology->index_map(tag_dim);
1562 auto e_to_v = topology->connectivity(tag_dim, 0);
1563 std::shared_ptr<const dolfinx::graph::AdjacencyList<std::int32_t>>
1564 e_to_sub_cell =
nullptr;
1565 if (tag_dim != submesh_tdim)
1567 e_to_sub_cell = topology->connectivity(tag_dim, submesh_tdim);
1570 throw std::runtime_error(
1571 std::format(
"Missing connectivity between {} and {} in parent mesh",
1572 tag_dim, submesh_tdim));
1578 throw std::runtime_error(std::format(
1579 "Missing connectivity between {} and {} in submesh", tag_dim, 0));
1583 throw std::runtime_error(
1584 std::format(
"Missing connectivity between {} and {} in submesh",
1585 submesh_tdim, tag_dim));
1587 if (!sub_entity_imap)
1589 throw std::runtime_error(std::format(
1590 "Entities of dimension {} does not exist in submesh topology.",
1595 throw std::runtime_error(
1596 std::format(
"Missing connectivity between {} and 0", tag_dim));
1600 std::size_t num_sub_entities
1601 = sub_entity_imap->size_local() + sub_entity_imap->num_ghosts();
1602 constexpr T max_val = std::numeric_limits<T>::max();
1603 std::vector<T> submesh_values(num_sub_entities, max_val);
1604 std::vector<std::int32_t> submesh_indices(num_sub_entities);
1605 std::iota(submesh_indices.begin(), submesh_indices.end(), 0);
1607 std::span<const std::int32_t> tagged_entities = tags.
indices();
1608 std::span<const T> tagged_values = tags.
values();
1611 for (std::size_t i = 0; i < tagged_entities.size(); ++i)
1613 auto find_and_map_sub_entity
1614 = [tag_dim, submesh_tdim, &e_to_v, &parent_entity_to_sub_cell,
1615 &sub_to_parent_vertex, &sub_e_to_v, &sub_c_to_e,
1616 &e_to_sub_cell](std::int32_t entity)
1620 if (tag_dim == submesh_tdim)
1621 return parent_entity_to_sub_cell[entity];
1625 auto entity_vertices = e_to_v->links(entity);
1626 auto parent_sub_cells = e_to_sub_cell->links(entity);
1629 | std::views::transform([&parent_entity_to_sub_cell](
auto c)
1630 {
return parent_entity_to_sub_cell[c]; })
1631 | std::views::filter([](
auto sub_cell) {
return sub_cell != -1; });
1632 for (
auto sub_cell : submesh_cells)
1634 for (
auto sub_entity : sub_c_to_e->links(sub_cell))
1637 auto parent_vertices
1638 = sub_e_to_v->links(sub_entity)
1639 | std::views::transform([&sub_to_parent_vertex](
auto v)
1640 {
return sub_to_parent_vertex[v]; });
1644 bool entity_matches = std::ranges::all_of(
1646 [&entity_vertices](
auto p_v)
1649 return std::ranges::find(entity_vertices, p_v)
1650 != std::ranges::end(entity_vertices);
1662 std::int32_t sub_entity = find_and_map_sub_entity(tagged_entities[i]);
1663 if (sub_entity != -1)
1664 submesh_values[sub_entity] = tagged_values[i];
1668 std::vector<std::int32_t> filtered_indices;
1669 std::vector<T> filtered_values;
1670 filtered_indices.reserve(num_sub_entities);
1671 filtered_values.reserve(num_sub_entities);
1672 for (std::size_t i = 0; i < submesh_values.size(); ++i)
1674 if (submesh_values[i] != max_val)
1676 filtered_indices.push_back(submesh_indices[i]);
1677 filtered_values.push_back(submesh_values[i]);
1680 filtered_indices.shrink_to_fit();
1681 filtered_values.shrink_to_fit();
1682 MeshTags<T> new_meshtag(submesh_topology, tag_dim, filtered_indices,
1683 filtered_values, tags.
name());
Definition CoordinateElement.h:38
ElementDofLayout create_dof_layout() const
Compute and return the dof layout.
Definition CoordinateElement.cpp:79
void permute_subentity_closure(std::span< std::int32_t > d, std::uint32_t cell_info, mesh::CellType entity_type, int entity_index) const
Given the closure DOFs of a cell sub-entity in reference ordering, this function computes the permut...
Definition CoordinateElement.cpp:68
Definition ElementDofLayout.h:31
const std::vector< int > & entity_closure_dofs(int dim, int entity_index) const
Definition ElementDofLayout.cpp:65
const std::vector< std::vector< std::vector< int > > > & entity_closure_dofs_all() const
Definition ElementDofLayout.cpp:77
This class provides a static adjacency list data structure.
Definition AdjacencyList.h:41
const std::vector< LinkData > & array() const
Return contiguous array of links for all nodes (const version).
Definition AdjacencyList.h:188
const std::vector< std::int32_t > & offsets() const
Offset for each node in array() (const version).
Definition AdjacencyList.h:194
A bidirectional map relating entities in one topology to another.
Definition EntityMap.h:22
std::vector< std::int32_t > sub_topology_to_topology(CellRange auto &&entities, bool inverse) const
Map entities between the sub-topology and the parent topology.
Definition EntityMap.h:104
Geometry stores the geometry imposed on a mesh.
Definition Geometry.h:37
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
std::shared_ptr< Topology > topology()
Get mesh topology.
Definition Mesh.h:69
Topology stores the topology of a mesh, consisting of mesh entities and connectivity (incidence relat...
Definition Topology.h:49
Requirements on function for geometry marking.
Definition utils.h:692
void reorder_list(std::span< T > list, std::span< const std::int32_t > nodemap)
Re-order the nodes of a fixed-degree adjacency list.
Definition utils.h:57
std::tuple< std::vector< std::int32_t >, std::vector< T >, std::vector< std::int32_t > > compute_vertex_coords_boundary(const mesh::Mesh< T > &mesh, int dim, std::span< const std::int32_t > facets)
Compute the coordinates of 'vertices' for entities of a given dimension that are attached to specifie...
Definition utils.h:88
std::pair< std::vector< T >, std::array< std::size_t, 2 > > compute_vertex_coords(const mesh::Mesh< T > &mesh)
The coordinates for all 'vertices' in the mesh.
Definition utils.h:646
int size(MPI_Comm comm)
Definition MPI.cpp:81
std::vector< std::ranges::range_value_t< U > > distribute_data(MPI_Comm comm0, std::span< const std::int64_t > indices, MPI_Comm comm1, const U &x, int shape1)
Distribute rows of a row-major array to the ranks that require them, via the post office pattern.
Definition MPI.h:734
std::pair< IndexMap, std::vector< std::int32_t > > create_sub_index_map(const IndexMap &imap, std::span< const std::int32_t > indices, IndexMapOrder order=IndexMapOrder::any, bool allow_owner_change=false)
Create a new index map from a subset of indices in an existing index map.
Definition IndexMap.cpp:825
@ any
Allow arbitrary ordering of ghost indices in sub-maps.
Definition IndexMap.h:27
Finite element method functionality.
Definition assemble_expression_impl.h:23
Geometry data structures and algorithms.
Definition BoundingBoxTree.h:24
std::tuple< graph::AdjacencyList< std::int64_t >, std::vector< int >, std::vector< std::int64_t >, std::vector< int > > distribute(MPI_Comm comm, const graph::AdjacencyList< std::int64_t > &list, const graph::AdjacencyList< std::int32_t > &destinations)
Distribute adjacency list nodes to destination ranks.
Definition partition.cpp:40
Graph data structures and algorithms.
Definition AdjacencyList.h:23
std::function< graph::AdjacencyList< std::int32_t >( MPI_Comm, int, const AdjacencyList< std::int64_t > &, bool)> partition_fn
Signature of functions for computing the parallel partitioning of a distributed graph.
Definition partition.h:31
std::vector< std::int32_t > reorder_rcm(const graph::AdjacencyList< std::int32_t > &graph)
Re-order a graph using the Reverse Cuthill-McKee algorithm.
Definition ordering.cpp:149
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
CellPartitionFunction create_cell_partitioner(mesh::GhostMode ghost_mode, graph::partition_fn partfn, std::optional< std::int32_t > max_facet_to_cell_links)
Create a function that computes destination rank for mesh cells on this rank by applying the default ...
Definition utils.cpp:102
Mesh< typename std::remove_reference_t< typename U::value_type > > create_mesh(MPI_Comm comm, MPI_Comm commt, std::vector< std::span< const std::int64_t > > cells, const std::vector< fem::CoordinateElement< typename std::remove_reference_t< typename U::value_type > > > &elements, MPI_Comm commg, const U &x, std::array< std::size_t, 2 > xshape, const CellPartitionFunction &partitioner, std::optional< std::int32_t > max_facet_to_cell_links, int num_threads, const CellReorderFunction &reorder_fn=graph::reorder_rcm)
Create a distributed mesh::Mesh from mesh data and using the provided graph partitioning function for...
Definition utils.h:1074
std::function< std::vector< std::int32_t >( const graph::AdjacencyList< std::int32_t > &)> CellReorderFunction
Function that reorders (locally) cells that are owned by this process. It takes the local mesh dual g...
Definition utils.h:221
std::tuple< graph::AdjacencyList< std::int32_t >, std::vector< std::int64_t >, int, std::vector< std::int32_t > > build_local_dual_graph(std::span< const CellType > celltypes, const std::vector< std::span< const std::int64_t > > &cells, std::optional< std::int32_t > max_facet_to_cell_links, int num_threads)
Compute the local part of the dual graph (cell-cell connections via facets) and facets with only one ...
Definition graphbuild.cpp:566
MeshTags< T > transfer_meshtags_to_submesh(const MeshTags< T > &tags, std::shared_ptr< const dolfinx::mesh::Topology > submesh_topology, const EntityMap &vertex_map, const EntityMap &cell_map)
Transfer a meshtags object from a parent to a submesh.
Definition utils.h:1502
std::function< graph::AdjacencyList< std::int32_t >( MPI_Comm comm, int nparts, const std::vector< CellType > &cell_types, const std::vector< std::span< const std::int64_t > > &cells)> CellPartitionFunction
Signature for the cell partitioning function. Function that implement this interface compute the dest...
Definition utils.h:213
std::vector< T > cell_normals(const Mesh< T > &mesh, int dim, std::span< const std::int32_t > entities)
Compute normal to given cell (viewed as embedded in 3D).
Definition utils.h:491
std::tuple< Topology, std::vector< int32_t >, std::vector< int32_t > > create_subtopology(const Topology &topology, int dim, std::span< const std::int32_t > entities)
Create a topology for a subset of entities of a given topological dimension.
Definition Topology.cpp:1521
std::tuple< Mesh< T >, EntityMap, EntityMap, std::vector< std::int32_t > > create_submesh(const Mesh< T > &mesh, int dim, std::span< const std::int32_t > entities)
Create a new mesh consisting of a subset of entities in a mesh.
Definition utils.h:1466
std::vector< std::int32_t > exterior_facet_indices(const Topology &topology, int facet_type_idx)
Compute the indices of all exterior facets that are owned by the caller.
Definition utils.cpp:61
std::vector< std::int32_t > locate_entities_boundary(const Mesh< T > &mesh, int dim, U marker)
Compute indices of all mesh entities that are attached to an owned boundary facet and evaluate to tru...
Definition utils.h:811
CellType
Cell type identifier.
Definition cell_types.h:22
int num_cell_vertices(CellType type)
Number vertices for a cell type.
Definition cell_types.cpp:100
std::vector< T > h(const Mesh< T > &mesh, std::span< const std::int32_t > entities, int dim)
Compute greatest distance between any two vertices of the mesh entities (h).
Definition utils.h:439
std::pair< Geometry< T >, std::vector< int32_t > > create_subgeometry(const Mesh< T > &mesh, int dim, std::span< const std::int32_t > subentity_to_entity)
Create a sub-geometry from a mesh and a subset of mesh entities to be included.
Definition utils.h:1372
auto create_boundary_vertices_fn(const CellReorderFunction &reorder_fn, std::optional< std::int32_t > max_facet_to_cell_links)
Creates the default boundary vertices routine for a given reorder function.
Definition utils.h:233
Geometry< typename std::remove_reference_t< typename U::value_type > > create_geometry(const Topology &topology, const std::vector< fem::CoordinateElement< std::remove_reference_t< typename U::value_type > > > &elements, std::span< const std::int64_t > nodes, std::span< const std::int64_t > xdofs, const U &x, int dim, const std::function< std::vector< int >(const graph::AdjacencyList< std::int32_t > &)> &reorder_fn=nullptr)
Build Geometry from input data.
Definition Geometry.h:235
std::pair< std::vector< std::int32_t >, std::array< std::size_t, 2 > > entities_to_geometry(const Mesh< T > &mesh, int dim, std::span< const std::int32_t > entities, bool permute=false)
Compute the geometry degrees of freedom associated with the closure of a given set of cell entities.
Definition utils.h:887
std::vector< std::int32_t > compute_incident_entities(const Topology &topology, std::span< const std::int32_t > entities, int d0, int d1)
Compute incident entities.
Definition utils.cpp:134
std::vector< std::int64_t > extract_topology(CellType cell_type, const fem::ElementDofLayout &layout, std::span< const std::int64_t > cells)
Extract topology from cell data, i.e. extract cell vertices.
Definition utils.cpp:32
std::vector< std::int32_t > locate_entities(const Mesh< T > &mesh, int dim, U marker, int entity_type_idx)
Compute indices of all mesh entities that evaluate to true for the provided geometric marking functio...
Definition utils.h:713
std::vector< T > compute_midpoints(const Mesh< T > &mesh, int dim, std::span< const std::int32_t > entities)
Compute the midpoints for mesh entities of a given dimension.
Definition utils.h:608
GhostMode
Enum for different partitioning ghost modes.
Definition utils.h:44
Topology create_topology(MPI_Comm comm, const std::vector< CellType > &cell_types, std::vector< std::span< const std::int64_t > > cells, std::vector< std::span< const std::int64_t > > original_cell_index, std::vector< std::span< const int > > ghost_owners, std::span< const std::int64_t > boundary_vertices, int num_threads)
Create a mesh topology.
Definition Topology.cpp:1135
CellType cell_entity_type(CellType type, int d, int index)
Return type of cell for entity of dimension d at given entity index.
Definition cell_types.h:111
constexpr void radix_sort(R &&range, P proj={})
Sort a range with radix sorting algorithm. The bucket size is determined by the number of bits to sor...
Definition sort.h:81
std::vector< std::int32_t > sort_by_perm(std::span< const T > x, std::size_t shape1, std::optional< std::size_t > ncols=std::nullopt)
Compute the permutation array that sorts a 2D array by row.
Definition sort.h:200