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.

284 lines
9.8 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2004-2015 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_ilutp.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date October 14, 2004.
  29. @brief ILUTP: Incomplete LU with threshold and K fill-in Preconditioner and
  30. column pivoting.
  31. */
  32. #ifndef GMM_PRECOND_ILUTP_H
  33. #define GMM_PRECOND_ILUTP_H
  34. #include "gmm_precond_ilut.h"
  35. namespace gmm {
  36. /**
  37. ILUTP: Incomplete LU with threshold and K fill-in Preconditioner and
  38. column pivoting.
  39. See Yousef Saad, Iterative Methods for
  40. sparse linear systems, PWS Publishing Company, section 10.4.4
  41. TODO : store the permutation by cycles to avoid the temporary vector
  42. */
  43. template <typename Matrix>
  44. class ilutp_precond {
  45. public :
  46. typedef typename linalg_traits<Matrix>::value_type value_type;
  47. typedef wsvector<value_type> _wsvector;
  48. typedef rsvector<value_type> _rsvector;
  49. typedef row_matrix<_rsvector> LU_Matrix;
  50. typedef col_matrix<_wsvector> CLU_Matrix;
  51. bool invert;
  52. LU_Matrix L, U;
  53. gmm::unsorted_sub_index indperm;
  54. gmm::unsorted_sub_index indperminv;
  55. mutable std::vector<value_type> temporary;
  56. protected:
  57. size_type K;
  58. double eps;
  59. template<typename M> void do_ilutp(const M&, row_major);
  60. void do_ilutp(const Matrix&, col_major);
  61. public:
  62. void build_with(const Matrix& A, int k_ = -1, double eps_ = double(-1)) {
  63. if (k_ >= 0) K = k_;
  64. if (eps_ >= double(0)) eps = eps_;
  65. invert = false;
  66. gmm::resize(L, mat_nrows(A), mat_ncols(A));
  67. gmm::resize(U, mat_nrows(A), mat_ncols(A));
  68. do_ilutp(A, typename principal_orientation_type<typename
  69. linalg_traits<Matrix>::sub_orientation>::potype());
  70. }
  71. ilutp_precond(const Matrix& A, size_type k_, double eps_)
  72. : L(mat_nrows(A), mat_ncols(A)), U(mat_nrows(A), mat_ncols(A)),
  73. K(k_), eps(eps_) { build_with(A); }
  74. ilutp_precond(int k_, double eps_) : K(k_), eps(eps_) {}
  75. ilutp_precond(void) { K = 10; eps = 1E-7; }
  76. size_type memsize() const {
  77. return sizeof(*this) + (nnz(U)+nnz(L))*sizeof(value_type);
  78. }
  79. };
  80. template<typename Matrix> template<typename M>
  81. void ilutp_precond<Matrix>::do_ilutp(const M& A, row_major) {
  82. typedef value_type T;
  83. typedef typename number_traits<T>::magnitude_type R;
  84. size_type n = mat_nrows(A);
  85. CLU_Matrix CU(n,n);
  86. if (n == 0) return;
  87. std::vector<T> indiag(n);
  88. temporary.resize(n);
  89. std::vector<size_type> ipvt(n), ipvtinv(n);
  90. for (size_type i = 0; i < n; ++i) ipvt[i] = ipvtinv[i] = i;
  91. indperm = unsorted_sub_index(ipvt);
  92. indperminv = unsorted_sub_index(ipvtinv);
  93. _wsvector w(mat_ncols(A));
  94. _rsvector ww(mat_ncols(A));
  95. T tmp = T(0);
  96. gmm::clear(L); gmm::clear(U);
  97. R prec = default_tol(R());
  98. R max_pivot = gmm::abs(A(0,0)) * prec;
  99. for (size_type i = 0; i < n; ++i) {
  100. copy(sub_vector(mat_const_row(A, i), indperm), w);
  101. double norm_row = gmm::vect_norm2(mat_const_row(A, i));
  102. typename _wsvector::iterator wkold = w.end();
  103. for (typename _wsvector::iterator wk = w.begin();
  104. wk != w.end() && wk->first < i; ) {
  105. size_type k = wk->first;
  106. tmp = (wk->second) * indiag[k];
  107. if (gmm::abs(tmp) < eps * norm_row) w.erase(k);
  108. else { wk->second += tmp; gmm::add(scaled(mat_row(U, k), -tmp), w); }
  109. if (wkold == w.end()) wk = w.begin(); else { wk = wkold; ++wk; }
  110. if (wk != w.end() && wk->first == k)
  111. { if (wkold == w.end()) wkold = w.begin(); else ++wkold; ++wk; }
  112. }
  113. gmm::clean(w, eps * norm_row);
  114. gmm::copy(w, ww);
  115. std::sort(ww.begin(), ww.end(), elt_rsvector_value_less_<T>());
  116. typename _rsvector::const_iterator wit = ww.begin(), wite = ww.end();
  117. size_type ip = size_type(-1);
  118. for (; wit != wite; ++wit)
  119. if (wit->c >= i) { ip = wit->c; tmp = wit->e; break; }
  120. if (ip == size_type(-1) || gmm::abs(tmp) <= max_pivot)
  121. { GMM_WARNING2("pivot " << i << " too small"); ip=i; ww[i]=tmp=T(1); }
  122. max_pivot = std::max(max_pivot, std::min(gmm::abs(tmp) * prec, R(1)));
  123. indiag[i] = T(1) / tmp;
  124. wit = ww.begin();
  125. size_type nnl = 0, nnu = 0;
  126. L[i].base_resize(K); U[i].base_resize(K+1);
  127. typename _rsvector::iterator witL = L[i].begin(), witU = U[i].begin();
  128. for (; wit != wite; ++wit) {
  129. if (wit->c < i) { if (nnl < K) { *witL++ = *wit; ++nnl; } }
  130. else if (nnu < K || wit->c == i)
  131. { CU(i, wit->c) = wit->e; *witU++ = *wit; ++nnu; }
  132. }
  133. L[i].base_resize(nnl); U[i].base_resize(nnu);
  134. std::sort(L[i].begin(), L[i].end());
  135. std::sort(U[i].begin(), U[i].end());
  136. if (ip != i) {
  137. typename _wsvector::const_iterator iti = CU.col(i).begin();
  138. typename _wsvector::const_iterator itie = CU.col(i).end();
  139. typename _wsvector::const_iterator itp = CU.col(ip).begin();
  140. typename _wsvector::const_iterator itpe = CU.col(ip).end();
  141. while (iti != itie && itp != itpe) {
  142. if (iti->first < itp->first)
  143. { U.row(iti->first).swap_indices(i, ip); ++iti; }
  144. else if (iti->first > itp->first)
  145. { U.row(itp->first).swap_indices(i,ip);++itp; }
  146. else
  147. { U.row(iti->first).swap_indices(i, ip); ++iti; ++itp; }
  148. }
  149. for( ; iti != itie; ++iti) U.row(iti->first).swap_indices(i, ip);
  150. for( ; itp != itpe; ++itp) U.row(itp->first).swap_indices(i, ip);
  151. CU.swap_col(i, ip);
  152. indperm.swap(i, ip);
  153. indperminv.swap(ipvt[i], ipvt[ip]);
  154. std::swap(ipvtinv[ipvt[i]], ipvtinv[ipvt[ip]]);
  155. std::swap(ipvt[i], ipvt[ip]);
  156. }
  157. }
  158. }
  159. template<typename Matrix>
  160. void ilutp_precond<Matrix>::do_ilutp(const Matrix& A, col_major) {
  161. do_ilutp(gmm::transposed(A), row_major());
  162. invert = true;
  163. }
  164. template <typename Matrix, typename V1, typename V2> inline
  165. void mult(const ilutp_precond<Matrix>& P, const V1 &v1, V2 &v2) {
  166. if (P.invert) {
  167. gmm::copy(gmm::sub_vector(v1, P.indperm), v2);
  168. gmm::lower_tri_solve(gmm::transposed(P.U), v2, false);
  169. gmm::upper_tri_solve(gmm::transposed(P.L), v2, true);
  170. }
  171. else {
  172. gmm::copy(v1, P.temporary);
  173. gmm::lower_tri_solve(P.L, P.temporary, true);
  174. gmm::upper_tri_solve(P.U, P.temporary, false);
  175. gmm::copy(gmm::sub_vector(P.temporary, P.indperminv), v2);
  176. }
  177. }
  178. template <typename Matrix, typename V1, typename V2> inline
  179. void transposed_mult(const ilutp_precond<Matrix>& P,const V1 &v1,V2 &v2) {
  180. if (P.invert) {
  181. gmm::copy(v1, P.temporary);
  182. gmm::lower_tri_solve(P.L, P.temporary, true);
  183. gmm::upper_tri_solve(P.U, P.temporary, false);
  184. gmm::copy(gmm::sub_vector(P.temporary, P.indperminv), v2);
  185. }
  186. else {
  187. gmm::copy(gmm::sub_vector(v1, P.indperm), v2);
  188. gmm::lower_tri_solve(gmm::transposed(P.U), v2, false);
  189. gmm::upper_tri_solve(gmm::transposed(P.L), v2, true);
  190. }
  191. }
  192. template <typename Matrix, typename V1, typename V2> inline
  193. void left_mult(const ilutp_precond<Matrix>& P, const V1 &v1, V2 &v2) {
  194. if (P.invert) {
  195. gmm::copy(gmm::sub_vector(v1, P.indperm), v2);
  196. gmm::lower_tri_solve(gmm::transposed(P.U), v2, false);
  197. }
  198. else {
  199. copy(v1, v2);
  200. gmm::lower_tri_solve(P.L, v2, true);
  201. }
  202. }
  203. template <typename Matrix, typename V1, typename V2> inline
  204. void right_mult(const ilutp_precond<Matrix>& P, const V1 &v1, V2 &v2) {
  205. if (P.invert) {
  206. copy(v1, v2);
  207. gmm::upper_tri_solve(gmm::transposed(P.L), v2, true);
  208. }
  209. else {
  210. copy(v1, P.temporary);
  211. gmm::upper_tri_solve(P.U, P.temporary, false);
  212. gmm::copy(gmm::sub_vector(P.temporary, P.indperminv), v2);
  213. }
  214. }
  215. template <typename Matrix, typename V1, typename V2> inline
  216. void transposed_left_mult(const ilutp_precond<Matrix>& P, const V1 &v1,
  217. V2 &v2) {
  218. if (P.invert) {
  219. copy(v1, P.temporary);
  220. gmm::upper_tri_solve(P.U, P.temporary, false);
  221. gmm::copy(gmm::sub_vector(P.temporary, P.indperminv), v2);
  222. }
  223. else {
  224. copy(v1, v2);
  225. gmm::upper_tri_solve(gmm::transposed(P.L), v2, true);
  226. }
  227. }
  228. template <typename Matrix, typename V1, typename V2> inline
  229. void transposed_right_mult(const ilutp_precond<Matrix>& P, const V1 &v1,
  230. V2 &v2) {
  231. if (P.invert) {
  232. copy(v1, v2);
  233. gmm::lower_tri_solve(P.L, v2, true);
  234. }
  235. else {
  236. gmm::copy(gmm::sub_vector(v1, P.indperm), v2);
  237. gmm::lower_tri_solve(gmm::transposed(P.U), v2, false);
  238. }
  239. }
  240. }
  241. #endif