btConjugateResidual.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Written by Xuchen Han <xuchenhan2015@u.northwestern.edu>
  3. Bullet Continuous Collision Detection and Physics Library
  4. Copyright (c) 2019 Google Inc. http://bulletphysics.org
  5. This software is provided 'as-is', without any express or implied warranty.
  6. In no event will the authors be held liable for any damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it freely,
  9. subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  11. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  12. 3. This notice may not be removed or altered from any source distribution.
  13. */
  14. #ifndef BT_CONJUGATE_RESIDUAL_H
  15. #define BT_CONJUGATE_RESIDUAL_H
  16. #include "btKrylovSolver.h"
  17. template <class MatrixX>
  18. class btConjugateResidual : public btKrylovSolver<MatrixX>
  19. {
  20. typedef btAlignedObjectArray<btVector3> TVStack;
  21. typedef btKrylovSolver<MatrixX> Base;
  22. TVStack r, p, z, temp_p, temp_r, best_x;
  23. // temp_r = A*r
  24. // temp_p = A*p
  25. // z = M^(-1) * temp_p = M^(-1) * A * p
  26. btScalar best_r;
  27. public:
  28. btConjugateResidual(const int max_it_in)
  29. : Base(max_it_in, 1e-8)
  30. {
  31. }
  32. virtual ~btConjugateResidual() {}
  33. // return the number of iterations taken
  34. int solve(MatrixX& A, TVStack& x, const TVStack& b, bool verbose = false)
  35. {
  36. BT_PROFILE("CRSolve");
  37. btAssert(x.size() == b.size());
  38. reinitialize(b);
  39. // r = b - A * x --with assigned dof zeroed out
  40. A.multiply(x, temp_r); // borrow temp_r here to store A*x
  41. r = this->sub(b, temp_r);
  42. // z = M^(-1) * r
  43. A.precondition(r, z); // borrow z to store preconditioned r
  44. r = z;
  45. btScalar residual_norm = this->norm(r);
  46. if (residual_norm <= Base::m_tolerance)
  47. {
  48. return 0;
  49. }
  50. p = r;
  51. btScalar r_dot_Ar, r_dot_Ar_new;
  52. // temp_p = A*p
  53. A.multiply(p, temp_p);
  54. // temp_r = A*r
  55. temp_r = temp_p;
  56. r_dot_Ar = this->dot(r, temp_r);
  57. for (int k = 1; k <= Base::m_maxIterations; k++)
  58. {
  59. // z = M^(-1) * Ap
  60. A.precondition(temp_p, z);
  61. // alpha = r^T * A * r / (Ap)^T * M^-1 * Ap)
  62. btScalar alpha = r_dot_Ar / this->dot(temp_p, z);
  63. // x += alpha * p;
  64. this->multAndAddTo(alpha, p, x);
  65. // r -= alpha * z;
  66. this->multAndAddTo(-alpha, z, r);
  67. btScalar norm_r = this->norm(r);
  68. if (norm_r < best_r)
  69. {
  70. best_x = x;
  71. best_r = norm_r;
  72. if (norm_r < Base::m_tolerance)
  73. {
  74. return k;
  75. }
  76. }
  77. // temp_r = A * r;
  78. A.multiply(r, temp_r);
  79. r_dot_Ar_new = this->dot(r, temp_r);
  80. btScalar beta = r_dot_Ar_new / r_dot_Ar;
  81. r_dot_Ar = r_dot_Ar_new;
  82. // p = beta*p + r;
  83. p = this->multAndAdd(beta, p, r);
  84. // temp_p = beta*temp_p + temp_r;
  85. temp_p = this->multAndAdd(beta, temp_p, temp_r);
  86. }
  87. if (verbose)
  88. {
  89. std::cout << "ConjugateResidual max iterations reached, residual = " << best_r << std::endl;
  90. }
  91. x = best_x;
  92. return Base::m_maxIterations;
  93. }
  94. void reinitialize(const TVStack& b)
  95. {
  96. r.resize(b.size());
  97. p.resize(b.size());
  98. z.resize(b.size());
  99. temp_p.resize(b.size());
  100. temp_r.resize(b.size());
  101. best_x.resize(b.size());
  102. best_r = SIMD_INFINITY;
  103. }
  104. };
  105. #endif /* btConjugateResidual_h */