11#include "graphbuild.h"
13#include <basix/mdspan.hpp>
15#include <dolfinx/graph/AdjacencyList.h>
16#include <dolfinx/graph/ordering.h>
17#include <dolfinx/graph/partition.h>
28class ElementDofLayout;
51void reorder_list(std::span<T> list, std::span<const std::int32_t> nodemap)
56 assert(list.size() % nodemap.size() == 0);
57 std::size_t degree = list.size() / nodemap.size();
58 const std::vector<T> orig(list.begin(), list.end());
59 for (std::size_t n = 0; n < nodemap.size(); ++n)
61 std::span links_old(orig.data() + n * degree, degree);
62 auto links_new = list.subspan(nodemap[n] * degree, degree);
63 std::ranges::copy(links_old, links_new.begin());
80template <std::
floating_po
int T>
81std::tuple<std::vector<std::int32_t>, std::vector<T>, std::vector<std::int32_t>>
83 std::span<const std::int32_t> facets)
85 auto topology = mesh.topology();
87 const int tdim = topology->dim();
90 throw std::runtime_error(
91 "Cannot use mesh::locate_entities_boundary (boundary) for cells.");
95 mesh.topology_mutable()->create_connectivity(tdim - 1, 0);
96 mesh.topology_mutable()->create_connectivity(tdim - 1, dim);
97 std::vector<std::int32_t> vertices, entities;
99 auto f_to_v = topology->connectivity(tdim - 1, 0);
101 auto f_to_e = topology->connectivity(tdim - 1, dim);
103 for (
auto f : facets)
105 auto v = f_to_v->links(f);
106 vertices.insert(vertices.end(), v.begin(), v.end());
107 auto e = f_to_e->links(f);
108 entities.insert(entities.end(), e.begin(), e.end());
113 std::ranges::sort(vertices);
114 auto [unique_end, range_end] = std::ranges::unique(vertices);
115 vertices.erase(unique_end, range_end);
119 std::ranges::sort(entities);
120 auto [unique_end, range_end] = std::ranges::unique(entities);
121 entities.erase(unique_end, range_end);
126 auto x_dofmap = mesh.geometry().dofmap();
127 std::span<const T> x_nodes = mesh.geometry().x();
130 mesh.topology_mutable()->create_connectivity(0, tdim);
131 mesh.topology_mutable()->create_connectivity(tdim, 0);
132 auto v_to_c = topology->connectivity(0, tdim);
134 auto c_to_v = topology->connectivity(tdim, 0);
136 std::vector<T> x_vertices(3 * vertices.size(), -1.0);
137 std::vector<std::int32_t> vertex_to_pos(v_to_c->num_nodes(), -1);
138 for (std::size_t i = 0; i < vertices.size(); ++i)
140 const std::int32_t v = vertices[i];
143 const std::int32_t c = v_to_c->links(v).front();
144 auto cell_vertices = c_to_v->links(c);
145 auto it = std::find(cell_vertices.begin(), cell_vertices.end(), v);
146 assert(it != cell_vertices.end());
147 const std::size_t local_pos = std::distance(cell_vertices.begin(), it);
149 auto dofs = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
150 x_dofmap, c, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
151 for (std::size_t j = 0; j < 3; ++j)
152 x_vertices[j * vertices.size() + i] = x_nodes[3 * dofs[local_pos] + j];
153 vertex_to_pos[v] = i;
156 return {std::move(entities), std::move(x_vertices), std::move(vertex_to_pos)};
192 MPI_Comm comm,
int nparts,
const std::vector<CellType>& cell_types,
193 const std::vector<std::span<const std::int64_t>>& cells)>;
206 std::span<const std::int64_t> cells);
216template <std::
floating_po
int T>
217std::vector<T>
h(
const Mesh<T>& mesh, std::span<const std::int32_t> entities,
220 if (entities.empty())
221 return std::vector<T>();
223 return std::vector<T>(entities.size(), 0);
226 const std::vector<std::int32_t> vertex_xdofs
228 assert(!entities.empty());
229 const std::size_t num_vertices = vertex_xdofs.size() / entities.size();
232 std::span<const T> x = mesh.geometry().x();
235 auto delta_norm = [](
auto&& p0,
auto&& p1)
238 for (std::size_t i = 0; i < 3; ++i)
239 norm += (p0[i] - p1[i]) * (p0[i] - p1[i]);
240 return std::sqrt(norm);
245 std::vector<T>
h(entities.size(), 0);
246 for (std::size_t e = 0; e < entities.size(); ++e)
249 std::span<const std::int32_t> e_vertices(
250 vertex_xdofs.data() + e * num_vertices, num_vertices);
253 for (std::size_t i = 0; i < e_vertices.size(); ++i)
255 std::span<const T, 3> p0(x.data() + 3 * e_vertices[i], 3);
256 for (std::size_t j = i + 1; j < e_vertices.size(); ++j)
258 std::span<const T, 3> p1(x.data() + 3 * e_vertices[j], 3);
259 h[e] = std::max(
h[e], delta_norm(p0, p1));
270template <std::
floating_po
int T>
272 std::span<const std::int32_t> entities)
274 if (entities.empty())
275 return std::vector<T>();
277 auto topology = mesh.topology();
279 if (topology->cell_type() == CellType::prism and dim == 2)
281 throw std::runtime_error(
282 "Cell normal computation for prism cells not yet supported.");
285 const int gdim = mesh.geometry().dim();
289 std::span<const T> x = mesh.geometry().x();
290 std::vector<std::int32_t> geometry_entities
293 const std::size_t shape1 = geometry_entities.size() / entities.size();
294 std::vector<T> n(entities.size() * 3);
297 case CellType::interval:
300 throw std::invalid_argument(
"Interval cell normal undefined in 3D.");
301 for (std::size_t i = 0; i < entities.size(); ++i)
304 std::array vertices{geometry_entities[i * shape1],
305 geometry_entities[i * shape1 + 1]};
306 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
307 std::span<const T, 3>(x.data() + 3 * vertices[1], 3)};
311 std::ranges::transform(p[1], p[0], t.begin(),
312 [](
auto x,
auto y) { return x - y; });
314 T norm = std::sqrt(t[0] * t[0] + t[1] * t[1]);
315 std::span<T, 3> ni(n.data() + 3 * i, 3);
316 ni[0] = -t[1] / norm;
322 case CellType::triangle:
324 for (std::size_t i = 0; i < entities.size(); ++i)
327 std::array vertices = {geometry_entities[i * shape1 + 0],
328 geometry_entities[i * shape1 + 1],
329 geometry_entities[i * shape1 + 2]};
330 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
331 std::span<const T, 3>(x.data() + 3 * vertices[1], 3),
332 std::span<const T, 3>(x.data() + 3 * vertices[2], 3)};
335 std::array<T, 3> dp1, dp2;
336 std::ranges::transform(p[1], p[0], dp1.begin(),
337 [](
auto x,
auto y) { return x - y; });
338 std::ranges::transform(p[2], p[0], dp2.begin(),
339 [](
auto x,
auto y) { return x - y; });
342 std::array<T, 3> ni = math::cross(dp1, dp2);
343 T norm = std::sqrt(ni[0] * ni[0] + ni[1] * ni[1] + ni[2] * ni[2]);
344 std::ranges::transform(ni, std::next(n.begin(), 3 * i),
345 [norm](
auto x) { return x / norm; });
350 case CellType::quadrilateral:
353 for (std::size_t i = 0; i < entities.size(); ++i)
356 std::array vertices = {geometry_entities[i * shape1 + 0],
357 geometry_entities[i * shape1 + 1],
358 geometry_entities[i * shape1 + 2]};
359 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
360 std::span<const T, 3>(x.data() + 3 * vertices[1], 3),
361 std::span<const T, 3>(x.data() + 3 * vertices[2], 3)};
364 std::array<T, 3> dp1, dp2;
365 std::ranges::transform(p[1], p[0], dp1.begin(),
366 [](
auto x,
auto y) { return x - y; });
367 std::ranges::transform(p[2], p[0], dp2.begin(),
368 [](
auto x,
auto y) { return x - y; });
371 std::array<T, 3> ni = math::cross(dp1, dp2);
372 T norm = std::sqrt(ni[0] * ni[0] + ni[1] * ni[1] + ni[2] * ni[2]);
373 std::ranges::transform(ni, std::next(n.begin(), 3 * i),
374 [norm](
auto x) { return x / norm; });
380 throw std::invalid_argument(
381 "cell_normal not supported for this cell type.");
388template <std::
floating_po
int T>
390 std::span<const std::int32_t> entities)
392 if (entities.empty())
393 return std::vector<T>();
395 std::span<const T> x = mesh.geometry().x();
398 const std::vector<std::int32_t> e_to_g
400 std::size_t shape1 = e_to_g.size() / entities.size();
402 std::vector<T> x_mid(entities.size() * 3, 0);
403 for (std::size_t e = 0; e < entities.size(); ++e)
405 std::span<T, 3> p(x_mid.data() + 3 * e, 3);
406 std::span<const std::int32_t> rows(e_to_g.data() + e * shape1, shape1);
407 for (
auto row : rows)
409 std::span<const T, 3> xg(x.data() + 3 * row, 3);
410 std::ranges::transform(p, xg, p.begin(),
411 [size = rows.size()](
auto x,
auto y)
412 { return x + y / size; });
425template <std::
floating_po
int T>
426std::pair<std::vector<T>, std::array<std::size_t, 2>>
429 auto topology = mesh.topology();
431 const int tdim = topology->dim();
434 mesh.topology_mutable()->create_connectivity(tdim, 0);
437 auto x_dofmap = mesh.geometry().dofmap();
438 const std::int32_t num_vertices = topology->index_map(0)->size_local()
439 + topology->index_map(0)->num_ghosts();
440 auto c_to_v = topology->connectivity(tdim, 0);
442 std::vector<std::int32_t> vertex_to_node(num_vertices);
443 for (
int c = 0; c < c_to_v->num_nodes(); ++c)
445 auto x_dofs = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
446 x_dofmap, c, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
447 auto vertices = c_to_v->links(c);
448 for (std::size_t i = 0; i < vertices.size(); ++i)
449 vertex_to_node[vertices[i]] = x_dofs[i];
453 std::span<const T> x_nodes = mesh.geometry().x();
454 std::vector<T> x_vertices(3 * vertex_to_node.size(), 0.0);
455 for (std::size_t i = 0; i < vertex_to_node.size(); ++i)
457 std::int32_t pos = 3 * vertex_to_node[i];
458 for (std::size_t j = 0; j < 3; ++j)
459 x_vertices[j * vertex_to_node.size() + i] = x_nodes[pos + j];
462 return {std::move(x_vertices), {3, vertex_to_node.size()}};
468template <
typename Fn,
typename T>
470 std::vector<std::int8_t>, Fn,
471 MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
472 const T, MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
474 MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>>::value;
489template <std::
floating_po
int T, MarkerFn<T> U>
493 using cmdspan3x_t = MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
495 MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
496 std::size_t, 3, MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>;
499 const auto [xdata, xshape] = impl::compute_vertex_coords(mesh);
500 cmdspan3x_t x(xdata.data(), xshape);
501 const std::vector<std::int8_t> marked = marker(x);
502 if (marked.size() != x.extent(1))
503 throw std::runtime_error(
"Length of array of markers is wrong.");
505 auto topology = mesh.topology();
507 const int tdim = topology->dim();
509 mesh.topology_mutable()->create_entities(dim);
510 mesh.topology_mutable()->create_connectivity(tdim, 0);
512 mesh.topology_mutable()->create_connectivity(dim, 0);
516 auto e_to_v = topology->connectivity(dim, 0);
518 std::vector<std::int32_t> entities;
519 for (
int e = 0; e < e_to_v->num_nodes(); ++e)
522 bool all_vertices_marked =
true;
523 for (std::int32_t v : e_to_v->links(e))
527 all_vertices_marked =
false;
532 if (all_vertices_marked)
533 entities.push_back(e);
562template <std::
floating_po
int T, MarkerFn<T> U>
566 auto topology = mesh.topology();
568 int tdim = topology->dim();
571 throw std::runtime_error(
572 "Cannot use mesh::locate_entities_boundary (boundary) for cells.");
576 mesh.topology_mutable()->create_entities(tdim - 1);
577 mesh.topology_mutable()->create_connectivity(tdim - 1, tdim);
580 using cmdspan3x_t = MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
582 MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
583 std::size_t, 3, MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>;
586 auto [facet_entities, xdata, vertex_to_pos]
587 = impl::compute_vertex_coords_boundary(mesh, dim, boundary_facets);
588 cmdspan3x_t x(xdata.data(), 3, xdata.size() / 3);
589 std::vector<std::int8_t> marked = marker(x);
590 if (marked.size() != x.extent(1))
591 throw std::runtime_error(
"Length of array of markers is wrong.");
594 mesh.topology_mutable()->create_entities(dim);
595 auto e_to_v = topology->connectivity(dim, 0);
597 std::vector<std::int32_t> entities;
598 for (
auto e : facet_entities)
601 bool all_vertices_marked =
true;
602 for (
auto v : e_to_v->links(e))
604 const std::int32_t pos = vertex_to_pos[v];
607 all_vertices_marked =
false;
613 if (all_vertices_marked)
614 entities.push_back(e);
638template <std::
floating_po
int T>
639std::vector<std::int32_t>
641 std::span<const std::int32_t> entities,
642 bool permute =
false)
644 auto topology = mesh.topology();
646 CellType cell_type = topology->cell_type();
647 if (cell_type == CellType::prism and dim == 2)
649 throw std::runtime_error(
650 "mesh::entities_to_geometry for prism cells not yet supported.");
653 const int tdim = topology->dim();
655 auto xdofs = geometry.
dofmap();
661 std::vector<std::int32_t> entity_xdofs;
662 entity_xdofs.reserve(entities.size() * num_entity_dofs);
665 const std::vector<std::vector<std::vector<int>>>& closure_dofs_all
671 for (std::size_t i = 0; i < entities.size(); ++i)
673 const std::int32_t c = entities[i];
675 auto x_c = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
676 xdofs, c, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
677 for (std::int32_t entity_dof : closure_dofs_all[tdim][0])
678 entity_xdofs.push_back(x_c[entity_dof]);
685 auto e_to_c = topology->connectivity(dim, tdim);
688 throw std::runtime_error(
689 "Entity-to-cell connectivity has not been computed. Missing dims "
690 + std::to_string(dim) +
"->" + std::to_string(tdim));
693 auto c_to_e = topology->connectivity(tdim, dim);
696 throw std::runtime_error(
697 "Cell-to-entity connectivity has not been computed. Missing dims "
698 + std::to_string(tdim) +
"->" + std::to_string(dim));
702 std::span<const std::uint32_t> cell_info;
704 cell_info = std::span(mesh.topology()->get_cell_permutation_info());
706 for (std::size_t i = 0; i < entities.size(); ++i)
708 const std::int32_t e = entities[i];
711 assert(!e_to_c->links(e).empty());
712 std::int32_t c = e_to_c->links(e).front();
715 std::span<const std::int32_t> cell_entities = c_to_e->links(c);
716 auto it = std::find(cell_entities.begin(), cell_entities.end(), e);
717 assert(it != cell_entities.end());
718 std::size_t local_entity = std::distance(cell_entities.begin(), it);
720 std::vector<std::int32_t> closure_dofs(closure_dofs_all[dim][local_entity]);
729 entity_type, local_entity);
733 auto x_c = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
734 xdofs, c, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
735 for (std::int32_t entity_dof : closure_dofs)
736 entity_xdofs.push_back(x_c[entity_dof]);
747 = mesh::GhostMode::none,
758std::vector<std::int32_t>
760 std::span<const std::int32_t> entities,
int d0,
804 MPI_Comm comm, MPI_Comm commt,
805 std::vector<std::span<const std::int64_t>> cells,
807 typename std::remove_reference_t<typename U::value_type>>>& elements,
808 MPI_Comm commg,
const U& x, std::array<std::size_t, 2> xshape,
811 assert(cells.size() == elements.size());
812 std::vector<CellType> celltypes;
813 std::ranges::transform(elements, std::back_inserter(celltypes),
814 [](
auto e) {
return e.cell_shape(); });
815 std::vector<fem::ElementDofLayout> doflayouts;
816 std::ranges::transform(elements, std::back_inserter(doflayouts),
817 [](
auto e) {
return e.create_dof_layout(); });
826 std::int32_t num_cell_types = cells.size();
829 std::vector<std::vector<std::int64_t>> cells1(num_cell_types);
830 std::vector<std::vector<std::int64_t>> original_idx1(num_cell_types);
831 std::vector<std::vector<int>> ghost_owners(num_cell_types);
834 spdlog::info(
"Using partitioner with cell data ({} cell types)",
837 if (commt != MPI_COMM_NULL)
840 std::vector<std::vector<std::int64_t>> t(num_cell_types);
841 std::vector<std::span<const std::int64_t>> tspan(num_cell_types);
842 for (std::int32_t i = 0; i < num_cell_types; ++i)
845 tspan[i] = std::span(t[i]);
847 dest = partitioner(commt, size, celltypes, tspan);
850 std::int32_t cell_offset = 0;
851 for (std::int32_t i = 0; i < num_cell_types; ++i)
853 std::size_t num_cell_nodes = doflayouts[i].num_dofs();
854 assert(cells[i].size() % num_cell_nodes == 0);
855 std::size_t num_cells = cells[i].size() / num_cell_nodes;
858 std::vector<std::int32_t> offsets_i(
859 std::next(dest.
offsets().begin(), cell_offset),
860 std::next(dest.
offsets().begin(), cell_offset + num_cells + 1));
861 std::vector<std::int32_t> data_i(
862 std::next(dest.
array().begin(), offsets_i.front()),
863 std::next(dest.
array().begin(), offsets_i.back()));
864 std::int32_t offset_0 = offsets_i.front();
865 std::ranges::for_each(offsets_i,
866 [&offset_0](std::int32_t& j) { j -= offset_0; });
868 cell_offset += num_cells;
872 std::vector<int> src_ranks;
873 std::tie(cells1[i], src_ranks, original_idx1[i], ghost_owners[i])
875 {num_cells, num_cell_nodes}, dest_i);
876 spdlog::debug(
"Got {} cells from distribution", cells1[i].size());
882 std::int64_t num_owned = 0;
883 for (std::int32_t i = 0; i < num_cell_types; ++i)
885 cells1[i] = std::vector<std::int64_t>(cells[i].begin(), cells[i].end());
886 std::int32_t num_cell_nodes = doflayouts[i].num_dofs();
887 assert(cells1[i].size() % num_cell_nodes == 0);
888 original_idx1[i].resize(cells1[i].size() / num_cell_nodes);
889 num_owned += original_idx1[i].size();
893 std::int64_t global_offset = 0;
894 MPI_Exscan(&num_owned, &global_offset, 1, MPI_INT64_T, MPI_SUM, comm);
895 for (std::int32_t i = 0; i < num_cell_types; ++i)
897 std::iota(original_idx1[i].begin(), original_idx1[i].end(),
899 global_offset += original_idx1[i].size();
905 std::vector<std::vector<std::int64_t>> cells1_v(num_cell_types);
906 for (std::int32_t i = 0; i < num_cell_types; ++i)
909 spdlog::info(
"Extract basic topology: {}->{}", cells1[i].size(),
916 auto boundary_v_fn = [](
const std::vector<CellType>& celltypes,
917 const std::vector<fem::ElementDofLayout>& doflayouts,
918 const std::vector<std::vector<int>>& ghost_owners,
919 std::vector<std::vector<std::int64_t>>& cells1,
920 std::vector<std::vector<std::int64_t>>& cells1_v,
921 std::vector<std::vector<std::int64_t>>& original_idx1)
923 spdlog::info(
"Build local dual graphs, re-order cells, and compute process "
924 "boundary vertices.");
926 std::vector<std::pair<std::vector<std::int64_t>,
int>> facets;
929 std::vector<std::span<const std::int64_t>> cells1_v_local;
930 for (std::size_t i = 0; i < celltypes.size(); ++i)
933 std::size_t num_owned_cells
935 cells1_v_local.emplace_back(cells1_v[i].data(),
939 auto [graph, unmatched_facets, max_v, _facet_attached_cells]
941 std::vector{cells1_v_local.back()});
944 facets.emplace_back(std::move(unmatched_facets), max_v);
950 const std::vector<std::int64_t>& orig_idx = original_idx1[i];
951 std::vector<std::int64_t> _original_idx(orig_idx.size());
952 std::copy_n(orig_idx.rbegin(), ghost_owners[i].size(),
953 _original_idx.rbegin());
955 for (std::size_t j = 0; j < remap.size(); ++j)
956 _original_idx[remap[j]] = orig_idx[j];
958 original_idx1[i] = _original_idx;
965 std::span(cells1[i].data(), remap.size() * doflayouts[i].num_dofs()),
969 if (facets.size() == 1)
971 std::vector<std::int64_t>& vertices = facets.front().first;
974 std::ranges::sort(vertices);
975 auto [unique_end, range_end] = std::ranges::unique(vertices);
976 vertices.erase(unique_end, range_end);
981 if (!vertices.empty() and vertices.front() == -1)
982 vertices.erase(vertices.begin());
990 std::vector<std::int64_t> facets0;
991 facets0.reserve(std::accumulate(facets.begin(), facets.end(),
992 std::size_t(0), [](std::size_t x,
auto& y)
993 { return x + y.first.size(); }));
994 int max_v = std::ranges::max_element(facets, [](
auto& a,
auto& b)
995 {
return a.second < b.second; })
997 for (
const auto& [v_data, num_v] : facets)
999 for (
auto it = v_data.begin(); it != v_data.end(); it += num_v)
1001 facets0.insert(facets0.end(), it, std::next(it, num_v));
1002 facets0.insert(facets0.end(), max_v - num_v, -1);
1008 std::span<const std::int64_t>(facets0), max_v);
1012 std::vector<std::int64_t> vertices;
1013 auto it = perm.begin();
1014 while (it != perm.end())
1018 std::span _f(facets0.data() + (*it) * max_v, max_v);
1019 auto end = std::find_if(_f.rbegin(), _f.rend(),
1020 [](
auto a) { return a >= 0; });
1021 auto f = _f.first(std::distance(end, _f.rend()));
1023 auto it1 = std::find_if_not(
1025 [f, max_v, it0 = facets0.begin()](
auto p) ->
bool
1027 return std::equal(f.begin(), f.end(), std::next(it0, p * max_v));
1031 if (std::distance(it, it1) == 1)
1032 vertices.insert(vertices.end(), f.begin(), f.end());
1033 else if (std::distance(it, it1) > 2)
1034 throw std::runtime_error(
"More than two matching facets found.");
1041 std::ranges::sort(vertices);
1042 auto [unique_end, range_end] = std::ranges::unique(vertices);
1043 vertices.erase(unique_end, range_end);
1049 const std::vector<std::int64_t> boundary_v = boundary_v_fn(
1050 celltypes, doflayouts, ghost_owners, cells1, cells1_v, original_idx1);
1052 spdlog::debug(
"Got {} boundary vertices", boundary_v.size());
1055 std::vector<std::span<const std::int64_t>> cells1_v_span;
1056 std::ranges::transform(cells1_v, std::back_inserter(cells1_v_span),
1057 [](
auto& c) {
return std::span(c); });
1058 std::vector<std::span<const std::int64_t>> original_idx1_span;
1059 std::ranges::transform(original_idx1, std::back_inserter(original_idx1_span),
1060 [](
auto& c) {
return std::span(c); });
1061 std::vector<std::span<const int>> ghost_owners_span;
1062 std::ranges::transform(ghost_owners, std::back_inserter(ghost_owners_span),
1063 [](
auto& c) {
return std::span(c); });
1066 ghost_owners_span, boundary_v);
1070 for (
int i = 0; i < num_cell_types; ++i)
1072 for (
int e = 1; e < topology.dim(); ++e)
1074 if (doflayouts[i].num_entity_dofs(e) > 0)
1075 topology.create_entities(e);
1078 if (elements[i].needs_dof_permutations())
1079 topology.create_entity_permutations();
1084 std::vector<std::int64_t> nodes1, nodes2;
1085 for (std::vector<std::int64_t>& c : cells1)
1086 nodes1.insert(nodes1.end(), c.begin(), c.end());
1087 for (std::vector<std::int64_t>& c : cells1)
1088 nodes2.insert(nodes2.end(), c.begin(), c.end());
1091 auto [unique_end, range_end] = std::ranges::unique(nodes1);
1092 nodes1.erase(unique_end, range_end);
1099 =
create_geometry(topology, elements, nodes1, nodes2, coords, xshape[1]);
1101 return Mesh(comm, std::make_shared<Topology>(std::move(topology)),
1102 std::move(geometry));
1138template <
typename U>
1140 MPI_Comm comm, MPI_Comm commt, std::span<const std::int64_t> cells,
1142 typename std::remove_reference_t<typename U::value_type>>& element,
1143 MPI_Comm commg,
const U& x, std::array<std::size_t, 2> xshape,
1146 return create_mesh(comm, commt, std::vector{cells}, std::vector{element},
1147 commg, x, xshape, partitioner);
1168template <
typename U>
1169Mesh<typename std::remove_reference_t<typename U::value_type>>
1172 std::remove_reference_t<typename U::value_type>>& elements,
1173 const U& x, std::array<std::size_t, 2> xshape,
GhostMode ghost_mode)
1176 return create_mesh(comm, comm, std::vector{cells}, std::vector{elements},
1177 comm, x, xshape,
nullptr);
1180 return create_mesh(comm, comm, std::vector{cells}, std::vector{elements},
1198template <std::
floating_po
int T>
1199std::pair<Geometry<T>, std::vector<int32_t>>
1201 std::span<const std::int32_t> subentity_to_entity)
1209 std::vector<std::int32_t> x_indices
1212 std::vector<std::int32_t> sub_x_dofs = x_indices;
1213 std::ranges::sort(sub_x_dofs);
1214 auto [unique_end, range_end] = std::ranges::unique(sub_x_dofs);
1215 sub_x_dofs.erase(unique_end, range_end);
1218 auto x_index_map = geometry.
index_map();
1219 assert(x_index_map);
1221 std::shared_ptr<common::IndexMap> sub_x_dof_index_map;
1222 std::vector<std::int32_t> subx_to_x_dofmap;
1226 sub_x_dof_index_map = std::make_shared<common::IndexMap>(std::move(map));
1227 subx_to_x_dofmap = std::move(new_to_old);
1231 std::span<const T> x = geometry.
x();
1232 std::int32_t sub_num_x_dofs = subx_to_x_dofmap.size();
1233 std::vector<T> sub_x(3 * sub_num_x_dofs);
1234 for (std::int32_t i = 0; i < sub_num_x_dofs; ++i)
1236 std::copy_n(std::next(x.begin(), 3 * subx_to_x_dofmap[i]), 3,
1237 std::next(sub_x.begin(), 3 * i));
1241 std::vector<std::int32_t> x_to_subx_dof_map(
1242 x_index_map->size_local() + x_index_map->num_ghosts(), -1);
1243 for (std::size_t i = 0; i < subx_to_x_dofmap.size(); ++i)
1244 x_to_subx_dof_map[subx_to_x_dofmap[i]] = i;
1247 std::vector<std::int32_t> sub_x_dofmap;
1248 sub_x_dofmap.reserve(x_indices.size());
1249 std::ranges::transform(x_indices, std::back_inserter(sub_x_dofmap),
1250 [&x_to_subx_dof_map](
auto x_dof)
1252 assert(x_to_subx_dof_map[x_dof] != -1);
1253 return x_to_subx_dof_map[x_dof];
1261 int degree = geometry.
cmap().degree();
1262 if (sub_coord_cell == CellType::point)
1265 geometry.
cmap().variant());
1269 std::vector<std::int64_t> sub_igi;
1270 sub_igi.reserve(subx_to_x_dofmap.size());
1271 std::ranges::transform(subx_to_x_dofmap, std::back_inserter(sub_igi),
1272 [&igi](
auto sub_x_dof) {
return igi[sub_x_dof]; });
1276 sub_x_dof_index_map,
1277 std::vector<std::vector<std::int32_t>>{std::move(sub_x_dofmap)},
1278 {sub_cmap}, std::move(sub_x), geometry.
dim(), std::move(sub_igi)),
1279 std::move(subx_to_x_dofmap)};
1291template <std::
floating_po
int T>
1292std::tuple<Mesh<T>, std::vector<std::int32_t>, std::vector<std::int32_t>,
1293 std::vector<std::int32_t>>
1295 std::span<const std::int32_t> entities)
1298 mesh.topology_mutable()->create_connectivity(dim, 0);
1299 auto [topology, subentity_to_entity, subvertex_to_vertex]
1303 const int tdim = mesh.topology()->dim();
1304 mesh.topology_mutable()->create_entities(dim);
1305 mesh.topology_mutable()->create_connectivity(dim, tdim);
1306 mesh.topology_mutable()->create_connectivity(tdim, dim);
1307 mesh.topology_mutable()->create_entity_permutations();
1308 auto [geometry, subx_to_x_dofmap]
1311 return {
Mesh(mesh.comm(), std::make_shared<Topology>(std::move(topology)),
1312 std::move(geometry)),
1313 std::move(subentity_to_entity), std::move(subvertex_to_vertex),
1314 std::move(subx_to_x_dofmap)};
ElementDofLayout create_dof_layout() const
Compute and return the dof layout.
Definition CoordinateElement.cpp:75
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:64
Definition ElementDofLayout.h:30
const std::vector< std::vector< std::vector< int > > > & entity_closure_dofs_all() const
Definition ElementDofLayout.cpp:92
int num_entity_closure_dofs(int dim) const
Definition ElementDofLayout.cpp:68
Definition topologycomputation.h:24
const std::vector< T > & array() const
Return contiguous array of links for all nodes (const version)
Definition AdjacencyList.h:128
const std::vector< std::int32_t > & offsets() const
Offset for each node in array() (const version)
Definition AdjacencyList.h:134
Geometry stores the geometry imposed on a mesh.
Definition Geometry.h:34
std::shared_ptr< const common::IndexMap > index_map() const
Index map for the geometry 'degrees-of-freedom'.
Definition Geometry.h:141
const fem::CoordinateElement< value_type > & cmap() const
The element that describes the geometry map.
Definition Geometry.h:173
int dim() const
Return dimension of the Euclidean coordinate system.
Definition Geometry.h:107
MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan< const std::int32_t, MDSPAN_IMPL_STANDARD_NAMESPACE::dextents< std::size_t, 2 > > dofmap() const
DofMap for the geometry.
Definition Geometry.h:114
const std::vector< std::int64_t > & input_global_indices() const
Global user indices.
Definition Geometry.h:181
std::span< const value_type > x() const
Access geometry degrees-of-freedom data (const version).
Definition Geometry.h:150
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
Topology stores the topology of a mesh, consisting of mesh entities and connectivity (incidence relat...
Definition Topology.h:46
Requirements on function for geometry marking.
Definition utils.h:469
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:51
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:82
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:427
std::vector< typename std::remove_reference_t< typename U::value_type > > distribute_data(MPI_Comm comm0, std::span< const std::int64_t > indices, MPI_Comm comm1, const U &x, int shape1)
Distribute rows of a rectangular data array to ranks where they are required (scalable version).
Definition MPI.h:681
int size(MPI_Comm comm)
Definition MPI.cpp:72
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:815
@ any
Allow arbitrary ordering of ghost indices in sub-maps.
Finite element method functionality.
Definition assemble_matrix_impl.h:26
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:38
std::vector< std::int32_t > reorder_gps(const graph::AdjacencyList< std::int32_t > &graph)
Re-order a graph using the Gibbs-Poole-Stockmeyer algorithm.
Definition ordering.cpp:360
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
AdjacencyList< std::int32_t > partition_graph(MPI_Comm comm, int nparts, const AdjacencyList< std::int64_t > &local_graph, bool ghosting)
Partition graph across processes using the default graph partitioner.
Definition partition.cpp:21
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
GhostMode
Enum for different partitioning ghost modes.
Definition utils.h:37
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)
Create a mesh topology.
Definition Topology.cpp:975
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, std::function< std::vector< int >(const graph::AdjacencyList< std::int32_t > &)> reorder_fn=nullptr)
Build Geometry from input data.
Definition Geometry.h:243
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:271
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.cpp:64
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:1261
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:563
int num_cell_vertices(CellType type)
Definition cell_types.cpp:147
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:217
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:1200
std::vector< std::int32_t > 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:640
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)
Create a distributed mesh::Mesh from mesh data and using the provided graph partitioning function for...
Definition utils.h:803
std::tuple< graph::AdjacencyList< std::int32_t >, std::vector< std::int64_t >, std::size_t, std::vector< std::int32_t > > build_local_dual_graph(std::span< const CellType > celltypes, const std::vector< std::span< const std::int64_t > > &cells)
Compute the local part of the dual graph (cell-cell connections via facets) and facets with only one ...
Definition graphbuild.cpp:354
std::vector< std::int32_t > locate_entities(const Mesh< T > &mesh, int dim, U marker)
Compute indices of all mesh entities that evaluate to true for the provided geometric marking functio...
Definition utils.h:490
std::vector< std::int32_t > exterior_facet_indices(const Topology &topology)
Compute the indices of all exterior facets that are owned by the caller.
Definition utils.cpp:58
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:108
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:29
CellType
Cell type identifier.
Definition cell_types.h:22
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:191
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:389
std::tuple< Mesh< T >, std::vector< std::int32_t >, std::vector< std::int32_t >, 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:1294
CellPartitionFunction create_cell_partitioner(mesh::GhostMode ghost_mode=mesh::GhostMode::none, const graph::partition_fn &partfn=&graph::partition_graph)
Create a function that computes destination rank for mesh cells on this rank by applying the default ...
Definition utils.cpp:85
std::vector< std::int32_t > sort_by_perm(std::span< const T > x, std::size_t shape1)
Compute the permutation array that sorts a 2D array by row.
Definition sort.h:135
constexpr __radix_sort radix_sort
Radix sort.
Definition sort.h:122