btPreconditioner.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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_PRECONDITIONER_H
  15. #define BT_PRECONDITIONER_H
  16. class Preconditioner
  17. {
  18. public:
  19. typedef btAlignedObjectArray<btVector3> TVStack;
  20. virtual void operator()(const TVStack& x, TVStack& b) = 0;
  21. virtual void reinitialize(bool nodeUpdated) = 0;
  22. virtual ~Preconditioner() {}
  23. };
  24. class DefaultPreconditioner : public Preconditioner
  25. {
  26. public:
  27. virtual void operator()(const TVStack& x, TVStack& b)
  28. {
  29. btAssert(b.size() == x.size());
  30. for (int i = 0; i < b.size(); ++i)
  31. b[i] = x[i];
  32. }
  33. virtual void reinitialize(bool nodeUpdated)
  34. {
  35. }
  36. virtual ~DefaultPreconditioner() {}
  37. };
  38. class MassPreconditioner : public Preconditioner
  39. {
  40. btAlignedObjectArray<btScalar> m_inv_mass;
  41. const btAlignedObjectArray<btSoftBody*>& m_softBodies;
  42. public:
  43. MassPreconditioner(const btAlignedObjectArray<btSoftBody*>& softBodies)
  44. : m_softBodies(softBodies)
  45. {
  46. }
  47. virtual void reinitialize(bool nodeUpdated)
  48. {
  49. if (nodeUpdated)
  50. {
  51. m_inv_mass.clear();
  52. for (int i = 0; i < m_softBodies.size(); ++i)
  53. {
  54. btSoftBody* psb = m_softBodies[i];
  55. for (int j = 0; j < psb->m_nodes.size(); ++j)
  56. m_inv_mass.push_back(psb->m_nodes[j].m_im);
  57. }
  58. }
  59. }
  60. virtual void operator()(const TVStack& x, TVStack& b)
  61. {
  62. btAssert(b.size() == x.size());
  63. btAssert(m_inv_mass.size() <= x.size());
  64. for (int i = 0; i < m_inv_mass.size(); ++i)
  65. {
  66. b[i] = x[i] * m_inv_mass[i];
  67. }
  68. for (int i = m_inv_mass.size(); i < b.size(); ++i)
  69. {
  70. b[i] = x[i];
  71. }
  72. }
  73. };
  74. class KKTPreconditioner : public Preconditioner
  75. {
  76. const btAlignedObjectArray<btSoftBody*>& m_softBodies;
  77. const btDeformableContactProjection& m_projections;
  78. const btAlignedObjectArray<btDeformableLagrangianForce*>& m_lf;
  79. TVStack m_inv_A, m_inv_S;
  80. const btScalar& m_dt;
  81. const bool& m_implicit;
  82. public:
  83. KKTPreconditioner(const btAlignedObjectArray<btSoftBody*>& softBodies, const btDeformableContactProjection& projections, const btAlignedObjectArray<btDeformableLagrangianForce*>& lf, const btScalar& dt, const bool& implicit)
  84. : m_softBodies(softBodies), m_projections(projections), m_lf(lf), m_dt(dt), m_implicit(implicit)
  85. {
  86. }
  87. virtual void reinitialize(bool nodeUpdated)
  88. {
  89. if (nodeUpdated)
  90. {
  91. int num_nodes = 0;
  92. for (int i = 0; i < m_softBodies.size(); ++i)
  93. {
  94. btSoftBody* psb = m_softBodies[i];
  95. num_nodes += psb->m_nodes.size();
  96. }
  97. m_inv_A.resize(num_nodes);
  98. }
  99. buildDiagonalA(m_inv_A);
  100. for (int i = 0; i < m_inv_A.size(); ++i)
  101. {
  102. // printf("A[%d] = %f, %f, %f \n", i, m_inv_A[i][0], m_inv_A[i][1], m_inv_A[i][2]);
  103. for (int d = 0; d < 3; ++d)
  104. {
  105. m_inv_A[i][d] = (m_inv_A[i][d] == 0) ? 0.0 : 1.0 / m_inv_A[i][d];
  106. }
  107. }
  108. m_inv_S.resize(m_projections.m_lagrangeMultipliers.size());
  109. // printf("S.size() = %d \n", m_inv_S.size());
  110. buildDiagonalS(m_inv_A, m_inv_S);
  111. for (int i = 0; i < m_inv_S.size(); ++i)
  112. {
  113. // printf("S[%d] = %f, %f, %f \n", i, m_inv_S[i][0], m_inv_S[i][1], m_inv_S[i][2]);
  114. for (int d = 0; d < 3; ++d)
  115. {
  116. m_inv_S[i][d] = (m_inv_S[i][d] == 0) ? 0.0 : 1.0 / m_inv_S[i][d];
  117. }
  118. }
  119. }
  120. void buildDiagonalA(TVStack& diagA) const
  121. {
  122. size_t counter = 0;
  123. for (int i = 0; i < m_softBodies.size(); ++i)
  124. {
  125. btSoftBody* psb = m_softBodies[i];
  126. for (int j = 0; j < psb->m_nodes.size(); ++j)
  127. {
  128. const btSoftBody::Node& node = psb->m_nodes[j];
  129. diagA[counter] = (node.m_im == 0) ? btVector3(0, 0, 0) : btVector3(1.0 / node.m_im, 1.0 / node.m_im, 1.0 / node.m_im);
  130. ++counter;
  131. }
  132. }
  133. if (m_implicit)
  134. {
  135. printf("implicit not implemented\n");
  136. btAssert(false);
  137. }
  138. for (int i = 0; i < m_lf.size(); ++i)
  139. {
  140. // add damping matrix
  141. m_lf[i]->buildDampingForceDifferentialDiagonal(-m_dt, diagA);
  142. }
  143. }
  144. void buildDiagonalS(const TVStack& inv_A, TVStack& diagS)
  145. {
  146. for (int c = 0; c < m_projections.m_lagrangeMultipliers.size(); ++c)
  147. {
  148. // S[k,k] = e_k^T * C A_d^-1 C^T * e_k
  149. const LagrangeMultiplier& lm = m_projections.m_lagrangeMultipliers[c];
  150. btVector3& t = diagS[c];
  151. t.setZero();
  152. for (int j = 0; j < lm.m_num_constraints; ++j)
  153. {
  154. for (int i = 0; i < lm.m_num_nodes; ++i)
  155. {
  156. for (int d = 0; d < 3; ++d)
  157. {
  158. t[j] += inv_A[lm.m_indices[i]][d] * lm.m_dirs[j][d] * lm.m_dirs[j][d] * lm.m_weights[i] * lm.m_weights[i];
  159. }
  160. }
  161. }
  162. }
  163. }
  164. //#define USE_FULL_PRECONDITIONER
  165. #ifndef USE_FULL_PRECONDITIONER
  166. virtual void operator()(const TVStack& x, TVStack& b)
  167. {
  168. btAssert(b.size() == x.size());
  169. for (int i = 0; i < m_inv_A.size(); ++i)
  170. {
  171. b[i] = x[i] * m_inv_A[i];
  172. }
  173. int offset = m_inv_A.size();
  174. for (int i = 0; i < m_inv_S.size(); ++i)
  175. {
  176. b[i + offset] = x[i + offset] * m_inv_S[i];
  177. }
  178. }
  179. #else
  180. virtual void operator()(const TVStack& x, TVStack& b)
  181. {
  182. btAssert(b.size() == x.size());
  183. int offset = m_inv_A.size();
  184. for (int i = 0; i < m_inv_A.size(); ++i)
  185. {
  186. b[i] = x[i] * m_inv_A[i];
  187. }
  188. for (int i = 0; i < m_inv_S.size(); ++i)
  189. {
  190. b[i + offset].setZero();
  191. }
  192. for (int c = 0; c < m_projections.m_lagrangeMultipliers.size(); ++c)
  193. {
  194. const LagrangeMultiplier& lm = m_projections.m_lagrangeMultipliers[c];
  195. // C * x
  196. for (int d = 0; d < lm.m_num_constraints; ++d)
  197. {
  198. for (int i = 0; i < lm.m_num_nodes; ++i)
  199. {
  200. b[offset + c][d] += lm.m_weights[i] * b[lm.m_indices[i]].dot(lm.m_dirs[d]);
  201. }
  202. }
  203. }
  204. for (int i = 0; i < m_inv_S.size(); ++i)
  205. {
  206. b[i + offset] = b[i + offset] * m_inv_S[i];
  207. }
  208. for (int i = 0; i < m_inv_A.size(); ++i)
  209. {
  210. b[i].setZero();
  211. }
  212. for (int c = 0; c < m_projections.m_lagrangeMultipliers.size(); ++c)
  213. {
  214. // C^T * lambda
  215. const LagrangeMultiplier& lm = m_projections.m_lagrangeMultipliers[c];
  216. for (int i = 0; i < lm.m_num_nodes; ++i)
  217. {
  218. for (int j = 0; j < lm.m_num_constraints; ++j)
  219. {
  220. b[lm.m_indices[i]] += b[offset + c][j] * lm.m_weights[i] * lm.m_dirs[j];
  221. }
  222. }
  223. }
  224. for (int i = 0; i < m_inv_A.size(); ++i)
  225. {
  226. b[i] = (x[i] - b[i]) * m_inv_A[i];
  227. }
  228. TVStack t;
  229. t.resize(b.size());
  230. for (int i = 0; i < m_inv_S.size(); ++i)
  231. {
  232. t[i + offset] = x[i + offset] * m_inv_S[i];
  233. }
  234. for (int i = 0; i < m_inv_A.size(); ++i)
  235. {
  236. t[i].setZero();
  237. }
  238. for (int c = 0; c < m_projections.m_lagrangeMultipliers.size(); ++c)
  239. {
  240. // C^T * lambda
  241. const LagrangeMultiplier& lm = m_projections.m_lagrangeMultipliers[c];
  242. for (int i = 0; i < lm.m_num_nodes; ++i)
  243. {
  244. for (int j = 0; j < lm.m_num_constraints; ++j)
  245. {
  246. t[lm.m_indices[i]] += t[offset + c][j] * lm.m_weights[i] * lm.m_dirs[j];
  247. }
  248. }
  249. }
  250. for (int i = 0; i < m_inv_A.size(); ++i)
  251. {
  252. b[i] += t[i] * m_inv_A[i];
  253. }
  254. for (int i = 0; i < m_inv_S.size(); ++i)
  255. {
  256. b[i + offset] -= x[i + offset] * m_inv_S[i];
  257. }
  258. }
  259. #endif
  260. };
  261. #endif /* BT_PRECONDITIONER_H */