btDeformableBackwardEulerObjective.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #include "btDeformableBackwardEulerObjective.h"
  15. #include "btPreconditioner.h"
  16. #include "LinearMath/btQuickprof.h"
  17. btDeformableBackwardEulerObjective::btDeformableBackwardEulerObjective(btAlignedObjectArray<btSoftBody*>& softBodies, const TVStack& backup_v)
  18. : m_softBodies(softBodies), m_projection(softBodies), m_backupVelocity(backup_v), m_implicit(false)
  19. {
  20. m_massPreconditioner = new MassPreconditioner(m_softBodies);
  21. m_KKTPreconditioner = new KKTPreconditioner(m_softBodies, m_projection, m_lf, m_dt, m_implicit);
  22. m_preconditioner = m_KKTPreconditioner;
  23. }
  24. btDeformableBackwardEulerObjective::~btDeformableBackwardEulerObjective()
  25. {
  26. delete m_KKTPreconditioner;
  27. delete m_massPreconditioner;
  28. }
  29. void btDeformableBackwardEulerObjective::reinitialize(bool nodeUpdated, btScalar dt)
  30. {
  31. BT_PROFILE("reinitialize");
  32. if (dt > 0)
  33. {
  34. setDt(dt);
  35. }
  36. if (nodeUpdated)
  37. {
  38. updateId();
  39. }
  40. for (int i = 0; i < m_lf.size(); ++i)
  41. {
  42. m_lf[i]->reinitialize(nodeUpdated);
  43. }
  44. btMatrix3x3 I;
  45. I.setIdentity();
  46. for (int i = 0; i < m_softBodies.size(); ++i)
  47. {
  48. btSoftBody* psb = m_softBodies[i];
  49. for (int j = 0; j < psb->m_nodes.size(); ++j)
  50. {
  51. if (psb->m_nodes[j].m_im > 0)
  52. psb->m_nodes[j].m_effectiveMass = I * (1.0 / psb->m_nodes[j].m_im);
  53. }
  54. }
  55. m_projection.reinitialize(nodeUpdated);
  56. // m_preconditioner->reinitialize(nodeUpdated);
  57. }
  58. void btDeformableBackwardEulerObjective::setDt(btScalar dt)
  59. {
  60. m_dt = dt;
  61. }
  62. void btDeformableBackwardEulerObjective::multiply(const TVStack& x, TVStack& b) const
  63. {
  64. BT_PROFILE("multiply");
  65. // add in the mass term
  66. size_t counter = 0;
  67. for (int i = 0; i < m_softBodies.size(); ++i)
  68. {
  69. btSoftBody* psb = m_softBodies[i];
  70. for (int j = 0; j < psb->m_nodes.size(); ++j)
  71. {
  72. const btSoftBody::Node& node = psb->m_nodes[j];
  73. b[counter] = (node.m_im == 0) ? btVector3(0, 0, 0) : x[counter] / node.m_im;
  74. ++counter;
  75. }
  76. }
  77. for (int i = 0; i < m_lf.size(); ++i)
  78. {
  79. // add damping matrix
  80. m_lf[i]->addScaledDampingForceDifferential(-m_dt, x, b);
  81. // Always integrate picking force implicitly for stability.
  82. if (m_implicit || m_lf[i]->getForceType() == BT_MOUSE_PICKING_FORCE)
  83. {
  84. m_lf[i]->addScaledElasticForceDifferential(-m_dt * m_dt, x, b);
  85. }
  86. }
  87. int offset = m_nodes.size();
  88. for (int i = offset; i < b.size(); ++i)
  89. {
  90. b[i].setZero();
  91. }
  92. // add in the lagrange multiplier terms
  93. for (int c = 0; c < m_projection.m_lagrangeMultipliers.size(); ++c)
  94. {
  95. // C^T * lambda
  96. const LagrangeMultiplier& lm = m_projection.m_lagrangeMultipliers[c];
  97. for (int i = 0; i < lm.m_num_nodes; ++i)
  98. {
  99. for (int j = 0; j < lm.m_num_constraints; ++j)
  100. {
  101. b[lm.m_indices[i]] += x[offset + c][j] * lm.m_weights[i] * lm.m_dirs[j];
  102. }
  103. }
  104. // C * x
  105. for (int d = 0; d < lm.m_num_constraints; ++d)
  106. {
  107. for (int i = 0; i < lm.m_num_nodes; ++i)
  108. {
  109. b[offset + c][d] += lm.m_weights[i] * x[lm.m_indices[i]].dot(lm.m_dirs[d]);
  110. }
  111. }
  112. }
  113. }
  114. void btDeformableBackwardEulerObjective::updateVelocity(const TVStack& dv)
  115. {
  116. for (int i = 0; i < m_softBodies.size(); ++i)
  117. {
  118. btSoftBody* psb = m_softBodies[i];
  119. for (int j = 0; j < psb->m_nodes.size(); ++j)
  120. {
  121. btSoftBody::Node& node = psb->m_nodes[j];
  122. node.m_v = m_backupVelocity[node.index] + dv[node.index];
  123. }
  124. }
  125. }
  126. void btDeformableBackwardEulerObjective::applyForce(TVStack& force, bool setZero)
  127. {
  128. size_t counter = 0;
  129. for (int i = 0; i < m_softBodies.size(); ++i)
  130. {
  131. btSoftBody* psb = m_softBodies[i];
  132. if (!psb->isActive())
  133. {
  134. counter += psb->m_nodes.size();
  135. continue;
  136. }
  137. if (m_implicit)
  138. {
  139. for (int j = 0; j < psb->m_nodes.size(); ++j)
  140. {
  141. if (psb->m_nodes[j].m_im != 0)
  142. {
  143. psb->m_nodes[j].m_v += psb->m_nodes[j].m_effectiveMass_inv * force[counter++];
  144. }
  145. }
  146. }
  147. else
  148. {
  149. for (int j = 0; j < psb->m_nodes.size(); ++j)
  150. {
  151. btScalar one_over_mass = (psb->m_nodes[j].m_im == 0) ? 0 : psb->m_nodes[j].m_im;
  152. psb->m_nodes[j].m_v += one_over_mass * force[counter++];
  153. }
  154. }
  155. }
  156. if (setZero)
  157. {
  158. for (int i = 0; i < force.size(); ++i)
  159. force[i].setZero();
  160. }
  161. }
  162. void btDeformableBackwardEulerObjective::computeResidual(btScalar dt, TVStack& residual)
  163. {
  164. BT_PROFILE("computeResidual");
  165. // add implicit force
  166. for (int i = 0; i < m_lf.size(); ++i)
  167. {
  168. // Always integrate picking force implicitly for stability.
  169. if (m_implicit || m_lf[i]->getForceType() == BT_MOUSE_PICKING_FORCE)
  170. {
  171. m_lf[i]->addScaledForces(dt, residual);
  172. }
  173. else
  174. {
  175. m_lf[i]->addScaledDampingForce(dt, residual);
  176. }
  177. }
  178. // m_projection.project(residual);
  179. }
  180. btScalar btDeformableBackwardEulerObjective::computeNorm(const TVStack& residual) const
  181. {
  182. btScalar mag = 0;
  183. for (int i = 0; i < residual.size(); ++i)
  184. {
  185. mag += residual[i].length2();
  186. }
  187. return std::sqrt(mag);
  188. }
  189. btScalar btDeformableBackwardEulerObjective::totalEnergy(btScalar dt)
  190. {
  191. btScalar e = 0;
  192. for (int i = 0; i < m_lf.size(); ++i)
  193. {
  194. e += m_lf[i]->totalEnergy(dt);
  195. }
  196. return e;
  197. }
  198. void btDeformableBackwardEulerObjective::applyExplicitForce(TVStack& force)
  199. {
  200. for (int i = 0; i < m_softBodies.size(); ++i)
  201. {
  202. m_softBodies[i]->advanceDeformation();
  203. }
  204. if (m_implicit)
  205. {
  206. // apply forces except gravity force
  207. btVector3 gravity;
  208. for (int i = 0; i < m_lf.size(); ++i)
  209. {
  210. if (m_lf[i]->getForceType() == BT_GRAVITY_FORCE)
  211. {
  212. gravity = static_cast<btDeformableGravityForce*>(m_lf[i])->m_gravity;
  213. }
  214. else
  215. {
  216. m_lf[i]->addScaledForces(m_dt, force);
  217. }
  218. }
  219. for (int i = 0; i < m_lf.size(); ++i)
  220. {
  221. m_lf[i]->addScaledHessian(m_dt);
  222. }
  223. for (int i = 0; i < m_softBodies.size(); ++i)
  224. {
  225. btSoftBody* psb = m_softBodies[i];
  226. if (psb->isActive())
  227. {
  228. for (int j = 0; j < psb->m_nodes.size(); ++j)
  229. {
  230. // add gravity explicitly
  231. psb->m_nodes[j].m_v += m_dt * psb->m_gravityFactor * gravity;
  232. }
  233. }
  234. }
  235. }
  236. else
  237. {
  238. for (int i = 0; i < m_lf.size(); ++i)
  239. {
  240. m_lf[i]->addScaledExplicitForce(m_dt, force);
  241. }
  242. }
  243. // calculate inverse mass matrix for all nodes
  244. for (int i = 0; i < m_softBodies.size(); ++i)
  245. {
  246. btSoftBody* psb = m_softBodies[i];
  247. if (psb->isActive())
  248. {
  249. for (int j = 0; j < psb->m_nodes.size(); ++j)
  250. {
  251. if (psb->m_nodes[j].m_im > 0)
  252. {
  253. psb->m_nodes[j].m_effectiveMass_inv = psb->m_nodes[j].m_effectiveMass.inverse();
  254. }
  255. }
  256. }
  257. }
  258. applyForce(force, true);
  259. }
  260. void btDeformableBackwardEulerObjective::initialGuess(TVStack& dv, const TVStack& residual)
  261. {
  262. size_t counter = 0;
  263. for (int i = 0; i < m_softBodies.size(); ++i)
  264. {
  265. btSoftBody* psb = m_softBodies[i];
  266. for (int j = 0; j < psb->m_nodes.size(); ++j)
  267. {
  268. dv[counter] = psb->m_nodes[j].m_im * residual[counter];
  269. ++counter;
  270. }
  271. }
  272. }
  273. //set constraints as projections
  274. void btDeformableBackwardEulerObjective::setConstraints(const btContactSolverInfo& infoGlobal)
  275. {
  276. m_projection.setConstraints(infoGlobal);
  277. }
  278. void btDeformableBackwardEulerObjective::applyDynamicFriction(TVStack& r)
  279. {
  280. m_projection.applyDynamicFriction(r);
  281. }