12#include "graphbuild.h"
14#include <basix/mdspan.hpp>
17#include <dolfinx/graph/AdjacencyList.h>
18#include <dolfinx/graph/ordering.h>
19#include <dolfinx/graph/partition.h>
53void reorder_list(std::span<T> list, std::span<const std::int32_t> nodemap)
58 assert(list.size() % nodemap.size() == 0);
59 std::size_t degree = list.size() / nodemap.size();
60 const std::vector<T> orig(list.begin(), list.end());
61 for (std::size_t n = 0; n < nodemap.size(); ++n)
63 std::span links_old(orig.data() + n * degree, degree);
64 auto links_new = list.subspan(nodemap[n] * degree, degree);
65 std::ranges::copy(links_old, links_new.begin());
82template <std::
floating_po
int T>
83std::tuple<std::vector<std::int32_t>, std::vector<T>, std::vector<std::int32_t>>
85 std::span<const std::int32_t> facets)
87 auto topology =
mesh.topology();
89 const int tdim = topology->dim();
92 throw std::runtime_error(
93 "Cannot use mesh::locate_entities_boundary (boundary) for cells.");
97 mesh.topology_mutable()->create_connectivity(tdim - 1, 0);
98 mesh.topology_mutable()->create_connectivity(tdim - 1, dim);
99 std::vector<std::int32_t> vertices, entities;
101 auto f_to_v = topology->connectivity(tdim - 1, 0);
103 auto f_to_e = topology->connectivity(tdim - 1, dim);
105 for (
auto f : facets)
107 auto v = f_to_v->links(f);
108 vertices.insert(vertices.end(), v.begin(), v.end());
109 auto e = f_to_e->links(f);
110 entities.insert(entities.end(), e.begin(), e.end());
115 std::ranges::sort(vertices);
116 auto [unique_end, range_end] = std::ranges::unique(vertices);
117 vertices.erase(unique_end, range_end);
121 std::ranges::sort(entities);
122 auto [unique_end, range_end] = std::ranges::unique(entities);
123 entities.erase(unique_end, range_end);
128 auto x_dofmap =
mesh.geometry().dofmap();
129 std::span<const T> x_nodes =
mesh.geometry().x();
132 mesh.topology_mutable()->create_connectivity(0, tdim);
133 mesh.topology_mutable()->create_connectivity(tdim, 0);
134 auto v_to_c = topology->connectivity(0, tdim);
136 auto c_to_v = topology->connectivity(tdim, 0);
138 std::vector<T> x_vertices(3 * vertices.size(), -1.0);
139 std::vector<std::int32_t> vertex_to_pos(v_to_c->num_nodes(), -1);
140 for (std::size_t i = 0; i < vertices.size(); ++i)
142 const std::int32_t v = vertices[i];
145 const std::int32_t c = v_to_c->links(v).front();
146 auto cell_vertices = c_to_v->links(c);
147 auto it = std::find(cell_vertices.begin(), cell_vertices.end(), v);
148 assert(it != cell_vertices.end());
149 const std::size_t local_pos = std::distance(cell_vertices.begin(), it);
151 auto dofs = md::submdspan(x_dofmap, c, md::full_extent);
152 for (std::size_t j = 0; j < 3; ++j)
153 x_vertices[j * vertices.size() + i] = x_nodes[3 * dofs[local_pos] + j];
154 vertex_to_pos[v] = i;
157 return {std::move(entities), std::move(x_vertices), std::move(vertex_to_pos)};
209 MPI_Comm comm,
int nparts,
const std::vector<CellType>& cell_types,
210 const std::vector<std::span<const std::int64_t>>& cells)>;
238 return [&](
const std::vector<CellType>& celltypes,
239 const std::vector<fem::ElementDofLayout>& doflayouts,
240 const std::vector<std::vector<int>>& ghost_owners,
241 std::vector<std::vector<std::int64_t>>& cells,
242 std::vector<std::vector<std::int64_t>>& cells_v,
243 std::vector<std::vector<std::int64_t>>& original_idx)
244 -> std::vector<std::int64_t>
250 spdlog::info(
"Build local dual graphs, re-order cells, and compute process "
251 "boundary vertices.");
253 std::vector<std::pair<std::vector<std::int64_t>,
int>> facets;
256 std::vector<std::span<const std::int64_t>> cells1_v_local;
257 for (std::size_t i = 0; i < celltypes.size(); ++i)
260 std::size_t num_owned_cells
262 cells1_v_local.emplace_back(cells_v[i].data(),
266 auto [
graph, unmatched_facets, max_v, _facet_attached_cells]
268 std::vector{cells1_v_local.back()});
271 facets.emplace_back(std::move(unmatched_facets), max_v);
274 const std::vector<std::int32_t> remap = reorder_fn(
graph);
277 const std::vector<std::int64_t>& orig_idx = original_idx[i];
278 std::vector<std::int64_t> _original_idx(orig_idx.size());
279 std::copy_n(orig_idx.rbegin(), ghost_owners[i].size(),
280 _original_idx.rbegin());
282 for (std::size_t j = 0; j < remap.size(); ++j)
283 _original_idx[remap[j]] = orig_idx[j];
285 original_idx[i] = _original_idx;
292 std::span(cells[i].data(), remap.size() * doflayouts[i].num_dofs()),
296 if (facets.size() == 1)
298 std::vector<std::int64_t>& vertices = facets.front().first;
301 std::ranges::sort(vertices);
302 auto [unique_end, range_end] = std::ranges::unique(vertices);
303 vertices.erase(unique_end, range_end);
308 if (!vertices.empty() and vertices.front() == -1)
309 vertices.erase(vertices.begin());
317 std::vector<std::int64_t> facets0;
318 facets0.reserve(std::accumulate(facets.begin(), facets.end(),
319 std::size_t(0), [](std::size_t x,
auto& y)
320 { return x + y.first.size(); }));
321 int max_v = std::ranges::max_element(facets, [](
auto& a,
auto& b)
322 {
return a.second < b.second; })
324 for (
const auto& [v_data, num_v] : facets)
326 for (
auto it = v_data.begin(); it != v_data.end(); it += num_v)
328 facets0.insert(facets0.end(), it, std::next(it, num_v));
329 facets0.insert(facets0.end(), max_v - num_v, -1);
335 std::span<const std::int64_t>(facets0), max_v);
339 std::vector<std::int64_t> vertices;
341 auto it = perm.begin();
342 while (it != perm.end())
346 std::span _f(facets0.data() + (*it) * max_v, max_v);
347 auto end = std::find_if(_f.rbegin(), _f.rend(),
348 [](
auto a) { return a >= 0; });
349 auto f = _f.first(std::distance(end, _f.rend()));
351 auto it1 = std::find_if_not(
353 [f, max_v, it0 = facets0.begin()](
auto p) ->
bool
355 return std::equal(f.begin(), f.end(), std::next(it0, p * max_v));
359 if (std::distance(it, it1) == 1)
360 vertices.insert(vertices.end(), f.begin(), f.end());
361 else if (std::distance(it, it1) > 2)
362 throw std::runtime_error(
"More than two matching facets found.");
369 std::ranges::sort(vertices);
370 auto [unique_end, range_end] = std::ranges::unique(vertices);
371 vertices.erase(unique_end, range_end);
389 std::span<const std::int64_t> cells);
399template <std::
floating_po
int T>
400std::vector<T>
h(
const Mesh<T>&
mesh, std::span<const std::int32_t> entities,
403 if (entities.empty())
404 return std::vector<T>();
406 return std::vector<T>(entities.size(), 0);
409 const auto [vertex_xdofs, xdof_shape]
413 std::span<const T> x =
mesh.geometry().x();
416 auto delta_norm = [](
auto&& p0,
auto&& p1)
419 for (std::size_t i = 0; i < 3; ++i)
420 norm += (p0[i] - p1[i]) * (p0[i] - p1[i]);
421 return std::sqrt(norm);
426 std::vector<T>
h(entities.size(), 0);
427 for (std::size_t e = 0; e < entities.size(); ++e)
430 std::span<const std::int32_t> e_vertices(
431 vertex_xdofs.data() + e * xdof_shape[1], xdof_shape[1]);
434 for (std::size_t i = 0; i < e_vertices.size(); ++i)
436 std::span<const T, 3> p0(x.data() + 3 * e_vertices[i], 3);
437 for (std::size_t j = i + 1; j < e_vertices.size(); ++j)
439 std::span<const T, 3> p1(x.data() + 3 * e_vertices[j], 3);
440 h[e] = std::max(
h[e], delta_norm(p0, p1));
451template <std::
floating_po
int T>
453 std::span<const std::int32_t> entities)
455 if (entities.empty())
456 return std::vector<T>();
458 auto topology =
mesh.topology();
460 if (topology->cell_type() == CellType::prism and dim == 2)
462 throw std::runtime_error(
463 "Cell normal computation for prism cells not yet supported.");
466 const int gdim =
mesh.geometry().dim();
470 std::span<const T> x =
mesh.geometry().x();
471 const auto [geometry_entities, eshape]
474 std::vector<T> n(entities.size() * 3);
477 case CellType::interval:
480 throw std::invalid_argument(
"Interval cell normal undefined in 3D.");
481 for (std::size_t i = 0; i < entities.size(); ++i)
484 std::array vertices{geometry_entities[i * eshape[1]],
485 geometry_entities[i * eshape[1] + 1]};
486 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
487 std::span<const T, 3>(x.data() + 3 * vertices[1], 3)};
491 std::ranges::transform(p[1], p[0], t.begin(),
492 [](
auto x,
auto y) { return x - y; });
494 T norm = std::sqrt(t[0] * t[0] + t[1] * t[1]);
495 std::span<T, 3> ni(n.data() + 3 * i, 3);
496 ni[0] = -t[1] / norm;
502 case CellType::triangle:
504 for (std::size_t i = 0; i < entities.size(); ++i)
507 std::array vertices = {geometry_entities[i * eshape[1] + 0],
508 geometry_entities[i * eshape[1] + 1],
509 geometry_entities[i * eshape[1] + 2]};
510 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
511 std::span<const T, 3>(x.data() + 3 * vertices[1], 3),
512 std::span<const T, 3>(x.data() + 3 * vertices[2], 3)};
515 std::array<T, 3> dp1, dp2;
516 std::ranges::transform(p[1], p[0], dp1.begin(),
517 [](
auto x,
auto y) { return x - y; });
518 std::ranges::transform(p[2], p[0], dp2.begin(),
519 [](
auto x,
auto y) { return x - y; });
522 std::array<T, 3> ni = math::cross(dp1, dp2);
523 T norm = std::sqrt(ni[0] * ni[0] + ni[1] * ni[1] + ni[2] * ni[2]);
524 std::ranges::transform(ni, std::next(n.begin(), 3 * i),
525 [norm](
auto x) { return x / norm; });
530 case CellType::quadrilateral:
533 for (std::size_t i = 0; i < entities.size(); ++i)
536 std::array vertices = {geometry_entities[i * eshape[1] + 0],
537 geometry_entities[i * eshape[1] + 1],
538 geometry_entities[i * eshape[1] + 2]};
539 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
540 std::span<const T, 3>(x.data() + 3 * vertices[1], 3),
541 std::span<const T, 3>(x.data() + 3 * vertices[2], 3)};
544 std::array<T, 3> dp1, dp2;
545 std::ranges::transform(p[1], p[0], dp1.begin(),
546 [](
auto x,
auto y) { return x - y; });
547 std::ranges::transform(p[2], p[0], dp2.begin(),
548 [](
auto x,
auto y) { return x - y; });
551 std::array<T, 3> ni = math::cross(dp1, dp2);
552 T norm = std::sqrt(ni[0] * ni[0] + ni[1] * ni[1] + ni[2] * ni[2]);
553 std::ranges::transform(ni, std::next(n.begin(), 3 * i),
554 [norm](
auto x) { return x / norm; });
560 throw std::invalid_argument(
561 "cell_normal not supported for this cell type.");
568template <std::
floating_po
int T>
570 std::span<const std::int32_t> entities)
572 if (entities.empty())
573 return std::vector<T>();
575 std::span<const T> x =
mesh.geometry().x();
578 const auto [e_to_g, eshape]
581 std::vector<T> x_mid(entities.size() * 3, 0);
582 for (std::size_t e = 0; e < entities.size(); ++e)
584 std::span<T, 3> p(x_mid.data() + 3 * e, 3);
585 std::span<const std::int32_t> rows(e_to_g.data() + e * eshape[1],
587 for (
auto row : rows)
589 std::span<const T, 3> xg(x.data() + 3 * row, 3);
590 std::ranges::transform(p, xg, p.begin(),
591 [size = rows.size()](
auto x,
auto y)
592 { return x + y / size; });
605template <std::
floating_po
int T>
606std::pair<std::vector<T>, std::array<std::size_t, 2>>
609 auto topology =
mesh.topology();
611 const int tdim = topology->dim();
616 const std::int32_t num_vertices = topology->index_map(0)->size_local()
617 + topology->index_map(0)->num_ghosts();
619 std::vector<std::int32_t> vertex_to_node(num_vertices);
620 for (
int cell_type_idx = 0,
621 num_cell_types = topology->entity_types(tdim).size();
622 cell_type_idx < num_cell_types; ++cell_type_idx)
624 auto x_dofmap =
mesh.geometry().dofmap(cell_type_idx);
625 auto c_to_v = topology->connectivity({tdim, cell_type_idx}, {0, 0});
627 for (
int c = 0; c < c_to_v->num_nodes(); ++c)
629 auto x_dofs = md::submdspan(x_dofmap, c, md::full_extent);
630 auto vertices = c_to_v->links(c);
631 for (std::size_t i = 0; i < vertices.size(); ++i)
632 vertex_to_node[vertices[i]] = x_dofs[i];
637 std::span<const T> x_nodes =
mesh.geometry().x();
638 std::vector<T> x_vertices(3 * vertex_to_node.size(), 0.0);
639 for (std::size_t i = 0; i < vertex_to_node.size(); ++i)
641 std::int32_t pos = 3 * vertex_to_node[i];
642 for (std::size_t j = 0; j < 3; ++j)
643 x_vertices[j * vertex_to_node.size() + i] = x_nodes[pos + j];
646 return {std::move(x_vertices), {3, vertex_to_node.size()}};
652template <
typename Fn,
typename T>
654 std::vector<std::int8_t>, Fn,
656 md::extents<std::size_t, 3, md::dynamic_extent>>>::value;
673template <std::
floating_po
int T, MarkerFn<T> U>
675 U marker,
int entity_type_idx)
679 = md::mdspan<const T, md::extents<std::size_t, 3, md::dynamic_extent>>;
684 cmdspan3x_t x(xdata.data(), xshape);
685 const std::vector<std::int8_t> marked = marker(x);
686 if (marked.size() != x.extent(1))
687 throw std::runtime_error(
"Length of array of markers is wrong.");
689 auto topology =
mesh.topology();
691 const int tdim = topology->dim();
693 mesh.topology_mutable()->create_entities(dim);
695 mesh.topology_mutable()->create_connectivity(dim, 0);
699 auto e_to_v = topology->connectivity({dim, entity_type_idx}, {0, 0});
701 std::vector<std::int32_t> entities;
702 for (
int e = 0; e < e_to_v->num_nodes(); ++e)
705 bool all_vertices_marked =
true;
706 for (std::int32_t v : e_to_v->links(e))
710 all_vertices_marked =
false;
715 if (all_vertices_marked)
716 entities.push_back(e);
735template <std::
floating_po
int T, MarkerFn<T> U>
739 const int num_entity_types =
mesh.topology()->entity_types(dim).size();
740 if (num_entity_types > 1)
742 throw std::runtime_error(
743 "Multiple entity types of this dimension. Specify entity type index");
771template <std::
floating_po
int T, MarkerFn<T> U>
776 auto topology =
mesh.topology();
778 int tdim = topology->dim();
781 throw std::runtime_error(
782 "Cannot use mesh::locate_entities_boundary (boundary) for cells.");
786 mesh.topology_mutable()->create_entities(tdim - 1);
787 mesh.topology_mutable()->create_connectivity(tdim - 1, tdim);
791 = md::mdspan<const T, md::extents<std::size_t, 3, md::dynamic_extent>>;
794 auto [facet_entities, xdata, vertex_to_pos]
796 cmdspan3x_t x(xdata.data(), 3, xdata.size() / 3);
797 std::vector<std::int8_t> marked = marker(x);
798 if (marked.size() != x.extent(1))
799 throw std::runtime_error(
"Length of array of markers is wrong.");
802 mesh.topology_mutable()->create_entities(dim);
803 auto e_to_v = topology->connectivity(dim, 0);
805 std::vector<std::int32_t> entities;
806 for (
auto e : facet_entities)
809 bool all_vertices_marked =
true;
810 for (
auto v : e_to_v->links(e))
812 const std::int32_t pos = vertex_to_pos[v];
815 all_vertices_marked =
false;
821 if (all_vertices_marked)
822 entities.push_back(e);
846template <std::
floating_po
int T>
847std::pair<std::vector<std::int32_t>, std::array<std::size_t, 2>>
849 std::span<const std::int32_t> entities,
850 bool permute =
false)
852 auto topology =
mesh.topology();
854 CellType cell_type = topology->cell_type();
855 if (cell_type == CellType::prism and dim == 2)
857 throw std::runtime_error(
858 "mesh::entities_to_geometry for prism cells not yet supported.");
861 const int tdim = topology->dim();
869 std::vector<std::int32_t> entity_xdofs;
870 entity_xdofs.reserve(entities.size() * num_entity_dofs);
871 std::array<std::size_t, 2> eshape{entities.size(), num_entity_dofs};
874 const std::vector<std::vector<std::vector<int>>>& closure_dofs_all
880 for (std::int32_t c : entities)
883 auto x_c = md::submdspan(xdofs, c, md::full_extent);
884 for (std::int32_t entity_dof : closure_dofs_all[tdim][0])
885 entity_xdofs.push_back(x_c[entity_dof]);
888 return {std::move(entity_xdofs), eshape};
893 auto e_to_c = topology->connectivity(dim, tdim);
896 throw std::runtime_error(
897 "Entity-to-cell connectivity has not been computed. Missing dims "
898 + std::to_string(dim) +
"->" + std::to_string(tdim));
901 auto c_to_e = topology->connectivity(tdim, dim);
904 throw std::runtime_error(
905 "Cell-to-entity connectivity has not been computed. Missing dims "
906 + std::to_string(tdim) +
"->" + std::to_string(dim));
910 std::span<const std::uint32_t> cell_info;
912 cell_info = std::span(
mesh.topology()->get_cell_permutation_info());
914 for (std::int32_t e : entities)
917 assert(!e_to_c->links(e).empty());
918 std::int32_t c = e_to_c->links(e).front();
921 std::span<const std::int32_t> cell_entities = c_to_e->links(c);
922 auto it = std::find(cell_entities.begin(), cell_entities.end(), e);
923 assert(it != cell_entities.end());
924 std::size_t local_entity = std::distance(cell_entities.begin(), it);
928 std::vector<std::int32_t> closure_dofs(closure_dofs_all[dim][local_entity]);
934 entity_type, local_entity);
938 auto x_c = md::submdspan(xdofs, c, md::full_extent);
939 for (std::int32_t entity_dof : closure_dofs)
940 entity_xdofs.push_back(x_c[entity_dof]);
943 return {std::move(entity_xdofs), eshape};
951 = mesh::GhostMode::none,
962std::vector<std::int32_t>
964 std::span<const std::int32_t> entities,
int d0,
1008template <
typename U>
1010 MPI_Comm comm, MPI_Comm commt,
1011 std::vector<std::span<const std::int64_t>> cells,
1013 typename std::remove_reference_t<typename U::value_type>>>& elements,
1014 MPI_Comm commg,
const U& x, std::array<std::size_t, 2> xshape,
1018 assert(cells.size() == elements.size());
1019 std::vector<CellType> celltypes;
1020 std::ranges::transform(elements, std::back_inserter(celltypes),
1021 [](
auto& e) {
return e.cell_shape(); });
1022 std::vector<fem::ElementDofLayout> doflayouts;
1023 std::ranges::transform(elements, std::back_inserter(doflayouts),
1024 [](
auto& e) {
return e.create_dof_layout(); });
1033 std::int32_t num_cell_types = cells.size();
1036 std::vector<std::vector<std::int64_t>> cells1(num_cell_types);
1037 std::vector<std::vector<std::int64_t>> original_idx1(num_cell_types);
1038 std::vector<std::vector<int>> ghost_owners(num_cell_types);
1041 spdlog::info(
"Using partitioner with cell data ({} cell types)",
1044 if (commt != MPI_COMM_NULL)
1047 std::vector<std::vector<std::int64_t>> t(num_cell_types);
1048 std::vector<std::span<const std::int64_t>> tspan(num_cell_types);
1049 for (std::int32_t i = 0; i < num_cell_types; ++i)
1052 tspan[i] = std::span(t[i]);
1054 dest = partitioner(commt, size, celltypes, tspan);
1057 std::int32_t cell_offset = 0;
1058 for (std::int32_t i = 0; i < num_cell_types; ++i)
1060 std::size_t num_cell_nodes = doflayouts[i].num_dofs();
1061 assert(cells[i].size() % num_cell_nodes == 0);
1062 std::size_t num_cells = cells[i].size() / num_cell_nodes;
1065 std::vector<std::int32_t> offsets_i(
1066 std::next(dest.
offsets().begin(), cell_offset),
1067 std::next(dest.
offsets().begin(), cell_offset + num_cells + 1));
1068 std::vector<std::int32_t> data_i(
1069 std::next(dest.
array().begin(), offsets_i.front()),
1070 std::next(dest.
array().begin(), offsets_i.back()));
1071 std::int32_t offset_0 = offsets_i.front();
1072 std::ranges::for_each(offsets_i,
1073 [&offset_0](std::int32_t& j) { j -= offset_0; });
1075 cell_offset += num_cells;
1079 std::vector<int> src_ranks;
1080 std::tie(cells1[i], src_ranks, original_idx1[i], ghost_owners[i])
1082 {num_cells, num_cell_nodes}, dest_i);
1083 spdlog::debug(
"Got {} cells from distribution", cells1[i].size());
1089 std::int64_t num_owned = 0;
1090 for (std::int32_t i = 0; i < num_cell_types; ++i)
1092 cells1[i] = std::vector<std::int64_t>(cells[i].begin(), cells[i].end());
1093 std::int32_t num_cell_nodes = doflayouts[i].num_dofs();
1094 assert(cells1[i].size() % num_cell_nodes == 0);
1095 original_idx1[i].resize(cells1[i].size() / num_cell_nodes);
1096 num_owned += original_idx1[i].size();
1100 std::int64_t global_offset = 0;
1101 MPI_Exscan(&num_owned, &global_offset, 1, MPI_INT64_T, MPI_SUM, comm);
1102 for (std::int32_t i = 0; i < num_cell_types; ++i)
1104 std::iota(original_idx1[i].begin(), original_idx1[i].end(),
1106 global_offset += original_idx1[i].size();
1112 std::vector<std::vector<std::int64_t>> cells1_v(num_cell_types);
1113 for (std::int32_t i = 0; i < num_cell_types; ++i)
1116 spdlog::info(
"Extract basic topology: {}->{}", cells1[i].size(),
1117 cells1_v[i].size());
1121 const std::vector<std::int64_t> boundary_v = boundary_v_fn(
1122 celltypes, doflayouts, ghost_owners, cells1, cells1_v, original_idx1);
1124 spdlog::debug(
"Got {} boundary vertices", boundary_v.size());
1127 std::vector<std::span<const std::int64_t>> cells1_v_span;
1128 std::ranges::transform(cells1_v, std::back_inserter(cells1_v_span),
1129 [](
auto& c) {
return std::span(c); });
1130 std::vector<std::span<const std::int64_t>> original_idx1_span;
1131 std::ranges::transform(original_idx1, std::back_inserter(original_idx1_span),
1132 [](
auto& c) {
return std::span(c); });
1133 std::vector<std::span<const int>> ghost_owners_span;
1134 std::ranges::transform(ghost_owners, std::back_inserter(ghost_owners_span),
1135 [](
auto& c) {
return std::span(c); });
1138 ghost_owners_span, boundary_v);
1142 for (
int i = 0; i < num_cell_types; ++i)
1144 for (
int e = 1; e < topology.dim(); ++e)
1146 if (doflayouts[i].num_entity_dofs(e) > 0)
1147 topology.create_entities(e);
1150 if (elements[i].needs_dof_permutations())
1151 topology.create_entity_permutations();
1156 std::vector<std::int64_t> nodes1, nodes2;
1157 for (std::vector<std::int64_t>& c : cells1)
1158 nodes1.insert(nodes1.end(), c.begin(), c.end());
1159 for (std::vector<std::int64_t>& c : cells1)
1160 nodes2.insert(nodes2.end(), c.begin(), c.end());
1163 auto [unique_end, range_end] = std::ranges::unique(nodes1);
1164 nodes1.erase(unique_end, range_end);
1171 =
create_geometry(topology, elements, nodes1, nodes2, coords, xshape[1]);
1173 return Mesh(comm, std::make_shared<Topology>(std::move(topology)),
1212template <
typename U>
1214 MPI_Comm comm, MPI_Comm commt, std::span<const std::int64_t> cells,
1216 typename std::remove_reference_t<typename U::value_type>>& element,
1217 MPI_Comm commg,
const U& x, std::array<std::size_t, 2> xshape,
1221 return create_mesh(comm, commt, std::vector{cells}, std::vector{element},
1222 commg, x, xshape, partitioner, reorder_fn);
1243template <
typename U>
1244Mesh<typename std::remove_reference_t<typename U::value_type>>
1247 std::remove_reference_t<typename U::value_type>>& elements,
1248 const U& x, std::array<std::size_t, 2> xshape,
GhostMode ghost_mode)
1251 return create_mesh(comm, comm, std::vector{cells}, std::vector{elements},
1252 comm, x, xshape,
nullptr);
1255 return create_mesh(comm, comm, std::vector{cells}, std::vector{elements},
1273template <std::
floating_po
int T>
1274std::pair<Geometry<T>, std::vector<int32_t>>
1276 std::span<const std::int32_t> subentity_to_entity)
1284 const std::vector<std::int32_t> x_indices
1287 std::vector<std::int32_t> sub_x_dofs = x_indices;
1288 std::ranges::sort(sub_x_dofs);
1289 auto [unique_end, range_end] = std::ranges::unique(sub_x_dofs);
1290 sub_x_dofs.erase(unique_end, range_end);
1293 auto x_index_map =
geometry.index_map();
1294 assert(x_index_map);
1296 std::shared_ptr<common::IndexMap> sub_x_dof_index_map;
1297 std::vector<std::int32_t> subx_to_x_dofmap;
1301 sub_x_dof_index_map = std::make_shared<common::IndexMap>(std::move(map));
1302 subx_to_x_dofmap = std::move(new_to_old);
1306 std::span<const T> x =
geometry.x();
1307 std::int32_t sub_num_x_dofs = subx_to_x_dofmap.size();
1308 std::vector<T> sub_x(3 * sub_num_x_dofs);
1309 for (std::int32_t i = 0; i < sub_num_x_dofs; ++i)
1311 std::copy_n(std::next(x.begin(), 3 * subx_to_x_dofmap[i]), 3,
1312 std::next(sub_x.begin(), 3 * i));
1316 std::vector<std::int32_t> x_to_subx_dof_map(
1317 x_index_map->size_local() + x_index_map->num_ghosts(), -1);
1318 for (std::size_t i = 0; i < subx_to_x_dofmap.size(); ++i)
1319 x_to_subx_dof_map[subx_to_x_dofmap[i]] = i;
1322 std::vector<std::int32_t> sub_x_dofmap;
1323 sub_x_dofmap.reserve(x_indices.size());
1324 std::ranges::transform(x_indices, std::back_inserter(sub_x_dofmap),
1325 [&x_to_subx_dof_map](
auto x_dof)
1327 assert(x_to_subx_dof_map[x_dof] != -1);
1328 return x_to_subx_dof_map[x_dof];
1336 int degree = (sub_xcell == CellType::point) ? 0 :
geometry.cmap().degree();
1341 const std::vector<std::int64_t>& igi =
geometry.input_global_indices();
1342 std::vector<std::int64_t> sub_igi;
1343 sub_igi.reserve(subx_to_x_dofmap.size());
1344 std::ranges::transform(subx_to_x_dofmap, std::back_inserter(sub_igi),
1345 [&igi](
auto sub_x_dof) {
return igi[sub_x_dof]; });
1349 sub_x_dof_index_map,
1350 std::vector<std::vector<std::int32_t>>{std::move(sub_x_dofmap)},
1351 {sub_cmap}, std::move(sub_x),
geometry.dim(), std::move(sub_igi)),
1352 std::move(subx_to_x_dofmap)};
1364template <std::
floating_po
int T>
1365std::tuple<Mesh<T>, EntityMap, EntityMap, std::vector<std::int32_t>>
1367 std::span<const std::int32_t> entities)
1370 mesh.topology_mutable()->create_connectivity(dim, 0);
1371 auto [topology, subentity_to_entity, subvertex_to_vertex]
1375 const int tdim =
mesh.topology()->dim();
1376 mesh.topology_mutable()->create_entities(dim);
1377 mesh.topology_mutable()->create_connectivity(dim, tdim);
1378 mesh.topology_mutable()->create_connectivity(tdim, dim);
1379 mesh.topology_mutable()->create_entity_permutations();
1384 =
Mesh(
mesh.comm(), std::make_shared<Topology>(std::move(topology)),
1386 EntityMap entity_map(
mesh.topology(), submesh.topology(), dim,
1387 subentity_to_entity);
1388 EntityMap vertex_map(
mesh.topology(), submesh.topology(), 0,
1389 subvertex_to_vertex);
1390 return {std::move(submesh), std::move(entity_map), std::move(vertex_map),
1391 std::move(subx_to_x_dofmap)};
Definition CoordinateElement.h:38
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:31
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
This class provides a static adjacency list data structure.
Definition AdjacencyList.h:38
const std::vector< LinkData > & array() const
Return contiguous array of links for all nodes (const version).
Definition AdjacencyList.h:173
const std::vector< std::int32_t > & offsets() const
Offset for each node in array() (const version).
Definition AdjacencyList.h:179
A bidirectional map relating entities in one topology to another.
Definition EntityMap.h:20
Geometry stores the geometry imposed on a mesh.
Definition Geometry.h:34
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:41
Requirements on function for geometry marking.
Definition utils.h:653
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:53
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:84
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:607
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:679
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:816
@ any
Allow arbitrary ordering of ghost indices in sub-maps.
Definition IndexMap.h:29
Finite element method functionality.
Definition assemble_expression_impl.h:23
Geometry data structures and algorithms.
Definition BoundingBoxTree.h:22
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
Graph data structures and algorithms.
Definition AdjacencyList.h:20
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:361
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
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:1025
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, std::optional< std::int32_t > max_facet_to_cell_links=2)
Compute the local part of the dual graph (cell-cell connections via facets) and facets with only one ...
Definition graphbuild.cpp:405
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:452
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::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:216
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:1313
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:1366
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:58
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:772
CellType
Cell type identifier.
Definition cell_types.h:22
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:400
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:1275
auto create_boundary_vertices_fn(const CellReorderFunction &reorder_fn)
Creates the default boundary vertices routine for a given reorder function.
Definition utils.h:225
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:237
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:848
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:123
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
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:674
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:208
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:569
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, const CellReorderFunction &reorder_fn=graph::reorder_gps)
Create a distributed mesh::Mesh from mesh data and using the provided graph partitioning function for...
Definition utils.h:1009
GhostMode
Enum for different partitioning ghost modes.
Definition utils.h:40
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:100
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:183
constexpr __radix_sort radix_sort
Radix sort.
Definition sort.h:170