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.

174 lines
6.7 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_ildltt.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date June 30, 2003.
  29. @brief incomplete LDL^t (cholesky) preconditioner with fill-in and threshold.
  30. */
  31. #ifndef GMM_PRECOND_ILDLTT_H
  32. #define GMM_PRECOND_ILDLTT_H
  33. // Store U = LT and D in indiag. On each line, the fill-in is the number
  34. // of non-zero elements on the line of the original matrix plus K, except if
  35. // the matrix is dense. In this case the fill-in is K on each line.
  36. #include "gmm_precond_ilut.h"
  37. namespace gmm {
  38. /** incomplete LDL^t (cholesky) preconditioner with fill-in and
  39. threshold. */
  40. template <typename Matrix>
  41. class ildltt_precond {
  42. public :
  43. typedef typename linalg_traits<Matrix>::value_type value_type;
  44. typedef typename number_traits<value_type>::magnitude_type magnitude_type;
  45. typedef rsvector<value_type> svector;
  46. row_matrix<svector> U;
  47. std::vector<magnitude_type> indiag;
  48. protected:
  49. size_type K;
  50. double eps;
  51. template<typename M> void do_ildltt(const M&, row_major);
  52. void do_ildltt(const Matrix&, col_major);
  53. public:
  54. void build_with(const Matrix& A, int k_ = -1, double eps_ = double(-1)) {
  55. if (k_ >= 0) K = k_;
  56. if (eps_ >= double(0)) eps = eps_;
  57. gmm::resize(U, mat_nrows(A), mat_ncols(A));
  58. indiag.resize(std::min(mat_nrows(A), mat_ncols(A)));
  59. do_ildltt(A, typename principal_orientation_type<typename
  60. linalg_traits<Matrix>::sub_orientation>::potype());
  61. }
  62. ildltt_precond(const Matrix& A, int k_, double eps_)
  63. : U(mat_nrows(A),mat_ncols(A)), K(k_), eps(eps_) { build_with(A); }
  64. ildltt_precond(void) { K=10; eps = 1E-7; }
  65. ildltt_precond(size_type k_, double eps_) : K(k_), eps(eps_) {}
  66. size_type memsize() const {
  67. return sizeof(*this) + nnz(U)*sizeof(value_type) + indiag.size() * sizeof(magnitude_type);
  68. }
  69. };
  70. template<typename Matrix> template<typename M>
  71. void ildltt_precond<Matrix>::do_ildltt(const M& A,row_major) {
  72. typedef value_type T;
  73. typedef typename number_traits<T>::magnitude_type R;
  74. size_type n = mat_nrows(A);
  75. if (n == 0) return;
  76. svector w(n);
  77. T tmp;
  78. R prec = default_tol(R()), max_pivot = gmm::abs(A(0,0)) * prec;
  79. gmm::clear(U);
  80. for (size_type i = 0; i < n; ++i) {
  81. gmm::copy(mat_const_row(A, i), w);
  82. double norm_row = gmm::vect_norm2(w);
  83. for (size_type krow = 0, k; krow < w.nb_stored(); ++krow) {
  84. typename svector::iterator wk = w.begin() + krow;
  85. if ((k = wk->c) >= i) break;
  86. if (gmm::is_complex(wk->e)) {
  87. tmp = gmm::conj(U(k, i))/indiag[k]; // not completely satisfactory ..
  88. gmm::add(scaled(mat_row(U, k), -tmp), w);
  89. }
  90. else {
  91. tmp = wk->e;
  92. if (gmm::abs(tmp) < eps * norm_row) { w.sup(k); --krow; }
  93. else { wk->e += tmp; gmm::add(scaled(mat_row(U, k), -tmp), w); }
  94. }
  95. }
  96. tmp = w[i];
  97. if (gmm::abs(gmm::real(tmp)) <= max_pivot)
  98. { GMM_WARNING2("pivot " << i << " is too small"); tmp = T(1); }
  99. max_pivot = std::max(max_pivot, std::min(gmm::abs(tmp) * prec, R(1)));
  100. indiag[i] = R(1) / gmm::real(tmp);
  101. gmm::clean(w, eps * norm_row);
  102. gmm::scale(w, T(indiag[i]));
  103. std::sort(w.begin(), w.end(), elt_rsvector_value_less_<T>());
  104. typename svector::const_iterator wit = w.begin(), wite = w.end();
  105. for (size_type nnu = 0; wit != wite; ++wit) // copy to be optimized ...
  106. if (wit->c > i) { if (nnu < K) { U(i, wit->c) = wit->e; ++nnu; } }
  107. }
  108. }
  109. template<typename Matrix>
  110. void ildltt_precond<Matrix>::do_ildltt(const Matrix& A, col_major)
  111. { do_ildltt(gmm::conjugated(A), row_major()); }
  112. template <typename Matrix, typename V1, typename V2> inline
  113. void mult(const ildltt_precond<Matrix>& P, const V1 &v1, V2 &v2) {
  114. gmm::copy(v1, v2);
  115. gmm::lower_tri_solve(gmm::conjugated(P.U), v2, true);
  116. for (size_type i = 0; i < P.indiag.size(); ++i) v2[i] *= P.indiag[i];
  117. gmm::upper_tri_solve(P.U, v2, true);
  118. }
  119. template <typename Matrix, typename V1, typename V2> inline
  120. void transposed_mult(const ildltt_precond<Matrix>& P,const V1 &v1, V2 &v2)
  121. { mult(P, v1, v2); }
  122. template <typename Matrix, typename V1, typename V2> inline
  123. void left_mult(const ildltt_precond<Matrix>& P, const V1 &v1, V2 &v2) {
  124. copy(v1, v2);
  125. gmm::lower_tri_solve(gmm::conjugated(P.U), v2, true);
  126. for (size_type i = 0; i < P.indiag.size(); ++i) v2[i] *= P.indiag[i];
  127. }
  128. template <typename Matrix, typename V1, typename V2> inline
  129. void right_mult(const ildltt_precond<Matrix>& P, const V1 &v1, V2 &v2)
  130. { copy(v1, v2); gmm::upper_tri_solve(P.U, v2, true); }
  131. template <typename Matrix, typename V1, typename V2> inline
  132. void transposed_left_mult(const ildltt_precond<Matrix>& P, const V1 &v1,
  133. V2 &v2) {
  134. copy(v1, v2);
  135. gmm::upper_tri_solve(P.U, v2, true);
  136. for (size_type i = 0; i < P.indiag.size(); ++i) v2[i] *= P.indiag[i];
  137. }
  138. template <typename Matrix, typename V1, typename V2> inline
  139. void transposed_right_mult(const ildltt_precond<Matrix>& P, const V1 &v1,
  140. V2 &v2)
  141. { copy(v1, v2); gmm::lower_tri_solve(gmm::conjugated(P.U), v2, true); }
  142. }
  143. #endif