numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
tune_coarse_grid_selection.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 <algorithm>
23#include <optional>
24#include <unordered_set>
25
31
33
44template <typename StorageIndex>
46 const node_connection_list<StorageIndex>& connections,
47 const std::unordered_set<StorageIndex>& neighbors_in_coarse_grid,
48 const std::unordered_set<StorageIndex>& neighbors_in_fine_grid)
49 -> std::optional<StorageIndex> {
50 for (const auto neighbor : neighbors_in_fine_grid) {
51 if (std::ranges::none_of(connections.connected_nodes_to(neighbor),
52 [neighbors_in_coarse_grid](StorageIndex index) {
53 return neighbors_in_coarse_grid.contains(index);
54 })) {
55 return neighbor;
56 }
57 }
58 return std::nullopt;
59}
60
70template <typename StorageIndex>
72 const node_connection_list<StorageIndex>& connections,
73 util::vector<node_layer>& node_classification,
74 index_type tested_node_index) {
76 node_classification[tested_node_index] == node_layer::fine);
77
78 std::unordered_set<StorageIndex> neighbors_in_coarse_grid;
79 std::unordered_set<StorageIndex> neighbors_in_fine_grid;
80 for (const auto neighbor :
81 connections.connected_nodes_to(tested_node_index)) {
82 if (node_classification[neighbor] == node_layer::coarse) {
83 neighbors_in_coarse_grid.insert(neighbor);
84 } else {
86 node_classification[neighbor] == node_layer::fine);
87 neighbors_in_fine_grid.insert(neighbor);
88 }
89 }
90
92 connections, neighbors_in_coarse_grid, neighbors_in_fine_grid);
93 if (!unsatisfying_node) {
94 return;
95 }
96 // Temporarily change the node.
97 neighbors_in_fine_grid.erase(*unsatisfying_node);
98 neighbors_in_coarse_grid.insert(*unsatisfying_node);
99
101 connections, neighbors_in_coarse_grid, neighbors_in_fine_grid)) {
102 // Two nodes are unsatisfying, so move this node to the coarse grid.
103 node_classification[tested_node_index] = node_layer::coarse;
104 } else {
105 // Only one node is unsatisfying, so move the unsatisfying node to the
106 // coarse grid.
107 node_classification[*unsatisfying_node] = node_layer::coarse;
108 }
109}
110
120template <typename StorageIndex>
122 const node_connection_list<StorageIndex>& connections,
123 const node_connection_list<StorageIndex>& transposed_connections,
124 util::vector<node_layer>& node_classification) {
125 // At least one node must be strongly connected to a node not in coarse
126 // grid.
127 for (index_type i = 0; i < node_classification.size(); ++i) {
128 if (node_classification[i] != node_layer::coarse) {
129 index_type num_connected_nodes = 0;
130 for (const index_type connected_node_index :
131 transposed_connections.connected_nodes_to(i)) {
132 if (node_classification[connected_node_index] ==
134 ++num_connected_nodes;
135 }
136 }
137 if (num_connected_nodes == 0) {
138 node_classification[i] = node_layer::coarse;
139 }
140 }
141 }
142
143 for (index_type i = 0; i < node_classification.size(); ++i) {
144 if (node_classification[i] == node_layer::fine) {
146 connections, node_classification, i);
147 }
148 }
149}
150
151} // namespace num_collect::linear::impl::amg
Definition of assertion macros.
#define NUM_COLLECT_DEBUG_ASSERT(CONDITION)
Macro to check whether a condition is satisfied in debug build only.
Definition assert.h:75
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
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_node_unsatisfying_interpolation_condition(const node_connection_list< StorageIndex > &connections, const std::unordered_set< StorageIndex > &neighbors_in_coarse_grid, const std::unordered_set< StorageIndex > &neighbors_in_fine_grid) -> std::optional< StorageIndex >
Find a neighboring node unsatisfying the condition of interpolation in ruge1987 for a node.
void tune_coarse_grid_selection(const node_connection_list< StorageIndex > &connections, const node_connection_list< StorageIndex > &transposed_connections, util::vector< node_layer > &node_classification)
Tune a coarse grid to satisfy the condition for interpolation specified in ruge1987.
@ fine
Only in finer grid.
Definition node_layer.h:37
void tune_coarse_grid_selection_for_one_node(const node_connection_list< StorageIndex > &connections, util::vector< node_layer > &node_classification, index_type tested_node_index)
Tune a node in a coarse grid to satisfy the condition for interpolation specified in ruge1987.
Definition of node_connection_list class.
Definition of node_class enumeration.
Definition of vector class.