Multiscale Universal Interface  2.0
A Concurrent Framework for Coupling Heterogeneous Solvers
solver_ge.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 
48 #ifndef MUI_GAUSSIAN_ELINATION_H_
49 #define MUI_GAUSSIAN_ELINATION_H_
50 
51 namespace mui {
52 namespace linalg {
53 
54 // Constructor for one-dimensional Gaussian Elimination solver
55 template<typename ITYPE, typename VTYPE>
57  : A_(A),
58  b_(b){
59  assert(A_.get_rows() == b_.get_rows() &&
60  "MUI Error [solver_ge.h]: Number of rows of A matrix must be the same as the number of rows of b matrix");
61  assert(b_.get_cols() == 1 &&
62  "MUI Error [solver_ge.h]: Number of column of b matrix must be 1");
63  x_.resize(b_.get_rows(),b_.get_cols());
64 }
65 
66 // Constructor for multidimensional Gaussian Elimination solver
67 template<typename ITYPE, typename VTYPE>
69  : A_(A),
70  b_(b){
71  assert(A_.get_rows() == b_.get_rows() &&
72  "MUI Error [solver_ge.h]: Number of rows of A matrix must be the same as the number of rows of b matrix");
73  x_.resize(b_.get_rows(),b_.get_cols());
74  b_column_.resize(b_.get_rows(), 1);
75 }
76 
77 // Destructor for one-dimensional Gaussian Elimination solver
78 template<typename ITYPE, typename VTYPE>
80  // Deallocate the memory for matrices
81  A_.set_zero();
82  x_.set_zero();
83  b_.set_zero();
84 }
85 
86 // Destructor for multidimensional Gaussian Elimination solver
87 template<typename ITYPE, typename VTYPE>
89  // Deallocate the memory for matrices
90  A_.set_zero();
91  x_.set_zero();
92  b_.set_zero();
93  b_column_.set_zero();
94 }
95 
96 // Member function for one-dimensional Gaussian Elimination solver to solve
97 template<typename ITYPE, typename VTYPE>
99  // x_init does not in used in Gaussian Elimination
100 
101  for (ITYPE i = 0; i < A_.get_rows(); ++i) {
102  // Find the pivot
103  ITYPE pivot = i;
104  for (ITYPE j = i+1; j < A_.get_cols(); ++j) {
105  if (std::abs(A_.get_value(j, i)) > std::abs(A_.get_value(pivot, i))) {
106  pivot = j;
107  }
108  }
109 
110  // Swap the pivot row with the current row
111  for (ITYPE j = 0; j < A_.get_cols(); ++j) {
112  A_.swap_elements(i, j, pivot, j);
113  }
114  b_.swap_elements(i, 0, pivot, 0);
115 
116  // Elimination
117  for (ITYPE j = i+1; j < A_.get_cols(); ++j) {
118  assert(std::abs(A_.get_value(i, i)) >= std::numeric_limits<VTYPE>::min() &&
119  "MUI Error [solver_ge.h]: Divide by zero assert for A_.get_value(i, i)");
120  VTYPE factor = A_.get_value(j, i) / A_.get_value(i, i);
121  for (ITYPE k = i; k < A_.get_cols(); ++k) {
122  A_.set_value(j, k, (A_.get_value(j, k) - (factor * A_.get_value(i, k))));
123  }
124  b_.set_value(j, 0, (b_.get_value(j, 0) - (factor * b_.get_value(i, 0))));
125  }
126  }
127 
128  // Back substitution
129  for (ITYPE i = A_.get_cols()-1; i >= 0; --i) {
130  x_.set_value(i, 0, b_.get_value(i, 0));
131  for (ITYPE j = i+1; j < A_.get_cols(); ++j) {
132  x_.set_value(i, 0, (x_.get_value(i, 0) - (A_.get_value(i, j) * x_.get_value(j, 0))));
133  }
134  assert(std::abs(A_.get_value(i, i)) >= std::numeric_limits<VTYPE>::min() &&
135  "MUI Error [solver_ge.h]: Divide by zero assert for A_.get_value(i, i)");
136  x_.set_value(i, 0, (x_.get_value(i, 0) / A_.get_value(i, i)));
137  }
138 
139  return std::make_pair(static_cast<ITYPE>(0),static_cast<VTYPE>(0));
140 }
141 
142 // Member function for multidimensional Gaussian Elimination solver to solve
143 template<typename ITYPE, typename VTYPE>
145  // x_init does not in used in Gaussian Elimination
146 
147  for (ITYPE j = 0; j < b_.get_cols(); ++j) {
148  b_column_.set_zero();
149  b_column_ = b_.segment(0,(b_.get_rows()-1),j,j);
150  gaussian_elimination_1d<ITYPE, VTYPE> ge(A_, b_column_);
151  std::pair<ITYPE, VTYPE> geReturnTemp = ge.solve();
152  sparse_matrix<ITYPE,VTYPE> x_column(b_.get_rows(),1);
153  x_column = ge.getSolution();
154  for (ITYPE i = 0; i < x_column.get_rows(); ++i) {
155  x_.set_value(i, j, x_column.get_value(i,0));
156  }
157  }
158 
159  return std::make_pair(static_cast<ITYPE>(0),static_cast<VTYPE>(0));
160 }
161 
162 // Member function for one-dimensional Gaussian Elimination solver to get the solution
163 template<typename ITYPE, typename VTYPE>
165  return x_;
166 }
167 
168 // Member function for multidimensional Gaussian Elimination solver to get the solution
169 template<typename ITYPE, typename VTYPE>
171  return x_;
172 }
173 
174 } // linalg
175 } // mui
176 
177 #endif /* MUI_GAUSSIAN_ELINATION_H_ */
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
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: matrix.h:61
ITYPE get_rows() const
Definition: matrix_io_info.h:579
VTYPE get_value(ITYPE, ITYPE) const
Definition: matrix_io_info.h:523
u u u u u u min
Definition: dim.h:289
Definition: comm.h:54