Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
numbering.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2025 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22
23#ifndef VULPES_DOFS_NUMBERING_HPP_
24#define VULPES_DOFS_NUMBERING_HPP_
25
26#include <algorithm>
27#include <cassert>
28#include <numeric>
29#include <utility>
30#include <vector>
31
32#include <boost/graph/adjacency_list.hpp>
33#include <boost/graph/cuthill_mckee_ordering.hpp>
34#include <boost/graph/graph_utility.hpp>
35#include <boost/graph/king_ordering.hpp>
36
38
39namespace vulpes {
40namespace dofs {
41
45template <typename T>
46using Permutation = std::pair<std::vector<T>, std::vector<T>>;
47
51template <typename T>
52using RenumberingFunctor = std::function<Permutation<T>(
53 const datastructure::CrsMap<T, T>& sparsity_pattern, bool reversed)>;
54
55namespace internal {
60template <typename T>
61std::vector<T> nodeDegrees(
62 const std::vector<T>& ind, const std::vector<T>& ptr, T num_rows) {
63 std::vector<T> degree(num_rows, 0);
64 for (T ii = 0; ii < num_rows; ++ii) {
65 degree[ii] = ptr[ii + 1] - ptr[ii];
66 for (T jj = ptr[ii]; jj < ptr[ii + 1]; ++jj) {
67 if (ind[jj] == ii) {
68 // Add one if the diagonal is in row ii
69 degree[ii] += 1;
70 break;
71 }
72 }
73 }
74 return degree;
75}
76} // namespace internal
77
88template <typename T>
90 const datastructure::CrsMap<T, T>& sparsity_pattern, bool reversed) {
91 const std::vector<T>& ind = sparsity_pattern.data();
92 const std::vector<T>& ptr = sparsity_pattern.indices();
93 assert(ptr.size() > 0);
94 const T num_rows = ptr.size() - 1;
95 std::vector<T> order(num_rows, 0);
96 std::vector<T> degree = internal::nodeDegrees(ind, ptr, num_rows);
97
98 // std::sort indices based on degrees
99 std::vector<T> inds(num_rows);
100 std::iota(inds.begin(), inds.end(), 0);
101 std::sort(inds.begin(), inds.end(), [&degree](T i, T j) {
102 return degree[i] < degree[j];
103 });
104
105 std::vector<T> rev_inds(num_rows);
106 for (T i = 0; i < num_rows; ++i) {
107 rev_inds[inds[i]] = i;
108 }
109
110 const T max_degree = *std::max_element(degree.begin(), degree.end());
111 std::vector<T> temp_degrees(max_degree, 0);
112 T N = 0;
113
114 // Loop over possible disconnected components
115 for (T zz = 0; zz < num_rows; ++zz) {
116 if (inds[zz] != static_cast<T>(-1)) {
117 T seed = inds[zz];
118 order[N++] = seed;
119 inds[rev_inds[seed]] = static_cast<T>(-1);
120
121 T level_start = N - 1;
122 T level_end = N;
123
124 while (level_start < level_end) {
125 for (T ii = level_start; ii < level_end; ++ii) {
126 T i = order[ii];
127 T N_old = N;
128
129 // Add unvisited neighbors
130 for (T jj = ptr[i]; jj < ptr[i + 1]; ++jj) {
131 T j = ind[jj];
132 if (inds[rev_inds[j]] != static_cast<T>(-1)) {
133 inds[rev_inds[j]] = static_cast<T>(-1);
134 order[N++] = j;
135 }
136 }
137
138 // Prepare for insertion sort
139 T level_len = 0;
140 for (T kk = N_old; kk < N; ++kk) {
141 temp_degrees[level_len++] = degree[order[kk]];
142 }
143
144 // Perform insertion sort
145 for (T kk = 1; kk < level_len; ++kk) {
146 T temp = temp_degrees[kk];
147 T temp2 = order[N_old + kk];
148 T ll = kk;
149 while (ll > 0 && temp < temp_degrees[ll - 1]) {
150 temp_degrees[ll] = temp_degrees[ll - 1];
151 order[N_old + ll] = order[N_old + ll - 1];
152 --ll;
153 }
154 temp_degrees[ll] = temp;
155 order[N_old + ll] = temp2;
156 }
157 }
158
159 level_start = level_end;
160 level_end = N;
161 }
162 }
163 if (N == num_rows) {
164 break;
165 }
166 }
167
168 // Reverse the order to get the RCM ordering
169 if (!reversed) {
170 std::reverse(order.begin(), order.end());
171 }
172
173 // compute the direct permutation
174 std::vector<T> perm(order.size());
175 for (std::size_t c = 0; c < order.size(); ++c) {
176 perm[order[c]] = c;
177 }
178
179 return {perm, order};
180}
181
182namespace boosttypes {
183using Graph = boost::adjacency_list<
184 boost::vecS,
185 boost::vecS,
186 boost::undirectedS,
187 boost::property<
188 boost::vertex_color_t,
189 boost::default_color_type,
190 boost::property<boost::vertex_degree_t, int>>>;
191using GraphDegree = boost::property_map<Graph, boost::vertex_degree_t>::type;
192using GraphIndex = boost::property_map<Graph, boost::vertex_index_t>::type;
193using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
194using size_type = boost::graph_traits<Graph>::vertices_size_type;
195
196using Pair = std::pair<size_type, size_type>;
197} // namespace boosttypes
198
199namespace internal {
200template <typename T>
202 const datastructure::CrsMap<T, T>& sparsity_pattern,
203 boosttypes::Graph& graph,
204 boosttypes::GraphDegree& graph_degree) {
205 for (T row = 0; row < sparsity_pattern.size(); ++row) {
206 for (const auto col : sparsity_pattern[row]) {
207 boost::add_edge(row, col, graph);
208 }
209 }
210
211 boost::graph_traits<boosttypes::Graph>::vertex_iterator ui, ui_end;
212
213 graph_degree = get(boost::vertex_degree, graph);
214 for (boost::tie(ui, ui_end) = vertices(graph); ui != ui_end; ++ui) {
215 graph_degree[*ui] = degree(*ui, graph);
216 }
217}
218} // namespace internal
219
226template <typename T>
228 const datastructure::CrsMap<T, T>& sparsity_pattern,
229 bool reversed_numbering) {
230 boosttypes::Graph graph(sparsity_pattern.size());
231 boosttypes::GraphDegree graph_degree;
232
233 internal::createGraph(sparsity_pattern, graph, graph_degree);
234
235 boosttypes::GraphIndex index_map = boost::get(boost::vertex_index, graph);
236
237 std::vector<boosttypes::Vertex> inv_perm(boost::num_vertices(graph));
238 std::vector<T> new_dof_indices(boost::num_vertices(graph));
239
240 if (reversed_numbering) {
241 boost::king_ordering(graph, inv_perm.begin());
242 } else {
243 boost::king_ordering(graph, inv_perm.rbegin());
244 }
245
246 for (boosttypes::size_type c = 0; c != inv_perm.size(); ++c) {
247 new_dof_indices[index_map[inv_perm[c]]] = c;
248 }
249
250 return {new_dof_indices, std::vector<T>(inv_perm.begin(), inv_perm.end())};
251}
252
260template <typename T>
262 const datastructure::CrsMap<T, T>& sparsity_pattern,
263 bool reversed_numbering) {
264 boosttypes::Graph graph(sparsity_pattern.size());
265 boosttypes::GraphDegree graph_degree;
266
267 internal::createGraph(sparsity_pattern, graph, graph_degree);
268
269 boosttypes::GraphIndex index_map = boost::get(boost::vertex_index, graph);
270
271 std::vector<boosttypes::Vertex> inv_perm(boost::num_vertices(graph));
272 std::vector<T> new_dof_indices(boost::num_vertices(graph));
273
274 if (reversed_numbering) {
275 boost::cuthill_mckee_ordering(
276 graph, inv_perm.begin(), boost::get(boost::vertex_color, graph),
277 boost::make_degree_map(graph));
278 } else {
279 boost::cuthill_mckee_ordering(
280 graph, inv_perm.rbegin(), boost::get(boost::vertex_color, graph),
281 boost::make_degree_map(graph));
282 }
283
284 for (boosttypes::size_type c = 0; c != inv_perm.size(); ++c) {
285 new_dof_indices[index_map[inv_perm[c]]] = c;
286 }
287
288 return {new_dof_indices, std::vector<T>(inv_perm.begin(), inv_perm.end())};
289}
290
291} // namespace dofs
292} // namespace vulpes
293
294#endif // VULPES_DOFS_NUMBERING_HPP_
Definition crs_map.hpp:72
IndexType size() const
Get the number of rows in the CRS map.
Definition crs_map.hpp:331
const auto & indices() const
Constant accessor to the indices buffer.
Definition crs_map.hpp:442
const auto & data() const
Constant accessor to the data buffer.
Definition crs_map.hpp:438
Definition numbering.hpp:182
boost::graph_traits< Graph >::vertices_size_type size_type
Definition numbering.hpp:194
std::pair< size_type, size_type > Pair
Definition numbering.hpp:196
boost::property_map< Graph, boost::vertex_degree_t >::type GraphDegree
Definition numbering.hpp:191
boost::graph_traits< Graph >::vertex_descriptor Vertex
Definition numbering.hpp:193
boost::property_map< Graph, boost::vertex_index_t >::type GraphIndex
Definition numbering.hpp:192
boost::adjacency_list< boost::vecS, boost::vecS, boost::undirectedS, boost::property< boost::vertex_color_t, boost::default_color_type, boost::property< boost::vertex_degree_t, int > > > Graph
Definition numbering.hpp:183
Definition numbering.hpp:55
std::vector< T > nodeDegrees(const std::vector< T > &ind, const std::vector< T > &ptr, T num_rows)
Find the degree of each node (matrix row) in a graph represented by a sparse CSR or CSC matrix.
Definition numbering.hpp:61
void createGraph(const datastructure::CrsMap< T, T > &sparsity_pattern, boosttypes::Graph &graph, boosttypes::GraphDegree &graph_degree)
Definition numbering.hpp:201
Definition dof_handler.hpp:50
std::pair< std::vector< T >, std::vector< T > > Permutation
A permutation is an index of permutations and their inverse.
Definition numbering.hpp:46
Permutation< T > computeBoostCuthillMckeeOrdering(const datastructure::CrsMap< T, T > &sparsity_pattern, bool reversed_numbering)
Returns the permutation array that orders a sparse CSR matrix in Reverse-Cuthill McKee ordering as im...
Definition numbering.hpp:261
std::function< Permutation< T >( const datastructure::CrsMap< T, T > &sparsity_pattern, bool reversed)> RenumberingFunctor
Type of a function that renumbers a CrsMap.
Definition numbering.hpp:52
Permutation< T > computeReverseCuthillMckeeOrdering(const datastructure::CrsMap< T, T > &sparsity_pattern, bool reversed)
Returns the permutation array that orders a sparse CSR matrix in Reverse-Cuthill McKee ordering.
Definition numbering.hpp:89
Permutation< T > computeBoostKingOrdering(const datastructure::CrsMap< T, T > &sparsity_pattern, bool reversed_numbering)
Returns the permutation array that orders a sparse CSR matrix in King ordering as implemented by boos...
Definition numbering.hpp:227
Definition cell_manager.hpp:38