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.

160 lines
6.7 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-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. // This file is a modified version of bicgstab.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_bicgstab.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 BiCGStab iterative solver.
  63. */
  64. #ifndef GMM_SOLVER_BICGSTAB_H__
  65. #define GMM_SOLVER_BICGSTAB_H__
  66. #include "gmm_kernel.h"
  67. #include "gmm_iter.h"
  68. namespace gmm {
  69. /* ******************************************************************** */
  70. /* BiConjugate Gradient Stabilized */
  71. /* (preconditionned, with parametrable scalar product) */
  72. /* ******************************************************************** */
  73. template <typename Matrix, typename Vector, typename VectorB,
  74. typename Preconditioner>
  75. void bicgstab(const Matrix& A, Vector& x, const VectorB& b,
  76. const Preconditioner& M, iteration &iter) {
  77. typedef typename linalg_traits<Vector>::value_type T;
  78. typedef typename number_traits<T>::magnitude_type R;
  79. typedef typename temporary_dense_vector<Vector>::vector_type temp_vector;
  80. T rho_1, rho_2(0), alpha(0), beta, omega(0);
  81. temp_vector p(vect_size(x)), phat(vect_size(x)), s(vect_size(x)),
  82. shat(vect_size(x)),
  83. t(vect_size(x)), v(vect_size(x)), r(vect_size(x)), rtilde(vect_size(x));
  84. gmm::mult(A, gmm::scaled(x, -T(1)), b, r);
  85. gmm::copy(r, rtilde);
  86. R norm_r = gmm::vect_norm2(r);
  87. iter.set_rhsnorm(gmm::vect_norm2(b));
  88. if (iter.get_rhsnorm() == 0.0) { clear(x); return; }
  89. while (!iter.finished(norm_r)) {
  90. rho_1 = gmm::vect_sp(rtilde, r);
  91. if (rho_1 == T(0)) {
  92. if (iter.get_maxiter() == size_type(-1))
  93. { GMM_ASSERT1(false, "Bicgstab failed to converge"); }
  94. else { GMM_WARNING1("Bicgstab failed to converge"); return; }
  95. }
  96. if (iter.first())
  97. gmm::copy(r, p);
  98. else {
  99. if (omega == T(0)) {
  100. if (iter.get_maxiter() == size_type(-1))
  101. { GMM_ASSERT1(false, "Bicgstab failed to converge"); }
  102. else { GMM_WARNING1("Bicgstab failed to converge"); return; }
  103. }
  104. beta = (rho_1 / rho_2) * (alpha / omega);
  105. gmm::add(gmm::scaled(v, -omega), p);
  106. gmm::add(r, gmm::scaled(p, beta), p);
  107. }
  108. gmm::mult(M, p, phat);
  109. gmm::mult(A, phat, v);
  110. alpha = rho_1 / gmm::vect_sp(v, rtilde);
  111. gmm::add(r, gmm::scaled(v, -alpha), s);
  112. if (iter.finished_vect(s))
  113. { gmm::add(gmm::scaled(phat, alpha), x); break; }
  114. gmm::mult(M, s, shat);
  115. gmm::mult(A, shat, t);
  116. omega = gmm::vect_sp(t, s) / gmm::vect_norm2_sqr(t);
  117. gmm::add(gmm::scaled(phat, alpha), x);
  118. gmm::add(gmm::scaled(shat, omega), x);
  119. gmm::add(s, gmm::scaled(t, -omega), r);
  120. norm_r = gmm::vect_norm2(r);
  121. rho_2 = rho_1;
  122. ++iter;
  123. }
  124. }
  125. template <typename Matrix, typename Vector, typename VectorB,
  126. typename Preconditioner>
  127. void bicgstab(const Matrix& A, const Vector& x, const VectorB& b,
  128. const Preconditioner& M, iteration &iter)
  129. { bicgstab(A, linalg_const_cast(x), b, M, iter); }
  130. }
  131. #endif // GMM_SOLVER_BICGSTAB_H__