49 inline std::vector< std::map<int, double> > reorder_matrix(std::vector< std::map<int, double> >
const & matrix, std::vector<int>
const & r)
51 std::vector< std::map<int, double> > matrix2(r.size());
53 for (std::size_t i = 0; i < r.size(); i++)
54 for (std::map<int, double>::const_iterator it = matrix[i].begin(); it != matrix[i].end(); it++)
55 matrix2[static_cast<std::size_t>(r[i])][r[
static_cast<std::size_t
>(it->first)]] = it->second;
64 inline int calc_bw(std::vector< std::map<int, double> >
const & matrix)
68 for (std::size_t i = 0; i < matrix.size(); i++)
70 int min_index =
static_cast<int>(matrix.size());
72 for (std::map<int, double>::const_iterator it = matrix[i].begin(); it != matrix[i].end(); it++)
74 if (it->first > max_index)
75 max_index = it->first;
76 if (it->first < min_index)
77 min_index = it->first;
80 if (max_index > min_index)
81 bw =
std::max(bw, max_index - min_index);
91 template<
typename IndexT>
92 int calc_reordered_bw(std::vector< std::map<int, double> >
const & matrix, std::vector<IndexT>
const & r)
96 for (std::size_t i = 0; i < r.size(); i++)
98 int min_index =
static_cast<int>(matrix.size());
100 for (std::map<int, double>::const_iterator it = matrix[i].begin(); it != matrix[i].end(); it++)
102 std::size_t col_idx =
static_cast<std::size_t
>(it->first);
103 if (r[col_idx] > max_index)
104 max_index = r[col_idx];
105 if (r[col_idx] < min_index)
106 min_index = r[col_idx];
108 if (max_index > min_index)
109 bw =
std::max(bw, max_index - min_index);
121 inline std::vector<int> generate_random_reordering(std::size_t n)
123 std::vector<int> r(n);
126 for (std::size_t i = 0; i < n; i++)
127 r[i] = static_cast<int>(i);
129 for (std::size_t i = 0; i < n - 1; i++)
131 std::size_t j = i +
static_cast<std::size_t
>((
static_cast<double>(rand()) / static_cast<double>(RAND_MAX)) * static_cast<double>(n - 1 - i));
152 inline std::vector< std::map<int, double> > gen_3d_mesh_matrix(std::size_t l, std::size_t m, std::size_t n,
bool tri)
154 std::vector< std::map<int, double> > matrix;
155 std::size_t s = l * m * n;
161 for (std::size_t i = 0; i < l; i++)
163 for (std::size_t j = 0; j < m; j++)
165 for (std::size_t k = 0; k < n; k++)
167 ind = i + l * j + l * m * k;
169 matrix[ind][
static_cast<int>(ind)] = 1.0;
174 matrix[ind][
static_cast<int>(ind2)] = 1.0;
175 matrix[ind2][
static_cast<int>(ind)] = 1.0;
180 matrix[ind][
static_cast<int>(ind2)] = 1.0;
181 matrix[ind2][
static_cast<int>(ind)] = 1.0;
186 matrix[ind][
static_cast<int>(ind2)] = 1.0;
187 matrix[ind2][
static_cast<int>(ind)] = 1.0;
192 if (i < l - 1 && j < m - 1)
194 if ((i + j + k) % 2 == 0)
204 matrix[ind1][
static_cast<int>(ind2)] = 1.0;
205 matrix[ind2][
static_cast<int>(ind1)] = 1.0;
207 if (i < l - 1 && k < n - 1)
209 if ((i + j + k) % 2 == 0)
212 ind2 = ind + 1 + l * m;
219 matrix[ind1][
static_cast<int>(ind2)] = 1.0;
220 matrix[ind2][
static_cast<int>(ind1)] = 1.0;
222 if (j < m - 1 && k < n - 1)
224 if ((i + j + k) % 2 == 0)
227 ind2 = ind + l + l * m;
234 matrix[ind1][
static_cast<int>(ind2)] = 1.0;
235 matrix[ind2][
static_cast<int>(ind1)] = 1.0;
252 int main(
int,
char **)
255 std::cout <<
"-- Generating matrix --" << std::endl;
256 std::size_t dof_per_dim = 64;
257 std::size_t n = dof_per_dim * dof_per_dim * dof_per_dim;
258 std::vector< std::map<int, double> > matrix = gen_3d_mesh_matrix(dof_per_dim, dof_per_dim, dof_per_dim,
false);
263 std::vector<int> r = generate_random_reordering(n);
264 std::vector< std::map<int, double> > matrix2 = reorder_matrix(matrix, r);
270 std::cout <<
" * Unknowns: " << n << std::endl;
271 std::cout <<
" * Initial bandwidth: " << calc_bw(matrix) << std::endl;
272 std::cout <<
" * Randomly reordered bandwidth: " << calc_bw(matrix2) << std::endl;
277 std::cout <<
"-- Cuthill-McKee algorithm --" << std::endl;
280 std::cout <<
" * Reordered bandwidth: " <<
calc_reordered_bw(matrix2, r) << std::endl;
285 std::cout <<
"-- Advanced Cuthill-McKee algorithm --" << std::endl;
287 std::size_t gmax = 1;
289 std::cout <<
" * Reordered bandwidth: " <<
calc_reordered_bw(matrix2, r) << std::endl;
294 std::cout <<
"-- Gibbs-Poole-Stockmeyer algorithm --" << std::endl;
296 std::cout <<
" * Reordered bandwidth: " <<
calc_reordered_bw(matrix2, r) << std::endl;
301 std::cout <<
"!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
std::vector< IndexT > reorder(std::vector< std::map< IndexT, ValueT > > const &matrix, cuthill_mckee_tag)
Function for the calculation of a node number permutation to reduce the bandwidth of an incidence mat...
Convenience include for bandwidth reduction algorithms such as Cuthill-McKee or Gibbs-Poole-Stockmeye...
T max(const T &lhs, const T &rhs)
Maximum.
Tag class for identifying the Gibbs-Poole-Stockmeyer algorithm for reducing the bandwidth of a sparse...
IndexT calc_reordered_bw(std::vector< std::map< IndexT, ValueT > > const &matrix, std::vector< bool > &dof_assigned_to_node, std::vector< IndexT > const &permutation)
Tag for the advanced Cuthill-McKee algorithm (i.e. running the 'standard' Cuthill-McKee algorithm for...
A tag class for selecting the Cuthill-McKee algorithm for reducing the bandwidth of a sparse matrix...