You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
5.1 KiB

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