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>
27class ElementDofLayout;
46void reorder_list(std::span<T> list, std::span<const std::int32_t> nodemap)
51 assert(list.size() % nodemap.size() == 0);
52 int degree = list.size() / nodemap.size();
53 const std::vector<T> orig(list.begin(), list.end());
54 for (std::size_t n = 0; n < nodemap.size(); ++n)
56 auto links_old = std::span(orig.data() + n * degree, degree);
57 auto links_new = list.subspan(nodemap[n] * degree, degree);
58 std::ranges::copy(links_old, links_new.begin());
75template <std::
floating_po
int T>
76std::tuple<std::vector<std::int32_t>, std::vector<T>, std::vector<std::int32_t>>
78 std::span<const std::int32_t> facets)
80 auto topology = mesh.topology();
82 const int tdim = topology->dim();
85 throw std::runtime_error(
86 "Cannot use mesh::locate_entities_boundary (boundary) for cells.");
90 mesh.topology_mutable()->create_connectivity(tdim - 1, 0);
91 mesh.topology_mutable()->create_connectivity(tdim - 1, dim);
92 std::vector<std::int32_t> vertices, entities;
94 auto f_to_v = topology->connectivity(tdim - 1, 0);
96 auto f_to_e = topology->connectivity(tdim - 1, dim);
100 auto v = f_to_v->links(f);
101 vertices.insert(vertices.end(), v.begin(), v.end());
102 auto e = f_to_e->links(f);
103 entities.insert(entities.end(), e.begin(), e.end());
108 std::ranges::sort(vertices);
109 auto [unique_end, range_end] = std::ranges::unique(vertices);
110 vertices.erase(unique_end, range_end);
114 std::ranges::sort(entities);
115 auto [unique_end, range_end] = std::ranges::unique(entities);
116 entities.erase(unique_end, range_end);
121 auto x_dofmap = mesh.geometry().dofmap();
122 std::span<const T> x_nodes = mesh.geometry().x();
125 mesh.topology_mutable()->create_connectivity(0, tdim);
126 mesh.topology_mutable()->create_connectivity(tdim, 0);
127 auto v_to_c = topology->connectivity(0, tdim);
129 auto c_to_v = topology->connectivity(tdim, 0);
131 std::vector<T> x_vertices(3 * vertices.size(), -1.0);
132 std::vector<std::int32_t> vertex_to_pos(v_to_c->num_nodes(), -1);
133 for (std::size_t i = 0; i < vertices.size(); ++i)
135 const std::int32_t v = vertices[i];
138 const int c = v_to_c->links(v).front();
139 auto cell_vertices = c_to_v->links(c);
140 auto it = std::find(cell_vertices.begin(), cell_vertices.end(), v);
141 assert(it != cell_vertices.end());
142 const int local_pos = std::distance(cell_vertices.begin(), it);
144 auto dofs = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
145 x_dofmap, c, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
146 for (std::size_t j = 0; j < 3; ++j)
147 x_vertices[j * vertices.size() + i] = x_nodes[3 * dofs[local_pos] + j];
148 vertex_to_pos[v] = i;
151 return {std::move(entities), std::move(x_vertices), std::move(vertex_to_pos)};
186 MPI_Comm comm,
int nparts,
const std::vector<CellType>& cell_types,
187 const std::vector<std::span<const std::int64_t>>& cells)>;
200 std::span<const std::int64_t> cells);
210template <std::
floating_po
int T>
211std::vector<T>
h(
const Mesh<T>& mesh, std::span<const std::int32_t> entities,
214 if (entities.empty())
215 return std::vector<T>();
217 return std::vector<T>(entities.size(), 0);
220 const std::vector<std::int32_t> vertex_xdofs
222 assert(!entities.empty());
223 const std::size_t num_vertices = vertex_xdofs.size() / entities.size();
226 std::span<const T> x = mesh.geometry().x();
229 auto delta_norm = [](
auto&& p0,
auto&& p1)
232 for (std::size_t i = 0; i < 3; ++i)
233 norm += (p0[i] - p1[i]) * (p0[i] - p1[i]);
234 return std::sqrt(norm);
239 std::vector<T>
h(entities.size(), 0);
240 for (std::size_t e = 0; e < entities.size(); ++e)
243 std::span<const std::int32_t> e_vertices(
244 vertex_xdofs.data() + e * num_vertices, num_vertices);
247 for (std::size_t i = 0; i < e_vertices.size(); ++i)
249 std::span<const T, 3> p0(x.data() + 3 * e_vertices[i], 3);
250 for (std::size_t j = i + 1; j < e_vertices.size(); ++j)
252 std::span<const T, 3> p1(x.data() + 3 * e_vertices[j], 3);
253 h[e] = std::max(
h[e], delta_norm(p0, p1));
264template <std::
floating_po
int T>
266 std::span<const std::int32_t> entities)
268 auto topology = mesh.topology();
271 if (entities.empty())
272 return std::vector<T>();
274 if (topology->cell_type() == CellType::prism and dim == 2)
275 throw std::runtime_error(
"More work needed for prism cell");
277 const int gdim = mesh.geometry().dim();
281 std::span<const T> x = mesh.geometry().x();
282 std::vector<std::int32_t> geometry_entities
285 const std::size_t shape1 = geometry_entities.size() / entities.size();
286 std::vector<T> n(entities.size() * 3);
289 case CellType::interval:
292 throw std::invalid_argument(
"Interval cell normal undefined in 3D");
293 for (std::size_t i = 0; i < entities.size(); ++i)
296 std::array vertices{geometry_entities[i * shape1],
297 geometry_entities[i * shape1 + 1]};
298 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
299 std::span<const T, 3>(x.data() + 3 * vertices[1], 3)};
303 std::ranges::transform(p[1], p[0], t.begin(),
304 [](
auto x,
auto y) { return x - y; });
306 T norm = std::sqrt(t[0] * t[0] + t[1] * t[1]);
307 std::span<T, 3> ni(n.data() + 3 * i, 3);
308 ni[0] = -t[1] / norm;
314 case CellType::triangle:
316 for (std::size_t i = 0; i < entities.size(); ++i)
319 std::array vertices = {geometry_entities[i * shape1 + 0],
320 geometry_entities[i * shape1 + 1],
321 geometry_entities[i * shape1 + 2]};
322 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
323 std::span<const T, 3>(x.data() + 3 * vertices[1], 3),
324 std::span<const T, 3>(x.data() + 3 * vertices[2], 3)};
327 std::array<T, 3> dp1, dp2;
328 std::ranges::transform(p[1], p[0], dp1.begin(),
329 [](
auto x,
auto y) { return x - y; });
330 std::ranges::transform(p[2], p[0], dp2.begin(),
331 [](
auto x,
auto y) { return x - y; });
334 std::array<T, 3> ni = math::cross(dp1, dp2);
335 T norm = std::sqrt(ni[0] * ni[0] + ni[1] * ni[1] + ni[2] * ni[2]);
336 std::ranges::transform(ni, std::next(n.begin(), 3 * i),
337 [norm](
auto x) { return x / norm; });
342 case CellType::quadrilateral:
345 for (std::size_t i = 0; i < entities.size(); ++i)
348 std::array vertices = {geometry_entities[i * shape1 + 0],
349 geometry_entities[i * shape1 + 1],
350 geometry_entities[i * shape1 + 2]};
351 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
352 std::span<const T, 3>(x.data() + 3 * vertices[1], 3),
353 std::span<const T, 3>(x.data() + 3 * vertices[2], 3)};
356 std::array<T, 3> dp1, dp2;
357 std::ranges::transform(p[1], p[0], dp1.begin(),
358 [](
auto x,
auto y) { return x - y; });
359 std::ranges::transform(p[2], p[0], dp2.begin(),
360 [](
auto x,
auto y) { return x - y; });
363 std::array<T, 3> ni = math::cross(dp1, dp2);
364 T norm = std::sqrt(ni[0] * ni[0] + ni[1] * ni[1] + ni[2] * ni[2]);
365 std::ranges::transform(ni, std::next(n.begin(), 3 * i),
366 [norm](
auto x) { return x / norm; });
372 throw std::invalid_argument(
373 "cell_normal not supported for this cell type.");
380template <std::
floating_po
int T>
382 std::span<const std::int32_t> entities)
384 if (entities.empty())
385 return std::vector<T>();
387 std::span<const T> x = mesh.geometry().x();
390 const std::vector<std::int32_t> e_to_g
392 std::size_t shape1 = e_to_g.size() / entities.size();
394 std::vector<T> x_mid(entities.size() * 3, 0);
395 for (std::size_t e = 0; e < entities.size(); ++e)
397 std::span<T, 3> p(x_mid.data() + 3 * e, 3);
398 std::span<const std::int32_t> rows(e_to_g.data() + e * shape1, shape1);
399 for (
auto row : rows)
401 std::span<const T, 3> xg(x.data() + 3 * row, 3);
402 std::ranges::transform(p, xg, p.begin(),
403 [size = rows.size()](
auto x,
auto y)
404 { return x + y / size; });
417template <std::
floating_po
int T>
418std::pair<std::vector<T>, std::array<std::size_t, 2>>
421 auto topology = mesh.topology();
423 const int tdim = topology->dim();
426 mesh.topology_mutable()->create_connectivity(tdim, 0);
429 auto x_dofmap = mesh.geometry().dofmap();
430 const std::int32_t num_vertices = topology->index_map(0)->size_local()
431 + topology->index_map(0)->num_ghosts();
432 auto c_to_v = topology->connectivity(tdim, 0);
434 std::vector<std::int32_t> vertex_to_node(num_vertices);
435 for (
int c = 0; c < c_to_v->num_nodes(); ++c)
437 auto x_dofs = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
438 x_dofmap, c, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
439 auto vertices = c_to_v->links(c);
440 for (std::size_t i = 0; i < vertices.size(); ++i)
441 vertex_to_node[vertices[i]] = x_dofs[i];
445 std::span<const T> x_nodes = mesh.geometry().x();
446 std::vector<T> x_vertices(3 * vertex_to_node.size(), 0.0);
447 for (std::size_t i = 0; i < vertex_to_node.size(); ++i)
449 const int pos = 3 * vertex_to_node[i];
450 for (std::size_t j = 0; j < 3; ++j)
451 x_vertices[j * vertex_to_node.size() + i] = x_nodes[pos + j];
454 return {std::move(x_vertices), {3, vertex_to_node.size()}};
460template <
typename Fn,
typename T>
462 std::vector<std::int8_t>, Fn,
463 MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
464 const T, MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
466 MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>>::value;
481template <std::
floating_po
int T, MarkerFn<T> U>
485 using cmdspan3x_t = MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
487 MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
488 std::size_t, 3, MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>;
491 const auto [xdata, xshape] = impl::compute_vertex_coords(mesh);
492 cmdspan3x_t x(xdata.data(), xshape);
493 const std::vector<std::int8_t> marked = marker(x);
494 if (marked.size() != x.extent(1))
495 throw std::runtime_error(
"Length of array of markers is wrong.");
497 auto topology = mesh.topology();
499 const int tdim = topology->dim();
501 mesh.topology_mutable()->create_entities(dim);
502 mesh.topology_mutable()->create_connectivity(tdim, 0);
504 mesh.topology_mutable()->create_connectivity(dim, 0);
508 auto e_to_v = topology->connectivity(dim, 0);
510 std::vector<std::int32_t> entities;
511 for (
int e = 0; e < e_to_v->num_nodes(); ++e)
514 bool all_vertices_marked =
true;
515 for (std::int32_t v : e_to_v->links(e))
519 all_vertices_marked =
false;
524 if (all_vertices_marked)
525 entities.push_back(e);
554template <std::
floating_po
int T, MarkerFn<T> U>
558 auto topology = mesh.topology();
560 const int tdim = topology->dim();
563 throw std::runtime_error(
564 "Cannot use mesh::locate_entities_boundary (boundary) for cells.");
568 mesh.topology_mutable()->create_entities(tdim - 1);
569 mesh.topology_mutable()->create_connectivity(tdim - 1, tdim);
570 const std::vector<std::int32_t> boundary_facets
573 using cmdspan3x_t = MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
575 MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
576 std::size_t, 3, MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>;
579 const auto [facet_entities, xdata, vertex_to_pos]
580 = impl::compute_vertex_coords_boundary(mesh, dim, boundary_facets);
581 cmdspan3x_t x(xdata.data(), 3, xdata.size() / 3);
582 const std::vector<std::int8_t> marked = marker(x);
583 if (marked.size() != x.extent(1))
584 throw std::runtime_error(
"Length of array of markers is wrong.");
587 mesh.topology_mutable()->create_entities(dim);
588 auto e_to_v = topology->connectivity(dim, 0);
590 std::vector<std::int32_t> entities;
591 for (
auto e : facet_entities)
594 bool all_vertices_marked =
true;
595 for (
auto v : e_to_v->links(e))
597 const std::int32_t pos = vertex_to_pos[v];
600 all_vertices_marked =
false;
606 if (all_vertices_marked)
607 entities.push_back(e);
631template <std::
floating_po
int T>
632std::vector<std::int32_t>
634 std::span<const std::int32_t> entities,
635 bool permute =
false)
637 auto topology = mesh.topology();
639 CellType cell_type = topology->cell_type();
640 if (cell_type == CellType::prism and dim == 2)
641 throw std::runtime_error(
"More work needed for prism cells");
643 const int tdim = topology->dim();
645 auto xdofs = geometry.
dofmap();
651 std::vector<std::int32_t> entity_xdofs;
652 entity_xdofs.reserve(entities.size() * num_entity_dofs);
655 const std::vector<std::vector<std::vector<int>>>& closure_dofs_all
661 for (std::size_t i = 0; i < entities.size(); ++i)
663 const std::int32_t c = entities[i];
665 auto x_c = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
666 xdofs, c, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
667 for (std::int32_t entity_dof : closure_dofs_all[tdim][0])
668 entity_xdofs.push_back(x_c[entity_dof]);
675 auto e_to_c = topology->connectivity(dim, tdim);
678 throw std::runtime_error(
679 "Entity-to-cell connectivity has not been computed. Missing dims "
680 + std::to_string(dim) +
"->" + std::to_string(tdim));
683 auto c_to_e = topology->connectivity(tdim, dim);
686 throw std::runtime_error(
687 "Cell-to-entity connectivity has not been computed. Missing dims "
688 + std::to_string(tdim) +
"->" + std::to_string(dim));
692 std::span<const std::uint32_t> cell_info;
694 cell_info = std::span(mesh.topology()->get_cell_permutation_info());
696 for (std::size_t i = 0; i < entities.size(); ++i)
698 const std::int32_t e = entities[i];
701 assert(!e_to_c->links(e).empty());
702 std::int32_t c = e_to_c->links(e).front();
705 std::span<const std::int32_t> cell_entities = c_to_e->links(c);
706 auto it = std::find(cell_entities.begin(), cell_entities.end(), e);
707 assert(it != cell_entities.end());
708 std::size_t local_entity = std::distance(cell_entities.begin(), it);
710 std::vector<std::int32_t> closure_dofs(closure_dofs_all[dim][local_entity]);
719 entity_type, local_entity);
723 auto x_c = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
724 xdofs, c, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
725 for (std::int32_t entity_dof : closure_dofs)
726 entity_xdofs.push_back(x_c[entity_dof]);
737 = mesh::GhostMode::none,
748std::vector<std::int32_t>
750 std::span<const std::int32_t> entities,
int d0,
784 MPI_Comm comm, MPI_Comm commt, std::span<const std::int64_t> cells,
786 typename std::remove_reference_t<typename U::value_type>>& element,
787 MPI_Comm commg,
const U& x, std::array<std::size_t, 2> xshape,
790 CellType celltype = element.cell_shape();
794 std::size_t num_cell_nodes = doflayout.
num_dofs();
804 std::vector<std::int64_t> cells1;
805 std::vector<std::int64_t> original_idx1;
806 std::vector<int> ghost_owners;
809 spdlog::info(
"Using partitioner with {} cell data", cells.size());
811 if (commt != MPI_COMM_NULL)
815 dest = partitioner(commt, size, {celltype}, {t});
820 assert(cells.size() % num_cell_nodes == 0);
821 std::size_t num_cells = cells.size() / num_cell_nodes;
822 std::vector<int> src_ranks;
823 std::tie(cells1, src_ranks, original_idx1, ghost_owners)
826 spdlog::debug(
"Got {} cells from distribution", cells1.size());
830 cells1 = std::vector<std::int64_t>(cells.begin(), cells.end());
831 assert(cells1.size() % num_cell_nodes == 0);
832 std::int64_t offset = 0;
833 std::int64_t num_owned = cells1.size() / num_cell_nodes;
834 MPI_Exscan(&num_owned, &offset, 1, MPI_INT64_T, MPI_SUM, comm);
835 original_idx1.resize(num_owned);
836 std::iota(original_idx1.begin(), original_idx1.end(), offset);
841 std::vector<std::int64_t> cells1_v
843 spdlog::info(
"Extract basic topology: {}->{}", cells1.size(),
849 std::vector<std::int64_t> boundary_v;
851 std::int32_t num_owned_cells
853 std::vector<std::int32_t> cell_offsets(num_owned_cells + 1, 0);
854 for (std::size_t i = 1; i < cell_offsets.size(); ++i)
856 spdlog::info(
"Build local dual graph");
857 auto [graph, unmatched_facets, max_v, facet_attached_cells]
859 std::vector{celltype},
864 std::vector<std::int64_t> _original_idx(original_idx1.size());
865 for (std::size_t i = 0; i < remap.size(); ++i)
866 _original_idx[remap[i]] = original_idx1[i];
867 std::copy_n(std::next(original_idx1.cbegin(), num_owned_cells),
869 std::next(_original_idx.begin(), num_owned_cells));
872 impl::reorder_list(std::span(cells1.data(), remap.size() * num_cell_nodes),
874 original_idx1 = _original_idx;
877 boundary_v = unmatched_facets;
878 std::ranges::sort(boundary_v);
879 auto [unique_end, range_end] = std::ranges::unique(boundary_v);
880 boundary_v.erase(unique_end, range_end);
884 if (!boundary_v.empty() > 0 and boundary_v[0] == -1)
885 boundary_v.erase(boundary_v.begin());
890 ghost_owners, celltype, boundary_v);
894 for (
int e = 1; e < topology.
dim(); ++e)
897 if (element.needs_dof_permutations())
902 std::vector<std::int64_t> nodes1 = cells1;
904 auto [unique_end, range_end] = std::ranges::unique(nodes1);
905 nodes1.erase(unique_end, range_end);
912 =
create_geometry(topology, element, nodes1, cells1, coords, xshape[1]);
914 return Mesh(comm, std::make_shared<Topology>(std::move(topology)),
915 std::move(geometry));
954 MPI_Comm comm, MPI_Comm commt,
955 const std::vector<std::span<const std::int64_t>>& cells,
957 typename std::remove_reference_t<typename U::value_type>>>& elements,
958 MPI_Comm commg,
const U& x, std::array<std::size_t, 2> xshape,
961 assert(cells.size() == elements.size());
962 std::int32_t num_cell_types = cells.size();
963 std::vector<CellType> celltypes;
964 std::ranges::transform(elements, std::back_inserter(celltypes),
965 [](
auto e) {
return e.cell_shape(); });
966 std::vector<fem::ElementDofLayout> doflayouts;
967 std::ranges::transform(elements, std::back_inserter(doflayouts),
968 [](
auto e) {
return e.create_dof_layout(); });
978 std::vector<std::vector<std::int64_t>> cells1(num_cell_types);
979 std::vector<std::vector<std::int64_t>> original_idx1(num_cell_types);
980 std::vector<std::vector<int>> ghost_owners(num_cell_types);
983 spdlog::info(
"Using partitioner with cell data ({} cell types)",
986 if (commt != MPI_COMM_NULL)
989 std::vector<std::vector<std::int64_t>> t(num_cell_types);
990 std::vector<std::span<const std::int64_t>> tspan(num_cell_types);
991 for (std::int32_t i = 0; i < num_cell_types; ++i)
994 tspan[i] = std::span(t[i]);
996 dest = partitioner(commt, size, celltypes, tspan);
999 std::int32_t cell_offset = 0;
1000 for (std::int32_t i = 0; i < num_cell_types; ++i)
1002 std::size_t num_cell_nodes = doflayouts[i].num_dofs();
1003 assert(cells[i].size() % num_cell_nodes == 0);
1004 std::size_t num_cells = cells[i].size() / num_cell_nodes;
1007 std::vector<std::int32_t> offsets_i(
1008 std::next(dest.
offsets().begin(), cell_offset),
1009 std::next(dest.
offsets().begin(), cell_offset + num_cells + 1));
1010 std::vector<std::int32_t> data_i(
1011 std::next(dest.
array().begin(), offsets_i.front()),
1012 std::next(dest.
array().begin(), offsets_i.back()));
1013 std::int32_t offset_0 = offsets_i.front();
1014 std::ranges::for_each(offsets_i,
1015 [&offset_0](std::int32_t& j) { j -= offset_0; });
1017 cell_offset += num_cells;
1021 std::vector<int> src_ranks;
1022 std::tie(cells1[i], src_ranks, original_idx1[i], ghost_owners[i])
1024 {num_cells, num_cell_nodes}, dest_i);
1025 spdlog::debug(
"Got {} cells from distribution", cells1[i].size());
1031 std::int64_t num_owned = 0;
1032 for (std::int32_t i = 0; i < num_cell_types; ++i)
1034 cells1[i] = std::vector<std::int64_t>(cells[i].begin(), cells[i].end());
1035 std::int32_t num_cell_nodes = doflayouts[i].num_dofs();
1036 assert(cells1[i].size() % num_cell_nodes == 0);
1037 original_idx1[i].resize(cells1[i].size() / num_cell_nodes);
1038 num_owned += original_idx1[i].size();
1041 std::int64_t global_offset = 0;
1042 MPI_Exscan(&num_owned, &global_offset, 1, MPI_INT64_T, MPI_SUM, comm);
1043 for (std::int32_t i = 0; i < num_cell_types; ++i)
1045 std::iota(original_idx1[i].begin(), original_idx1[i].end(),
1047 global_offset += original_idx1[i].size();
1053 std::vector<std::vector<std::int64_t>> cells1_v(num_cell_types);
1054 for (std::int32_t i = 0; i < num_cell_types; ++i)
1057 spdlog::info(
"Extract basic topology: {}->{}", cells1[i].size(),
1058 cells1_v[i].size());
1064 std::vector<std::int64_t> boundary_v;
1066 std::vector<std::span<const std::int64_t>> cells1_v_local_cells;
1068 for (std::int32_t i = 0; i < num_cell_types; ++i)
1071 std::int32_t num_owned_cells
1073 cells1_v_local_cells.push_back(
1076 spdlog::info(
"Build local dual graph");
1077 auto [graph, unmatched_facets, max_v, facet_attached_cells]
1084 boundary_v = unmatched_facets;
1085 std::ranges::sort(boundary_v);
1086 auto [unique_end, range_end] = std::ranges::unique(boundary_v);
1087 boundary_v.erase(unique_end, range_end);
1091 if (!boundary_v.empty() > 0 and boundary_v[0] == -1)
1092 boundary_v.erase(boundary_v.begin());
1095 spdlog::debug(
"Got {} boundary vertices", boundary_v.size());
1099 std::vector<std::span<const std::int64_t>> cells1_v_span;
1100 std::ranges::transform(cells1_v, std::back_inserter(cells1_v_span),
1101 [](
auto& c) {
return std::span(c); });
1102 std::vector<std::span<const std::int64_t>> original_idx1_span;
1103 std::ranges::transform(original_idx1, std::back_inserter(original_idx1_span),
1104 [](
auto& c) {
return std::span(c); });
1105 std::vector<std::span<const int>> ghost_owners_span;
1106 std::ranges::transform(ghost_owners, std::back_inserter(ghost_owners_span),
1107 [](
auto& c) {
return std::span(c); });
1111 ghost_owners_span, boundary_v);
1115 for (
int i = 0; i < num_cell_types; ++i)
1117 for (
int e = 1; e < topology.dim(); ++e)
1118 if (doflayouts[i].num_entity_dofs(e) > 0)
1119 topology.create_entities(e);
1120 if (elements[i].needs_dof_permutations())
1121 topology.create_entity_permutations();
1126 std::vector<std::int64_t> nodes1, nodes2;
1127 for (std::vector<std::int64_t>& c : cells1)
1128 nodes1.insert(nodes1.end(), c.begin(), c.end());
1129 for (std::vector<std::int64_t>& c : cells1)
1130 nodes2.insert(nodes2.end(), c.begin(), c.end());
1133 auto [unique_end, range_end] = std::ranges::unique(nodes1);
1134 nodes1.erase(unique_end, range_end);
1141 =
create_geometry(topology, elements, nodes1, nodes2, coords, xshape[1]);
1143 return Mesh(comm, std::make_shared<Topology>(std::move(topology)),
1144 std::move(geometry));
1165template <
typename U>
1166Mesh<typename std::remove_reference_t<typename U::value_type>>
1169 std::remove_reference_t<typename U::value_type>>& elements,
1170 const U& x, std::array<std::size_t, 2> xshape,
GhostMode ghost_mode)
1173 return create_mesh(comm, comm, cells, elements, comm, x, xshape,
nullptr);
1176 return create_mesh(comm, comm, cells, elements, comm, x, xshape,
1192template <std::
floating_po
int T>
1193std::pair<Geometry<T>, std::vector<int32_t>>
1195 std::span<const std::int32_t> subentity_to_entity)
1203 std::vector<std::int32_t> x_indices
1206 std::vector<std::int32_t> sub_x_dofs = x_indices;
1207 std::ranges::sort(sub_x_dofs);
1208 auto [unique_end, range_end] = std::ranges::unique(sub_x_dofs);
1209 sub_x_dofs.erase(unique_end, range_end);
1212 auto x_index_map = geometry.
index_map();
1213 assert(x_index_map);
1215 std::shared_ptr<common::IndexMap> sub_x_dof_index_map;
1216 std::vector<std::int32_t> subx_to_x_dofmap;
1220 sub_x_dof_index_map = std::make_shared<common::IndexMap>(std::move(map));
1221 subx_to_x_dofmap = std::move(new_to_old);
1225 std::span<const T> x = geometry.
x();
1226 std::int32_t sub_num_x_dofs = subx_to_x_dofmap.size();
1227 std::vector<T> sub_x(3 * sub_num_x_dofs);
1228 for (
int i = 0; i < sub_num_x_dofs; ++i)
1230 std::copy_n(std::next(x.begin(), 3 * subx_to_x_dofmap[i]), 3,
1231 std::next(sub_x.begin(), 3 * i));
1235 std::vector<std::int32_t> x_to_subx_dof_map(
1236 x_index_map->size_local() + x_index_map->num_ghosts(), -1);
1237 for (std::size_t i = 0; i < subx_to_x_dofmap.size(); ++i)
1238 x_to_subx_dof_map[subx_to_x_dofmap[i]] = i;
1241 std::vector<std::int32_t> sub_x_dofmap;
1242 sub_x_dofmap.reserve(x_indices.size());
1243 std::ranges::transform(x_indices, std::back_inserter(sub_x_dofmap),
1244 [&x_to_subx_dof_map](
auto x_dof)
1246 assert(x_to_subx_dof_map[x_dof] != -1);
1247 return x_to_subx_dof_map[x_dof];
1255 int degree = geometry.
cmap().degree();
1256 if (sub_coord_cell == CellType::point)
1259 geometry.
cmap().variant());
1263 std::vector<std::int64_t> sub_igi;
1264 sub_igi.reserve(subx_to_x_dofmap.size());
1265 std::ranges::transform(subx_to_x_dofmap, std::back_inserter(sub_igi),
1266 [&igi](
auto sub_x_dof) {
return igi[sub_x_dof]; });
1269 return {
Geometry(sub_x_dof_index_map, std::move(sub_x_dofmap), {sub_cmap},
1270 std::move(sub_x), geometry.
dim(), std::move(sub_igi)),
1271 std::move(subx_to_x_dofmap)};
1282template <std::
floating_po
int T>
1283std::tuple<Mesh<T>, std::vector<std::int32_t>, std::vector<std::int32_t>,
1284 std::vector<std::int32_t>>
1286 std::span<const std::int32_t> entities)
1289 mesh.topology_mutable()->create_connectivity(dim, 0);
1290 auto [topology, subentity_to_entity, subvertex_to_vertex]
1294 const int tdim = mesh.topology()->dim();
1295 mesh.topology_mutable()->create_entities(dim);
1296 mesh.topology_mutable()->create_connectivity(dim, tdim);
1297 mesh.topology_mutable()->create_connectivity(tdim, dim);
1298 mesh.topology_mutable()->create_entity_permutations();
1299 auto [geometry, subx_to_x_dofmap]
1302 return {
Mesh(mesh.comm(), std::make_shared<Topology>(std::move(topology)),
1303 std::move(geometry)),
1304 std::move(subentity_to_entity), std::move(subvertex_to_vertex),
1305 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
int num_entity_dofs(int dim) const
Definition ElementDofLayout.cpp:63
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
int num_dofs() const
Definition ElementDofLayout.cpp:61
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.
Definition Geometry.h:160
const fem::CoordinateElement< value_type > & cmap() const
The element that describes the geometry map.
Definition Geometry.h:181
int dim() const
Return dimension of the Euclidean coordinate system.
Definition Geometry.h:117
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:124
const std::vector< std::int64_t > & input_global_indices() const
Global user indices.
Definition Geometry.h:202
std::span< const value_type > x() const
Access geometry degrees-of-freedom data (const version).
Definition Geometry.h:169
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:44
void create_entity_permutations()
Compute entity permutations and reflections.
Definition Topology.cpp:910
std::int32_t create_entities(int dim)
Create entities of given topological dimension.
Definition Topology.cpp:831
int dim() const noexcept
Return the topological dimension of the mesh.
Definition Topology.cpp:794
Requirements on function for geometry marking.
Definition utils.h:461
void reorder_list(std::span< T > list, std::span< const std::int32_t > nodemap)
Re-order an adjacency list of fixed degree.
Definition utils.h:46
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)
The coordinates of 'vertices' for for entities of a give dimension that are attached to specified fac...
Definition utils.h:77
std::pair< std::vector< T >, std::array< std::size_t, 2 > > compute_vertex_coords(const mesh::Mesh< T > &mesh)
Definition utils.h:419
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:676
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:814
@ 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
Topology create_topology(MPI_Comm comm, std::span< const std::int64_t > cells, std::span< const std::int64_t > original_cell_index, std::span< const int > ghost_owners, CellType cell_type, std::span< const std::int64_t > boundary_vertices)
Create a mesh topology.
Definition Topology.cpp:1319
GhostMode
Enum for different partitioning ghost modes.
Definition utils.h:36
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:264
Mesh< typename std::remove_reference_t< typename U::value_type > > create_mesh(MPI_Comm comm, MPI_Comm commt, std::span< const std::int64_t > cells, const fem::CoordinateElement< typename std::remove_reference_t< typename U::value_type > > &element, MPI_Comm commg, const U &x, std::array< std::size_t, 2 > xshape, const CellPartitionFunction &partitioner)
Create a distributed mesh from mesh data using a provided graph partitioning function for determining...
Definition utils.h:783
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:265
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:1331
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:555
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:211
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. A sub-geometry is sim...
Definition utils.h:1194
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:633
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:482
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 indices.
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. The function should compute the destination rank for ce...
Definition utils.h:185
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:381
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:1285
CellPartitionFunction create_cell_partitioner(mesh::GhostMode ghost_mode=mesh::GhostMode::none, const graph::partition_fn &partfn=&graph::partition_graph)
Definition utils.cpp:85
constexpr __radix_sort radix_sort
Radix sort.
Definition sort.h:124