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.

165 lines
5.9 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. /**@file gmm_solver_constrained_cg.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date October 13, 2002.
  29. @brief Constrained conjugate gradient. */
  30. // preconditionning does not work
  31. #ifndef GMM_SOLVER_CCG_H__
  32. #define GMM_SOLVER_CCG_H__
  33. #include "gmm_kernel.h"
  34. #include "gmm_iter.h"
  35. namespace gmm {
  36. template <typename CMatrix, typename CINVMatrix, typename Matps,
  37. typename VectorX>
  38. void pseudo_inverse(const CMatrix &C, CINVMatrix &CINV,
  39. const Matps& /* PS */, VectorX&) {
  40. // compute the pseudo inverse of the non-square matrix C such
  41. // CINV = inv(C * trans(C)) * C.
  42. // based on a conjugate gradient method.
  43. // optimisable : copie de la ligne, precalcul de C * trans(C).
  44. typedef VectorX TmpVec;
  45. typedef typename linalg_traits<VectorX>::value_type value_type;
  46. size_type nr = mat_nrows(C), nc = mat_ncols(C);
  47. TmpVec d(nr), e(nr), l(nc), p(nr), q(nr), r(nr);
  48. value_type rho, rho_1, alpha;
  49. clear(d);
  50. clear(CINV);
  51. for (size_type i = 0; i < nr; ++i) {
  52. d[i] = 1.0; rho = 1.0;
  53. clear(e);
  54. copy(d, r);
  55. copy(d, p);
  56. while (rho >= 1E-38) { /* conjugate gradient to compute e */
  57. /* which is the i nd row of inv(C * trans(C)) */
  58. mult(gmm::transposed(C), p, l);
  59. mult(C, l, q);
  60. alpha = rho / vect_sp(p, q);
  61. add(scaled(p, alpha), e);
  62. add(scaled(q, -alpha), r);
  63. rho_1 = rho;
  64. rho = vect_sp(r, r);
  65. add(r, scaled(p, rho / rho_1), p);
  66. }
  67. mult(transposed(C), e, l); /* l is the i nd row of CINV */
  68. // cout << "l = " << l << endl;
  69. clean(l, 1E-15);
  70. copy(l, mat_row(CINV, i));
  71. d[i] = 0.0;
  72. }
  73. }
  74. /** Compute the minimum of @f$ 1/2((Ax).x) - bx @f$ under the contraint @f$ Cx <= f @f$ */
  75. template < typename Matrix, typename CMatrix, typename Matps,
  76. typename VectorX, typename VectorB, typename VectorF,
  77. typename Preconditioner >
  78. void constrained_cg(const Matrix& A, const CMatrix& C, VectorX& x,
  79. const VectorB& b, const VectorF& f,const Matps& PS,
  80. const Preconditioner& M, iteration &iter) {
  81. typedef typename temporary_dense_vector<VectorX>::vector_type TmpVec;
  82. typedef typename temporary_vector<CMatrix>::vector_type TmpCVec;
  83. typedef row_matrix<TmpCVec> TmpCmat;
  84. typedef typename linalg_traits<VectorX>::value_type value_type;
  85. value_type rho = 1.0, rho_1, lambda, gamma;
  86. TmpVec p(vect_size(x)), q(vect_size(x)), q2(vect_size(x)),
  87. r(vect_size(x)), old_z(vect_size(x)), z(vect_size(x)),
  88. memox(vect_size(x));
  89. std::vector<bool> satured(mat_nrows(C));
  90. clear(p);
  91. iter.set_rhsnorm(sqrt(vect_sp(PS, b, b)));
  92. if (iter.get_rhsnorm() == 0.0) iter.set_rhsnorm(1.0);
  93. TmpCmat CINV(mat_nrows(C), mat_ncols(C));
  94. pseudo_inverse(C, CINV, PS, x);
  95. while(true) {
  96. // computation of residu
  97. copy(z, old_z);
  98. copy(x, memox);
  99. mult(A, scaled(x, -1.0), b, r);
  100. mult(M, r, z); // preconditionner not coherent
  101. bool transition = false;
  102. for (size_type i = 0; i < mat_nrows(C); ++i) {
  103. value_type al = vect_sp(mat_row(C, i), x) - f[i];
  104. if (al >= -1.0E-15) {
  105. if (!satured[i]) { satured[i] = true; transition = true; }
  106. value_type bb = vect_sp(mat_row(CINV, i), z);
  107. if (bb > 0.0) add(scaled(mat_row(C, i), -bb), z);
  108. }
  109. else
  110. satured[i] = false;
  111. }
  112. // descent direction
  113. rho_1 = rho; rho = vect_sp(PS, r, z); // ...
  114. if (iter.finished(rho)) break;
  115. if (iter.get_noisy() > 0 && transition) std::cout << "transition\n";
  116. if (transition || iter.first()) gamma = 0.0;
  117. else gamma = std::max(0.0, (rho - vect_sp(PS, old_z, z) ) / rho_1);
  118. // std::cout << "gamma = " << gamma << endl;
  119. // itl::add(r, itl::scaled(p, gamma), p);
  120. add(z, scaled(p, gamma), p); // ...
  121. ++iter;
  122. // one dimensionnal optimization
  123. mult(A, p, q);
  124. lambda = rho / vect_sp(PS, q, p);
  125. for (size_type i = 0; i < mat_nrows(C); ++i)
  126. if (!satured[i]) {
  127. value_type bb = vect_sp(mat_row(C, i), p) - f[i];
  128. if (bb > 0.0)
  129. lambda = std::min(lambda, (f[i]-vect_sp(mat_row(C, i), x)) / bb);
  130. }
  131. add(x, scaled(p, lambda), x);
  132. add(memox, scaled(x, -1.0), memox);
  133. }
  134. }
  135. }
  136. #endif // GMM_SOLVER_CCG_H__