DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
plaza.h
1// Copyright (C) 2014-2018 Chris Richardson
2//
3// This file is part of DOLFINx (https://www.fenicsproject.org)
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#include "dolfinx/graph/AdjacencyList.h"
8#include "dolfinx/mesh/Mesh.h"
9#include "dolfinx/mesh/Topology.h"
10#include "dolfinx/mesh/utils.h"
11#include "option.h"
12#include "utils.h"
13#include <algorithm>
14#include <cmath>
15#include <cstdint>
16#include <optional>
17#include <span>
18#include <tuple>
19#include <utility>
20#include <vector>
21
22#pragma once
23
30{
31namespace impl
32{
36template <int tdim>
37auto compute_parent_facets(std::span<const std::int32_t> simplex_set)
38{
39 static_assert(tdim == 2 or tdim == 3);
40 assert(simplex_set.size() % (tdim + 1) == 0);
41 using parent_facet_t
42 = std::conditional_t<tdim == 2, std::array<std::int8_t, 12>,
43 std::array<std::int8_t, 32>>;
44 parent_facet_t parent_facet;
45 parent_facet.fill(-1);
46 assert(simplex_set.size() <= parent_facet.size());
47
48 // Index lookups in 'indices' for the child vertices that occur on
49 // each parent facet in 2D and 3D. In 2D each edge has 3 child
50 // vertices, and in 3D each triangular facet has six child vertices.
51 constexpr std::array<std::array<int, 3>, 3> facet_table_2d{
52 {{1, 2, 3}, {0, 2, 4}, {0, 1, 5}}};
53
54 constexpr std::array<std::array<int, 6>, 4> facet_table_3d{
55 {{1, 2, 3, 4, 5, 6},
56 {0, 2, 3, 4, 7, 8},
57 {0, 1, 3, 5, 7, 9},
58 {0, 1, 2, 6, 8, 9}}};
59
60 const int ncells = simplex_set.size() / (tdim + 1);
61 for (int fpi = 0; fpi < (tdim + 1); ++fpi)
62 {
63 // For each child cell, consider all facets
64 for (int cc = 0; cc < ncells; ++cc)
65 {
66 for (int fci = 0; fci < (tdim + 1); ++fci)
67 {
68 // Indices of all vertices on child facet, sorted
69 std::array<int, tdim> cf, set_output;
70
71 int num_common_vertices;
72 if constexpr (tdim == 2)
73 {
74 for (int j = 0; j < tdim; ++j)
75 cf[j] = simplex_set[cc * 3 + facet_table_2d[fci][j]];
76
77 std::ranges::sort(cf);
78 auto [last1, last2, it_last] = std::ranges::set_intersection(
79 facet_table_2d[fpi], cf, set_output.begin());
80 num_common_vertices
81 = std::ranges::distance(set_output.begin(), it_last);
82 }
83 else
84 {
85 for (int j = 0; j < tdim; ++j)
86 cf[j] = simplex_set[cc * 4 + facet_table_3d[fci][j]];
87
88 std::ranges::sort(cf);
89 auto [last1, last2, it_last] = std::ranges::set_intersection(
90 facet_table_3d[fpi], cf, set_output.begin());
91 num_common_vertices
92 = std::ranges::distance(set_output.begin(), it_last);
93 }
94
95 if (num_common_vertices == tdim)
96 {
97 assert(parent_facet[cc * (tdim + 1) + fci] == -1);
98 // Child facet "fci" of cell cc, lies on parent facet "fpi"
99 parent_facet[cc * (tdim + 1) + fci] = fpi;
100 }
101 }
102 }
103 }
104
105 return parent_facet;
106}
107
126std::pair<std::array<std::int32_t, 32>, std::size_t>
127get_simplices(std::span<const std::int64_t> indices,
128 std::span<const std::int32_t> longest_edge, int tdim,
129 bool uniform);
130
133void enforce_rules(MPI_Comm comm, const graph::AdjacencyList<int>& shared_edges,
134 std::span<std::int8_t> marked_edges,
135 const mesh::Topology& topology,
136 std::span<const std::int32_t> long_edge);
137
147template <std::floating_point T>
148std::pair<std::vector<std::int32_t>, std::vector<std::int8_t>>
149face_long_edge(const mesh::Mesh<T>& mesh)
150{
151 const int tdim = mesh.topology()->dim();
152 // FIXME: cleanup these calls? Some of the happen internally again.
153 mesh.topology_mutable()->create_entities(1);
154 mesh.topology_mutable()->create_entities(2);
155 mesh.topology_mutable()->create_connectivity(2, 1);
156 mesh.topology_mutable()->create_connectivity(1, tdim);
157 mesh.topology_mutable()->create_connectivity(tdim, 2);
158
159 std::int64_t num_faces = mesh.topology()->index_map(2)->size_local()
160 + mesh.topology()->index_map(2)->num_ghosts();
161
162 // Storage for face-local index of longest edge
163 std::vector<std::int32_t> long_edge(num_faces);
164 std::vector<std::int8_t> edge_ratio_ok;
165
166 // Check mesh face quality (may be used in 2D to switch to "uniform"
167 // refinement)
168 const T min_ratio = std::sqrt(2.0) / 2.0;
169 if (tdim == 2)
170 edge_ratio_ok.resize(num_faces);
171
172 auto x_dofmap = mesh.geometry().dofmaps().front();
173
174 auto c_to_v = mesh.topology()->connectivity(tdim, 0);
175 assert(c_to_v);
176 auto e_to_c = mesh.topology()->connectivity(1, tdim);
177 assert(e_to_c);
178 auto e_to_v = mesh.topology()->connectivity(1, 0);
179 assert(e_to_v);
180
181 // Store all edge lengths in Mesh to save recalculating for each Face
182 auto map_e = mesh.topology()->index_map(1);
183 assert(map_e);
184 std::vector<T> edge_length(map_e->size_local() + map_e->num_ghosts());
185 for (std::size_t e = 0; e < edge_length.size(); ++e)
186 {
187 // Get first attached cell
188 auto cells = e_to_c->links(e);
189 assert(!cells.empty());
190 auto cell_vertices = c_to_v->links(cells.front());
191 auto edge_vertices = e_to_v->links(e);
192
193 // Find local index of edge vertices in the cell geometry map
194 auto it0 = std::find(cell_vertices.begin(), cell_vertices.end(),
195 edge_vertices[0]);
196 assert(it0 != cell_vertices.end());
197 const std::size_t local0
198 = std::ranges::distance(cell_vertices.begin(), it0);
199 auto it1 = std::find(cell_vertices.begin(), cell_vertices.end(),
200 edge_vertices[1]);
201 assert(it1 != cell_vertices.end());
202 const std::size_t local1
203 = std::ranges::distance(cell_vertices.begin(), it1);
204
205 auto x_dofs = md::submdspan(x_dofmap, cells.front(), md::full_extent);
206 std::span<const T, 3> x0(mesh.geometry().x().data() + 3 * x_dofs[local0],
207 3);
208 std::span<const T, 3> x1(mesh.geometry().x().data() + 3 * x_dofs[local1],
209 3);
210
211 // Compute length of edge between vertex x0 and x1
212 edge_length[e] = std::sqrt(std::transform_reduce(
213 x0.begin(), x0.end(), x1.begin(), 0.0, std::plus<>(),
214 [](auto x0, auto x1) { return (x0 - x1) * (x0 - x1); }));
215 }
216
217 // Get longest edge of each face
218 auto f_to_v = mesh.topology()->connectivity(2, 0);
219 assert(f_to_v);
220 auto f_to_e = mesh.topology()->connectivity(2, 1);
221 assert(f_to_e);
222 const std::vector global_indices
223 = mesh.topology()->index_map(0)->global_indices();
224 for (int f = 0; f < f_to_v->num_nodes(); ++f)
225 {
226 auto face_edges = f_to_e->links(f);
227
228 std::int32_t imax = 0;
229 T max_len = 0.0;
230 T min_len = std::numeric_limits<T>::max();
231
232 for (int i = 0; i < 3; ++i)
233 {
234 const T e_len = edge_length[face_edges[i]];
235 min_len = std::min(e_len, min_len);
236 if (e_len > max_len)
237 {
238 max_len = e_len;
239 imax = i;
240 }
241 else if (tdim == 3 and e_len == max_len)
242 {
243 // If edges are the same length, compare global index of
244 // opposite vertex. Only important so that tetrahedral faces
245 // have a matching refinement pattern across processes.
246 auto vertices = f_to_v->links(f);
247 const int vmax = vertices[imax];
248 const int vi = vertices[i];
249 if (global_indices[vi] > global_indices[vmax])
250 imax = i;
251 }
252 }
253
254 // Only save edge ratio in 2D
255 if (tdim == 2)
256 edge_ratio_ok[f] = (min_len / max_len >= min_ratio);
257
258 long_edge[f] = face_edges[imax];
259 }
260
261 return std::pair(std::move(long_edge), std::move(edge_ratio_ok));
262}
263
288template <std::floating_point T>
289std::tuple<graph::AdjacencyList<std::int64_t>, std::vector<T>,
290 std::array<std::size_t, 2>, std::optional<std::vector<std::int32_t>>,
291 std::optional<std::vector<std::int8_t>>>
292compute_refinement(MPI_Comm neighbor_comm,
293 std::span<const std::int8_t> marked_edges,
294 const graph::AdjacencyList<int>& shared_edges,
295 const mesh::Mesh<T>& mesh,
296 std::span<const std::int32_t> long_edge,
297 std::span<const std::int8_t> edge_ratio_ok, Option option)
298{
299 int tdim = mesh.topology()->dim();
300 int num_cell_edges = tdim * 3 - 3;
301 int num_cell_vertices = tdim + 1;
302
303 bool compute_facets = option_parent_facet(option);
304 bool compute_parent_cell = option_parent_cell(option);
305
306 // Make new vertices in parallel
307 const auto [new_vertex_map, new_vertex_coords, xshape]
308 = create_new_vertices(neighbor_comm, shared_edges, mesh, marked_edges);
309
310 std::optional<std::vector<std::int32_t>> parent_cell(std::nullopt);
311 if (compute_parent_cell)
312 parent_cell.emplace();
313
314 std::optional<std::vector<std::int8_t>> parent_facet(std::nullopt);
315 if (compute_facets)
316 parent_facet.emplace();
317
318 std::vector<std::int64_t> indices(num_cell_vertices + num_cell_edges);
319 std::vector<std::int32_t> simplex_set;
320
321 auto map_c = mesh.topology()->index_map(tdim);
322 assert(map_c);
323 auto c_to_v = mesh.topology()->connectivity(tdim, 0);
324 assert(c_to_v);
325 auto c_to_e = mesh.topology()->connectivity(tdim, 1);
326 assert(c_to_e);
327 auto c_to_f = mesh.topology()->connectivity(tdim, 2);
328 assert(c_to_f);
329
330 std::int32_t num_new_vertices_local = std::count(
331 marked_edges.begin(),
332 marked_edges.begin() + mesh.topology()->index_map(1)->size_local(), true);
333
334 std::vector<std::int64_t> global_indices
335 = adjust_indices(*mesh.topology()->index_map(0), num_new_vertices_local);
336
337 const std::int32_t num_cells = map_c->size_local();
338
339 // Iterate over all cells, and refine if cell has a marked edge
340 std::vector<std::int64_t> cell_topology;
341 for (int c = 0; c < num_cells; ++c)
342 {
343 // Create vector of indices in the order [vertices][edges], 3+3 in
344 // 2D, 4+6 in 3D
345
346 // Copy vertices
347 auto vertices = c_to_v->links(c);
348 for (std::size_t v = 0; v < vertices.size(); ++v)
349 indices[v] = global_indices[vertices[v]];
350
351 // Get cell-local indices of marked edges
352 auto edges = c_to_e->links(c);
353 bool no_edge_marked = true;
354 for (std::size_t ei = 0; ei < edges.size(); ++ei)
355 {
356 if (marked_edges[edges[ei]])
357 {
358 no_edge_marked = false;
359 auto nv = new_vertex_map.links(edges[ei]);
360 assert(nv.size() == 1);
361 indices[num_cell_vertices + ei] = nv[0];
362 }
363 else
364 indices[num_cell_vertices + ei] = -1;
365 }
366
367 if (no_edge_marked)
368 {
369 // Copy over existing cell to new topology
370 for (auto v : vertices)
371 cell_topology.push_back(global_indices[v]);
372
373 if (compute_parent_cell)
374 parent_cell->push_back(c);
375
376 if (compute_facets)
377 {
378 if (tdim == 3)
379 parent_facet->insert(parent_facet->end(), {0, 1, 2, 3});
380 else
381 parent_facet->insert(parent_facet->end(), {0, 1, 2});
382 }
383 }
384 else
385 {
386 // Need longest edges of each face in cell local indexing. NB in
387 // 2D the face is the cell itself, and there is just one entry.
388 std::vector<std::int32_t> longest_edge;
389 for (auto f : c_to_f->links(c))
390 longest_edge.push_back(long_edge[f]);
391
392 // Convert to cell local index
393 for (std::int32_t& p : longest_edge)
394 {
395 for (std::size_t ej = 0; ej < edges.size(); ++ej)
396 {
397 if (p == edges[ej])
398 {
399 p = ej;
400 break;
401 }
402 }
403 }
404
405 const bool uniform = (tdim == 2) ? edge_ratio_ok[c] : false;
406 const auto [simplex_set_b, simplex_set_size]
407 = get_simplices(indices, longest_edge, tdim, uniform);
408 std::span<const std::int32_t> simplex_set(simplex_set_b.data(),
409 simplex_set_size);
410
411 // Save parent index
412 const std::int32_t ncells = simplex_set.size() / num_cell_vertices;
413 if (compute_parent_cell)
414 {
415 for (std::int32_t i = 0; i < ncells; ++i)
416 parent_cell->push_back(c);
417 }
418
419 if (compute_facets)
420 {
421 if (tdim == 3)
422 {
423 auto npf = compute_parent_facets<3>(simplex_set);
424 parent_facet->insert(parent_facet->end(), npf.begin(),
425 std::next(npf.begin(), simplex_set.size()));
426 }
427 else
428 {
429 auto npf = compute_parent_facets<2>(simplex_set);
430 parent_facet->insert(parent_facet->end(), npf.begin(),
431 std::next(npf.begin(), simplex_set.size()));
432 }
433 }
434
435 // Convert from cell local index to mesh index and add to cells
436 for (std::int32_t v : simplex_set)
437 cell_topology.push_back(indices[v]);
438 }
439 }
440
441 assert(cell_topology.size() % num_cell_vertices == 0);
442 std::vector<std::int32_t> offsets(
443 cell_topology.size() / num_cell_vertices + 1, 0);
444 for (std::size_t i = 0; i < offsets.size() - 1; ++i)
445 offsets[i + 1] = offsets[i] + num_cell_vertices;
446 graph::AdjacencyList cell_adj(std::move(cell_topology), std::move(offsets));
447
448 return {std::move(cell_adj), std::move(new_vertex_coords), xshape,
449 std::move(parent_cell), std::move(parent_facet)};
450}
451} // namespace impl
452
462template <std::floating_point T>
463std::tuple<graph::AdjacencyList<std::int64_t>, std::vector<T>,
464 std::array<std::size_t, 2>, std::optional<std::vector<std::int32_t>>,
465 std::optional<std::vector<std::int8_t>>>
467 std::optional<std::span<const std::int32_t>> edges,
468 Option option)
469{
470 common::Timer t0("PLAZA: refine");
471 auto topology = mesh.topology();
472 assert(topology);
473
474 if (topology->cell_type() != mesh::CellType::triangle
475 and topology->cell_type() != mesh::CellType::tetrahedron)
476 {
477 throw std::runtime_error("Cell type not supported");
478 }
479
480 auto map_e = topology->index_map(1);
481 if (!map_e)
482 throw std::runtime_error("Edges must be initialised");
483
484 // Get sharing ranks for each edge
485 auto [_data, _offsets] = map_e->index_to_dest_ranks();
486 graph::AdjacencyList<int> edge_ranks(std::move(_data), std::move(_offsets));
487
488 // Create unique list of ranks that share edges (owners of ghosts plus
489 // ranks that ghost owned indices)
490 std::vector<int> ranks(edge_ranks.array().begin(), edge_ranks.array().end());
491 std::ranges::sort(ranks);
492 auto [unique_end, range_end] = std::ranges::unique(ranks);
493 ranks.erase(unique_end, range_end);
494
495 // Convert edge_ranks from global rank to to neighbourhood ranks
496 std::ranges::transform(edge_ranks.array(), edge_ranks.array().begin(),
497 [&ranks](auto r)
498 {
499 auto it = std::ranges::lower_bound(ranks, r);
500 assert(it != ranks.end() and *it == r);
501 return std::ranges::distance(ranks.begin(), it);
502 });
503
504 // Get number of neighbors
505 std::vector<std::int8_t> marked_edges(
506 map_e->size_local() + map_e->num_ghosts(), !edges.has_value());
507 std::vector<std::vector<std::int32_t>> marked_for_update(ranks.size());
508
509 if (edges)
510 {
511 for (auto edge : edges.value())
512 {
513 if (!marked_edges[edge])
514 {
515 marked_edges[edge] = true;
516
517 // If it is a shared edge, add all sharing neighbors to update set
518 for (int rank : edge_ranks.links(edge))
519 marked_for_update[rank].push_back(edge);
520 }
521 }
522 }
523
524 MPI_Comm comm;
525 MPI_Dist_graph_create_adjacent(mesh.comm(), ranks.size(), ranks.data(),
526 MPI_UNWEIGHTED, ranks.size(), ranks.data(),
527 MPI_UNWEIGHTED, MPI_INFO_NULL, false, &comm);
528
529 // Communicate any shared edges
530 update_logical_edgefunction(comm, marked_for_update, marked_edges, *map_e);
531
532 // Enforce rules about refinement (i.e. if any edge is marked in a
533 // triangle, then the longest edge must also be marked).
534 const auto [long_edge, edge_ratio_ok] = impl::face_long_edge(mesh);
535 impl::enforce_rules(comm, edge_ranks, marked_edges, *topology, long_edge);
536
537 auto [cell_adj, new_vertex_coords, xshape, parent_cell, parent_facet]
538 = impl::compute_refinement(comm, marked_edges, edge_ranks, mesh,
539 long_edge, edge_ratio_ok, option);
540 MPI_Comm_free(&comm);
541
542 return {std::move(cell_adj), std::move(new_vertex_coords), xshape,
543 std::move(parent_cell), std::move(parent_facet)};
544}
545
546} // namespace dolfinx::refinement::plaza
Timer for measuring and logging elapsed time durations.
Definition Timer.h:40
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
std::span< LinkData > links(std::size_t node)
Get the links (edges) for given node.
Definition AdjacencyList.h:169
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
Topology stores the topology of a mesh, consisting of mesh entities and connectivity (incidence relat...
Definition Topology.h:49
Functions supporting mesh operations.
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
Plaza mesh refinement.
Definition plaza.h:30
std::tuple< graph::AdjacencyList< std::int64_t >, std::vector< T >, std::array< std::size_t, 2 >, std::optional< std::vector< std::int32_t > >, std::optional< std::vector< std::int8_t > > > compute_refinement_data(const mesh::Mesh< T > &mesh, std::optional< std::span< const std::int32_t > > edges, Option option)
Definition plaza.h:466
std::tuple< graph::AdjacencyList< std::int64_t >, std::vector< T >, std::array< std::size_t, 2 > > create_new_vertices(MPI_Comm comm, const graph::AdjacencyList< int > &shared_edges, const mesh::Mesh< T > &mesh, std::span< const std::int8_t > marked_edges)
Add new vertex for each marked edge, and create new_vertex_coordinates and global_edge->new_vertex ma...
Definition utils.h:148
constexpr bool option_parent_cell(Option a)
Check if parent_cell flag is set.
Definition option.h:45
Option
Options for data to compute during mesh refinement.
Definition option.h:16
@ parent_cell
Definition option.h:20
@ parent_facet
Definition option.h:18
constexpr bool option_parent_facet(Option a)
Check if parent_facet flag is set.
Definition option.h:36
std::vector< std::int64_t > adjust_indices(const common::IndexMap &map, std::int32_t n)
Given an index map, add "n" extra indices at the end of local range.
Definition utils.cpp:103
void update_logical_edgefunction(MPI_Comm comm, const std::vector< std::vector< std::int32_t > > &marked_for_update, std::span< std::int8_t > marked_edges, const common::IndexMap &map)
Communicate edge markers between processes that share edges.
Definition utils.cpp:47