Multiscale Universal Interface  2.0
A Concurrent Framework for Coupling Heterogeneous Solvers
preconditioner_ic.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 
47 #ifndef MUI_PRECONDITIONER_IC_H_
48 #define MUI_PRECONDITIONER_IC_H_
49 
50 #include <math.h>
51 #include <limits>
52 
53 namespace mui {
54 namespace linalg {
55 
56 // Constructor
57 template<typename ITYPE, typename VTYPE>
59  // Initialise the lower triangular matrix
60  L_.resize(A.get_rows(), A.get_cols());
61  // Construct the lower triangular matrix
62  for (ITYPE i = 0; i < A.get_rows(); ++i) {
63  for (ITYPE j = 0; j <= i; ++j) {
64  if (i == j) {
65  VTYPE sum = 0;
66  for (ITYPE k = 0; k < j; ++k) {
67  sum += std::pow(L_.get_value(j, k), 2);
68  }
69  L_.set_value(j, j, (std::sqrt(A.get_value(j, j) - sum)));
70  } else {
71  VTYPE sum = 0;
72  for (ITYPE k = 0; k < j; ++k) {
73  sum += L_.get_value(i, k) * L_.get_value(j, k);
74  }
75  assert(std::abs(L_.get_value(j, j)) >= std::numeric_limits<VTYPE>::min() &&
76  "MUI Error [preconditioner_ic.h]: Divide by zero assert for L_.get_value(j, j)");
77  L_.set_value(i, j, ((A.get_value(i, j) - sum) / L_.get_value(j, j)));
78  }
79  }
80  }
81  }
82 
83 // Destructor
84 template<typename ITYPE, typename VTYPE>
86  // Deallocate the memory for the lower triangular matrix
87  L_.set_zero();
88 }
89 
90 // Member function on preconditioner apply
91 template<typename ITYPE, typename VTYPE>
93  assert((x.get_cols()==1) &&
94  "MUI Error [preconditioner_ic.h]: apply only works for column vectors");
97 
98  // Forward substitution
99  for (ITYPE i = 0; i < x.get_rows(); ++i) {
100  VTYPE sum = 0;
101  for (ITYPE j = 0; j < i; ++j) {
102  sum += L_.get_value(i, j) * y.get_value(j,0);
103  }
104  assert(std::abs(L_.get_value(i, i)) >= std::numeric_limits<VTYPE>::min() &&
105  "MUI Error [preconditioner_ic.h]: Divide by zero assert for L_.get_value(i, i)");
106  y.set_value(i, 0, ((x.get_value(i, 0) - sum) / L_.get_value(i, i)));
107  }
108 
109  // Backward substitution
110  for (ITYPE i = x.get_rows() - 1; i >= 0; --i) {
111  VTYPE sum = 0;
112  for (ITYPE j = i + 1; j < x.get_rows(); ++j) {
113  sum += L_.get_value(j, i) * z.get_value(j,0);
114  }
115  assert(std::abs(L_.get_value(i, i)) >= std::numeric_limits<VTYPE>::min() &&
116  "MUI Error [preconditioner_ic.h]: Divide by zero assert for L_.get_value(i, i)");
117  z.set_value(i, 0, ((y.get_value(i, 0) - sum) / L_.get_value(i, i)));
118  }
119  return z;
120 }
121 
122 } // linalg
123 } // mui
124 
125 #endif /* MUI_PRECONDITIONER_IC_H_ */
~incomplete_cholesky_preconditioner()
Definition: preconditioner_ic.h:85
incomplete_cholesky_preconditioner(const sparse_matrix< ITYPE, VTYPE > &)
Definition: preconditioner_ic.h:58
sparse_matrix< ITYPE, VTYPE > apply(const sparse_matrix< ITYPE, VTYPE > &)
Definition: preconditioner_ic.h:92
Definition: matrix.h:61
ITYPE get_rows() const
Definition: matrix_io_info.h:579
void set_value(ITYPE, ITYPE, VTYPE, bool=true)
Definition: matrix_manipulation.h:292
VTYPE get_value(ITYPE, ITYPE) const
Definition: matrix_io_info.h:523
ITYPE get_cols() const
Definition: matrix_io_info.h:585
u u u u u u min
Definition: dim.h:289
Definition: comm.h:54
SCALAR sum(vexpr< E, SCALAR, D > const &u)
Definition: point.h:362