numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
build_first_coarse_grid_candidate.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 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 <limits>
23#include <optional>
24
29
31
39template <typename StorageIndex>
40[[nodiscard]] inline auto compute_node_scores(
41 const node_connection_list<StorageIndex>& transposed_connections)
43 util::vector<StorageIndex> scores(transposed_connections.num_nodes());
44 for (StorageIndex i = 0; i < transposed_connections.num_nodes(); ++i) {
45 scores[i] = static_cast<StorageIndex>(
46 transposed_connections.connected_nodes_to(i).size());
47 }
48 return scores;
49}
50
59template <typename StorageIndex>
60[[nodiscard]] auto find_max_score_index(
61 const util::vector<StorageIndex>& scores,
62 const util::vector<node_layer>& classification)
63 -> std::optional<index_type> {
64 std::optional<index_type> index;
65 auto score = std::numeric_limits<StorageIndex>::min();
66 for (index_type i = 0; i < scores.size(); ++i) {
67 if (classification[i] == node_layer::unclassified) {
68 if (scores[i] > score) {
69 index = i;
70 score = scores[i];
71 }
72 }
73 }
74 return index;
75}
76
85template <typename StorageIndex>
86[[nodiscard]] inline auto build_first_coarse_grid_candidate(
87 const node_connection_list<StorageIndex>& connections,
88 const node_connection_list<StorageIndex>& transposed_connections)
90 util::vector<node_layer> classification(
91 connections.num_nodes(), node_layer::unclassified);
92
94 compute_node_scores(transposed_connections);
95
96 while (true) {
97 auto selection = find_max_score_index(scores, classification);
98 if (!selection) {
99 break;
100 }
101
102 classification[*selection] = node_layer::coarse;
103 for (const auto j :
104 transposed_connections.connected_nodes_to(*selection)) {
105 if (classification[j] == node_layer::unclassified) {
106 classification[j] = node_layer::fine;
107 for (const auto k : connections.connected_nodes_to(j)) {
108 scores[k] += 1;
109 }
110 }
111 }
112 for (const auto j : connections.connected_nodes_to(*selection)) {
113 if (classification[j] == node_layer::unclassified) {
114 scores[j] -= 1;
115 }
116 }
117 }
118
119 return classification;
120}
121
122} // namespace num_collect::linear::impl::amg
Class of lists of connected nodes per node.
Class of vectors wrapping std::vector class to use singed integers as indices.
Definition vector.h:37
Definition of index_type type.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Namespace of internal implementations of algebraic multigrid method ruge1987.
auto find_max_score_index(const util::vector< StorageIndex > &scores, const util::vector< node_layer > &classification) -> std::optional< index_type >
Find the index of the maximum score among unclassified nodes.
auto compute_node_scores(const node_connection_list< StorageIndex > &transposed_connections) -> util::vector< StorageIndex >
Compute the first scores of nodes.
auto build_first_coarse_grid_candidate(const node_connection_list< StorageIndex > &connections, const node_connection_list< StorageIndex > &transposed_connections) -> util::vector< node_layer >
Build the first candidate of a coarse grid.
@ fine
Only in finer grid.
Definition node_layer.h:37
Definition of node_connection_list class.
Definition of node_class enumeration.
Definition of vector class.