GetFEM  5.4.2
gmm_precond_diagonal.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2003-2020 Yves Renard
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program; if not, write to the Free Software Foundation,
19  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, you may use this file as it is a part of a free
22  software library without restriction. Specifically, if other files
23  instantiate templates or use macros or inline functions from this file,
24  or you compile this file and link it with other files to produce an
25  executable, this file does not by itself cause the resulting executable
26  to be covered by the GNU Lesser General Public License. This exception
27  does not however invalidate any other reasons why the executable file
28  might be covered by the GNU Lesser General Public License.
29 
30 ===========================================================================*/
31 
32 /**@file gmm_precond_diagonal.h
33  @author Yves Renard <Yves.Renard@insa-lyon.fr>
34  @date June 5, 2003.
35  @brief Diagonal matrix preconditoner.
36 */
37 
38 #ifndef GMM_PRECOND_DIAGONAL_H
39 #define GMM_PRECOND_DIAGONAL_H
40 
41 #include "gmm_precond.h"
42 
43 namespace gmm {
44 
45  /** Diagonal preconditioner. */
46  template<typename Matrix> struct diagonal_precond {
47  typedef typename linalg_traits<Matrix>::value_type value_type;
48  typedef typename number_traits<value_type>::magnitude_type magnitude_type;
49 
50  std::vector<magnitude_type> diag;
51 
52  void build_with(const Matrix &M) {
53  diag.resize(mat_nrows(M));
54  for (size_type i = 0; i < mat_nrows(M); ++i) {
55  magnitude_type x = gmm::abs(M(i, i));
56  if (x == magnitude_type(0)) {
57  x = magnitude_type(1);
58  GMM_WARNING2("The matrix has a zero on its diagonal");
59  }
60  diag[i] = magnitude_type(1) / x;
61  }
62  }
63  size_type memsize() const { return sizeof(*this) + diag.size() * sizeof(value_type); }
64  diagonal_precond(const Matrix &M) { build_with(M); }
65  diagonal_precond(void) {}
66  };
67 
68  template <typename Matrix, typename V2> inline
69  void mult_diag_p(const diagonal_precond<Matrix>& P, V2 &v2, abstract_sparse){
70  typename linalg_traits<V2>::iterator it = vect_begin(v2),
71  ite = vect_end(v2);
72  for (; it != ite; ++it) *it *= P.diag[it.index()];
73  }
74 
75  template <typename Matrix, typename V2> inline
76  void mult_diag_p(const diagonal_precond<Matrix>& P,V2 &v2, abstract_skyline)
77  { mult_diag_p(P, v2, abstract_sparse()); }
78 
79  template <typename Matrix, typename V2> inline
80  void mult_diag_p(const diagonal_precond<Matrix>& P, V2 &v2, abstract_dense){
81  for (size_type i = 0; i < P.diag.size(); ++i) v2[i] *= P.diag[i];
82  }
83 
84  template <typename Matrix, typename V1, typename V2> inline
85  void mult(const diagonal_precond<Matrix>& P, const V1 &v1, V2 &v2) {
86  GMM_ASSERT2(P.diag.size() == vect_size(v2),"dimensions mismatch");
87  copy(v1, v2);
88  mult_diag_p(P, v2, typename linalg_traits<V2>::storage_type());
89  }
90 
91  template <typename Matrix, typename V1, typename V2> inline
92  void transposed_mult(const diagonal_precond<Matrix>& P,const V1 &v1,V2 &v2) {
93  mult(P, v1, v2);
94  }
95 
96  // # define DIAG_LEFT_MULT_SQRT
97 
98  template <typename Matrix, typename V1, typename V2> inline
99  void left_mult(const diagonal_precond<Matrix>& P, const V1 &v1, V2 &v2) {
100  GMM_ASSERT2(P.diag.size() == vect_size(v2), "dimensions mismatch");
101  copy(v1, v2);
102 # ifdef DIAG_LEFT_MULT_SQRT
103  for (size_type i= 0; i < P.diag.size(); ++i) v2[i] *= gmm::sqrt(P.diag[i]);
104 # else
105  for (size_type i= 0; i < P.diag.size(); ++i) v2[i] *= P.diag[i];
106 # endif
107  }
108 
109  template <typename Matrix, typename V1, typename V2> inline
110  void transposed_left_mult(const diagonal_precond<Matrix>& P,
111  const V1 &v1, V2 &v2)
112  { left_mult(P, v1, v2); }
113 
114  template <typename Matrix, typename V1, typename V2> inline
115  void right_mult(const diagonal_precond<Matrix>& P, const V1 &v1, V2 &v2) {
116  // typedef typename linalg_traits<Matrix>::value_type T;
117  GMM_ASSERT2(P.diag.size() == vect_size(v2), "dimensions mismatch");
118  copy(v1, v2);
119 # ifdef DIAG_LEFT_MULT_SQRT
120  for (size_type i= 0; i < P.diag.size(); ++i) v2[i] *= gmm::sqrt(P.diag[i]);
121 # endif
122  }
123 
124  template <typename Matrix, typename V1, typename V2> inline
125  void transposed_right_mult(const diagonal_precond<Matrix>& P,
126  const V1 &v1, V2 &v2)
127  { right_mult(P, v1, v2); }
128 
129 }
130 
131 #endif
132 
bgeot::size_type
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:49
gmm::diagonal_precond
Diagonal preconditioner.
Definition: gmm_precond_diagonal.h:46
gmm_precond.h
gmm preconditioners.
gmm::copy
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:977