DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1// Copyright (C) 2019-2026 Garth N. Wells and Jørgen S. Dokken
2//
3// This file is part of DOLFINx (https://www.fenicsproject.org)
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#include "EntityMap.h"
10#include "Mesh.h"
11#include "MeshTags.h"
12#include "Topology.h"
13#include "graphbuild.h"
14#include "types.h"
15#include <algorithm>
16#include <array>
17#include <basix/mdspan.hpp>
18#include <boost/unordered/unordered_flat_map.hpp>
19#include <cassert>
20#include <concepts>
21#include <cstdint>
22#include <dolfinx/common/MPI.h>
23#include <dolfinx/common/Timer.h>
24#include <dolfinx/common/sort.h>
25#include <dolfinx/graph/AdjacencyList.h>
26#include <dolfinx/graph/ordering.h>
27#include <dolfinx/graph/partition.h>
28#include <exception>
29#include <format>
30#include <iterator>
31#include <memory>
32#include <mpi.h>
33#include <numeric>
34#include <optional>
35#include <ranges>
36#include <span>
37#include <stdexcept>
38#include <string_view>
39#include <tuple>
40#include <type_traits>
41#include <utility>
42#include <variant>
43#include <vector>
44
47
48namespace dolfinx::fem
49{
51}
52
53namespace dolfinx::mesh
54{
55enum class CellType : std::int8_t;
56
57namespace impl
58{
64template <typename T>
65void reorder_list(std::span<T> list, std::span<const std::int32_t> nodemap)
66{
67 if (nodemap.empty())
68 return;
69
70 assert(list.size() % nodemap.size() == 0);
71 std::size_t degree = list.size() / nodemap.size();
72 const std::vector<T> orig(list.begin(), list.end());
73 for (std::size_t n = 0; n < nodemap.size(); ++n)
74 {
75 std::span links_old(orig.data() + n * degree, degree);
76 auto links_new = list.subspan(nodemap[n] * degree, degree);
77 std::ranges::copy(links_old, links_new.begin());
78 }
79}
80
94template <std::floating_point T>
95std::tuple<std::vector<std::int32_t>, std::vector<T>, std::vector<std::int32_t>>
97 std::span<const std::int32_t> facets)
98{
99 auto topology = mesh.topology();
100 assert(topology);
101 const int tdim = topology->dim();
102 if (dim == tdim)
103 {
104 throw std::invalid_argument(
105 "Cannot use mesh::locate_entities_boundary (boundary) for cells.");
106 }
107
108 // Build set of vertices on boundary and set of boundary entities
109 mesh.topology_mutable()->create_connectivity(tdim - 1, 0);
110 mesh.topology_mutable()->create_connectivity(tdim - 1, dim);
111 std::vector<std::int32_t> vertices, entities;
112 {
113 auto f_to_v = topology->connectivity(tdim - 1, 0);
114 assert(f_to_v);
115 auto f_to_e = topology->connectivity(tdim - 1, dim);
116 assert(f_to_e);
117 for (auto f : facets)
118 {
119 auto v = f_to_v->links(f);
120 vertices.insert(vertices.end(), v.begin(), v.end());
121 auto e = f_to_e->links(f);
122 entities.insert(entities.end(), e.begin(), e.end());
123 }
124
125 // Build vector of boundary vertices
126 {
127 std::ranges::sort(vertices);
128 auto [unique_end, range_end] = std::ranges::unique(vertices);
129 vertices.erase(unique_end, range_end);
130 }
131
132 {
133 std::ranges::sort(entities);
134 auto [unique_end, range_end] = std::ranges::unique(entities);
135 entities.erase(unique_end, range_end);
136 }
137 }
138
139 // Get geometry data
140 auto x_dofmap = mesh.geometry().dofmaps().front();
141 std::span<const T> x_nodes = mesh.geometry().x();
142
143 // Get all vertex 'node' indices
144 mesh.topology_mutable()->create_connectivity(0, tdim);
145 mesh.topology_mutable()->create_connectivity(tdim, 0);
146 auto v_to_c = topology->connectivity(0, tdim);
147 assert(v_to_c);
148 auto c_to_v = topology->connectivity(tdim, 0);
149 assert(c_to_v);
150 std::vector<T> x_vertices(3 * vertices.size(), -1.0);
151 std::vector<std::int32_t> vertex_to_pos(v_to_c->num_nodes(), -1);
152 for (std::size_t i = 0; i < vertices.size(); ++i)
153 {
154 const std::int32_t v = vertices[i];
155
156 // Get first cell and find position
157 const std::int32_t c = v_to_c->links(v).front();
158 auto cell_vertices = c_to_v->links(c);
159 auto it = std::ranges::find(cell_vertices, v);
160 assert(it != cell_vertices.end());
161 const std::size_t local_pos
162 = std::ranges::distance(cell_vertices.begin(), it);
163
164 auto dofs = md::submdspan(x_dofmap, c, md::full_extent);
165 for (std::size_t j = 0; j < 3; ++j)
166 x_vertices[j * vertices.size() + i] = x_nodes[3 * dofs[local_pos] + j];
167 vertex_to_pos[v] = i;
168 }
169
170 return {std::move(entities), std::move(x_vertices), std::move(vertex_to_pos)};
171}
172
173} // namespace impl
174
192std::vector<std::int32_t> exterior_facet_indices(const Topology& topology,
193 int facet_type_idx);
194
210std::vector<std::int32_t> exterior_facet_indices(const Topology& topology);
211
212namespace impl
213{
247std::vector<std::int64_t> reorder_cells(
248 const graph::Reorder& reorder_fn, std::span<const double> cell_centroids,
249 int gdim, std::optional<std::int32_t> max_facet_to_cell_links,
250 const std::vector<CellType>& celltypes,
251 const std::vector<fem::ElementDofLayout>& doflayouts,
252 const std::vector<std::vector<int>>& ghost_owners,
253 std::vector<std::vector<std::int64_t>>& cells,
254 std::vector<std::span<std::int64_t>>& cells_v,
255 std::vector<std::vector<std::int64_t>>& original_idx, int num_threads);
256} // namespace impl
257
267std::vector<std::int64_t> extract_topology(CellType cell_type,
268 const fem::ElementDofLayout& layout,
269 std::span<const std::int64_t> cells);
270
282bool is_vertex_dof_layout(CellType cell_type,
283 const fem::ElementDofLayout& layout);
284
298template <std::floating_point T>
299std::vector<T> h(const Mesh<T>& mesh, std::span<const std::int32_t> entities,
300 int dim)
301{
302 if (entities.empty())
303 return std::vector<T>();
304 if (dim == 0)
305 return std::vector<T>(entities.size(), 0);
306
307 // Get the geometry dofs for the vertices of each entity
308 const auto [vertex_xdofs, xdof_shape]
309 = entities_to_geometry(mesh, dim, entities, false);
310
311 // Get the geometry coordinate
312 std::span<const T> x = mesh.geometry().x();
313
314 // Function to compute the length of (p0 - p1)
315 auto delta_norm = [](auto&& p0, auto&& p1)
316 {
317 T norm = 0;
318 for (std::size_t i = 0; i < 3; ++i)
319 norm += (p0[i] - p1[i]) * (p0[i] - p1[i]);
320 return std::sqrt(norm);
321 };
322
323 // Compute greatest distance between any to vertices
324 assert(dim > 0);
325 std::vector<T> h(entities.size(), 0);
326 for (std::size_t e = 0; e < entities.size(); ++e)
327 {
328 // Get geometry 'dof' for each vertex of entity e
329 std::span<const std::int32_t> e_vertices(
330 vertex_xdofs.data() + e * xdof_shape[1], xdof_shape[1]);
331
332 // Compute maximum distance between any two vertices
333 for (std::size_t i = 0; i < e_vertices.size(); ++i)
334 {
335 std::span<const T, 3> p0(x.data() + 3 * e_vertices[i], 3);
336 for (std::size_t j = i + 1; j < e_vertices.size(); ++j)
337 {
338 std::span<const T, 3> p1(x.data() + 3 * e_vertices[j], 3);
339 h[e] = std::max(h[e], delta_norm(p0, p1));
340 }
341 }
342 }
343
344 return h;
345}
346
350template <std::floating_point T>
351std::vector<T> cell_normals(const Mesh<T>& mesh, int dim,
352 std::span<const std::int32_t> entities)
353{
354 if (entities.empty())
355 return std::vector<T>();
356
357 auto topology = mesh.topology();
358 assert(topology);
359 if (topology->cell_type() == CellType::prism and dim == 2)
360 {
361 throw std::invalid_argument(
362 "Cell normal computation for prism cells not yet supported.");
363 }
364
365 const int gdim = mesh.geometry().dim();
366 const CellType type = cell_entity_type(topology->cell_type(), dim, 0);
367
368 // Find geometry nodes for topology entities
369 std::span<const T> x = mesh.geometry().x();
370 const auto [geometry_entities, eshape]
371 = entities_to_geometry(mesh, dim, entities, false);
372
373 std::vector<T> n(entities.size() * 3);
374 switch (type)
375 {
376 case CellType::interval:
377 {
378 if (gdim > 2)
379 throw std::invalid_argument("Interval cell normal undefined in 3D.");
380 for (std::size_t i = 0; i < entities.size(); ++i)
381 {
382 // Get the two vertices as points
383 std::array vertices{geometry_entities[i * eshape[1]],
384 geometry_entities[i * eshape[1] + 1]};
385 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
386 std::span<const T, 3>(x.data() + 3 * vertices[1], 3)};
387
388 // Define normal by rotating tangent counter-clockwise
389 std::array<T, 3> t;
390 std::ranges::transform(p[1], p[0], t.begin(),
391 [](auto x, auto y) { return x - y; });
392
393 T norm = std::sqrt(t[0] * t[0] + t[1] * t[1]);
394 std::span<T, 3> ni(n.data() + 3 * i, 3);
395 ni[0] = -t[1] / norm;
396 ni[1] = t[0] / norm;
397 ni[2] = 0.0;
398 }
399 return n;
400 }
401 case CellType::triangle:
402 {
403 for (std::size_t i = 0; i < entities.size(); ++i)
404 {
405 // Get the three vertices as points
406 std::array vertices = {geometry_entities[i * eshape[1] + 0],
407 geometry_entities[i * eshape[1] + 1],
408 geometry_entities[i * eshape[1] + 2]};
409 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
410 std::span<const T, 3>(x.data() + 3 * vertices[1], 3),
411 std::span<const T, 3>(x.data() + 3 * vertices[2], 3)};
412
413 // Compute (p1 - p0) and (p2 - p0)
414 std::array<T, 3> dp1, dp2;
415 std::ranges::transform(p[1], p[0], dp1.begin(),
416 [](auto x, auto y) { return x - y; });
417 std::ranges::transform(p[2], p[0], dp2.begin(),
418 [](auto x, auto y) { return x - y; });
419
420 // Define cell normal via cross product of first two edges
421 std::array<T, 3> ni = math::cross(dp1, dp2);
422 T norm = std::sqrt(ni[0] * ni[0] + ni[1] * ni[1] + ni[2] * ni[2]);
423 std::ranges::transform(ni, std::next(n.begin(), 3 * i),
424 [norm](auto x) { return x / norm; });
425 }
426
427 return n;
428 }
429 case CellType::quadrilateral:
430 {
431 // TODO: check
432 for (std::size_t i = 0; i < entities.size(); ++i)
433 {
434 // Get the three vertices as points
435 std::array vertices = {geometry_entities[i * eshape[1] + 0],
436 geometry_entities[i * eshape[1] + 1],
437 geometry_entities[i * eshape[1] + 2]};
438 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
439 std::span<const T, 3>(x.data() + 3 * vertices[1], 3),
440 std::span<const T, 3>(x.data() + 3 * vertices[2], 3)};
441
442 // Compute (p1 - p0) and (p2 - p0)
443 std::array<T, 3> dp1, dp2;
444 std::ranges::transform(p[1], p[0], dp1.begin(),
445 [](auto x, auto y) { return x - y; });
446 std::ranges::transform(p[2], p[0], dp2.begin(),
447 [](auto x, auto y) { return x - y; });
448
449 // Define cell normal via cross product of first two edges
450 std::array<T, 3> ni = math::cross(dp1, dp2);
451 T norm = std::sqrt(ni[0] * ni[0] + ni[1] * ni[1] + ni[2] * ni[2]);
452 std::ranges::transform(ni, std::next(n.begin(), 3 * i),
453 [norm](auto x) { return x / norm; });
454 }
455
456 return n;
457 }
458 default:
459 throw std::invalid_argument(
460 "cell_normal not supported for this cell type.");
461 }
462}
463
473template <std::floating_point T>
474std::vector<T> compute_midpoints(const Mesh<T>& mesh, int dim,
475 std::span<const std::int32_t> entities)
476{
477 if (entities.empty())
478 return std::vector<T>();
479
480 std::span<const T> x = mesh.geometry().x();
481
482 // Build map from entity -> geometry dof
483 const auto [e_to_g, eshape]
484 = entities_to_geometry(mesh, dim, entities, false);
485
486 std::vector<T> x_mid(entities.size() * 3, 0);
487 for (std::size_t e = 0; e < entities.size(); ++e)
488 {
489 std::span<T, 3> p(x_mid.data() + 3 * e, 3);
490 std::span<const std::int32_t> rows(e_to_g.data() + e * eshape[1],
491 eshape[1]);
492 for (auto row : rows)
493 {
494 std::span<const T, 3> xg(x.data() + 3 * row, 3);
495 std::ranges::transform(p, xg, p.begin(),
496 [size = rows.size()](auto x, auto y)
497 { return x + y / size; });
498 }
499 }
500
501 return x_mid;
502}
503
504namespace impl
505{
510template <std::floating_point T>
511std::pair<std::vector<T>, std::array<std::size_t, 2>>
513{
514 auto topology = mesh.topology();
515 assert(topology);
516 const int tdim = topology->dim();
517
518 // Create entities and connectivities
519
520 // Get all vertex 'node' indices
521 const std::int32_t num_vertices = topology->index_map(0)->size_local()
522 + topology->index_map(0)->num_ghosts();
523
524 std::vector<std::int32_t> vertex_to_node(num_vertices);
525 for (int cell_type_idx = 0,
526 num_cell_types = topology->entity_types(tdim).size();
527 cell_type_idx < num_cell_types; ++cell_type_idx)
528 {
529 auto x_dofmap = mesh.geometry().dofmaps().at(cell_type_idx);
530 auto c_to_v = topology->connectivity({tdim, cell_type_idx}, {0, 0});
531 assert(c_to_v);
532 for (int c = 0; c < c_to_v->num_nodes(); ++c)
533 {
534 auto x_dofs = md::submdspan(x_dofmap, c, md::full_extent);
535 auto vertices = c_to_v->links(c);
536 for (std::size_t i = 0; i < vertices.size(); ++i)
537 vertex_to_node[vertices[i]] = x_dofs[i];
538 }
539 }
540
541 // Pack coordinates of vertices
542 std::span<const T> x_nodes = mesh.geometry().x();
543 std::vector<T> x_vertices(3 * vertex_to_node.size(), 0.0);
544 for (std::size_t i = 0; i < vertex_to_node.size(); ++i)
545 {
546 std::int32_t pos = 3 * vertex_to_node[i];
547 for (std::size_t j = 0; j < 3; ++j)
548 x_vertices[j * vertex_to_node.size() + i] = x_nodes[pos + j];
549 }
550
551 return {std::move(x_vertices), {3, vertex_to_node.size()}};
552}
553
554} // namespace impl
555
557template <typename Fn, typename T>
558concept MarkerFn = std::is_invocable_r<
559 std::vector<std::int8_t>, Fn,
560 md::mdspan<const T,
561 md::extents<std::size_t, 3, md::dynamic_extent>>>::value;
562
580template <std::floating_point T, MarkerFn<T> U>
581std::vector<std::int32_t> locate_entities(const Mesh<T>& mesh, int dim,
582 U marker, int entity_type_idx)
583{
584
585 using cmdspan3x_t
586 = md::mdspan<const T, md::extents<std::size_t, 3, md::dynamic_extent>>;
587
588 // Run marker function on vertex coordinates
589 const auto [xdata, xshape] = impl::compute_vertex_coords(mesh);
590
591 cmdspan3x_t x(xdata.data(), xshape);
592 const std::vector<std::int8_t> marked = marker(x);
593 if (marked.size() != x.extent(1))
594 throw std::invalid_argument("Length of array of markers is wrong.");
595
596 auto topology = mesh.topology();
597 assert(topology);
598 const int tdim = topology->dim();
599
600 if (entity_type_idx < 0
601 or static_cast<std::size_t>(entity_type_idx)
602 >= topology->entity_types(dim).size())
603 {
604 throw std::out_of_range(
605 "entity_type_idx out of range for Topology::entity_types(dim).");
606 }
607
608 mesh.topology_mutable()->create_entities(dim);
609 if (dim < tdim)
610 mesh.topology_mutable()->create_connectivity(dim, 0);
611
612 // Iterate over entities of dimension 'dim' to build vector of marked
613 // entities
614 auto e_to_v = topology->connectivity({dim, entity_type_idx}, {0, 0});
615 assert(e_to_v);
616 std::vector<std::int32_t> entities;
617 for (int e = 0; e < e_to_v->num_nodes(); ++e)
618 {
619 // Iterate over entity vertices
620 bool all_vertices_marked = true;
621 for (std::int32_t v : e_to_v->links(e))
622 {
623 if (!marked[v])
624 {
625 all_vertices_marked = false;
626 break;
627 }
628 }
629
630 if (all_vertices_marked)
631 entities.push_back(e);
632 }
633
634 return entities;
635}
636
652template <std::floating_point T, MarkerFn<T> U>
653std::vector<std::int32_t> locate_entities(const Mesh<T>& mesh, int dim,
654 U marker)
655{
656 const int num_entity_types = mesh.topology()->entity_types(dim).size();
657 if (num_entity_types > 1)
658 {
659 throw std::runtime_error(
660 "Multiple entity types of this dimension. Specify entity type index");
661 }
662 return locate_entities(mesh, dim, marker, 0);
663}
664
691template <std::floating_point T, MarkerFn<T> U>
692std::vector<std::int32_t> locate_entities_boundary(const Mesh<T>& mesh, int dim,
693 U marker)
694{
695 // TODO Rewrite this function, it should be possible to simplify considerably
696 auto topology = mesh.topology();
697 assert(topology);
698 int tdim = topology->dim();
699 if (dim == tdim)
700 {
701 throw std::invalid_argument(
702 "Cannot use mesh::locate_entities_boundary (boundary) for cells.");
703 }
704
705 // Compute list of boundary facets
706 mesh.topology_mutable()->create_entities(tdim - 1);
707 mesh.topology_mutable()->create_connectivity(tdim - 1, tdim);
708 std::vector<std::int32_t> boundary_facets = exterior_facet_indices(*topology);
709 mesh.topology_mutable()->create_entities(dim);
710
711 using cmdspan3x_t
712 = md::mdspan<const T, md::extents<std::size_t, 3, md::dynamic_extent>>;
713
714 // Run marker function on the vertex coordinates
715 auto [facet_entities, xdata, vertex_to_pos]
716 = impl::compute_vertex_coords_boundary(mesh, dim, boundary_facets);
717 cmdspan3x_t x(xdata.data(), 3, xdata.size() / 3);
718 std::vector<std::int8_t> marked = marker(x);
719 if (marked.size() != x.extent(1))
720 throw std::invalid_argument("Length of array of markers is wrong.");
721
722 // Loop over entities and check vertex markers
723 auto e_to_v = topology->connectivity(dim, 0);
724 assert(e_to_v);
725 std::vector<std::int32_t> entities;
726 for (auto e : facet_entities)
727 {
728 // Iterate over entity vertices
729 bool all_vertices_marked = true;
730 for (auto v : e_to_v->links(e))
731 {
732 const std::int32_t pos = vertex_to_pos[v];
733 if (!marked[pos])
734 {
735 all_vertices_marked = false;
736 break;
737 }
738 }
739
740 // Mark facet with all vertices marked
741 if (all_vertices_marked)
742 entities.push_back(e);
743 }
744
745 return entities;
746}
747
771template <std::floating_point T>
772std::pair<std::vector<std::int32_t>, std::array<std::size_t, 2>>
774 std::span<const std::int32_t> entities,
775 bool permute = false)
776{
777 auto topology = mesh.topology();
778 assert(topology);
779 CellType cell_type = topology->cell_type();
780 if ((cell_type == CellType::prism or cell_type == CellType::pyramid)
781 and dim == 2)
782 {
783 throw std::invalid_argument("mesh::entities_to_geometry for prism/pyramid "
784 "cell facets not yet supported.");
785 }
786
787 const int tdim = topology->dim();
788 const Geometry<T>& geometry = mesh.geometry();
789 auto xdofs = geometry.dofmaps().front();
790
791 // Get the DOF layout and the number of DOFs per entity
792 const fem::CoordinateElement<T>& coord_ele = geometry.cmaps().front();
793 if (dim < tdim and coord_ele.is_discontinuous())
794 {
795 throw std::invalid_argument(
796 "mesh::entities_to_geometry for sub-entities of a cell is not "
797 "supported for a discontinuous geometry, which has no coordinate "
798 "degrees-of-freedom associated with sub-entities.");
799 }
800
801 const fem::ElementDofLayout layout = coord_ele.create_dof_layout();
802 const std::size_t num_entity_dofs = layout.entity_closure_dofs(dim, 0).size();
803 std::vector<std::int32_t> entity_xdofs;
804 entity_xdofs.reserve(entities.size() * num_entity_dofs);
805 std::array<std::size_t, 2> eshape{entities.size(), num_entity_dofs};
806
807 // Get the element's closure DOFs
808 const std::vector<std::vector<std::vector<int>>>& closure_dofs_all
809 = layout.entity_closure_dofs_all();
810
811 // Special case when dim == tdim (cells)
812 if (dim == tdim)
813 {
814 for (std::int32_t c : entities)
815 {
816 // Extract degrees of freedom
817 auto x_c = md::submdspan(xdofs, c, md::full_extent);
818 for (std::int32_t entity_dof : closure_dofs_all[tdim][0])
819 entity_xdofs.push_back(x_c[entity_dof]);
820 }
821
822 return {std::move(entity_xdofs), eshape};
823 }
824
825 assert(dim != tdim);
826
827 auto e_to_c = topology->connectivity(dim, tdim);
828 if (!e_to_c)
829 {
830 throw std::runtime_error(std::format(
831 "Entity-to-cell connectivity has not been computed. Missing dims "
832 "{}->{}",
833 dim, tdim));
834 }
835
836 auto c_to_e = topology->connectivity(tdim, dim);
837 if (!c_to_e)
838 {
839 throw std::runtime_error(std::format(
840 "Cell-to-entity connectivity has not been computed. Missing dims "
841 "{}->{}",
842 tdim, dim));
843 }
844
845 // Get the cell info, which is needed to permute the closure dofs
846 std::span<const std::uint32_t> cell_info;
847 if (permute)
848 cell_info = std::span(mesh.topology()->get_cell_permutation_info());
849
850 // Closure DOF count is the same for every local entity of dimension
851 // `dim` (the one case where it isn't, prism/pyramid facets, is
852 // rejected above), so size once and reuse across entities.
853 std::vector<std::int32_t> closure_dofs(num_entity_dofs);
854 for (std::int32_t e : entities)
855 {
856 // Get a cell connected to the entity
857 if (e_to_c->links(e).empty())
858 throw std::runtime_error("No cell incident to entity.");
859 std::int32_t c = e_to_c->links(e).front();
860
861 // Get the local index of the entity
862 std::span<const std::int32_t> cell_entities = c_to_e->links(c);
863 auto it = std::find(cell_entities.begin(), cell_entities.end(), e);
864 assert(it != cell_entities.end());
865 std::size_t local_entity = std::ranges::distance(cell_entities.begin(), it);
866
867 // Cell sub-entities must be permuted so that their local
868 // orientation agrees with their global orientation
869 const std::vector<int>& e_closure_dofs
870 = closure_dofs_all[dim][local_entity];
871 assert(e_closure_dofs.size() == closure_dofs.size());
872 std::ranges::copy(e_closure_dofs, closure_dofs.begin());
873 if (permute)
874 {
875 mesh::CellType entity_type
876 = mesh::cell_entity_type(cell_type, dim, local_entity);
877 coord_ele.permute_subentity_closure(closure_dofs, cell_info[c],
878 entity_type, local_entity);
879 }
880
881 // Extract degrees of freedom
882 auto x_c = md::submdspan(xdofs, c, md::full_extent);
883 for (std::int32_t entity_dof : closure_dofs)
884 entity_xdofs.push_back(x_c[entity_dof]);
885 }
886
887 return {std::move(entity_xdofs), eshape};
888}
889
898std::vector<std::int32_t>
899compute_incident_entities(const Topology& topology,
900 std::span<const std::int32_t> entities, int d0,
901 int d1);
902
903namespace impl
904{
920template <typename F>
921void try_locally(F&& fn, std::exception_ptr& error)
922{
923 if (error)
924 return;
925
926 try
927 {
928 fn();
929 }
930 catch (...)
931 {
932 error = std::current_exception();
933 }
934}
935
958inline void mpi_check(MPI_Comm comm, std::string_view op_name,
959 const std::exception_ptr& error)
960{
961 int failed = error ? 1 : 0;
962 int any_failed = 0;
963 MPI_Allreduce(&failed, &any_failed, 1, MPI_INT, MPI_MAX, comm);
964 if (error)
965 std::rethrow_exception(error);
966 else if (any_failed)
967 {
968 throw std::runtime_error(
969 std::format("{} failed on another rank.", op_name));
970 }
971}
972
996template <std::floating_point T>
997std::vector<double> cell_centroids_local(
998 std::span<const int> num_vertices_per_cell,
999 const std::vector<std::span<const std::int64_t>>& cells,
1000 const boost::unordered_flat_map<std::int64_t, std::size_t>& node_to_pos,
1001 std::span<const T> coords, int gdim)
1002{
1003 std::size_t num_cells = 0;
1004 for (std::size_t i = 0; i < cells.size(); ++i)
1005 num_cells += cells[i].size() / num_vertices_per_cell[i];
1006 std::vector<double> centroid(gdim * num_cells, 0);
1007
1008 std::size_t c0 = 0;
1009 for (std::size_t i = 0; i < cells.size(); ++i)
1010 {
1011 const int nv = num_vertices_per_cell[i];
1012 const double w = 1.0 / nv;
1013 for (std::size_t c = 0; c < cells[i].size() / nv; ++c)
1014 {
1015 for (int v = 0; v < nv; ++v)
1016 {
1017 auto it = node_to_pos.find(cells[i][nv * c + v]);
1018 assert(it != node_to_pos.end());
1019 std::size_t pos = it->second;
1020 for (int d = 0; d < gdim; ++d)
1021 centroid[gdim * (c0 + c) + d] += w * coords[gdim * pos + d];
1022 }
1023 }
1024
1025 c0 += cells[i].size() / nv;
1026 }
1027
1028 return centroid;
1029}
1030
1054template <std::floating_point T>
1055std::vector<double>
1057 std::span<const int> num_vertices_per_cell,
1058 const std::vector<std::span<const std::int64_t>>& cells,
1059 MPI_Comm commg, std::span<const T> x, int gdim)
1060{
1061 // Vertices of the cells on this rank, sorted and with duplicates
1062 // removed, and the coordinates for them
1063 std::vector<std::int64_t> nodes;
1064 {
1065 std::size_t size = 0;
1066 for (std::span<const std::int64_t> c : cells)
1067 size += c.size();
1068 nodes.reserve(size);
1069 for (std::span<const std::int64_t> c : cells)
1070 nodes.insert(nodes.end(), c.begin(), c.end());
1071 dolfinx::radix_sort(nodes);
1072 auto [unique_end, range_end] = std::ranges::unique(nodes);
1073 nodes.erase(unique_end, range_end);
1074 }
1075 const std::vector<T> coords
1076 = dolfinx::MPI::distribute_data(comm, nodes, commg, x, gdim);
1077
1078 // Hash map from global vertex index to its position in `nodes` (and
1079 // so its row in `coords`), turning the many repeated cell-vertex
1080 // lookups below into an O(1) average lookup rather than an
1081 // O(log(nodes.size())) binary search each time -- most vertices are
1082 // shared by several cells, so the same key is looked up repeatedly.
1083 boost::unordered_flat_map<std::int64_t, std::size_t> node_to_pos;
1084 node_to_pos.reserve(nodes.size());
1085 for (std::size_t i = 0; i < nodes.size(); ++i)
1086 node_to_pos.emplace(nodes[i], i);
1087
1088 return cell_centroids_local(num_vertices_per_cell, cells, node_to_pos,
1089 std::span<const T>(coords), gdim);
1090}
1091
1140template <std::floating_point T>
1141std::tuple<std::vector<std::vector<std::int64_t>>,
1142 std::vector<std::vector<std::int64_t>>,
1143 std::vector<std::vector<int>>>
1144partition_cells(MPI_Comm comm, MPI_Comm commt,
1145 const std::vector<std::span<const std::int64_t>>& cells,
1146 const std::vector<CellType>& celltypes,
1147 const std::vector<fem::ElementDofLayout>& doflayouts,
1148 bool p1_geometry, const graph::Partitioner& partitioner,
1149 bool ghosting,
1150 std::optional<std::int32_t> max_facet_to_cell_links,
1151 int num_threads, MPI_Comm commg, std::span<const T> x,
1152 std::array<std::size_t, 2> xshape)
1153{
1154 const std::int32_t num_cell_types = cells.size();
1155 std::vector<std::vector<std::int64_t>> cells1(num_cell_types);
1156 std::vector<std::vector<std::int64_t>> original_idx1(num_cell_types);
1157 std::vector<std::vector<int>> ghost_owners(num_cell_types);
1158 if (graph::has_partitioner(partitioner.fn))
1159 {
1160 spdlog::info("Using partitioner with cell data ({} cell types)",
1161 num_cell_types);
1163 std::exception_ptr error;
1164
1165 // Geometric data can be distributed on ranks that do not participate in
1166 // topology partitioning. Gather cell centroids collectively over `comm`
1167 // so that every rank in `commg` participates in the coordinate exchange.
1168 std::vector<double> centroid;
1169 const bool needs_centroids
1170 = std::holds_alternative<graph::geom_partition_fn>(partitioner.fn)
1171 or std::holds_alternative<graph::hybrid_partition_fn>(partitioner.fn);
1172 std::vector<std::vector<std::int64_t>> topology(num_cell_types);
1173 std::vector<std::span<const std::int64_t>> topology_view(num_cell_types);
1174 if (needs_centroids or commt != MPI_COMM_NULL)
1175 {
1176 for (std::int32_t i = 0; i < num_cell_types; ++i)
1177 {
1178 if (p1_geometry)
1179 topology_view[i] = cells[i];
1180 else
1181 {
1182 topology[i] = extract_topology(celltypes[i], doflayouts[i], cells[i]);
1183 topology_view[i] = topology[i];
1184 }
1185 }
1186 }
1187
1188 if (needs_centroids)
1189 {
1190 std::vector<int> num_vertices_per_cell;
1191 num_vertices_per_cell.reserve(celltypes.size());
1192 std::ranges::transform(celltypes,
1193 std::back_inserter(num_vertices_per_cell),
1194 [](CellType c) { return num_cell_vertices(c); });
1195 centroid = compute_cell_centroids(comm, num_vertices_per_cell,
1196 topology_view, commg, x, xshape[1]);
1197 }
1198
1199 if (std::holds_alternative<graph::geom_partition_fn>(partitioner.fn))
1200 {
1202 [&dest, &comm, &centroid, &partitioner, &xshape]
1203 {
1204 int size = dolfinx::MPI::size(comm);
1205 const auto& p = std::get<graph::geom_partition_fn>(partitioner.fn);
1207 p(comm, size, std::span<const double>(centroid), xshape[1],
1208 partitioner.node_weights),
1209 1);
1210 },
1211 error);
1212 }
1213
1214 if (commt != MPI_COMM_NULL)
1215 {
1216 // A partitioner such as graph::parmetis::geom_partitioner (which
1217 // requires nparts to equal the number of ranks calling it) can
1218 // throw only on the ranks with commt != MPI_COMM_NULL, which may
1219 // be a strict subset of comm (e.g. cells built on rank 0 only).
1220 // mpi_check below turns that into a comm-wide decision before any
1221 // rank reaches the graph::build::distribute collective, or a throw
1222 // here would leave the rest of comm blocked on it forever.
1224 [&dest, &comm, &commt, &celltypes, &topology_view,
1225 &max_facet_to_cell_links, &num_threads, &partitioner, &centroid,
1226 &ghosting]
1227 {
1228 int size = dolfinx::MPI::size(comm);
1229 // Shared by the graph::partition_fn and
1230 // graph::hybrid_partition_fn alternatives below: neither has any
1231 // other way to obtain the mesh dual graph.
1232 auto dual_graph
1233 = [&commt, &celltypes, &topology_view, &max_facet_to_cell_links,
1234 &num_threads]() -> graph::AdjacencyList<std::int64_t>
1235 {
1236 return build_dual_graph(commt, celltypes, topology_view,
1237 max_facet_to_cell_links, num_threads);
1238 };
1239
1240 // `dest` is already correct for graph::geom_partition_fn (set
1241 // above); skip the visit's dispatch (and the AdjacencyList
1242 // copy an unconditional assignment would cost) for that case.
1243 if (!std::holds_alternative<graph::geom_partition_fn>(
1244 partitioner.fn))
1245 {
1246 dest = std::visit(
1247 [&dual_graph, &commt, size, &centroid, &partitioner,
1248 &ghosting](
1249 const auto& p) -> graph::AdjacencyList<std::int32_t>
1250 {
1251 using P = std::decay_t<decltype(p)>;
1252 if constexpr (std::is_same_v<P, graph::hybrid_partition_fn>)
1253 {
1254 return p(commt, size, dual_graph(),
1255 std::span<const double>(centroid),
1256 partitioner.node_weights, std::nullopt,
1257 ghosting);
1258 }
1259 else if constexpr (std::is_same_v<P, graph::partition_fn>)
1260 {
1261 return p(commt, size, dual_graph(),
1262 partitioner.node_weights, std::nullopt,
1263 ghosting);
1264 }
1265 else
1266 {
1267 // std::visit still requires this branch to
1268 // compile for graph::geom_partition_fn.
1269 static_assert(
1270 std::is_same_v<P, graph::geom_partition_fn>);
1271 throw std::logic_error("Unreachable.");
1272 }
1273 },
1274 partitioner.fn);
1275 }
1276 },
1277 error);
1278 }
1279
1280 mpi_check(comm, "Cell partitioning", error);
1281
1282 std::int32_t cell_offset = 0;
1283 for (std::int32_t i = 0; i < num_cell_types; ++i)
1284 {
1285 std::size_t num_cell_nodes = doflayouts[i].num_dofs();
1286 std::size_t num_cells = cells[i].size() / num_cell_nodes;
1287
1288 // Extract destination AdjacencyList for this cell type
1289 std::vector<std::int32_t> offsets_i(
1290 std::next(dest.offsets().begin(), cell_offset),
1291 std::next(dest.offsets().begin(), cell_offset + num_cells + 1));
1292 std::vector<std::int32_t> data_i(
1293 std::next(dest.array().begin(), offsets_i.front()),
1294 std::next(dest.array().begin(), offsets_i.back()));
1295 const std::int32_t offset_0 = offsets_i.front();
1296 std::ranges::transform(offsets_i, offsets_i.begin(),
1297 [offset_0](std::int32_t j)
1298 { return j - offset_0; });
1299 graph::AdjacencyList<std::int32_t> dest_i(data_i, offsets_i);
1300 cell_offset += num_cells;
1301
1302 // Distribute cells (topology, includes higher-order 'nodes') to
1303 // destination rank
1304 std::vector<int> src_ranks;
1305 std::tie(cells1[i], src_ranks, original_idx1[i], ghost_owners[i])
1306 = graph::build::distribute(comm, cells[i],
1307 {num_cells, num_cell_nodes}, dest_i);
1308 spdlog::debug("Got {} cells from distribution", cells1[i].size());
1309 }
1310 }
1311 else // No partitioning: keep cells on their current rank
1312 {
1313 // Count cells of each type on this rank. Each cell still needs a
1314 // globally unique index (assigned below), even though it is not
1315 // being redistributed, and the counts are needed first to size
1316 // `original_idx1` and to determine this rank's share via the
1317 // exclusive scan that follows.
1318 std::int64_t num_owned = 0;
1319 for (std::int32_t i = 0; i < num_cell_types; ++i)
1320 {
1321 cells1[i] = std::vector<std::int64_t>(cells[i].begin(), cells[i].end());
1322 std::int32_t num_cell_nodes = doflayouts[i].num_dofs();
1323 original_idx1[i].resize(cells1[i].size() / num_cell_nodes);
1324 num_owned += original_idx1[i].size();
1325 }
1326
1327 // Assign a globally unique index to each cell. `global_offset`
1328 // starts as the number of cells owned by lower-ranked processes
1329 // (from the exclusive scan), and is advanced by each cell type's
1330 // count in turn so that the numbering is contiguous across cell
1331 // types too.
1332 std::int64_t global_offset = 0;
1333 MPI_Exscan(&num_owned, &global_offset, 1, MPI_INT64_T, MPI_SUM, comm);
1334 for (std::int32_t i = 0; i < num_cell_types; ++i)
1335 {
1336 std::iota(original_idx1[i].begin(), original_idx1[i].end(),
1337 global_offset);
1338 global_offset += original_idx1[i].size();
1339 }
1340 }
1341
1342 return {std::move(cells1), std::move(original_idx1), std::move(ghost_owners)};
1343}
1344} // namespace impl
1345
1400template <typename U>
1402 MPI_Comm comm, MPI_Comm commt,
1403 std::vector<std::span<const std::int64_t>> cells,
1404 const std::vector<fem::CoordinateElement<
1405 typename std::remove_reference_t<typename U::value_type>>>& elements,
1406 MPI_Comm commg, const U& x, std::array<std::size_t, 2> xshape,
1407 const graph::Partitioner& partitioner, GhostMode ghost_mode,
1408 std::optional<std::int32_t> max_facet_to_cell_links, int num_threads,
1409 const graph::Reorder& reorder_fn = graph::Reorder{})
1410{
1411 using T = typename std::remove_reference_t<typename U::value_type>;
1412
1413 if (cells.size() != elements.size())
1414 {
1415 throw std::invalid_argument(
1416 "Number of cell arrays and elements must match.");
1417 }
1418 std::vector<CellType> celltypes;
1419 std::ranges::transform(elements, std::back_inserter(celltypes),
1420 [](auto& e) { return e.cell_shape(); });
1421 std::vector<fem::ElementDofLayout> doflayouts;
1422 std::ranges::transform(elements, std::back_inserter(doflayouts),
1423 [](auto& e) { return e.create_dof_layout(); });
1424 for (std::size_t i = 0; i < cells.size(); ++i)
1425 {
1426 if (cells[i].size() % doflayouts[i].num_dofs() != 0)
1427 {
1428 throw std::invalid_argument("Cell array size is not a multiple of the "
1429 "number of nodes per cell.");
1430 }
1431 }
1432
1433 // Note: `extract_topology` extracts topology data, i.e. just the
1434 // vertices. For other elements the filtered lists may have 'gaps',
1435 // i.e. the indices might not be contiguous.
1436 //
1437 // For 'P1 geometry' the extraction is the identity operator, and cell
1438 // node data is used directly as cell topology. This avoids copies of
1439 // the (large) cell array, and lets the geometry node indices be taken
1440 // from the topology vertices rather than re-derived by sorting the
1441 // cell array (see below).
1442 const bool p1_geometry = std::ranges::all_of(
1443 std::views::iota(std::size_t(0), elements.size()),
1444 [&celltypes, &doflayouts](std::size_t i)
1445 { return is_vertex_dof_layout(celltypes[i], doflayouts[i]); });
1446
1447 const std::int32_t num_cell_types = cells.size();
1448
1449 // Partition cells across ranks of `comm` (or, if `partitioner` is not
1450 // callable, keep them on their current rank and just assign each a
1451 // globally unique index)
1452 const bool ghosting = (ghost_mode != GhostMode::none);
1453 auto [cells1, original_idx1, ghost_owners] = impl::partition_cells(
1454 comm, commt, cells, celltypes, doflayouts, p1_geometry, partitioner,
1455 ghosting, max_facet_to_cell_links, num_threads, commg,
1456 std::span<const T>(x), xshape);
1457
1458 // Extract cell 'topology', i.e. extract the vertices for each cell
1459 // and discard any 'higher-order' nodes. `cells1_v_storage` is empty
1460 // for 'P1 geometry', where `cells1_v` views `cells1` directly.
1461 std::vector<std::vector<std::int64_t>> cells1_v_storage(num_cell_types);
1462 std::vector<std::span<std::int64_t>> cells1_v(num_cell_types);
1463 for (std::int32_t i = 0; i < num_cell_types; ++i)
1464 {
1465 if (p1_geometry)
1466 cells1_v[i] = cells1[i];
1467 else
1468 {
1469 cells1_v_storage[i]
1470 = extract_topology(celltypes[i], doflayouts[i], cells1[i]);
1471 cells1_v[i] = cells1_v_storage[i];
1472 }
1473
1474 spdlog::info("Extract basic topology: {}->{}", cells1[i].size(),
1475 cells1_v[i].size());
1476 }
1477
1478 // Re-order cells and get boundary vertices. The re-ordering is done
1479 // on the cell topology, i.e. the vertex indices, and the higher-order
1480 // nodes are re-ordered accordingly.
1481 // Centroids are required only by a callable graph::reorder_geom_fn.
1482 // As elsewhere, `reorder_fn` is assumed to hold the same alternative
1483 // on every rank, so an empty function skips the collective exchange
1484 // everywhere; impl::reorder_cells then throws, and the failure is
1485 // made comm-wide by try_locally/mpi_check below.
1486 std::vector<double> cell_centroids;
1487 if (const graph::reorder_geom_fn* fn
1488 = std::get_if<graph::reorder_geom_fn>(&reorder_fn);
1489 fn and *fn)
1490 {
1491 std::vector<int> num_cell_vertices;
1492 num_cell_vertices.reserve(celltypes.size());
1493 std::ranges::transform(celltypes, std::back_inserter(num_cell_vertices),
1494 [](CellType c)
1495 { return mesh::num_cell_vertices(c); });
1496
1497 std::vector<std::span<const std::int64_t>> cells1_v_owned;
1498 cells1_v_owned.reserve(cells1_v.size());
1499 for (std::size_t i = 0; i < cells1_v.size(); ++i)
1500 {
1501 const int num_cell_vertices_i = num_cell_vertices[i];
1502 const std::size_t num_owned_cells
1503 = cells1_v[i].size() / num_cell_vertices_i - ghost_owners[i].size();
1504 cells1_v_owned.emplace_back(cells1_v[i].data(),
1505 num_owned_cells * num_cell_vertices_i);
1506 }
1507
1508 cell_centroids
1509 = impl::compute_cell_centroids(comm, num_cell_vertices, cells1_v_owned,
1510 commg, std::span<const T>(x), xshape[1]);
1511 }
1512
1513 std::vector<std::int64_t> boundary_v;
1514 std::exception_ptr error;
1516 [&boundary_v, &reorder_fn, &cell_centroids, &xshape,
1517 &max_facet_to_cell_links, &celltypes, &doflayouts, &ghost_owners,
1518 &cells1, &cells1_v, &original_idx1, &num_threads]
1519 {
1520 boundary_v = impl::reorder_cells(reorder_fn, cell_centroids, xshape[1],
1521 max_facet_to_cell_links, celltypes,
1522 doflayouts, ghost_owners, cells1,
1523 cells1_v, original_idx1, num_threads);
1524 },
1525 error);
1526 impl::mpi_check(comm, "Cell reordering", error);
1527
1528 spdlog::debug("Got {} boundary vertices", boundary_v.size());
1529
1530 // Create Topology
1531 std::vector<std::span<const std::int64_t>> cells1_v_span(cells1_v.begin(),
1532 cells1_v.end());
1533 std::vector<std::span<const std::int64_t>> original_idx1_span;
1534 std::ranges::transform(original_idx1, std::back_inserter(original_idx1_span),
1535 [](auto& c) { return std::span(c); });
1536 std::vector<std::span<const int>> ghost_owners_span;
1537 std::ranges::transform(ghost_owners, std::back_inserter(ghost_owners_span),
1538 [](auto& c) { return std::span(c); });
1539
1540 // Note: `vertex_index` holds the sorted input global indices of the
1541 // topology vertices, which for 'P1 geometry' are exactly the geometry
1542 // node indices required below.
1543 auto [topology, vertex_index] = mesh::impl::create_topology(
1544 comm, celltypes, cells1_v_span, original_idx1_span, ghost_owners_span,
1545 boundary_v, num_threads);
1546
1547 // Create connectivities required higher-order geometries for creating
1548 // a Geometry object
1549 for (int i = 0; i < num_cell_types; ++i)
1550 {
1551 const auto& entity_dofs = doflayouts[i].entity_dofs_all();
1552 for (int dim = 1; dim < topology.dim(); ++dim)
1553 {
1554 // Accumulate count of all dofs on this dimension
1555 int dim_sum
1556 = std::accumulate(entity_dofs[dim].begin(), entity_dofs[dim].end(), 0,
1557 [](int c, auto v) { return c + v.size(); });
1558
1559 spdlog::debug("Counting entity dofs, dim={}: {}", dim, dim_sum);
1560 if (dim_sum > 0)
1561 topology.create_entities(dim, num_threads);
1562 }
1563
1564 if (elements[i].needs_dof_permutations())
1565 topology.create_cell_permutations(num_threads);
1566 }
1567
1568 // Cell 'node' indices (global), as a single flat array. This is
1569 // `cells1` for a single cell type, and concatenated otherwise.
1570 std::vector<std::int64_t> nodes2_storage;
1571 std::span<const std::int64_t> nodes2;
1572 if (num_cell_types == 1)
1573 nodes2 = cells1.front();
1574 else
1575 {
1576 std::size_t size = 0;
1577 for (const std::vector<std::int64_t>& c : cells1)
1578 size += c.size();
1579 nodes2_storage.reserve(size);
1580 for (const std::vector<std::int64_t>& c : cells1)
1581 nodes2_storage.insert(nodes2_storage.end(), c.begin(), c.end());
1582 nodes2 = nodes2_storage;
1583 }
1584
1585 // Sorted list of unique (global) node indices. For 'P1 geometry' the
1586 // nodes are the vertices, which `create_topology` has already sorted
1587 // and made unique, so re-deriving them from the (much larger) cell
1588 // array is avoided.
1589 std::vector<std::int64_t> nodes1;
1590 if (p1_geometry)
1591 nodes1 = std::move(vertex_index);
1592 else
1593 {
1594 nodes1.assign(nodes2.begin(), nodes2.end());
1595 dolfinx::radix_sort(nodes1);
1596 auto [unique_end, range_end] = std::ranges::unique(nodes1);
1597 nodes1.erase(unique_end, range_end);
1598 }
1599
1600 std::vector coords
1601 = dolfinx::MPI::distribute_data(comm, nodes1, commg, x, xshape[1]);
1602
1603 // Create geometry object
1604 Geometry geometry
1605 = create_geometry(topology, elements, nodes1, nodes2, coords, xshape[1]);
1606
1607#ifndef NDEBUG
1608 // Nodes in `x` that no cell references are dropped silently.
1609 {
1610 std::int64_t num_nodes_local = xshape[0];
1611 std::int64_t num_nodes = 0;
1612 int err = MPI_Allreduce(&num_nodes_local, &num_nodes, 1,
1613 dolfinx::MPI::mpi_t<std::int64_t>, MPI_SUM, comm);
1614 dolfinx::MPI::check_error(comm, err);
1615 if (std::int64_t num_used = geometry.index_map()->size_global();
1616 num_used != num_nodes and dolfinx::MPI::rank(comm) == 0)
1617 {
1618 spdlog::warn("{} of {} input geometry nodes are not referenced by any "
1619 "cell and have been dropped.",
1620 num_nodes - num_used, num_nodes);
1621 }
1622 }
1623#endif
1624
1625 return Mesh(comm, std::make_shared<Topology>(std::move(topology)),
1626 std::move(geometry));
1627}
1628
1677template <typename U>
1679 MPI_Comm comm, MPI_Comm commt, std::span<const std::int64_t> cells,
1681 typename std::remove_reference_t<typename U::value_type>>& element,
1682 MPI_Comm commg, const U& x, std::array<std::size_t, 2> xshape,
1683 const graph::Partitioner& partitioner, GhostMode ghost_mode,
1684 std::optional<std::int32_t> max_facet_to_cell_links, int num_threads,
1685 const graph::Reorder& reorder_fn = graph::Reorder{})
1686{
1687 return create_mesh(comm, commt, std::vector{cells}, std::vector{element},
1688 commg, x, xshape, partitioner, ghost_mode,
1689 max_facet_to_cell_links, num_threads, reorder_fn);
1690}
1691
1714template <typename U>
1716create_mesh(MPI_Comm comm, std::span<const std::int64_t> cells,
1718 std::remove_reference_t<typename U::value_type>>& element,
1719 const U& x, std::array<std::size_t, 2> xshape, GhostMode ghost_mode,
1720 std::optional<std::int32_t> max_facet_to_cell_links = 2)
1721{
1722 // A single rank has nothing to partition, so skip the default
1723 // partitioner and just assign global indices.
1724 graph::Partitioner partitioner
1725 = dolfinx::MPI::size(comm) == 1
1726 ? graph::Partitioner{.fn = graph::partition_fn(nullptr)}
1728 return create_mesh(comm, comm, std::vector{cells}, std::vector{element}, comm,
1729 x, xshape, partitioner, ghost_mode,
1730 max_facet_to_cell_links, 1);
1731}
1732
1748template <std::floating_point T>
1749std::pair<Geometry<T>, std::vector<int32_t>>
1751 std::span<const std::int32_t> subentity_to_entity)
1752{
1753 const Geometry<T>& geometry = mesh.geometry();
1754
1755 // Get the geometry dofs in the sub-geometry based on the entities in
1756 // sub-geometry
1757 const std::vector<std::int32_t> x_indices
1758 = entities_to_geometry(mesh, dim, subentity_to_entity, true).first;
1759
1760 std::vector<std::int32_t> sub_x_dofs = x_indices;
1761 std::ranges::sort(sub_x_dofs);
1762 auto [unique_end, range_end] = std::ranges::unique(sub_x_dofs);
1763 sub_x_dofs.erase(unique_end, range_end);
1764
1765 // Get the sub-geometry dofs owned by this process
1766 auto x_index_map = geometry.index_map();
1767 assert(x_index_map);
1768
1769 std::shared_ptr<common::IndexMap> sub_x_dof_index_map;
1770 std::vector<std::int32_t> subx_to_x_dofmap;
1771 {
1772 // An owner change is permitted here: a coordinate dof may be needed
1773 // by a sub-mesh cell on a ghosting rank but not on its owner.
1774 auto [map, new_to_old, owners_changed] = common::create_sub_index_map(
1775 *x_index_map, sub_x_dofs, common::IndexMapOrder::any);
1776 sub_x_dof_index_map = std::make_shared<common::IndexMap>(std::move(map));
1777 subx_to_x_dofmap = std::move(new_to_old);
1778 }
1779
1780 // Create sub-geometry coordinates
1781 std::span<const T> x = geometry.x();
1782 std::int32_t sub_num_x_dofs = subx_to_x_dofmap.size();
1783 std::vector<T> sub_x(3 * sub_num_x_dofs);
1784 for (std::int32_t i = 0; i < sub_num_x_dofs; ++i)
1785 {
1786 std::copy_n(std::next(x.begin(), 3 * subx_to_x_dofmap[i]), 3,
1787 std::next(sub_x.begin(), 3 * i));
1788 }
1789
1790 // Create geometry to sub-geometry map
1791 std::vector<std::int32_t> x_to_subx_dof_map(
1792 x_index_map->size_local() + x_index_map->num_ghosts(), -1);
1793 for (std::size_t i = 0; i < subx_to_x_dofmap.size(); ++i)
1794 x_to_subx_dof_map[subx_to_x_dofmap[i]] = i;
1795
1796 // Create sub-geometry dofmap
1797 std::vector<std::int32_t> sub_x_dofmap;
1798 sub_x_dofmap.reserve(x_indices.size());
1799 std::ranges::transform(x_indices, std::back_inserter(sub_x_dofmap),
1800 [&x_to_subx_dof_map](auto x_dof)
1801 {
1802 assert(x_to_subx_dof_map[x_dof] != -1);
1803 return x_to_subx_dof_map[x_dof];
1804 });
1805
1806 // Sub-geometry coordinate element
1807 CellType sub_xcell
1808 = cell_entity_type(geometry.cmaps().front().cell_shape(), dim, 0);
1809
1810 // Special handling of point meshes, as they only support constant
1811 // basis functions
1812 int degree
1813 = (sub_xcell == CellType::point) ? 0 : geometry.cmaps().front().degree();
1814 fem::CoordinateElement<T> sub_cmap(sub_xcell, degree,
1815 geometry.cmaps().front().variant());
1816
1817 // Sub-geometry input_global_indices
1818 const std::vector<std::int64_t>& igi = geometry.input_global_indices();
1819 std::vector<std::int64_t> sub_igi;
1820 sub_igi.reserve(subx_to_x_dofmap.size());
1821 std::ranges::transform(subx_to_x_dofmap, std::back_inserter(sub_igi),
1822 [&igi](auto sub_x_dof) { return igi[sub_x_dof]; });
1823
1824 // Create geometry
1825 return {Geometry(
1826 sub_x_dof_index_map,
1827 std::vector<std::vector<std::int32_t>>{std::move(sub_x_dofmap)},
1828 {sub_cmap}, std::move(sub_x), geometry.dim(), std::move(sub_igi)),
1829 std::move(subx_to_x_dofmap)};
1830}
1831
1841template <std::floating_point T>
1842std::tuple<Mesh<T>, EntityMap, EntityMap, std::vector<std::int32_t>>
1844 std::span<const std::int32_t> entities)
1845{
1846 if (dim < 0 or dim > mesh.topology()->dim())
1847 throw std::invalid_argument("dim out of range for mesh topology.");
1848
1849 // Create sub-topology
1850 mesh.topology_mutable()->create_connectivity(dim, 0);
1851 auto [topology, subentity_to_entity, subvertex_to_vertex]
1852 = mesh::create_subtopology(*mesh.topology(), dim, entities);
1853
1854 // Create sub-geometry
1855 const int tdim = mesh.topology()->dim();
1856 mesh.topology_mutable()->create_entities(dim);
1857 mesh.topology_mutable()->create_connectivity(dim, tdim);
1858 mesh.topology_mutable()->create_connectivity(tdim, dim);
1859 mesh.topology_mutable()->create_cell_permutations();
1860 auto [geometry, subx_to_x_dofmap]
1861 = mesh::create_subgeometry(mesh, dim, subentity_to_entity);
1862
1863 Mesh<T> submesh
1864 = Mesh(mesh.comm(), std::make_shared<Topology>(std::move(topology)),
1865 std::move(geometry));
1866 EntityMap entity_map(mesh.topology(), submesh.topology(), dim,
1867 subentity_to_entity);
1868 EntityMap vertex_map(mesh.topology(), submesh.topology(), 0,
1869 subvertex_to_vertex);
1870 return {std::move(submesh), std::move(entity_map), std::move(vertex_map),
1871 std::move(subx_to_x_dofmap)};
1872}
1873
1885template <typename T>
1887 const MeshTags<T>& tags,
1888 std::shared_ptr<const dolfinx::mesh::Topology> submesh_topology,
1889 const EntityMap& cell_map, const EntityMap& vertex_map)
1890{
1891 int tag_dim = tags.dim();
1892 int submesh_tdim = submesh_topology->dim();
1893 auto topology = tags.topology();
1894 if (tag_dim > submesh_tdim)
1895 {
1896 throw std::invalid_argument("Tag dimension must be less than or equal to "
1897 "submesh dimension");
1898 }
1899
1900 // Validate that cell_map/vertex_map relate `topology` (the tags'
1901 // parent topology) to `submesh_topology`, and have the dimension
1902 // this function assumes.
1903 if (cell_map.dim() != submesh_tdim)
1904 {
1905 throw std::invalid_argument(
1906 "cell_map dimension must equal the submesh topology dimension.");
1907 }
1908 if (cell_map.topology() != topology)
1909 {
1910 throw std::invalid_argument(
1911 "cell_map topology must match tags.topology().");
1912 }
1913 if (cell_map.sub_topology() != submesh_topology)
1914 {
1915 throw std::invalid_argument(
1916 "cell_map sub_topology must match submesh_topology.");
1917 }
1918 if (vertex_map.dim() != 0)
1919 throw std::invalid_argument("vertex_map dimension must be 0.");
1920 if (vertex_map.topology() != topology)
1921 {
1922 throw std::invalid_argument(
1923 "vertex_map topology must match tags.topology().");
1924 }
1925 if (vertex_map.sub_topology() != submesh_topology)
1926 {
1927 throw std::invalid_argument(
1928 "vertex_map sub_topology must match submesh_topology.");
1929 }
1930 std::shared_ptr<const dolfinx::common::IndexMap> sub_cell_imap
1931 = submesh_topology->index_map(submesh_tdim);
1932 if (!sub_cell_imap)
1933 {
1934 throw std::runtime_error(
1935 std::format("Entities of dimension {} does not exist in mesh topology.",
1936 submesh_tdim));
1937 }
1938
1939 // Create a map from parent entity to submesh cell
1940 std::int32_t submesh_num_cells
1941 = sub_cell_imap->size_local() + sub_cell_imap->num_ghosts();
1942 auto sub_cells = std::ranges::views::iota(0, submesh_num_cells);
1943 std::vector<std::int32_t> sub_cell_to_parent_entity
1944 = cell_map.sub_topology_to_topology(sub_cells, false);
1945
1946 // Create a full lookup for all cells on the parent mesh, as the tag can have
1947 // entities that are not in the submesh
1948 auto parent_entity_imap = topology->index_map(submesh_tdim);
1949 if (!parent_entity_imap)
1950 {
1951 throw std::runtime_error(std::format(
1952 "Entities of dimension {} does not exist in parent mesh topology.",
1953 submesh_tdim));
1954 }
1955 std::size_t num_parent_entities
1956 = parent_entity_imap->size_local() + parent_entity_imap->num_ghosts();
1957 std::vector<std::int32_t> parent_entity_to_sub_cell(num_parent_entities, -1);
1958 for (std::size_t i = 0; i < sub_cell_to_parent_entity.size(); ++i)
1959 parent_entity_to_sub_cell[sub_cell_to_parent_entity[i]]
1960 = static_cast<std::int32_t>(i);
1961
1962 // Get map from submesh vertex to parent vertex
1963 std::vector<std::int32_t> sub_to_parent_vertex;
1964 {
1965 auto sub_vertex_map = submesh_topology->index_map(0);
1966 std::int32_t num_sub_vertices
1967 = sub_vertex_map->size_local() + sub_vertex_map->num_ghosts();
1968 auto sub_vertices = std::ranges::views::iota(0, num_sub_vertices);
1969
1970 sub_to_parent_vertex
1971 = vertex_map.sub_topology_to_topology(sub_vertices, false);
1972 }
1973 // Access various connectivity maps
1974 auto sub_e_to_v = submesh_topology->connectivity(tag_dim, 0);
1975 auto sub_c_to_e = submesh_topology->connectivity(submesh_tdim, tag_dim);
1976 auto sub_entity_imap = submesh_topology->index_map(tag_dim);
1977 auto e_to_v = topology->connectivity(tag_dim, 0);
1978 std::shared_ptr<const dolfinx::graph::AdjacencyList<std::int32_t>>
1979 e_to_sub_cell = nullptr;
1980 if (tag_dim != submesh_tdim)
1981 {
1982 e_to_sub_cell = topology->connectivity(tag_dim, submesh_tdim);
1983 if (!e_to_sub_cell)
1984 {
1985 throw std::runtime_error(
1986 std::format("Missing connectivity between {} and {} in parent mesh",
1987 tag_dim, submesh_tdim));
1988 }
1989 }
1990
1991 if (!sub_e_to_v)
1992 {
1993 throw std::runtime_error(std::format(
1994 "Missing connectivity between {} and {} in submesh", tag_dim, 0));
1995 }
1996 if (!sub_c_to_e)
1997 {
1998 throw std::runtime_error(
1999 std::format("Missing connectivity between {} and {} in submesh",
2000 submesh_tdim, tag_dim));
2001 }
2002 if (!sub_entity_imap)
2003 {
2004 throw std::runtime_error(std::format(
2005 "Entities of dimension {} does not exist in submesh topology.",
2006 tag_dim));
2007 }
2008 if (!e_to_v)
2009 {
2010 throw std::runtime_error(
2011 std::format("Missing connectivity between {} and 0", tag_dim));
2012 }
2013
2014 // Prepare sub entity to parent map
2015 std::size_t num_sub_entities
2016 = sub_entity_imap->size_local() + sub_entity_imap->num_ghosts();
2017 std::vector<T> submesh_values(num_sub_entities);
2018 std::vector<std::int8_t> submesh_value_found(num_sub_entities, 0);
2019 std::vector<std::int32_t> submesh_indices(num_sub_entities);
2020 std::iota(submesh_indices.begin(), submesh_indices.end(), 0);
2021
2022 std::span<const std::int32_t> tagged_entities = tags.indices();
2023 std::span<const T> tagged_values = tags.values();
2024 // For each entity in the tag, find all cells of the submesh connected to this
2025 // entity
2026 for (std::size_t i = 0; i < tagged_entities.size(); ++i)
2027 {
2028 auto find_and_map_sub_entity
2029 = [tag_dim, submesh_tdim, &e_to_v, &parent_entity_to_sub_cell,
2030 &sub_to_parent_vertex, &sub_e_to_v, &sub_c_to_e,
2031 &e_to_sub_cell](std::int32_t entity)
2032 {
2033 // Fast exit if the tag dimension is the same as the submesh dimension,
2034 // as we can directly map the parent entity to the submesh cell
2035 if (tag_dim == submesh_tdim)
2036 return parent_entity_to_sub_cell[entity];
2037
2038 // Given an entity in the parent meshtag, find all submesh-cells that are
2039 // entities in parent mesh that contain this entity.
2040 auto entity_vertices = e_to_v->links(entity);
2041 auto parent_sub_cells = e_to_sub_cell->links(entity);
2042 auto submesh_cells
2043 = parent_sub_cells
2044 | std::views::transform([&parent_entity_to_sub_cell](auto c)
2045 { return parent_entity_to_sub_cell[c]; })
2046 | std::views::filter([](auto sub_cell) { return sub_cell != -1; });
2047 for (auto sub_cell : submesh_cells)
2048 {
2049 for (auto sub_entity : sub_c_to_e->links(sub_cell))
2050 {
2051 // Convert submesh entity vertices to parent vertices
2052 auto parent_vertices
2053 = sub_e_to_v->links(sub_entity)
2054 | std::views::transform([&sub_to_parent_vertex](auto v)
2055 { return sub_to_parent_vertex[v]; });
2056
2057 // Check if all parent vertices of the submesh entity are in the
2058 // parent entity
2059 bool entity_matches = std::ranges::all_of(
2060 parent_vertices,
2061 [&entity_vertices](auto p_v)
2062 {
2063 // With C++23 this can use std::ranges::contains
2064 return std::ranges::find(entity_vertices, p_v)
2065 != std::ranges::end(entity_vertices);
2066 });
2067
2068 // If a match is found, apply values and exit the lambda immediately
2069 if (entity_matches)
2070 return sub_entity;
2071 }
2072 }
2073 return -1;
2074 };
2075
2076 // Execute the search for the current entity
2077 std::int32_t sub_entity = find_and_map_sub_entity(tagged_entities[i]);
2078 if (sub_entity != -1)
2079 {
2080 submesh_values[sub_entity] = tagged_values[i];
2081 submesh_value_found[sub_entity] = 1;
2082 }
2083 }
2084
2085 // Filter out the entities that were never mapped. Tracked with a
2086 // separate flag rather than a sentinel value, since any T value
2087 // (including numeric_limits<T>::max()) is a legitimate tag value.
2088 std::vector<std::int32_t> filtered_indices;
2089 std::vector<T> filtered_values;
2090 filtered_indices.reserve(num_sub_entities);
2091 filtered_values.reserve(num_sub_entities);
2092 for (std::size_t i = 0; i < submesh_values.size(); ++i)
2093 {
2094 if (submesh_value_found[i])
2095 {
2096 filtered_indices.push_back(submesh_indices[i]);
2097 filtered_values.push_back(submesh_values[i]);
2098 }
2099 }
2100 filtered_indices.shrink_to_fit();
2101 filtered_values.shrink_to_fit();
2102 MeshTags<T> new_meshtag(submesh_topology, tag_dim, filtered_indices,
2103 filtered_values, tags.name());
2104 return new_meshtag;
2105}
2106
2107} // namespace dolfinx::mesh
Definition CoordinateElement.h:39
ElementDofLayout create_dof_layout() const
Compute and return the dof layout.
Definition CoordinateElement.cpp:80
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:69
bool is_discontinuous() const
Check if the element is the discontinuous version of the coordinate element.
Definition CoordinateElement.cpp:244
Definition ElementDofLayout.h:31
const std::vector< int > & entity_closure_dofs(int dim, int entity_index) const
Definition ElementDofLayout.cpp:65
const std::vector< std::vector< std::vector< int > > > & entity_closure_dofs_all() const
Definition ElementDofLayout.cpp:77
This class provides a static adjacency list data structure.
Definition AdjacencyList.h:41
const std::vector< LinkData > & array() const
Return contiguous array of links for all nodes (const version).
Definition AdjacencyList.h:188
const std::vector< std::int32_t > & offsets() const
Offset for each node in array() (const version).
Definition AdjacencyList.h:194
A bidirectional map relating entities in one topology to another.
Definition EntityMap.h:27
std::vector< std::int32_t > sub_topology_to_topology(CellRange auto &&entities, bool inverse) const
Map entities between the sub-topology and the parent topology.
Definition EntityMap.h:119
int dim() const
Get the topological dimension of the entities related by this EntityMap.
Definition EntityMap.cpp:13
std::shared_ptr< const Topology > sub_topology() const
Get the sub-topology.
Definition EntityMap.cpp:20
std::shared_ptr< const Topology > topology() const
Get the (parent) topology.
Definition EntityMap.cpp:15
Geometry stores the geometry imposed on a mesh.
Definition Geometry.h:39
MeshTags associate values with mesh topology entities.
Definition MeshTags.h:37
const std::string & name() const
Return name.
Definition MeshTags.h:124
std::span< const std::int32_t > indices() const
Definition MeshTags.h:112
std::span< const T > values() const
Values attached to topology entities.
Definition MeshTags.h:115
int dim() const
Return topological dimension of tagged entities.
Definition MeshTags.h:118
std::shared_ptr< const Topology > topology() const
Return topology.
Definition MeshTags.h:121
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:25
std::shared_ptr< Topology > topology()
Get mesh topology.
Definition Mesh.h:74
Requirements on function for geometry marking.
Definition utils.h:558
Small, foundational mesh types (enums, etc.) with minimal dependencies.
void mpi_check(MPI_Comm comm, std::string_view op_name, const std::exception_ptr &error)
Collectively propagate a local failure captured by try_locally to every rank of comm,...
Definition utils.h:958
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:65
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:96
std::vector< std::int64_t > reorder_cells(const graph::Reorder &reorder_fn, std::span< const double > cell_centroids, int gdim, std::optional< std::int32_t > max_facet_to_cell_links, const std::vector< CellType > &celltypes, const std::vector< fem::ElementDofLayout > &doflayouts, const std::vector< std::vector< int > > &ghost_owners, std::vector< std::vector< std::int64_t > > &cells, std::vector< std::span< std::int64_t > > &cells_v, std::vector< std::vector< std::int64_t > > &original_idx, int num_threads)
Find a mesh's process boundary vertices, reordering cells for locality as a side effect.
Definition utils.cpp:35
std::vector< double > cell_centroids_local(std::span< const int > num_vertices_per_cell, const std::vector< std::span< const std::int64_t > > &cells, const boost::unordered_flat_map< std::int64_t, std::size_t > &node_to_pos, std::span< const T > coords, int gdim)
Compute the centroid of each cell from vertex coordinates already available locally.
Definition utils.h:997
std::tuple< std::vector< std::vector< std::int64_t > >, std::vector< std::vector< std::int64_t > >, std::vector< std::vector< int > > > partition_cells(MPI_Comm comm, MPI_Comm commt, const std::vector< std::span< const std::int64_t > > &cells, const std::vector< CellType > &celltypes, const std::vector< fem::ElementDofLayout > &doflayouts, bool p1_geometry, const graph::Partitioner &partitioner, bool ghosting, std::optional< std::int32_t > max_facet_to_cell_links, int num_threads, MPI_Comm commg, std::span< const T > x, std::array< std::size_t, 2 > xshape)
Partition cells across ranks of comm, or, if partitioner does not hold a callable function,...
Definition utils.h:1144
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:512
std::vector< double > compute_cell_centroids(MPI_Comm comm, std::span< const int > num_vertices_per_cell, const std::vector< std::span< const std::int64_t > > &cells, MPI_Comm commg, std::span< const T > x, int gdim)
Compute the centroid of each cell from its vertex positions.
Definition utils.h:1056
void try_locally(F &&fn, std::exception_ptr &error)
Run fn locally, capturing any exception it throws instead of letting it propagate.
Definition utils.h:921
MPI_Datatype mpi_t
Retrieves the MPI data type associated to the provided type.
Definition MPI.h:326
void check_error(MPI_Comm comm, int code) noexcept
Check MPI error code. If the error code is not equal to MPI_SUCCESS, then std::abort is called.
Definition MPI.cpp:89
int size(MPI_Comm comm)
Definition MPI.cpp:81
std::vector< std::ranges::range_value_t< U > > distribute_data(MPI_Comm comm0, std::span< const std::int64_t > indices, MPI_Comm comm1, const U &x, int shape1)
Distribute rows of a row-major array to the ranks that require them, via the post office pattern.
Definition MPI.h:817
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition MPI.cpp:73
void error(PetscErrorCode error_code, std::string_view petsc_function, std::source_location loc=std::source_location::current())
Print error message for a PETSc call that returned an error and throw a std::runtime_error.
Definition petsc.cpp:21
@ any
Allow arbitrary ghost-index ordering.
Definition IndexMap.h:28
std::tuple< IndexMap, std::vector< std::int32_t >, bool > create_sub_index_map(const IndexMap &imap, std::span< const std::int32_t > indices, IndexMapOrder order=IndexMapOrder::any)
Create an index map from a subset of an existing map.
Definition IndexMap.cpp:931
void cells(la::SparsityPattern &pattern, const std::pair< R0, R1 > &cells, std::array< std::reference_wrapper< const DofMap >, 2 > dofmaps)
Iterate over cells and insert entries into sparsity pattern.
Definition sparsitybuild.h:37
Finite element method functionality.
Definition assemble_expression_impl.h:22
Geometry data structures and algorithms.
Definition BoundingBoxTree.h:24
std::tuple< graph::AdjacencyList< std::int64_t >, std::vector< int >, std::vector< std::int64_t >, std::vector< int > > distribute(MPI_Comm comm, const graph::AdjacencyList< std::int64_t > &list, const graph::AdjacencyList< std::int32_t > &destinations)
Distribute adjacency list nodes to destination ranks.
Definition partition.cpp:158
std::variant< reorder_graph_fn, reorder_geom_fn > Reorder
A graph or geometric reordering function for mesh cells.
Definition ordering.h:59
bool has_partitioner(const AnyPartitionFunction &partitioner)
Whether an AnyPartitionFunction holds a callable partitioner.
Definition partition.cpp:131
AdjacencyList< typename std::decay_t< U >::value_type, V > regular_adjacency_list(U &&data, int degree)
Construct a constant degree (valency) adjacency list.
Definition AdjacencyList.h:262
std::function< graph::AdjacencyList< std::int32_t >( MPI_Comm, int, const AdjacencyList< std::int64_t > &, std::optional< std::span< const std::int32_t > >, std::optional< std::span< const std::int32_t > >, bool)> partition_fn
Signature of functions for computing the parallel partitioning of a distributed graph,...
Definition partition.h:38
std::function< std::vector< std::int32_t >( std::span< const double > x, int gdim)> reorder_geom_fn
Signature of functions that reorder points from their positions.
Definition ordering.h:52
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
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 graph::Partitioner &partitioner, GhostMode ghost_mode, std::optional< std::int32_t > max_facet_to_cell_links, int num_threads, const graph::Reorder &reorder_fn=graph::Reorder{})
Create a distributed mesh::Mesh from mesh data and using the provided graph partitioning function for...
Definition utils.h:1401
graph::AdjacencyList< std::int64_t > build_dual_graph(MPI_Comm comm, std::span< const CellType > celltypes, const std::vector< std::span< const std::int64_t > > &cells, std::optional< std::int32_t > max_facet_to_cell_links, int num_threads=1)
Build distributed mesh dual graph (cell-cell connections via facets) from minimal mesh data.
Definition graphbuild.cpp:913
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:351
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:1599
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:1843
bool is_vertex_dof_layout(CellType cell_type, const fem::ElementDofLayout &layout)
Check if extract_topology is the identity operation for a dof layout, i.e. the cell 'nodes' are exact...
Definition utils.cpp:285
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:302
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:692
CellType
Cell type identifier.
Definition cell_types.h:24
int num_cell_vertices(CellType type)
Number vertices for a cell type.
Definition cell_types.cpp:106
std::vector< T > h(const Mesh< T > &mesh, std::span< const std::int32_t > entities, int dim)
Compute greatest distance between any two geometry nodes of the mesh entities (h).
Definition utils.h:299
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:1750
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:242
MeshTags< T > transfer_meshtags_to_submesh(const MeshTags< T > &tags, std::shared_ptr< const dolfinx::mesh::Topology > submesh_topology, const EntityMap &cell_map, const EntityMap &vertex_map)
Transfer a meshtags object from a parent to a submesh.
Definition utils.h:1886
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:773
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:344
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:251
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:581
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:474
GhostMode
Enum for different partitioning ghost modes.
Definition types.h:19
CellType cell_entity_type(CellType type, int d, int index)
Return type of cell for entity of dimension d at given entity index.
Definition cell_types.h:113
constexpr void radix_sort(R &&range, P proj={})
Sort a range with radix sorting algorithm. The bucket size is determined by the number of bits to sor...
Definition sort.h:81
An AnyPartitionFunction together with the node weights it should be called with, if any.
Definition partition.h:156