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.

180 lines
7.0 KiB

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