DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
interval.h
1// Copyright (C) 2024 Paul T. Kühner
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 "dolfinx/mesh/Mesh.h"
10#include "dolfinx/mesh/cell_types.h"
11#include "dolfinx/mesh/utils.h"
12#include "dolfinx/refinement/plaza.h"
13#include <algorithm>
14#include <concepts>
15#include <cstddef>
16#include <cstdint>
17#include <mpi.h>
18#include <optional>
19#include <stdexcept>
20#include <vector>
21
22#include "dolfinx/refinement/option.h"
23#include "dolfinx/refinement/utils.h"
24
25namespace dolfinx::refinement::interval
26{
35template <std::floating_point T>
36std::tuple<graph::AdjacencyList<std::int64_t>, std::vector<T>,
37 std::array<std::size_t, 2>, std::optional<std::vector<std::int32_t>>,
38 std::optional<std::vector<std::int8_t>>>
39compute_refinement_data(const mesh::Mesh<T>& mesh,
40 std::optional<std::span<const std::int32_t>> cells,
41 Option option)
42{
43 bool compute_parent_facet = option_parent_facet(option);
44 bool compute_parent_cell = option_parent_cell(option);
45
46 if (compute_parent_facet)
47 throw std::runtime_error("Parent facet computation not yet supported!");
48
49 auto topology = mesh.topology();
50 assert(topology);
51 assert(topology->dim() == 1);
52 auto map_c = topology->index_map(1);
53 assert(map_c);
54
55 // TODO: creation of sharing ranks in external function? Also same
56 // code in use for plaza
57 // Get sharing ranks for each cell
58 auto [_data, _offsets_g] = map_c->index_to_dest_ranks();
59 graph::AdjacencyList<int> cell_ranks(std::move(_data), std::move(_offsets_g));
60
61 // Create unique list of ranks that share cells (owners of ghosts plus
62 // ranks that ghost owned indices)
63 std::vector<int> ranks = cell_ranks.array();
64 std::ranges::sort(ranks);
65 auto to_remove = std::ranges::unique(ranks);
66 ranks.erase(to_remove.begin(), to_remove.end());
67
68 // Convert cell_ranks from global rank to to neighbourhood ranks
69 std::ranges::transform(cell_ranks.array(), cell_ranks.array().begin(),
70 [&ranks](auto r)
71 {
72 auto it = std::lower_bound(ranks.begin(),
73 ranks.end(), r);
74 assert(it != ranks.end() and *it == r);
75 return std::ranges::distance(ranks.begin(), it);
76 });
77
78 // Create refinement flag for cells
79 std::vector<std::int8_t> refinement_marker(
80 map_c->size_local() + map_c->num_ghosts(), !cells.has_value());
81
82 // Mark cells for refinement
83 std::vector<std::vector<std::int32_t>> marked_for_update(ranks.size());
84 if (cells)
85 {
86 std::ranges::for_each(
87 cells.value(),
88 [&refinement_marker, &cell_ranks, &marked_for_update](auto cell)
89 {
90 if (!refinement_marker[cell])
91 {
92 refinement_marker[cell] = true;
93 for (int rank : cell_ranks.links(cell))
94 marked_for_update[rank].push_back(cell);
95 }
96 });
97 }
98
99 // Create neighborhood communicator for vertex creation
100 MPI_Comm neighbor_comm;
101 MPI_Dist_graph_create_adjacent(
102 mesh.comm(), ranks.size(), ranks.data(), MPI_UNWEIGHTED, ranks.size(),
103 ranks.data(), MPI_UNWEIGHTED, MPI_INFO_NULL, false, &neighbor_comm);
104
105 // Communicate ghost cells that might have been marked. This is not
106 // necessary for a uniform refinement.
107 if (cells)
108 {
109 update_logical_edgefunction(neighbor_comm, marked_for_update,
110 refinement_marker, *map_c);
111 }
112
113 // Construct the new vertices
114 const auto [new_vertex_map, new_vertex_coords, xshape]
115 = create_new_vertices(neighbor_comm, cell_ranks, mesh, refinement_marker);
116 MPI_Comm_free(&neighbor_comm);
117
118 auto c_to_v = mesh.topology()->connectivity(1, 0);
119 assert(c_to_v);
120
121 // Get the count of cells to refine, note: we only consider non-ghost
122 // cells
123 const std::int32_t number_of_refined_cells
124 = std::count(refinement_marker.begin(),
125 std::next(refinement_marker.begin(),
126 mesh.topology()->index_map(1)->size_local()),
127 true);
128
129 // Produce local global indices, by padding out the previous index map
130 std::vector<std::int64_t> global_indices
131 = adjust_indices(*mesh.topology()->index_map(0), number_of_refined_cells);
132
133 // Build the topology on the new vertices
134 std::size_t refined_cell_count
135 = mesh.topology()->index_map(1)->size_local() + number_of_refined_cells;
136
137 std::vector<std::int64_t> cell_topology;
138 cell_topology.reserve(refined_cell_count * 2);
139
140 std::optional<std::vector<std::int32_t>> parent_cell(std::nullopt);
141 if (compute_parent_cell)
142 {
143 parent_cell.emplace();
144 parent_cell->reserve(refined_cell_count);
145 }
146
147 for (std::int32_t cell = 0; cell < map_c->size_local(); ++cell)
148 {
149 std::span vertices = c_to_v->links(cell);
150 assert(vertices.size() == 2);
151
152 // We consider a cell (defined by global vertices)
153 // a ----------- b
154 const std::int64_t a = global_indices[vertices[0]];
155 const std::int64_t b = global_indices[vertices[1]];
156 if (refinement_marker[cell])
157 {
158 // Find (global) index of new midpoint vertex:
159 // a --- c --- b
160 std::span nv = new_vertex_map.links(cell);
161 assert(nv.size() == 1);
162 const std::int64_t c = nv[0];
163
164 // Add new cells/edges to refined topology
165 cell_topology.insert(cell_topology.end(), {a, c, c, b});
166
167 if (compute_parent_cell)
168 parent_cell->insert(parent_cell->end(), {cell, cell});
169 }
170 else
171 {
172 // Copy the previous cell
173 cell_topology.insert(cell_topology.end(), {a, b});
174
175 if (compute_parent_cell)
176 parent_cell->push_back(cell);
177 }
178 }
179
180 assert(cell_topology.size() == 2 * refined_cell_count);
181 assert(!compute_parent_cell or parent_cell->size() == refined_cell_count);
182
183 std::vector<std::int32_t> offsets(refined_cell_count + 1);
184 std::ranges::generate(offsets, [i = 0]() mutable { return 2 * i++; });
185 graph::AdjacencyList cell_adj(std::move(cell_topology), std::move(offsets));
186
187 return {std::move(cell_adj), std::move(new_vertex_coords), xshape,
188 std::move(parent_cell), std::nullopt};
189}
190
191} // namespace dolfinx::refinement::interval
Functions supporting mesh operations.
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
@ cell
Cell.
Definition Form.h:42
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
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