GetFEM  5.4.3
gmm_solver_cg.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2002-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 // This file is a modified version of cg.h from ITL.
33 // See http://osl.iu.edu/research/itl/
34 // Following the corresponding Copyright notice.
35 //===========================================================================
36 //
37 // Copyright (c) 1998-2020, University of Notre Dame. All rights reserved.
38 // Redistribution and use in source and binary forms, with or without
39 // modification, are permitted provided that the following conditions are met:
40 //
41 // * Redistributions of source code must retain the above copyright
42 // notice, this list of conditions and the following disclaimer.
43 // * Redistributions in binary form must reproduce the above copyright
44 // notice, this list of conditions and the following disclaimer in the
45 // documentation and/or other materials provided with the distribution.
46 // * Neither the name of the University of Notre Dame nor the
47 // names of its contributors may be used to endorse or promote products
48 // derived from this software without specific prior written permission.
49 //
50 // THIS SOFTWARE IS PROVIDED BY THE TRUSTEES OF INDIANA UNIVERSITY AND
51 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
52 // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
53 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES
54 // OF INDIANA UNIVERSITY AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
55 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 //
62 //===========================================================================
63 
64 /**@file gmm_solver_cg.h
65  @author Andrew Lumsdaine <[email protected]>
66  @author Lie-Quan Lee <[email protected]>
67  @author Yves Renard <[email protected]>
68  @date October 13, 2002.
69  @brief Conjugate gradient iterative solver.
70 */
71 #ifndef GMM_SOLVER_CG_H__
72 #define GMM_SOLVER_CG_H__
73 
74 #include "gmm_kernel.h"
75 #include "gmm_iter.h"
76 
77 namespace gmm {
78 
79  /* ******************************************************************** */
80  /* conjugate gradient */
81  /* (preconditionned, with parametrable additional scalar product) */
82  /* ******************************************************************** */
83 
84  template <typename Matrix, typename Matps, typename Precond,
85  typename Vector1, typename Vector2>
86  void cg(const Matrix& A, Vector1& x, const Vector2& b, const Matps& PS,
87  const Precond &P, iteration &iter) {
88 
89  typedef typename temporary_dense_vector<Vector1>::vector_type temp_vector;
90  typedef typename linalg_traits<Vector1>::value_type T;
91 
92  T rho, rho_1(0), a;
93  temp_vector p(vect_size(x)), q(vect_size(x)), r(vect_size(x)),
94  z(vect_size(x));
95  iter.set_rhsnorm(gmm::sqrt(gmm::abs(vect_hp(PS, b, b))));
96 
97  if (iter.get_rhsnorm() == 0.0)
98  clear(x);
99  else {
100  mult(A, scaled(x, T(-1)), b, r);
101  mult(P, r, z);
102  rho = vect_hp(PS, z, r);
103  copy(z, p);
104 
105  while (!iter.finished_vect(r)) {
106 
107  if (!iter.first()) {
108  mult(P, r, z);
109  rho = vect_hp(PS, z, r);
110  add(z, scaled(p, rho / rho_1), p);
111  }
112  mult(A, p, q);
113 
114  a = rho / vect_hp(PS, q, p);
115  add(scaled(p, a), x);
116  add(scaled(q, -a), r);
117  rho_1 = rho;
118 
119  ++iter;
120  }
121  }
122  }
123 
124  template <typename Matrix, typename Matps, typename Precond,
125  typename Vector1, typename Vector2>
126  void cg(const Matrix& A, Vector1& x, const Vector2& b, const Matps& PS,
127  const gmm::identity_matrix &, iteration &iter) {
128 
129  typedef typename temporary_dense_vector<Vector1>::vector_type temp_vector;
130  typedef typename linalg_traits<Vector1>::value_type T;
131 
132  T rho, rho_1(0), a;
133  temp_vector p(vect_size(x)), q(vect_size(x)), r(vect_size(x));
134  iter.set_rhsnorm(gmm::sqrt(gmm::abs(vect_hp(PS, b, b))));
135 
136  if (iter.get_rhsnorm() == 0.0)
137  clear(x);
138  else {
139  mult(A, scaled(x, T(-1)), b, r);
140  rho = vect_hp(PS, r, r);
141  copy(r, p);
142 
143  while (!iter.finished_vect(r)) {
144 
145  if (!iter.first()) {
146  rho = vect_hp(PS, r, r);
147  add(r, scaled(p, rho / rho_1), p);
148  }
149  mult(A, p, q);
150  a = rho / vect_hp(PS, q, p);
151  add(scaled(p, a), x);
152  add(scaled(q, -a), r);
153  rho_1 = rho;
154  ++iter;
155  }
156  }
157  }
158 
159  template <typename Matrix, typename Matps, typename Precond,
160  typename Vector1, typename Vector2> inline
161  void cg(const Matrix& A, const Vector1& x, const Vector2& b, const Matps& PS,
162  const Precond &P, iteration &iter)
163  { cg(A, linalg_const_cast(x), b, PS, P, iter); }
164 
165  template <typename Matrix, typename Precond,
166  typename Vector1, typename Vector2> inline
167  void cg(const Matrix& A, Vector1& x, const Vector2& b,
168  const Precond &P, iteration &iter)
169  { cg(A, x , b, identity_matrix(), P, iter); }
170 
171  template <typename Matrix, typename Precond,
172  typename Vector1, typename Vector2> inline
173  void cg(const Matrix& A, const Vector1& x, const Vector2& b,
174  const Precond &P, iteration &iter)
175  { cg(A, x , b , identity_matrix(), P , iter); }
176 
177 }
178 
179 
180 #endif // GMM_SOLVER_CG_H__
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:978
strongest_value_type< V1, V2 >::value_type vect_hp(const V1 &v1, const V2 &v2)
*‍/
Definition: gmm_blas.h:512
void clear(L &l)
clear (fill with zeros) a vector or matrix.
Definition: gmm_blas.h:59
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1664
void add(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:1277
Iteration object.
Include the base gmm files.