23#ifndef VULPES_DOFS_NUMBERING_HPP_
24#define VULPES_DOFS_NUMBERING_HPP_
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>
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) {
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);
99 std::vector<T> inds(num_rows);
100 std::iota(inds.begin(), inds.end(), 0);
101 std::sort(inds.begin(), inds.end(), [°ree](T i, T j) {
102 return degree[i] < degree[j];
105 std::vector<T> rev_inds(num_rows);
106 for (T i = 0; i < num_rows; ++i) {
107 rev_inds[inds[i]] = i;
110 const T max_degree = *std::max_element(degree.begin(), degree.end());
111 std::vector<T> temp_degrees(max_degree, 0);
115 for (T zz = 0; zz < num_rows; ++zz) {
116 if (inds[zz] !=
static_cast<T
>(-1)) {
119 inds[rev_inds[seed]] =
static_cast<T
>(-1);
121 T level_start = N - 1;
124 while (level_start < level_end) {
125 for (T ii = level_start; ii < level_end; ++ii) {
130 for (T jj = ptr[i]; jj < ptr[i + 1]; ++jj) {
132 if (inds[rev_inds[j]] !=
static_cast<T
>(-1)) {
133 inds[rev_inds[j]] =
static_cast<T
>(-1);
140 for (T kk = N_old; kk < N; ++kk) {
141 temp_degrees[level_len++] = degree[order[kk]];
145 for (T kk = 1; kk < level_len; ++kk) {
146 T temp = temp_degrees[kk];
147 T temp2 = order[N_old + 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];
154 temp_degrees[ll] = temp;
155 order[N_old + ll] = temp2;
159 level_start = level_end;
170 std::reverse(order.begin(), order.end());
174 std::vector<T> perm(order.size());
175 for (std::size_t c = 0; c < order.size(); ++c) {
179 return {perm, order};
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;
196using Pair = std::pair<size_type, size_type>;
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);
211 boost::graph_traits<boosttypes::Graph>::vertex_iterator ui, ui_end;
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);
229 bool reversed_numbering) {
237 std::vector<boosttypes::Vertex> inv_perm(boost::num_vertices(graph));
238 std::vector<T> new_dof_indices(boost::num_vertices(graph));
240 if (reversed_numbering) {
241 boost::king_ordering(graph, inv_perm.begin());
243 boost::king_ordering(graph, inv_perm.rbegin());
247 new_dof_indices[index_map[inv_perm[c]]] = c;
250 return {new_dof_indices, std::vector<T>(inv_perm.begin(), inv_perm.end())};
263 bool reversed_numbering) {
271 std::vector<boosttypes::Vertex> inv_perm(boost::num_vertices(graph));
272 std::vector<T> new_dof_indices(boost::num_vertices(graph));
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));
279 boost::cuthill_mckee_ordering(
280 graph, inv_perm.rbegin(), boost::get(boost::vertex_color, graph),
281 boost::make_degree_map(graph));
285 new_dof_indices[index_map[inv_perm[c]]] = c;
288 return {new_dof_indices, std::vector<T>(inv_perm.begin(), inv_perm.end())};
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