DOLFINx 0.10.0.0
DOLFINx C++ interface
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1// Copyright (C) 2019-2024 Garth N. Wells
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 "Topology.h"
12#include "graphbuild.h"
13#include <algorithm>
14#include <basix/mdspan.hpp>
15#include <concepts>
16#include <cstdint>
17#include <dolfinx/graph/AdjacencyList.h>
18#include <dolfinx/graph/ordering.h>
19#include <dolfinx/graph/partition.h>
20#include <functional>
21#include <mpi.h>
22#include <numeric>
23#include <span>
24#include <vector>
25
28
29namespace dolfinx::fem
30{
32}
33
34namespace dolfinx::mesh
35{
36enum class CellType : std::int8_t;
37
39enum class GhostMode : std::uint8_t
40{
41 none,
42 shared_facet
43};
44
45namespace impl
46{
52template <typename T>
53void reorder_list(std::span<T> list, std::span<const std::int32_t> nodemap)
54{
55 if (nodemap.empty())
56 return;
57
58 assert(list.size() % nodemap.size() == 0);
59 std::size_t degree = list.size() / nodemap.size();
60 const std::vector<T> orig(list.begin(), list.end());
61 for (std::size_t n = 0; n < nodemap.size(); ++n)
62 {
63 std::span links_old(orig.data() + n * degree, degree);
64 auto links_new = list.subspan(nodemap[n] * degree, degree);
65 std::ranges::copy(links_old, links_new.begin());
66 }
67}
68
82template <std::floating_point T>
83std::tuple<std::vector<std::int32_t>, std::vector<T>, std::vector<std::int32_t>>
85 std::span<const std::int32_t> facets)
86{
87 auto topology = mesh.topology();
88 assert(topology);
89 const int tdim = topology->dim();
90 if (dim == tdim)
91 {
92 throw std::runtime_error(
93 "Cannot use mesh::locate_entities_boundary (boundary) for cells.");
94 }
95
96 // Build set of vertices on boundary and set of boundary entities
97 mesh.topology_mutable()->create_connectivity(tdim - 1, 0);
98 mesh.topology_mutable()->create_connectivity(tdim - 1, dim);
99 std::vector<std::int32_t> vertices, entities;
100 {
101 auto f_to_v = topology->connectivity(tdim - 1, 0);
102 assert(f_to_v);
103 auto f_to_e = topology->connectivity(tdim - 1, dim);
104 assert(f_to_e);
105 for (auto f : facets)
106 {
107 auto v = f_to_v->links(f);
108 vertices.insert(vertices.end(), v.begin(), v.end());
109 auto e = f_to_e->links(f);
110 entities.insert(entities.end(), e.begin(), e.end());
111 }
112
113 // Build vector of boundary vertices
114 {
115 std::ranges::sort(vertices);
116 auto [unique_end, range_end] = std::ranges::unique(vertices);
117 vertices.erase(unique_end, range_end);
118 }
119
120 {
121 std::ranges::sort(entities);
122 auto [unique_end, range_end] = std::ranges::unique(entities);
123 entities.erase(unique_end, range_end);
124 }
125 }
126
127 // Get geometry data
128 auto x_dofmap = mesh.geometry().dofmap();
129 std::span<const T> x_nodes = mesh.geometry().x();
130
131 // Get all vertex 'node' indices
132 mesh.topology_mutable()->create_connectivity(0, tdim);
133 mesh.topology_mutable()->create_connectivity(tdim, 0);
134 auto v_to_c = topology->connectivity(0, tdim);
135 assert(v_to_c);
136 auto c_to_v = topology->connectivity(tdim, 0);
137 assert(c_to_v);
138 std::vector<T> x_vertices(3 * vertices.size(), -1.0);
139 std::vector<std::int32_t> vertex_to_pos(v_to_c->num_nodes(), -1);
140 for (std::size_t i = 0; i < vertices.size(); ++i)
141 {
142 const std::int32_t v = vertices[i];
143
144 // Get first cell and find position
145 const std::int32_t c = v_to_c->links(v).front();
146 auto cell_vertices = c_to_v->links(c);
147 auto it = std::find(cell_vertices.begin(), cell_vertices.end(), v);
148 assert(it != cell_vertices.end());
149 const std::size_t local_pos = std::distance(cell_vertices.begin(), it);
150
151 auto dofs = md::submdspan(x_dofmap, c, md::full_extent);
152 for (std::size_t j = 0; j < 3; ++j)
153 x_vertices[j * vertices.size() + i] = x_nodes[3 * dofs[local_pos] + j];
154 vertex_to_pos[v] = i;
155 }
156
157 return {std::move(entities), std::move(x_vertices), std::move(vertex_to_pos)};
158}
159
160} // namespace impl
161
175std::vector<std::int32_t> exterior_facet_indices(const Topology& topology,
176 int facet_type_idx);
177
189std::vector<std::int32_t> exterior_facet_indices(const Topology& topology);
190
208using CellPartitionFunction = std::function<graph::AdjacencyList<std::int32_t>(
209 MPI_Comm comm, int nparts, const std::vector<CellType>& cell_types,
210 const std::vector<std::span<const std::int64_t>>& cells)>;
211
216using CellReorderFunction = std::function<std::vector<std::int32_t>(
218
226{
238 return [&](const std::vector<CellType>& celltypes,
239 const std::vector<fem::ElementDofLayout>& doflayouts,
240 const std::vector<std::vector<int>>& ghost_owners,
241 std::vector<std::vector<std::int64_t>>& cells,
242 std::vector<std::vector<std::int64_t>>& cells_v,
243 std::vector<std::vector<std::int64_t>>& original_idx)
244 -> std::vector<std::int64_t>
245 {
246 // Build local dual graph for owned cells to (i) get list of vertices
247 // on the process boundary and (ii) apply re-ordering to cells for
248 // locality
249
250 spdlog::info("Build local dual graphs, re-order cells, and compute process "
251 "boundary vertices.");
252
253 std::vector<std::pair<std::vector<std::int64_t>, int>> facets;
254
255 // Build lists of cells (by cell type) that excludes ghosts
256 std::vector<std::span<const std::int64_t>> cells1_v_local;
257 for (std::size_t i = 0; i < celltypes.size(); ++i)
258 {
259 int num_cell_vertices = mesh::num_cell_vertices(celltypes[i]);
260 std::size_t num_owned_cells
261 = cells_v[i].size() / num_cell_vertices - ghost_owners[i].size();
262 cells1_v_local.emplace_back(cells_v[i].data(),
263 num_owned_cells * num_cell_vertices);
264
265 // Build local dual graph for cell type
266 auto [graph, unmatched_facets, max_v, _facet_attached_cells]
267 = build_local_dual_graph(std::vector{celltypes[i]},
268 std::vector{cells1_v_local.back()});
269
270 // Store unmatched_facets for current cell type
271 facets.emplace_back(std::move(unmatched_facets), max_v);
272
273 // Compute re-ordering of graph
274 const std::vector<std::int32_t> remap = reorder_fn(graph);
275
276 // Update 'original' indices
277 const std::vector<std::int64_t>& orig_idx = original_idx[i];
278 std::vector<std::int64_t> _original_idx(orig_idx.size());
279 std::copy_n(orig_idx.rbegin(), ghost_owners[i].size(),
280 _original_idx.rbegin());
281 {
282 for (std::size_t j = 0; j < remap.size(); ++j)
283 _original_idx[remap[j]] = orig_idx[j];
284 }
285 original_idx[i] = _original_idx;
286
287 // Reorder cells
289 std::span(cells_v[i].data(), remap.size() * num_cell_vertices),
290 remap);
292 std::span(cells[i].data(), remap.size() * doflayouts[i].num_dofs()),
293 remap);
294 }
295
296 if (facets.size() == 1) // Optimisation for single cell type
297 {
298 std::vector<std::int64_t>& vertices = facets.front().first;
299
300 // Remove duplicated vertex indices
301 std::ranges::sort(vertices);
302 auto [unique_end, range_end] = std::ranges::unique(vertices);
303 vertices.erase(unique_end, range_end);
304
305 // Remove -1 if it appears as first entity. This can happen in
306 // mixed topology meshes where '-1' is used to pad facet data when
307 // cells facets have differing numbers of vertices.
308 if (!vertices.empty() and vertices.front() == -1)
309 vertices.erase(vertices.begin());
310
311 return vertices;
312 }
313 else
314 {
315 // Pack 'unmatched' facets for all cell types into single array
316 // (facets0)
317 std::vector<std::int64_t> facets0;
318 facets0.reserve(std::accumulate(facets.begin(), facets.end(),
319 std::size_t(0), [](std::size_t x, auto& y)
320 { return x + y.first.size(); }));
321 int max_v = std::ranges::max_element(facets, [](auto& a, auto& b)
322 { return a.second < b.second; })
323 ->second;
324 for (const auto& [v_data, num_v] : facets)
325 {
326 for (auto it = v_data.begin(); it != v_data.end(); it += num_v)
327 {
328 facets0.insert(facets0.end(), it, std::next(it, num_v));
329 facets0.insert(facets0.end(), max_v - num_v, -1);
330 }
331 }
332
333 // Compute row permutation
334 const std::vector<std::int32_t> perm = dolfinx::sort_by_perm(
335 std::span<const std::int64_t>(facets0), max_v);
336
337 // For facets in facets0 that appear only once, store the facet
338 // vertices
339 std::vector<std::int64_t> vertices;
340 // TODO: allocate memory for vertices
341 auto it = perm.begin();
342 while (it != perm.end())
343 {
344 // Find iterator to next facet different from f and trim any -1
345 // padding
346 std::span _f(facets0.data() + (*it) * max_v, max_v);
347 auto end = std::find_if(_f.rbegin(), _f.rend(),
348 [](auto a) { return a >= 0; });
349 auto f = _f.first(std::distance(end, _f.rend()));
350
351 auto it1 = std::find_if_not(
352 it, perm.end(),
353 [f, max_v, it0 = facets0.begin()](auto p) -> bool
354 {
355 return std::equal(f.begin(), f.end(), std::next(it0, p * max_v));
356 });
357
358 // If no repeated facet found, insert f vertices
359 if (std::distance(it, it1) == 1)
360 vertices.insert(vertices.end(), f.begin(), f.end());
361 else if (std::distance(it, it1) > 2)
362 throw std::runtime_error("More than two matching facets found.");
363
364 // Advance iterator
365 it = it1;
366 }
367
368 // Remove duplicate indices
369 std::ranges::sort(vertices);
370 auto [unique_end, range_end] = std::ranges::unique(vertices);
371 vertices.erase(unique_end, range_end);
372
373 return vertices;
374 }
375 };
376}
377
387std::vector<std::int64_t> extract_topology(CellType cell_type,
388 const fem::ElementDofLayout& layout,
389 std::span<const std::int64_t> cells);
390
399template <std::floating_point T>
400std::vector<T> h(const Mesh<T>& mesh, std::span<const std::int32_t> entities,
401 int dim)
402{
403 if (entities.empty())
404 return std::vector<T>();
405 if (dim == 0)
406 return std::vector<T>(entities.size(), 0);
407
408 // Get the geometry dofs for the vertices of each entity
409 const auto [vertex_xdofs, xdof_shape]
410 = entities_to_geometry(mesh, dim, entities, false);
411
412 // Get the geometry coordinate
413 std::span<const T> x = mesh.geometry().x();
414
415 // Function to compute the length of (p0 - p1)
416 auto delta_norm = [](auto&& p0, auto&& p1)
417 {
418 T norm = 0;
419 for (std::size_t i = 0; i < 3; ++i)
420 norm += (p0[i] - p1[i]) * (p0[i] - p1[i]);
421 return std::sqrt(norm);
422 };
423
424 // Compute greatest distance between any to vertices
425 assert(dim > 0);
426 std::vector<T> h(entities.size(), 0);
427 for (std::size_t e = 0; e < entities.size(); ++e)
428 {
429 // Get geometry 'dof' for each vertex of entity e
430 std::span<const std::int32_t> e_vertices(
431 vertex_xdofs.data() + e * xdof_shape[1], xdof_shape[1]);
432
433 // Compute maximum distance between any two vertices
434 for (std::size_t i = 0; i < e_vertices.size(); ++i)
435 {
436 std::span<const T, 3> p0(x.data() + 3 * e_vertices[i], 3);
437 for (std::size_t j = i + 1; j < e_vertices.size(); ++j)
438 {
439 std::span<const T, 3> p1(x.data() + 3 * e_vertices[j], 3);
440 h[e] = std::max(h[e], delta_norm(p0, p1));
441 }
442 }
443 }
444
445 return h;
446}
447
451template <std::floating_point T>
452std::vector<T> cell_normals(const Mesh<T>& mesh, int dim,
453 std::span<const std::int32_t> entities)
454{
455 if (entities.empty())
456 return std::vector<T>();
457
458 auto topology = mesh.topology();
459 assert(topology);
460 if (topology->cell_type() == CellType::prism and dim == 2)
461 {
462 throw std::runtime_error(
463 "Cell normal computation for prism cells not yet supported.");
464 }
465
466 const int gdim = mesh.geometry().dim();
467 const CellType type = cell_entity_type(topology->cell_type(), dim, 0);
468
469 // Find geometry nodes for topology entities
470 std::span<const T> x = mesh.geometry().x();
471 const auto [geometry_entities, eshape]
472 = entities_to_geometry(mesh, dim, entities, false);
473
474 std::vector<T> n(entities.size() * 3);
475 switch (type)
476 {
477 case CellType::interval:
478 {
479 if (gdim > 2)
480 throw std::invalid_argument("Interval cell normal undefined in 3D.");
481 for (std::size_t i = 0; i < entities.size(); ++i)
482 {
483 // Get the two vertices as points
484 std::array vertices{geometry_entities[i * eshape[1]],
485 geometry_entities[i * eshape[1] + 1]};
486 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
487 std::span<const T, 3>(x.data() + 3 * vertices[1], 3)};
488
489 // Define normal by rotating tangent counter-clockwise
490 std::array<T, 3> t;
491 std::ranges::transform(p[1], p[0], t.begin(),
492 [](auto x, auto y) { return x - y; });
493
494 T norm = std::sqrt(t[0] * t[0] + t[1] * t[1]);
495 std::span<T, 3> ni(n.data() + 3 * i, 3);
496 ni[0] = -t[1] / norm;
497 ni[1] = t[0] / norm;
498 ni[2] = 0.0;
499 }
500 return n;
501 }
502 case CellType::triangle:
503 {
504 for (std::size_t i = 0; i < entities.size(); ++i)
505 {
506 // Get the three vertices as points
507 std::array vertices = {geometry_entities[i * eshape[1] + 0],
508 geometry_entities[i * eshape[1] + 1],
509 geometry_entities[i * eshape[1] + 2]};
510 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
511 std::span<const T, 3>(x.data() + 3 * vertices[1], 3),
512 std::span<const T, 3>(x.data() + 3 * vertices[2], 3)};
513
514 // Compute (p1 - p0) and (p2 - p0)
515 std::array<T, 3> dp1, dp2;
516 std::ranges::transform(p[1], p[0], dp1.begin(),
517 [](auto x, auto y) { return x - y; });
518 std::ranges::transform(p[2], p[0], dp2.begin(),
519 [](auto x, auto y) { return x - y; });
520
521 // Define cell normal via cross product of first two edges
522 std::array<T, 3> ni = math::cross(dp1, dp2);
523 T norm = std::sqrt(ni[0] * ni[0] + ni[1] * ni[1] + ni[2] * ni[2]);
524 std::ranges::transform(ni, std::next(n.begin(), 3 * i),
525 [norm](auto x) { return x / norm; });
526 }
527
528 return n;
529 }
530 case CellType::quadrilateral:
531 {
532 // TODO: check
533 for (std::size_t i = 0; i < entities.size(); ++i)
534 {
535 // Get the three vertices as points
536 std::array vertices = {geometry_entities[i * eshape[1] + 0],
537 geometry_entities[i * eshape[1] + 1],
538 geometry_entities[i * eshape[1] + 2]};
539 std::array p = {std::span<const T, 3>(x.data() + 3 * vertices[0], 3),
540 std::span<const T, 3>(x.data() + 3 * vertices[1], 3),
541 std::span<const T, 3>(x.data() + 3 * vertices[2], 3)};
542
543 // Compute (p1 - p0) and (p2 - p0)
544 std::array<T, 3> dp1, dp2;
545 std::ranges::transform(p[1], p[0], dp1.begin(),
546 [](auto x, auto y) { return x - y; });
547 std::ranges::transform(p[2], p[0], dp2.begin(),
548 [](auto x, auto y) { return x - y; });
549
550 // Define cell normal via cross product of first two edges
551 std::array<T, 3> ni = math::cross(dp1, dp2);
552 T norm = std::sqrt(ni[0] * ni[0] + ni[1] * ni[1] + ni[2] * ni[2]);
553 std::ranges::transform(ni, std::next(n.begin(), 3 * i),
554 [norm](auto x) { return x / norm; });
555 }
556
557 return n;
558 }
559 default:
560 throw std::invalid_argument(
561 "cell_normal not supported for this cell type.");
562 }
563}
564
568template <std::floating_point T>
569std::vector<T> compute_midpoints(const Mesh<T>& mesh, int dim,
570 std::span<const std::int32_t> entities)
571{
572 if (entities.empty())
573 return std::vector<T>();
574
575 std::span<const T> x = mesh.geometry().x();
576
577 // Build map from entity -> geometry dof
578 const auto [e_to_g, eshape]
579 = entities_to_geometry(mesh, dim, entities, false);
580
581 std::vector<T> x_mid(entities.size() * 3, 0);
582 for (std::size_t e = 0; e < entities.size(); ++e)
583 {
584 std::span<T, 3> p(x_mid.data() + 3 * e, 3);
585 std::span<const std::int32_t> rows(e_to_g.data() + e * eshape[1],
586 eshape[1]);
587 for (auto row : rows)
588 {
589 std::span<const T, 3> xg(x.data() + 3 * row, 3);
590 std::ranges::transform(p, xg, p.begin(),
591 [size = rows.size()](auto x, auto y)
592 { return x + y / size; });
593 }
594 }
595
596 return x_mid;
597}
598
599namespace impl
600{
605template <std::floating_point T>
606std::pair<std::vector<T>, std::array<std::size_t, 2>>
608{
609 auto topology = mesh.topology();
610 assert(topology);
611 const int tdim = topology->dim();
612
613 // Create entities and connectivities
614
615 // Get all vertex 'node' indices
616 const std::int32_t num_vertices = topology->index_map(0)->size_local()
617 + topology->index_map(0)->num_ghosts();
618
619 std::vector<std::int32_t> vertex_to_node(num_vertices);
620 for (int cell_type_idx = 0,
621 num_cell_types = topology->entity_types(tdim).size();
622 cell_type_idx < num_cell_types; ++cell_type_idx)
623 {
624 auto x_dofmap = mesh.geometry().dofmap(cell_type_idx);
625 auto c_to_v = topology->connectivity({tdim, cell_type_idx}, {0, 0});
626 assert(c_to_v);
627 for (int c = 0; c < c_to_v->num_nodes(); ++c)
628 {
629 auto x_dofs = md::submdspan(x_dofmap, c, md::full_extent);
630 auto vertices = c_to_v->links(c);
631 for (std::size_t i = 0; i < vertices.size(); ++i)
632 vertex_to_node[vertices[i]] = x_dofs[i];
633 }
634 }
635
636 // Pack coordinates of vertices
637 std::span<const T> x_nodes = mesh.geometry().x();
638 std::vector<T> x_vertices(3 * vertex_to_node.size(), 0.0);
639 for (std::size_t i = 0; i < vertex_to_node.size(); ++i)
640 {
641 std::int32_t pos = 3 * vertex_to_node[i];
642 for (std::size_t j = 0; j < 3; ++j)
643 x_vertices[j * vertex_to_node.size() + i] = x_nodes[pos + j];
644 }
645
646 return {std::move(x_vertices), {3, vertex_to_node.size()}};
647}
648
649} // namespace impl
650
652template <typename Fn, typename T>
653concept MarkerFn = std::is_invocable_r<
654 std::vector<std::int8_t>, Fn,
655 md::mdspan<const T,
656 md::extents<std::size_t, 3, md::dynamic_extent>>>::value;
657
673template <std::floating_point T, MarkerFn<T> U>
674std::vector<std::int32_t> locate_entities(const Mesh<T>& mesh, int dim,
675 U marker, int entity_type_idx)
676{
677
678 using cmdspan3x_t
679 = md::mdspan<const T, md::extents<std::size_t, 3, md::dynamic_extent>>;
680
681 // Run marker function on vertex coordinates
682 const auto [xdata, xshape] = impl::compute_vertex_coords(mesh);
683
684 cmdspan3x_t x(xdata.data(), xshape);
685 const std::vector<std::int8_t> marked = marker(x);
686 if (marked.size() != x.extent(1))
687 throw std::runtime_error("Length of array of markers is wrong.");
688
689 auto topology = mesh.topology();
690 assert(topology);
691 const int tdim = topology->dim();
692
693 mesh.topology_mutable()->create_entities(dim);
694 if (dim < tdim)
695 mesh.topology_mutable()->create_connectivity(dim, 0);
696
697 // Iterate over entities of dimension 'dim' to build vector of marked
698 // entities
699 auto e_to_v = topology->connectivity({dim, entity_type_idx}, {0, 0});
700 assert(e_to_v);
701 std::vector<std::int32_t> entities;
702 for (int e = 0; e < e_to_v->num_nodes(); ++e)
703 {
704 // Iterate over entity vertices
705 bool all_vertices_marked = true;
706 for (std::int32_t v : e_to_v->links(e))
707 {
708 if (!marked[v])
709 {
710 all_vertices_marked = false;
711 break;
712 }
713 }
714
715 if (all_vertices_marked)
716 entities.push_back(e);
717 }
718
719 return entities;
720}
721
735template <std::floating_point T, MarkerFn<T> U>
736std::vector<std::int32_t> locate_entities(const Mesh<T>& mesh, int dim,
737 U marker)
738{
739 const int num_entity_types = mesh.topology()->entity_types(dim).size();
740 if (num_entity_types > 1)
741 {
742 throw std::runtime_error(
743 "Multiple entity types of this dimension. Specify entity type index");
744 }
745 return locate_entities(mesh, dim, marker, 0);
746}
747
771template <std::floating_point T, MarkerFn<T> U>
772std::vector<std::int32_t> locate_entities_boundary(const Mesh<T>& mesh, int dim,
773 U marker)
774{
775 // TODO Rewrite this function, it should be possible to simplify considerably
776 auto topology = mesh.topology();
777 assert(topology);
778 int tdim = topology->dim();
779 if (dim == tdim)
780 {
781 throw std::runtime_error(
782 "Cannot use mesh::locate_entities_boundary (boundary) for cells.");
783 }
784
785 // Compute list of boundary facets
786 mesh.topology_mutable()->create_entities(tdim - 1);
787 mesh.topology_mutable()->create_connectivity(tdim - 1, tdim);
788 std::vector<std::int32_t> boundary_facets = exterior_facet_indices(*topology);
789
790 using cmdspan3x_t
791 = md::mdspan<const T, md::extents<std::size_t, 3, md::dynamic_extent>>;
792
793 // Run marker function on the vertex coordinates
794 auto [facet_entities, xdata, vertex_to_pos]
795 = impl::compute_vertex_coords_boundary(mesh, dim, boundary_facets);
796 cmdspan3x_t x(xdata.data(), 3, xdata.size() / 3);
797 std::vector<std::int8_t> marked = marker(x);
798 if (marked.size() != x.extent(1))
799 throw std::runtime_error("Length of array of markers is wrong.");
800
801 // Loop over entities and check vertex markers
802 mesh.topology_mutable()->create_entities(dim);
803 auto e_to_v = topology->connectivity(dim, 0);
804 assert(e_to_v);
805 std::vector<std::int32_t> entities;
806 for (auto e : facet_entities)
807 {
808 // Iterate over entity vertices
809 bool all_vertices_marked = true;
810 for (auto v : e_to_v->links(e))
811 {
812 const std::int32_t pos = vertex_to_pos[v];
813 if (!marked[pos])
814 {
815 all_vertices_marked = false;
816 break;
817 }
818 }
819
820 // Mark facet with all vertices marked
821 if (all_vertices_marked)
822 entities.push_back(e);
823 }
824
825 return entities;
826}
827
846template <std::floating_point T>
847std::pair<std::vector<std::int32_t>, std::array<std::size_t, 2>>
849 std::span<const std::int32_t> entities,
850 bool permute = false)
851{
852 auto topology = mesh.topology();
853 assert(topology);
854 CellType cell_type = topology->cell_type();
855 if (cell_type == CellType::prism and dim == 2)
856 {
857 throw std::runtime_error(
858 "mesh::entities_to_geometry for prism cells not yet supported.");
859 }
860
861 const int tdim = topology->dim();
862 const Geometry<T>& geometry = mesh.geometry();
863 auto xdofs = geometry.dofmap();
864
865 // Get the DOF layout and the number of DOFs per entity
866 const fem::CoordinateElement<T>& coord_ele = geometry.cmap();
867 const fem::ElementDofLayout layout = coord_ele.create_dof_layout();
868 const std::size_t num_entity_dofs = layout.num_entity_closure_dofs(dim);
869 std::vector<std::int32_t> entity_xdofs;
870 entity_xdofs.reserve(entities.size() * num_entity_dofs);
871 std::array<std::size_t, 2> eshape{entities.size(), num_entity_dofs};
872
873 // Get the element's closure DOFs
874 const std::vector<std::vector<std::vector<int>>>& closure_dofs_all
875 = layout.entity_closure_dofs_all();
876
877 // Special case when dim == tdim (cells)
878 if (dim == tdim)
879 {
880 for (std::int32_t c : entities)
881 {
882 // Extract degrees of freedom
883 auto x_c = md::submdspan(xdofs, c, md::full_extent);
884 for (std::int32_t entity_dof : closure_dofs_all[tdim][0])
885 entity_xdofs.push_back(x_c[entity_dof]);
886 }
887
888 return {std::move(entity_xdofs), eshape};
889 }
890
891 assert(dim != tdim);
892
893 auto e_to_c = topology->connectivity(dim, tdim);
894 if (!e_to_c)
895 {
896 throw std::runtime_error(
897 "Entity-to-cell connectivity has not been computed. Missing dims "
898 + std::to_string(dim) + "->" + std::to_string(tdim));
899 }
900
901 auto c_to_e = topology->connectivity(tdim, dim);
902 if (!c_to_e)
903 {
904 throw std::runtime_error(
905 "Cell-to-entity connectivity has not been computed. Missing dims "
906 + std::to_string(tdim) + "->" + std::to_string(dim));
907 }
908
909 // Get the cell info, which is needed to permute the closure dofs
910 std::span<const std::uint32_t> cell_info;
911 if (permute)
912 cell_info = std::span(mesh.topology()->get_cell_permutation_info());
913
914 for (std::int32_t e : entities)
915 {
916 // Get a cell connected to the entity
917 assert(!e_to_c->links(e).empty());
918 std::int32_t c = e_to_c->links(e).front();
919
920 // Get the local index of the entity
921 std::span<const std::int32_t> cell_entities = c_to_e->links(c);
922 auto it = std::find(cell_entities.begin(), cell_entities.end(), e);
923 assert(it != cell_entities.end());
924 std::size_t local_entity = std::distance(cell_entities.begin(), it);
925
926 // Cell sub-entities must be permuted so that their local
927 // orientation agrees with their global orientation
928 std::vector<std::int32_t> closure_dofs(closure_dofs_all[dim][local_entity]);
929 if (permute)
930 {
931 mesh::CellType entity_type
932 = mesh::cell_entity_type(cell_type, dim, local_entity);
933 coord_ele.permute_subentity_closure(closure_dofs, cell_info[c],
934 entity_type, local_entity);
935 }
936
937 // Extract degrees of freedom
938 auto x_c = md::submdspan(xdofs, c, md::full_extent);
939 for (std::int32_t entity_dof : closure_dofs)
940 entity_xdofs.push_back(x_c[entity_dof]);
941 }
942
943 return {std::move(entity_xdofs), eshape};
944}
945
951 = mesh::GhostMode::none,
952 const graph::partition_fn& partfn
954
962std::vector<std::int32_t>
963compute_incident_entities(const Topology& topology,
964 std::span<const std::int32_t> entities, int d0,
965 int d1);
966
1008template <typename U>
1010 MPI_Comm comm, MPI_Comm commt,
1011 std::vector<std::span<const std::int64_t>> cells,
1012 const std::vector<fem::CoordinateElement<
1013 typename std::remove_reference_t<typename U::value_type>>>& elements,
1014 MPI_Comm commg, const U& x, std::array<std::size_t, 2> xshape,
1015 const CellPartitionFunction& partitioner,
1016 const CellReorderFunction& reorder_fn = graph::reorder_gps)
1017{
1018 assert(cells.size() == elements.size());
1019 std::vector<CellType> celltypes;
1020 std::ranges::transform(elements, std::back_inserter(celltypes),
1021 [](auto& e) { return e.cell_shape(); });
1022 std::vector<fem::ElementDofLayout> doflayouts;
1023 std::ranges::transform(elements, std::back_inserter(doflayouts),
1024 [](auto& e) { return e.create_dof_layout(); });
1025
1026 // Note: `extract_topology` extracts topology data, i.e. just the
1027 // vertices. For P1 geometry this should just be the identity
1028 // operator. For other elements the filtered lists may have 'gaps',
1029 // i.e. the indices might not be contiguous.
1030 //
1031 // `extract_topology` could be skipped for 'P1 geometry' elements
1032
1033 std::int32_t num_cell_types = cells.size();
1034
1035 // -- Partition topology across ranks of comm
1036 std::vector<std::vector<std::int64_t>> cells1(num_cell_types);
1037 std::vector<std::vector<std::int64_t>> original_idx1(num_cell_types);
1038 std::vector<std::vector<int>> ghost_owners(num_cell_types);
1039 if (partitioner)
1040 {
1041 spdlog::info("Using partitioner with cell data ({} cell types)",
1042 num_cell_types);
1044 if (commt != MPI_COMM_NULL)
1045 {
1046 int size = dolfinx::MPI::size(comm);
1047 std::vector<std::vector<std::int64_t>> t(num_cell_types);
1048 std::vector<std::span<const std::int64_t>> tspan(num_cell_types);
1049 for (std::int32_t i = 0; i < num_cell_types; ++i)
1050 {
1051 t[i] = extract_topology(celltypes[i], doflayouts[i], cells[i]);
1052 tspan[i] = std::span(t[i]);
1053 }
1054 dest = partitioner(commt, size, celltypes, tspan);
1055 }
1056
1057 std::int32_t cell_offset = 0;
1058 for (std::int32_t i = 0; i < num_cell_types; ++i)
1059 {
1060 std::size_t num_cell_nodes = doflayouts[i].num_dofs();
1061 assert(cells[i].size() % num_cell_nodes == 0);
1062 std::size_t num_cells = cells[i].size() / num_cell_nodes;
1063
1064 // Extract destination AdjacencyList for this cell type
1065 std::vector<std::int32_t> offsets_i(
1066 std::next(dest.offsets().begin(), cell_offset),
1067 std::next(dest.offsets().begin(), cell_offset + num_cells + 1));
1068 std::vector<std::int32_t> data_i(
1069 std::next(dest.array().begin(), offsets_i.front()),
1070 std::next(dest.array().begin(), offsets_i.back()));
1071 std::int32_t offset_0 = offsets_i.front();
1072 std::ranges::for_each(offsets_i,
1073 [&offset_0](std::int32_t& j) { j -= offset_0; });
1074 graph::AdjacencyList<std::int32_t> dest_i(data_i, offsets_i);
1075 cell_offset += num_cells;
1076
1077 // Distribute cells (topology, includes higher-order 'nodes') to
1078 // destination rank
1079 std::vector<int> src_ranks;
1080 std::tie(cells1[i], src_ranks, original_idx1[i], ghost_owners[i])
1081 = graph::build::distribute(comm, cells[i],
1082 {num_cells, num_cell_nodes}, dest_i);
1083 spdlog::debug("Got {} cells from distribution", cells1[i].size());
1084 }
1085 }
1086 else
1087 {
1088 // No partitioning, construct a global index
1089 std::int64_t num_owned = 0;
1090 for (std::int32_t i = 0; i < num_cell_types; ++i)
1091 {
1092 cells1[i] = std::vector<std::int64_t>(cells[i].begin(), cells[i].end());
1093 std::int32_t num_cell_nodes = doflayouts[i].num_dofs();
1094 assert(cells1[i].size() % num_cell_nodes == 0);
1095 original_idx1[i].resize(cells1[i].size() / num_cell_nodes);
1096 num_owned += original_idx1[i].size();
1097 }
1098
1099 // Add on global offset
1100 std::int64_t global_offset = 0;
1101 MPI_Exscan(&num_owned, &global_offset, 1, MPI_INT64_T, MPI_SUM, comm);
1102 for (std::int32_t i = 0; i < num_cell_types; ++i)
1103 {
1104 std::iota(original_idx1[i].begin(), original_idx1[i].end(),
1105 global_offset);
1106 global_offset += original_idx1[i].size();
1107 }
1108 }
1109
1110 // Extract cell 'topology', i.e. extract the vertices for each cell
1111 // and discard any 'higher-order' nodes
1112 std::vector<std::vector<std::int64_t>> cells1_v(num_cell_types);
1113 for (std::int32_t i = 0; i < num_cell_types; ++i)
1114 {
1115 cells1_v[i] = extract_topology(celltypes[i], doflayouts[i], cells1[i]);
1116 spdlog::info("Extract basic topology: {}->{}", cells1[i].size(),
1117 cells1_v[i].size());
1118 }
1119
1120 auto boundary_v_fn = create_boundary_vertices_fn(reorder_fn);
1121 const std::vector<std::int64_t> boundary_v = boundary_v_fn(
1122 celltypes, doflayouts, ghost_owners, cells1, cells1_v, original_idx1);
1123
1124 spdlog::debug("Got {} boundary vertices", boundary_v.size());
1125
1126 // Create Topology
1127 std::vector<std::span<const std::int64_t>> cells1_v_span;
1128 std::ranges::transform(cells1_v, std::back_inserter(cells1_v_span),
1129 [](auto& c) { return std::span(c); });
1130 std::vector<std::span<const std::int64_t>> original_idx1_span;
1131 std::ranges::transform(original_idx1, std::back_inserter(original_idx1_span),
1132 [](auto& c) { return std::span(c); });
1133 std::vector<std::span<const int>> ghost_owners_span;
1134 std::ranges::transform(ghost_owners, std::back_inserter(ghost_owners_span),
1135 [](auto& c) { return std::span(c); });
1136 Topology topology
1137 = create_topology(comm, celltypes, cells1_v_span, original_idx1_span,
1138 ghost_owners_span, boundary_v);
1139
1140 // Create connectivities required higher-order geometries for creating
1141 // a Geometry object
1142 for (int i = 0; i < num_cell_types; ++i)
1143 {
1144 for (int e = 1; e < topology.dim(); ++e)
1145 {
1146 if (doflayouts[i].num_entity_dofs(e) > 0)
1147 topology.create_entities(e);
1148 }
1149
1150 if (elements[i].needs_dof_permutations())
1151 topology.create_entity_permutations();
1152 }
1153
1154 // Build list of unique (global) node indices from cells1 and
1155 // distribute coordinate data
1156 std::vector<std::int64_t> nodes1, nodes2;
1157 for (std::vector<std::int64_t>& c : cells1)
1158 nodes1.insert(nodes1.end(), c.begin(), c.end());
1159 for (std::vector<std::int64_t>& c : cells1)
1160 nodes2.insert(nodes2.end(), c.begin(), c.end());
1161
1162 dolfinx::radix_sort(nodes1);
1163 auto [unique_end, range_end] = std::ranges::unique(nodes1);
1164 nodes1.erase(unique_end, range_end);
1165
1166 std::vector coords
1167 = dolfinx::MPI::distribute_data(comm, nodes1, commg, x, xshape[1]);
1168
1169 // Create geometry object
1171 = create_geometry(topology, elements, nodes1, nodes2, coords, xshape[1]);
1172
1173 return Mesh(comm, std::make_shared<Topology>(std::move(topology)),
1174 std::move(geometry));
1175}
1176
1212template <typename U>
1214 MPI_Comm comm, MPI_Comm commt, std::span<const std::int64_t> cells,
1216 typename std::remove_reference_t<typename U::value_type>>& element,
1217 MPI_Comm commg, const U& x, std::array<std::size_t, 2> xshape,
1218 const CellPartitionFunction& partitioner,
1219 const CellReorderFunction& reorder_fn = graph::reorder_gps)
1220{
1221 return create_mesh(comm, commt, std::vector{cells}, std::vector{element},
1222 commg, x, xshape, partitioner, reorder_fn);
1223}
1224
1243template <typename U>
1244Mesh<typename std::remove_reference_t<typename U::value_type>>
1245create_mesh(MPI_Comm comm, std::span<const std::int64_t> cells,
1247 std::remove_reference_t<typename U::value_type>>& elements,
1248 const U& x, std::array<std::size_t, 2> xshape, GhostMode ghost_mode)
1249{
1250 if (dolfinx::MPI::size(comm) == 1)
1251 return create_mesh(comm, comm, std::vector{cells}, std::vector{elements},
1252 comm, x, xshape, nullptr);
1253 else
1254 {
1255 return create_mesh(comm, comm, std::vector{cells}, std::vector{elements},
1256 comm, x, xshape, create_cell_partitioner(ghost_mode));
1257 }
1258}
1259
1273template <std::floating_point T>
1274std::pair<Geometry<T>, std::vector<int32_t>>
1276 std::span<const std::int32_t> subentity_to_entity)
1277{
1278 const Geometry<T>& geometry = mesh.geometry();
1279
1280 // Get the geometry dofs in the sub-geometry based on the entities in
1281 // sub-geometry
1282 const fem::ElementDofLayout layout = geometry.cmap().create_dof_layout();
1283
1284 const std::vector<std::int32_t> x_indices
1285 = entities_to_geometry(mesh, dim, subentity_to_entity, true).first;
1286
1287 std::vector<std::int32_t> sub_x_dofs = x_indices;
1288 std::ranges::sort(sub_x_dofs);
1289 auto [unique_end, range_end] = std::ranges::unique(sub_x_dofs);
1290 sub_x_dofs.erase(unique_end, range_end);
1291
1292 // Get the sub-geometry dofs owned by this process
1293 auto x_index_map = geometry.index_map();
1294 assert(x_index_map);
1295
1296 std::shared_ptr<common::IndexMap> sub_x_dof_index_map;
1297 std::vector<std::int32_t> subx_to_x_dofmap;
1298 {
1299 auto [map, new_to_old] = common::create_sub_index_map(
1300 *x_index_map, sub_x_dofs, common::IndexMapOrder::any, true);
1301 sub_x_dof_index_map = std::make_shared<common::IndexMap>(std::move(map));
1302 subx_to_x_dofmap = std::move(new_to_old);
1303 }
1304
1305 // Create sub-geometry coordinates
1306 std::span<const T> x = geometry.x();
1307 std::int32_t sub_num_x_dofs = subx_to_x_dofmap.size();
1308 std::vector<T> sub_x(3 * sub_num_x_dofs);
1309 for (std::int32_t i = 0; i < sub_num_x_dofs; ++i)
1310 {
1311 std::copy_n(std::next(x.begin(), 3 * subx_to_x_dofmap[i]), 3,
1312 std::next(sub_x.begin(), 3 * i));
1313 }
1314
1315 // Create geometry to sub-geometry map
1316 std::vector<std::int32_t> x_to_subx_dof_map(
1317 x_index_map->size_local() + x_index_map->num_ghosts(), -1);
1318 for (std::size_t i = 0; i < subx_to_x_dofmap.size(); ++i)
1319 x_to_subx_dof_map[subx_to_x_dofmap[i]] = i;
1320
1321 // Create sub-geometry dofmap
1322 std::vector<std::int32_t> sub_x_dofmap;
1323 sub_x_dofmap.reserve(x_indices.size());
1324 std::ranges::transform(x_indices, std::back_inserter(sub_x_dofmap),
1325 [&x_to_subx_dof_map](auto x_dof)
1326 {
1327 assert(x_to_subx_dof_map[x_dof] != -1);
1328 return x_to_subx_dof_map[x_dof];
1329 });
1330
1331 // Sub-geometry coordinate element
1332 CellType sub_xcell = cell_entity_type(geometry.cmap().cell_shape(), dim, 0);
1333
1334 // Special handling of point meshes, as they only support constant
1335 // basis functions
1336 int degree = (sub_xcell == CellType::point) ? 0 : geometry.cmap().degree();
1337 fem::CoordinateElement<T> sub_cmap(sub_xcell, degree,
1338 geometry.cmap().variant());
1339
1340 // Sub-geometry input_global_indices
1341 const std::vector<std::int64_t>& igi = geometry.input_global_indices();
1342 std::vector<std::int64_t> sub_igi;
1343 sub_igi.reserve(subx_to_x_dofmap.size());
1344 std::ranges::transform(subx_to_x_dofmap, std::back_inserter(sub_igi),
1345 [&igi](auto sub_x_dof) { return igi[sub_x_dof]; });
1346
1347 // Create geometry
1348 return {Geometry(
1349 sub_x_dof_index_map,
1350 std::vector<std::vector<std::int32_t>>{std::move(sub_x_dofmap)},
1351 {sub_cmap}, std::move(sub_x), geometry.dim(), std::move(sub_igi)),
1352 std::move(subx_to_x_dofmap)};
1353}
1354
1364template <std::floating_point T>
1365std::tuple<Mesh<T>, EntityMap, EntityMap, std::vector<std::int32_t>>
1367 std::span<const std::int32_t> entities)
1368{
1369 // Create sub-topology
1370 mesh.topology_mutable()->create_connectivity(dim, 0);
1371 auto [topology, subentity_to_entity, subvertex_to_vertex]
1372 = mesh::create_subtopology(*mesh.topology(), dim, entities);
1373
1374 // Create sub-geometry
1375 const int tdim = mesh.topology()->dim();
1376 mesh.topology_mutable()->create_entities(dim);
1377 mesh.topology_mutable()->create_connectivity(dim, tdim);
1378 mesh.topology_mutable()->create_connectivity(tdim, dim);
1379 mesh.topology_mutable()->create_entity_permutations();
1380 auto [geometry, subx_to_x_dofmap]
1381 = mesh::create_subgeometry(mesh, dim, subentity_to_entity);
1382
1383 Mesh<T> submesh
1384 = Mesh(mesh.comm(), std::make_shared<Topology>(std::move(topology)),
1385 std::move(geometry));
1386 EntityMap entity_map(mesh.topology(), submesh.topology(), dim,
1387 subentity_to_entity);
1388 EntityMap vertex_map(mesh.topology(), submesh.topology(), 0,
1389 subvertex_to_vertex);
1390 return {std::move(submesh), std::move(entity_map), std::move(vertex_map),
1391 std::move(subx_to_x_dofmap)};
1392}
1393
1394} // namespace dolfinx::mesh
Definition CoordinateElement.h:38
ElementDofLayout create_dof_layout() const
Compute and return the dof layout.
Definition CoordinateElement.cpp:75
void permute_subentity_closure(std::span< std::int32_t > d, std::uint32_t cell_info, mesh::CellType entity_type, int entity_index) const
Given the closure DOFs of a cell sub-entity in reference ordering, this function computes the permut...
Definition CoordinateElement.cpp:64
Definition ElementDofLayout.h:31
const std::vector< std::vector< std::vector< int > > > & entity_closure_dofs_all() const
Definition ElementDofLayout.cpp:92
int num_entity_closure_dofs(int dim) const
Definition ElementDofLayout.cpp:68
This class provides a static adjacency list data structure.
Definition AdjacencyList.h:38
const std::vector< LinkData > & array() const
Return contiguous array of links for all nodes (const version).
Definition AdjacencyList.h:173
const std::vector< std::int32_t > & offsets() const
Offset for each node in array() (const version).
Definition AdjacencyList.h:179
A bidirectional map relating entities in one topology to another.
Definition EntityMap.h:20
Geometry stores the geometry imposed on a mesh.
Definition Geometry.h:34
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
Topology stores the topology of a mesh, consisting of mesh entities and connectivity (incidence relat...
Definition Topology.h:41
Requirements on function for geometry marking.
Definition utils.h:653
void reorder_list(std::span< T > list, std::span< const std::int32_t > nodemap)
Re-order the nodes of a fixed-degree adjacency list.
Definition utils.h:53
std::tuple< std::vector< std::int32_t >, std::vector< T >, std::vector< std::int32_t > > compute_vertex_coords_boundary(const mesh::Mesh< T > &mesh, int dim, std::span< const std::int32_t > facets)
Compute the coordinates of 'vertices' for entities of a given dimension that are attached to specifie...
Definition utils.h:84
std::pair< std::vector< T >, std::array< std::size_t, 2 > > compute_vertex_coords(const mesh::Mesh< T > &mesh)
The coordinates for all 'vertices' in the mesh.
Definition utils.h:607
std::vector< typename std::remove_reference_t< typename U::value_type > > distribute_data(MPI_Comm comm0, std::span< const std::int64_t > indices, MPI_Comm comm1, const U &x, int shape1)
Distribute rows of a rectangular data array to ranks where they are required (scalable version).
Definition MPI.h:679
int size(MPI_Comm comm)
Definition MPI.cpp:72
std::pair< IndexMap, std::vector< std::int32_t > > create_sub_index_map(const IndexMap &imap, std::span< const std::int32_t > indices, IndexMapOrder order=IndexMapOrder::any, bool allow_owner_change=false)
Create a new index map from a subset of indices in an existing index map.
Definition IndexMap.cpp:816
@ any
Allow arbitrary ordering of ghost indices in sub-maps.
Definition IndexMap.h:29
Finite element method functionality.
Definition assemble_expression_impl.h:23
Geometry data structures and algorithms.
Definition BoundingBoxTree.h:22
std::tuple< graph::AdjacencyList< std::int64_t >, std::vector< int >, std::vector< std::int64_t >, std::vector< int > > distribute(MPI_Comm comm, const graph::AdjacencyList< std::int64_t > &list, const graph::AdjacencyList< std::int32_t > &destinations)
Distribute adjacency list nodes to destination ranks.
Definition partition.cpp:38
Graph data structures and algorithms.
Definition AdjacencyList.h:20
std::vector< std::int32_t > reorder_gps(const graph::AdjacencyList< std::int32_t > &graph)
Re-order a graph using the Gibbs-Poole-Stockmeyer algorithm.
Definition ordering.cpp:361
std::function< graph::AdjacencyList< std::int32_t >( MPI_Comm, int, const AdjacencyList< std::int64_t > &, bool)> partition_fn
Signature of functions for computing the parallel partitioning of a distributed graph.
Definition partition.h:31
AdjacencyList< std::int32_t > partition_graph(MPI_Comm comm, int nparts, const AdjacencyList< std::int64_t > &local_graph, bool ghosting)
Partition graph across processes using the default graph partitioner.
Definition partition.cpp:21
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
Topology create_topology(MPI_Comm comm, const std::vector< CellType > &cell_types, std::vector< std::span< const std::int64_t > > cells, std::vector< std::span< const std::int64_t > > original_cell_index, std::vector< std::span< const int > > ghost_owners, std::span< const std::int64_t > boundary_vertices)
Create a mesh topology.
Definition Topology.cpp:1025
std::tuple< graph::AdjacencyList< std::int32_t >, std::vector< std::int64_t >, std::size_t, std::vector< std::int32_t > > build_local_dual_graph(std::span< const CellType > celltypes, const std::vector< std::span< const std::int64_t > > &cells, std::optional< std::int32_t > max_facet_to_cell_links=2)
Compute the local part of the dual graph (cell-cell connections via facets) and facets with only one ...
Definition graphbuild.cpp:405
std::vector< T > cell_normals(const Mesh< T > &mesh, int dim, std::span< const std::int32_t > entities)
Compute normal to given cell (viewed as embedded in 3D).
Definition utils.h:452
CellType cell_entity_type(CellType type, int d, int index)
Return type of cell for entity of dimension d at given entity index.
Definition cell_types.cpp:64
std::function< std::vector< std::int32_t >( const graph::AdjacencyList< std::int32_t > &)> CellReorderFunction
Function that reorders (locally) cells that are owned by this process. It takes the local mesh dual g...
Definition utils.h:216
std::tuple< Topology, std::vector< int32_t >, std::vector< int32_t > > create_subtopology(const Topology &topology, int dim, std::span< const std::int32_t > entities)
Create a topology for a subset of entities of a given topological dimension.
Definition Topology.cpp:1313
std::tuple< Mesh< T >, EntityMap, EntityMap, std::vector< std::int32_t > > create_submesh(const Mesh< T > &mesh, int dim, std::span< const std::int32_t > entities)
Create a new mesh consisting of a subset of entities in a mesh.
Definition utils.h:1366
std::vector< std::int32_t > exterior_facet_indices(const Topology &topology, int facet_type_idx)
Compute the indices of all exterior facets that are owned by the caller.
Definition utils.cpp:58
std::vector< std::int32_t > locate_entities_boundary(const Mesh< T > &mesh, int dim, U marker)
Compute indices of all mesh entities that are attached to an owned boundary facet and evaluate to tru...
Definition utils.h:772
CellType
Cell type identifier.
Definition cell_types.h:22
int num_cell_vertices(CellType type)
Definition cell_types.cpp:147
std::vector< T > h(const Mesh< T > &mesh, std::span< const std::int32_t > entities, int dim)
Compute greatest distance between any two vertices of the mesh entities (h).
Definition utils.h:400
std::pair< Geometry< T >, std::vector< int32_t > > create_subgeometry(const Mesh< T > &mesh, int dim, std::span< const std::int32_t > subentity_to_entity)
Create a sub-geometry from a mesh and a subset of mesh entities to be included.
Definition utils.h:1275
auto create_boundary_vertices_fn(const CellReorderFunction &reorder_fn)
Creates the default boundary vertices routine for a given reorder function.
Definition utils.h:225
Geometry< typename std::remove_reference_t< typename U::value_type > > create_geometry(const Topology &topology, const std::vector< fem::CoordinateElement< std::remove_reference_t< typename U::value_type > > > &elements, std::span< const std::int64_t > nodes, std::span< const std::int64_t > xdofs, const U &x, int dim, const std::function< std::vector< int >(const graph::AdjacencyList< std::int32_t > &)> &reorder_fn=nullptr)
Build Geometry from input data.
Definition Geometry.h:237
std::pair< std::vector< std::int32_t >, std::array< std::size_t, 2 > > entities_to_geometry(const Mesh< T > &mesh, int dim, std::span< const std::int32_t > entities, bool permute=false)
Compute the geometry degrees of freedom associated with the closure of a given set of cell entities.
Definition utils.h:848
std::vector< std::int32_t > compute_incident_entities(const Topology &topology, std::span< const std::int32_t > entities, int d0, int d1)
Compute incident entities.
Definition utils.cpp:123
std::vector< std::int64_t > extract_topology(CellType cell_type, const fem::ElementDofLayout &layout, std::span< const std::int64_t > cells)
Extract topology from cell data, i.e. extract cell vertices.
Definition utils.cpp:29
std::vector< std::int32_t > locate_entities(const Mesh< T > &mesh, int dim, U marker, int entity_type_idx)
Compute indices of all mesh entities that evaluate to true for the provided geometric marking functio...
Definition utils.h:674
std::function< graph::AdjacencyList< std::int32_t >( MPI_Comm comm, int nparts, const std::vector< CellType > &cell_types, const std::vector< std::span< const std::int64_t > > &cells)> CellPartitionFunction
Signature for the cell partitioning function. Function that implement this interface compute the dest...
Definition utils.h:208
std::vector< T > compute_midpoints(const Mesh< T > &mesh, int dim, std::span< const std::int32_t > entities)
Compute the midpoints for mesh entities of a given dimension.
Definition utils.h:569
Mesh< typename std::remove_reference_t< typename U::value_type > > create_mesh(MPI_Comm comm, MPI_Comm commt, std::vector< std::span< const std::int64_t > > cells, const std::vector< fem::CoordinateElement< typename std::remove_reference_t< typename U::value_type > > > &elements, MPI_Comm commg, const U &x, std::array< std::size_t, 2 > xshape, const CellPartitionFunction &partitioner, const CellReorderFunction &reorder_fn=graph::reorder_gps)
Create a distributed mesh::Mesh from mesh data and using the provided graph partitioning function for...
Definition utils.h:1009
GhostMode
Enum for different partitioning ghost modes.
Definition utils.h:40
CellPartitionFunction create_cell_partitioner(mesh::GhostMode ghost_mode=mesh::GhostMode::none, const graph::partition_fn &partfn=&graph::partition_graph)
Create a function that computes destination rank for mesh cells on this rank by applying the default ...
Definition utils.cpp:100
std::vector< std::int32_t > sort_by_perm(std::span< const T > x, std::size_t shape1)
Compute the permutation array that sorts a 2D array by row.
Definition sort.h:183
constexpr __radix_sort radix_sort
Radix sort.
Definition sort.h:170