Multiscale Universal Interface  2.0
A Concurrent Framework for Coupling Heterogeneous Solvers
solver.h
Go to the documentation of this file.
1 /*****************************************************************************
2 * Multiscale Universal Interface Code Coupling Library *
3 * *
4 * Copyright (C) 2023 W. Liu *
5 * *
6 * This software is jointly licensed under the Apache License, Version 2.0 *
7 * and the GNU General Public License version 3, you may use it according *
8 * to either. *
9 * *
10 * ** Apache License, version 2.0 ** *
11 * *
12 * Licensed under the Apache License, Version 2.0 (the "License"); *
13 * you may not use this file except in compliance with the License. *
14 * You may obtain a copy of the License at *
15 * *
16 * http://www.apache.org/licenses/LICENSE-2.0 *
17 * *
18 * Unless required by applicable law or agreed to in writing, software *
19 * distributed under the License is distributed on an "AS IS" BASIS, *
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
21 * See the License for the specific language governing permissions and *
22 * limitations under the License. *
23 * *
24 * ** GNU General Public License, version 3 ** *
25 * *
26 * This program is free software: you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation, either version 3 of the License, or *
29 * (at your option) any later version. *
30 * *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
35 * *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 *****************************************************************************/
39 
51 #ifndef MUI_SOLVER_H_
52 #define MUI_SOLVER_H_
53 
54 #include <cmath>
55 
56 #include "matrix.h"
57 #include "preconditioner.h"
58 
59 namespace mui {
60 namespace linalg {
61 
62 // Base linear equation solver class
63 template<typename ITYPE, typename VTYPE>
64 class solver {
65 
66  public:
67  // Abstract function for solve
68  virtual std::pair<ITYPE, VTYPE> solve(sparse_matrix<ITYPE,VTYPE> = sparse_matrix<ITYPE,VTYPE>()) = 0;
69  // Abstract function to get the solution
71 };
72 
73 // Class of one-dimensional Conjugate Gradient solver
74 template<typename ITYPE, typename VTYPE>
75 class conjugate_gradient_1d : public solver<ITYPE,VTYPE> {
76 
77  public:
78  // Constructor
80  // Destructor
82  // Member function for solve
83  std::pair<ITYPE, VTYPE> solve(sparse_matrix<ITYPE,VTYPE> = sparse_matrix<ITYPE,VTYPE>());
84  // Member function to get the solution
86 
87  private:
88  // The coefficient matrix of the matrix equation
90  // The variable matrix of the matrix equation
92  // The constant matrix of the matrix equation
94  // The residual matrix of the CG solver
96  // The preconditioned residual matrix of the CG solver
98  // The direction matrix of the CG solver
100  // Tolerance of CG solver
101  VTYPE cg_solve_tol_;
102  // Maximum iteration of CG solver
103  ITYPE cg_max_iter_;
104  // Preconditioner pointer
106 
107 };
108 
109 // Class of multidimensional Conjugate Gradient solver
110 template<typename ITYPE, typename VTYPE>
111 class conjugate_gradient : public solver<ITYPE,VTYPE> {
112 
113  public:
114  // Constructor
116  // Destructor
118  // Member function for solve
119  std::pair<ITYPE, VTYPE> solve(sparse_matrix<ITYPE,VTYPE> = sparse_matrix<ITYPE,VTYPE>());
120  // Member function to get the solution
122 
123  private:
124  // The coefficient matrix of the matrix equation
126  // The constant matrix of the matrix equation
128  // The column segments of the constant matrix of the matrix equation
129  sparse_matrix<ITYPE,VTYPE> b_column_;
130  // The column segments of the initial guess of the variable matrix of the matrix equation
131  sparse_matrix<ITYPE,VTYPE> x_init_column_;
132  // The variable matrix of the matrix equation
134  // Tolerance of CG solver
135  VTYPE cg_solve_tol_;
136  // Maximum iteration of CG solver
137  ITYPE cg_max_iter_;
138  // Preconditioner pointer
140 
141 };
142 
143 // Class of one-dimensional Biconjugate Gradient Stabilized solver
144 template<typename ITYPE, typename VTYPE>
145 class biconjugate_gradient_stabilized_1d : public solver<ITYPE,VTYPE> {
146 
147  public:
148  // Constructor
150  // Destructor
152  // Member function for solve
153  std::pair<ITYPE, VTYPE> solve(sparse_matrix<ITYPE,VTYPE> = sparse_matrix<ITYPE,VTYPE>());
154  // Member function to get the solution
156 
157  private:
158  // The coefficient matrix of the matrix equation
160  // The variable matrix of the matrix equation
162  // The constant matrix of the matrix equation
164  // The residual matrix of the CG solver
167  // The preconditioned residual matrix of the CG solver
170  // The direction matrix of the CG solver
176  // Variables
177  VTYPE alpha_;
178  VTYPE beta_;
179  VTYPE omega_;
180  VTYPE rho_;
181  VTYPE rhoTilde_;
182  // Tolerance of CG solver
183  VTYPE bicgstab_solve_tol_;
184  // Maximum iteration of CG solver
185  ITYPE bicgstab_max_iter_;
186  // Preconditioner pointer
188 
189 };
190 
191 // Class of multidimensional Biconjugate Gradient Stabilized solver
192 template<typename ITYPE, typename VTYPE>
193 class biconjugate_gradient_stabilized : public solver<ITYPE,VTYPE> {
194 
195  public:
196  // Constructor
198  // Destructor
200  // Member function for solve
201  std::pair<ITYPE, VTYPE> solve(sparse_matrix<ITYPE,VTYPE> = sparse_matrix<ITYPE,VTYPE>());
202  // Member function to get the solution
204 
205  private:
206  // The coefficient matrix of the matrix equation
208  // The constant matrix of the matrix equation
210  // The column segments of the constant matrix of the matrix equation
211  sparse_matrix<ITYPE,VTYPE> b_column_;
212  // The column segments of the initial guess of the variable matrix of the matrix equation
213  sparse_matrix<ITYPE,VTYPE> x_init_column_;
214  // The variable matrix of the matrix equation
216  // Tolerance of CG solver
217  VTYPE bicgstab_solve_tol_;
218  // Maximum iteration of CG solver
219  ITYPE bicgstab_max_iter_;
220  // Preconditioner pointer
222 
223 };
224 
225 // Class of one-dimensional Gaussian Elimination solver
226 template<typename ITYPE, typename VTYPE>
227 class gaussian_elimination_1d : public solver<ITYPE,VTYPE> {
228 
229  public:
230  // Constructor
232  // Destructor
234  // Member function for solve
235  std::pair<ITYPE, VTYPE> solve(sparse_matrix<ITYPE,VTYPE> = sparse_matrix<ITYPE,VTYPE>());
236  // Member function to get the solution
238 
239  private:
240  // The coefficient matrix of the matrix equation
242  // The constant matrix of the matrix equation
244  // The variable matrix of the matrix equation
246 
247 };
248 
249 // Class of multidimensional Gaussian Elimination solver
250 template<typename ITYPE, typename VTYPE>
251 class gaussian_elimination : public solver<ITYPE,VTYPE> {
252 
253  public:
254  // Constructor
256  // Destructor
258  // Member function for solve
259  std::pair<ITYPE, VTYPE> solve(sparse_matrix<ITYPE,VTYPE> = sparse_matrix<ITYPE,VTYPE>());
260  // Member function to get the solution
262 
263  private:
264  // The coefficient matrix of the matrix equation
266  // The constant matrix of the matrix equation
268  // The variable matrix of the matrix equation
270  // The column segments of the constant matrix of the matrix equation
271  sparse_matrix<ITYPE,VTYPE> b_column_;
272 
273 };
274 
275 } // linalg
276 } // mui
277 
278 // Include implementations
279 #include "../linear_algebra/solver_cg.h"
280 #include "../linear_algebra/solver_ge.h"
281 #include "../linear_algebra/solver_bicgstab.h"
282 
283 #endif /* MUI_SOLVER_H_ */
sparse_matrix< ITYPE, VTYPE > getSolution()
Definition: solver_bicgstab.h:318
biconjugate_gradient_stabilized_1d(sparse_matrix< ITYPE, VTYPE >, sparse_matrix< ITYPE, VTYPE >, VTYPE=1e-6, ITYPE=0, preconditioner< ITYPE, VTYPE > *=nullptr)
Definition: solver_bicgstab.h:58
std::pair< ITYPE, VTYPE > solve(sparse_matrix< ITYPE, VTYPE >=sparse_matrix< ITYPE, VTYPE >())
Definition: solver_bicgstab.h:150
~biconjugate_gradient_stabilized_1d()
Definition: solver_bicgstab.h:100
~biconjugate_gradient_stabilized()
Definition: solver_bicgstab.h:131
biconjugate_gradient_stabilized(sparse_matrix< ITYPE, VTYPE >, sparse_matrix< ITYPE, VTYPE >, VTYPE=1e-6, ITYPE=0, preconditioner< ITYPE, VTYPE > *=nullptr)
Definition: solver_bicgstab.h:85
sparse_matrix< ITYPE, VTYPE > getSolution()
Definition: solver_bicgstab.h:324
std::pair< ITYPE, VTYPE > solve(sparse_matrix< ITYPE, VTYPE >=sparse_matrix< ITYPE, VTYPE >())
Definition: solver_bicgstab.h:286
Definition: solver.h:75
std::pair< ITYPE, VTYPE > solve(sparse_matrix< ITYPE, VTYPE >=sparse_matrix< ITYPE, VTYPE >())
Definition: solver_cg.h:130
sparse_matrix< ITYPE, VTYPE > getSolution()
Definition: solver_cg.h:245
~conjugate_gradient_1d()
Definition: solver_cg.h:91
conjugate_gradient_1d(sparse_matrix< ITYPE, VTYPE >, sparse_matrix< ITYPE, VTYPE >, VTYPE=1e-6, ITYPE=0, preconditioner< ITYPE, VTYPE > *=nullptr)
Definition: solver_cg.h:60
Definition: solver.h:111
sparse_matrix< ITYPE, VTYPE > getSolution()
Definition: solver_cg.h:251
std::pair< ITYPE, VTYPE > solve(sparse_matrix< ITYPE, VTYPE >=sparse_matrix< ITYPE, VTYPE >())
Definition: solver_cg.h:213
conjugate_gradient(sparse_matrix< ITYPE, VTYPE >, sparse_matrix< ITYPE, VTYPE >, VTYPE=1e-6, ITYPE=0, preconditioner< ITYPE, VTYPE > *=nullptr)
Definition: solver_cg.h:76
~conjugate_gradient()
Definition: solver_cg.h:111
Definition: solver.h:227
sparse_matrix< ITYPE, VTYPE > getSolution()
Definition: solver_ge.h:164
~gaussian_elimination_1d()
Definition: solver_ge.h:79
std::pair< ITYPE, VTYPE > solve(sparse_matrix< ITYPE, VTYPE >=sparse_matrix< ITYPE, VTYPE >())
Definition: solver_ge.h:98
gaussian_elimination_1d(sparse_matrix< ITYPE, VTYPE >, sparse_matrix< ITYPE, VTYPE >)
Definition: solver_ge.h:56
Definition: solver.h:251
gaussian_elimination(sparse_matrix< ITYPE, VTYPE >, sparse_matrix< ITYPE, VTYPE >)
Definition: solver_ge.h:68
sparse_matrix< ITYPE, VTYPE > getSolution()
Definition: solver_ge.h:170
std::pair< ITYPE, VTYPE > solve(sparse_matrix< ITYPE, VTYPE >=sparse_matrix< ITYPE, VTYPE >())
Definition: solver_ge.h:144
~gaussian_elimination()
Definition: solver_ge.h:88
Definition: preconditioner.h:55
Definition: solver.h:64
virtual std::pair< ITYPE, VTYPE > solve(sparse_matrix< ITYPE, VTYPE >=sparse_matrix< ITYPE, VTYPE >())=0
virtual sparse_matrix< ITYPE, VTYPE > getSolution()=0
Definition: matrix.h:61
Base class for sparse matrix based on COO, CSR & CSC formats includes basic arithmetic operations,...
e
Definition: dim.h:347
Definition: comm.h:54
Preconditioner classes.