numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
create_prolongation_matrix.h
Go to the documentation of this file.
1/*
2 * Copyright 2024 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <vector>
23
24#include <Eigen/SparseCore>
25
31
33
42template <base::concepts::sparse_matrix Matrix>
43void create_prolongation_matrix(Matrix& prolongation_matrix,
45 transposed_connections,
46 const util::vector<node_layer>& node_classification) {
47 using storage_index_type = typename Matrix::StorageIndex;
48 using scalar_type = typename Matrix::Scalar;
49
50 const auto num_nodes =
51 static_cast<storage_index_type>(node_classification.size());
52
53 util::vector<storage_index_type> coarse_grid_indices;
54 storage_index_type num_coarse_grid_nodes = 0;
55 coarse_grid_indices.reserve(num_nodes);
56 for (const node_layer node_class : node_classification) {
57 if (node_class == node_layer::coarse) {
58 coarse_grid_indices.push_back(num_coarse_grid_nodes);
59 ++num_coarse_grid_nodes;
60 } else {
61 coarse_grid_indices.push_back(num_nodes);
62 }
63 }
64
65 std::vector<Eigen::Triplet<scalar_type, storage_index_type>> triplets;
66 for (storage_index_type row_index = 0; row_index < num_nodes; ++row_index) {
67 if (node_classification[row_index] == node_layer::coarse) {
68 const storage_index_type col_index = coarse_grid_indices[row_index];
69 const auto value = static_cast<scalar_type>(1);
70 triplets.emplace_back(row_index, col_index, value);
71 } else {
72 storage_index_type num_connected_nodes = 0;
73 for (const storage_index_type connected_node_index :
74 transposed_connections.connected_nodes_to(row_index)) {
75 if (node_classification[connected_node_index] ==
77 ++num_connected_nodes;
78 }
79 }
80
81 NUM_COLLECT_ASSERT(num_connected_nodes != 0);
82
83 const scalar_type value = static_cast<scalar_type>(1) /
84 static_cast<scalar_type>(num_connected_nodes);
85 for (const storage_index_type connected_node_index :
86 transposed_connections.connected_nodes_to(row_index)) {
87 if (node_classification[connected_node_index] ==
89 const storage_index_type col_index =
90 coarse_grid_indices[connected_node_index];
91 triplets.emplace_back(row_index, col_index, value);
92 }
93 }
94 }
95 }
96
97 prolongation_matrix.resize(num_nodes, num_coarse_grid_nodes);
98 prolongation_matrix.setFromTriplets(triplets.begin(), triplets.end());
99}
100
101} // namespace num_collect::linear::impl::amg
Definition of assertion macros.
#define NUM_COLLECT_ASSERT(CONDITION)
Macro to check whether a condition is satisfied.
Definition assert.h:66
Class of lists of connected nodes per node.
auto connected_nodes_to(index_type node_index) const -> std::span< const storage_index_type >
Get the list of indices of connected nodes to the node with the given index.
Class of vectors wrapping std::vector class to use singed integers as indices.
Definition vector.h:37
auto size() const -> index_type
Get the size of this vector.
Definition vector.h:226
void reserve(index_type size)
Reserve memory.
Definition vector.h:235
void push_back(const value_type &value)
Add an element.
Definition vector.h:258
Namespace of internal implementations of algebraic multigrid method ruge1987.
void create_prolongation_matrix(Matrix &prolongation_matrix, const node_connection_list< typename Matrix::StorageIndex > &transposed_connections, const util::vector< node_layer > &node_classification)
Create a prolongation matrix.
node_layer
Enumeration of types of grids which nodes belong to in AMG method.
Definition node_layer.h:29
Definition of node_connection_list class.
Definition of node_class enumeration.
Definition of sparse_matrix concept.
Definition of vector class.