Multiscale Universal Interface  2.0
A Concurrent Framework for Coupling Heterogeneous Solvers
preconditioner_diagonal.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_DIAGONAL_H_
48 #define MUI_PRECONDITIONER_DIAGONAL_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  inv_diag_.resize(A.get_rows(), A.get_cols());
61  // Construct the inverse diagonal matrix
62  for (int i = 0; i < A.get_rows(); i++) {
63  if (std::abs(A.get_value(i,i)) >= std::numeric_limits<VTYPE>::min()) {
64  inv_diag_.set_value(i, i, 1.0 / A.get_value(i,i));
65  } else {
66  inv_diag_.set_value(i, i, 1.0);
67  }
68  }
69  }
70 
71 // Destructor
72 template<typename ITYPE, typename VTYPE>
74  // Deallocate the memory for the inverse diagonal matrix
75  inv_diag_.set_zero();
76 }
77 
78 // Member function on preconditioner apply
79 template<typename ITYPE, typename VTYPE>
81  assert((x.get_cols()==1) &&
82  "MUI Error [preconditioner_diagonal.h]: apply only works for column vectors");
84 
85  for (int i = 0; i < x.get_rows(); i++) {
86  if (std::abs(inv_diag_.get_value(i,i)) >= std::numeric_limits<VTYPE>::min()) {
87  z.set_value(i, 0, inv_diag_.get_value(i,i)*x.get_value(i,0));
88  } else {
89  z.set_value(i, 0, 0.0);
90  }
91  }
92 
93  return z;
94 }
95 
96 } // linalg
97 } // mui
98 
99 #endif /* MUI_PRECONDITIONER_DIAGONAL_H_ */
diagonal_preconditioner(const sparse_matrix< ITYPE, VTYPE > &)
Definition: preconditioner_diagonal.h:58
~diagonal_preconditioner()
Definition: preconditioner_diagonal.h:73
sparse_matrix< ITYPE, VTYPE > apply(const sparse_matrix< ITYPE, VTYPE > &)
Definition: preconditioner_diagonal.h:80
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