GetFEM  5.5
gmm_precond_ildlt.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2003-2026 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, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 // This file is a modified version of cholesky.h from ITL.
32 // See http://osl.iu.edu/research/itl/
33 // Following the corresponding Copyright notice.
34 //===========================================================================
35 //
36 // Copyright (c) 1998-2020, University of Notre Dame. All rights reserved.
37 // Redistribution and use in source and binary forms, with or without
38 // modification, are permitted provided that the following conditions are met:
39 //
40 // * Redistributions of source code must retain the above copyright
41 // notice, this list of conditions and the following disclaimer.
42 // * Redistributions in binary form must reproduce the above copyright
43 // notice, this list of conditions and the following disclaimer in the
44 // documentation and/or other materials provided with the distribution.
45 // * Neither the name of the University of Notre Dame nor the
46 // names of its contributors may be used to endorse or promote products
47 // derived from this software without specific prior written permission.
48 //
49 // THIS SOFTWARE IS PROVIDED BY THE TRUSTEES OF INDIANA UNIVERSITY AND
50 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
51 // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
52 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES
53 // OF INDIANA UNIVERSITY AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 //
61 //===========================================================================
62 
63 #ifndef GMM_PRECOND_ILDLT_H
64 #define GMM_PRECOND_ILDLT_H
65 
66 /**@file gmm_precond_ildlt.h
67  @author Andrew Lumsdaine <lums@osl.iu.edu>
68  @author Lie-Quan Lee <llee@osl.iu.edu>
69  @author Yves Renard <yves.renard@insa-lyon.fr>
70  @date June 5, 2003.
71  @brief Incomplete Level 0 ILDLT Preconditioner.
72 */
73 
74 #include "gmm_precond.h"
75 
76 namespace gmm {
77 
78  /** Incomplete Level 0 LDLT Preconditioner.
79 
80  For use with symmetric real or hermitian complex sparse matrices.
81 
82  Notes: The idea under a concrete Preconditioner such as Incomplete
83  Cholesky is to create a Preconditioner object to use in iterative
84  methods.
85 
86 
87  Y. Renard : Transformed in LDLT for stability reason.
88 
89  U=LT is stored in csr format. D is stored on the diagonal of U.
90  */
91  template <typename Matrix>
92  class ildlt_precond {
93 
94  public :
95  typedef typename linalg_traits<Matrix>::value_type value_type;
96  typedef typename number_traits<value_type>::magnitude_type magnitude_type;
97  typedef csr_matrix_ref<value_type *, size_type *, size_type *, 0> tm_type;
98 
99  tm_type U;
100 
101  protected :
102  std::vector<value_type> Tri_val;
103  std::vector<size_type> Tri_ind, Tri_ptr;
104 
105  template<typename M> void do_ildlt(const M& A, row_major);
106  void do_ildlt(const Matrix& A, col_major);
107 
108  public:
109 
110  size_type nrows(void) const { return mat_nrows(U); }
111  size_type ncols(void) const { return mat_ncols(U); }
112  value_type &D(size_type i) { return Tri_val[Tri_ptr[i]]; }
113  const value_type &D(size_type i) const { return Tri_val[Tri_ptr[i]]; }
114  ildlt_precond(void) {}
115  void build_with(const Matrix& A) {
116  Tri_ptr.resize(mat_nrows(A)+1);
117  do_ildlt(A, typename principal_orientation_type<typename
118  linalg_traits<Matrix>::sub_orientation>::potype());
119  }
120  ildlt_precond(const Matrix& A) { build_with(A); }
121  size_type memsize() const {
122  return sizeof(*this) +
123  Tri_val.size() * sizeof(value_type) +
124  (Tri_ind.size()+Tri_ptr.size()) * sizeof(size_type);
125  }
126  };
127 
128  template <typename Matrix> template<typename M>
129  void ildlt_precond<Matrix>::do_ildlt(const M& A, row_major) {
130  typedef typename linalg_traits<Matrix>::storage_type store_type;
131  typedef value_type T;
132  typedef typename number_traits<T>::magnitude_type R;
133 
134  size_type Tri_loc = 0, n = mat_nrows(A), d, g, h, i, j, k;
135  if (n == 0) return;
136  T z, zz;
137  Tri_ptr[0] = 0;
138  R prec = default_tol(R());
139  R max_pivot = gmm::abs(A(0,0)) * prec;
140 
141  for (int count = 0; count < 2; ++count) {
142  if (count) { Tri_val.resize(Tri_loc); Tri_ind.resize(Tri_loc); }
143  for (Tri_loc = 0, i = 0; i < n; ++i) {
144  typedef typename linalg_traits<M>::const_sub_row_type row_type;
145  row_type row = mat_const_row(A, i);
146  typename linalg_traits<typename org_type<row_type>::t>::const_iterator
147  it = vect_const_begin(row), ite = vect_const_end(row);
148 
149  if (count) { Tri_val[Tri_loc] = T(0); Tri_ind[Tri_loc] = i; }
150  ++Tri_loc; // diagonal element
151 
152  for (k = 0; it != ite; ++it, ++k) {
153  j = index_of_it(it, k, store_type());
154  if (i == j) {
155  if (count) Tri_val[Tri_loc-1] = *it;
156  }
157  else if (j > i) {
158  if (count) { Tri_val[Tri_loc] = *it; Tri_ind[Tri_loc]=j; }
159  ++Tri_loc;
160  }
161  }
162  Tri_ptr[i+1] = Tri_loc;
163  }
164  }
165 
166  if (A(0,0) == T(0)) {
167  Tri_val[Tri_ptr[0]] = T(1);
168  GMM_WARNING2("pivot 0 is too small");
169  }
170 
171  for (k = 0; k < n; k++) {
172  d = Tri_ptr[k];
173  z = T(gmm::real(Tri_val[d])); Tri_val[d] = z;
174  if (gmm::abs(z) <= max_pivot) {
175  Tri_val[d] = z = T(1);
176  GMM_WARNING2("pivot " << k << " is too small [" << gmm::abs(z) << "]");
177  }
178  max_pivot = std::max(max_pivot, std::min(gmm::abs(z) * prec, R(1)));
179 
180  for (i = d + 1; i < Tri_ptr[k+1]; ++i) Tri_val[i] /= z;
181  for (i = d + 1; i < Tri_ptr[k+1]; ++i) {
182  zz = gmm::conj(Tri_val[i] * z);
183  h = Tri_ind[i];
184  g = i;
185 
186  for (j = Tri_ptr[h] ; j < Tri_ptr[h+1]; ++j)
187  for ( ; g < Tri_ptr[k+1] && Tri_ind[g] <= Tri_ind[j]; ++g)
188  if (Tri_ind[g] == Tri_ind[j])
189  Tri_val[j] -= zz * Tri_val[g];
190  }
191  }
192  U = tm_type(&(Tri_val[0]), &(Tri_ind[0]), &(Tri_ptr[0]),
193  n, mat_ncols(A));
194  }
195 
196  template <typename Matrix>
197  void ildlt_precond<Matrix>::do_ildlt(const Matrix& A, col_major)
198  { do_ildlt(gmm::conjugated(A), row_major()); }
199 
200  template <typename Matrix, typename V1, typename V2> inline
201  void mult(const ildlt_precond<Matrix>& P, const V1 &v1, V2 &v2) {
202  gmm::copy(v1, v2);
203  gmm::lower_tri_solve(gmm::conjugated(P.U), v2, true);
204  for (size_type i = 0; i < mat_nrows(P.U); ++i) v2[i] /= P.D(i);
205  gmm::upper_tri_solve(P.U, v2, true);
206  }
207 
208  template <typename Matrix, typename V1, typename V2> inline
209  void transposed_mult(const ildlt_precond<Matrix>& P,const V1 &v1,V2 &v2)
210  { mult(P, v1, v2); }
211 
212  template <typename Matrix, typename V1, typename V2> inline
213  void left_mult(const ildlt_precond<Matrix>& P, const V1 &v1, V2 &v2) {
214  copy(v1, v2);
215  gmm::lower_tri_solve(gmm::conjugated(P.U), v2, true);
216  for (size_type i = 0; i < mat_nrows(P.U); ++i) v2[i] /= P.D(i);
217  }
218 
219  template <typename Matrix, typename V1, typename V2> inline
220  void right_mult(const ildlt_precond<Matrix>& P, const V1 &v1, V2 &v2)
221  { copy(v1, v2); gmm::upper_tri_solve(P.U, v2, true); }
222 
223  template <typename Matrix, typename V1, typename V2> inline
224  void transposed_left_mult(const ildlt_precond<Matrix>& P, const V1 &v1,
225  V2 &v2) {
226  copy(v1, v2);
227  gmm::upper_tri_solve(P.U, v2, true);
228  for (size_type i = 0; i < mat_nrows(P.U); ++i) v2[i] /= P.D(i);
229  }
230 
231  template <typename Matrix, typename V1, typename V2> inline
232  void transposed_right_mult(const ildlt_precond<Matrix>& P, const V1 &v1,
233  V2 &v2)
234  { copy(v1, v2); gmm::lower_tri_solve(gmm::conjugated(P.U), v2, true); }
235 
236 
237 }
238 
239 #endif
240 
Incomplete Level 0 LDLT Preconditioner.
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:976
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1663
gmm preconditioners.
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48