btConjugateGradient.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_GRADIENT_H
  15. #define BT_CONJUGATE_GRADIENT_H
  16. #include "btKrylovSolver.h"
  17. template <class MatrixX>
  18. class btConjugateGradient : public btKrylovSolver<MatrixX>
  19. {
  20. typedef btAlignedObjectArray<btVector3> TVStack;
  21. typedef btKrylovSolver<MatrixX> Base;
  22. TVStack r, p, z, temp;
  23. public:
  24. btConjugateGradient(const int max_it_in)
  25. : btKrylovSolver<MatrixX>(max_it_in, SIMD_EPSILON)
  26. {
  27. }
  28. virtual ~btConjugateGradient() {}
  29. // return the number of iterations taken
  30. int solve(MatrixX& A, TVStack& x, const TVStack& b, bool verbose = false)
  31. {
  32. BT_PROFILE("CGSolve");
  33. btAssert(x.size() == b.size());
  34. reinitialize(b);
  35. temp = b;
  36. A.project(temp);
  37. p = temp;
  38. A.precondition(p, z);
  39. btScalar d0 = this->dot(z, temp);
  40. d0 = btMin(btScalar(1), d0);
  41. // r = b - A * x --with assigned dof zeroed out
  42. A.multiply(x, temp);
  43. r = this->sub(b, temp);
  44. A.project(r);
  45. // z = M^(-1) * r
  46. A.precondition(r, z);
  47. A.project(z);
  48. btScalar r_dot_z = this->dot(z, r);
  49. if (r_dot_z <= Base::m_tolerance * d0)
  50. {
  51. if (verbose)
  52. {
  53. std::cout << "Iteration = 0" << std::endl;
  54. std::cout << "Two norm of the residual = " << r_dot_z << std::endl;
  55. }
  56. return 0;
  57. }
  58. p = z;
  59. btScalar r_dot_z_new = r_dot_z;
  60. for (int k = 1; k <= Base::m_maxIterations; k++)
  61. {
  62. // temp = A*p
  63. A.multiply(p, temp);
  64. A.project(temp);
  65. if (this->dot(p, temp) < 0)
  66. {
  67. if (verbose)
  68. std::cout << "Encountered negative direction in CG!" << std::endl;
  69. if (k == 1)
  70. {
  71. x = b;
  72. }
  73. return k;
  74. }
  75. // alpha = r^T * z / (p^T * A * p)
  76. btScalar alpha = r_dot_z_new / this->dot(p, temp);
  77. // x += alpha * p;
  78. this->multAndAddTo(alpha, p, x);
  79. // r -= alpha * temp;
  80. this->multAndAddTo(-alpha, temp, r);
  81. // z = M^(-1) * r
  82. A.precondition(r, z);
  83. r_dot_z = r_dot_z_new;
  84. r_dot_z_new = this->dot(r, z);
  85. if (r_dot_z_new < Base::m_tolerance * d0)
  86. {
  87. if (verbose)
  88. {
  89. std::cout << "ConjugateGradient iterations " << k << " residual = " << r_dot_z_new << std::endl;
  90. }
  91. return k;
  92. }
  93. btScalar beta = r_dot_z_new / r_dot_z;
  94. p = this->multAndAdd(beta, p, z);
  95. }
  96. if (verbose)
  97. {
  98. std::cout << "ConjugateGradient max iterations reached " << Base::m_maxIterations << " error = " << r_dot_z_new << std::endl;
  99. }
  100. return Base::m_maxIterations;
  101. }
  102. void reinitialize(const TVStack& b)
  103. {
  104. r.resize(b.size());
  105. p.resize(b.size());
  106. z.resize(b.size());
  107. temp.resize(b.size());
  108. }
  109. };
  110. #endif /* btConjugateGradient_h */