btDeformableBodySolver.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 <stdio.h>
  15. #include <limits>
  16. #include "btDeformableBodySolver.h"
  17. #include "btSoftBodyInternals.h"
  18. #include "LinearMath/btQuickprof.h"
  19. static const int kMaxConjugateGradientIterations = 300;
  20. btDeformableBodySolver::btDeformableBodySolver()
  21. : m_numNodes(0), m_cg(kMaxConjugateGradientIterations), m_cr(kMaxConjugateGradientIterations), m_maxNewtonIterations(1), m_newtonTolerance(1e-4), m_lineSearch(false), m_useProjection(false)
  22. {
  23. m_objective = new btDeformableBackwardEulerObjective(m_softBodies, m_backupVelocity);
  24. }
  25. btDeformableBodySolver::~btDeformableBodySolver()
  26. {
  27. delete m_objective;
  28. }
  29. void btDeformableBodySolver::solveDeformableConstraints(btScalar solverdt)
  30. {
  31. BT_PROFILE("solveDeformableConstraints");
  32. if (!m_implicit)
  33. {
  34. m_objective->computeResidual(solverdt, m_residual);
  35. m_objective->applyDynamicFriction(m_residual);
  36. if (m_useProjection)
  37. {
  38. computeStep(m_dv, m_residual);
  39. }
  40. else
  41. {
  42. TVStack rhs, x;
  43. m_objective->addLagrangeMultiplierRHS(m_residual, m_dv, rhs);
  44. m_objective->addLagrangeMultiplier(m_dv, x);
  45. m_objective->m_preconditioner->reinitialize(true);
  46. computeStep(x, rhs);
  47. for (int i = 0; i < m_dv.size(); ++i)
  48. {
  49. m_dv[i] = x[i];
  50. }
  51. }
  52. updateVelocity();
  53. }
  54. else
  55. {
  56. for (int i = 0; i < m_maxNewtonIterations; ++i)
  57. {
  58. updateState();
  59. // add the inertia term in the residual
  60. int counter = 0;
  61. for (int k = 0; k < m_softBodies.size(); ++k)
  62. {
  63. btSoftBody* psb = m_softBodies[k];
  64. for (int j = 0; j < psb->m_nodes.size(); ++j)
  65. {
  66. if (psb->m_nodes[j].m_im > 0)
  67. {
  68. m_residual[counter] = (-1. / psb->m_nodes[j].m_im) * m_dv[counter];
  69. }
  70. ++counter;
  71. }
  72. }
  73. m_objective->computeResidual(solverdt, m_residual);
  74. if (m_objective->computeNorm(m_residual) < m_newtonTolerance && i > 0)
  75. {
  76. break;
  77. }
  78. // todo xuchenhan@: this really only needs to be calculated once
  79. m_objective->applyDynamicFriction(m_residual);
  80. if (m_lineSearch)
  81. {
  82. btScalar inner_product = computeDescentStep(m_ddv, m_residual);
  83. btScalar alpha = 0.01, beta = 0.5; // Boyd & Vandenberghe suggested alpha between 0.01 and 0.3, beta between 0.1 to 0.8
  84. btScalar scale = 2;
  85. btScalar f0 = m_objective->totalEnergy(solverdt) + kineticEnergy(), f1, f2;
  86. backupDv();
  87. do
  88. {
  89. scale *= beta;
  90. if (scale < 1e-8)
  91. {
  92. return;
  93. }
  94. updateEnergy(scale);
  95. f1 = m_objective->totalEnergy(solverdt) + kineticEnergy();
  96. f2 = f0 - alpha * scale * inner_product;
  97. } while (!(f1 < f2 + SIMD_EPSILON)); // if anything here is nan then the search continues
  98. revertDv();
  99. updateDv(scale);
  100. }
  101. else
  102. {
  103. computeStep(m_ddv, m_residual);
  104. updateDv();
  105. }
  106. for (int j = 0; j < m_numNodes; ++j)
  107. {
  108. m_ddv[j].setZero();
  109. m_residual[j].setZero();
  110. }
  111. }
  112. updateVelocity();
  113. }
  114. }
  115. btScalar btDeformableBodySolver::kineticEnergy()
  116. {
  117. btScalar ke = 0;
  118. for (int i = 0; i < m_softBodies.size(); ++i)
  119. {
  120. btSoftBody* psb = m_softBodies[i];
  121. for (int j = 0; j < psb->m_nodes.size(); ++j)
  122. {
  123. btSoftBody::Node& node = psb->m_nodes[j];
  124. if (node.m_im > 0)
  125. {
  126. ke += m_dv[node.index].length2() * 0.5 / node.m_im;
  127. }
  128. }
  129. }
  130. return ke;
  131. }
  132. void btDeformableBodySolver::backupDv()
  133. {
  134. m_backup_dv.resize(m_dv.size());
  135. for (int i = 0; i < m_backup_dv.size(); ++i)
  136. {
  137. m_backup_dv[i] = m_dv[i];
  138. }
  139. }
  140. void btDeformableBodySolver::revertDv()
  141. {
  142. for (int i = 0; i < m_backup_dv.size(); ++i)
  143. {
  144. m_dv[i] = m_backup_dv[i];
  145. }
  146. }
  147. void btDeformableBodySolver::updateEnergy(btScalar scale)
  148. {
  149. for (int i = 0; i < m_dv.size(); ++i)
  150. {
  151. m_dv[i] = m_backup_dv[i] + scale * m_ddv[i];
  152. }
  153. updateState();
  154. }
  155. btScalar btDeformableBodySolver::computeDescentStep(TVStack& ddv, const TVStack& residual, bool verbose)
  156. {
  157. m_cg.solve(*m_objective, ddv, residual, false);
  158. btScalar inner_product = m_cg.dot(residual, m_ddv);
  159. btScalar res_norm = m_objective->computeNorm(residual);
  160. btScalar tol = 1e-5 * res_norm * m_objective->computeNorm(m_ddv);
  161. if (inner_product < -tol)
  162. {
  163. if (verbose)
  164. {
  165. std::cout << "Looking backwards!" << std::endl;
  166. }
  167. for (int i = 0; i < m_ddv.size(); ++i)
  168. {
  169. m_ddv[i] = -m_ddv[i];
  170. }
  171. inner_product = -inner_product;
  172. }
  173. else if (std::abs(inner_product) < tol)
  174. {
  175. if (verbose)
  176. {
  177. std::cout << "Gradient Descent!" << std::endl;
  178. }
  179. btScalar scale = m_objective->computeNorm(m_ddv) / res_norm;
  180. for (int i = 0; i < m_ddv.size(); ++i)
  181. {
  182. m_ddv[i] = scale * residual[i];
  183. }
  184. inner_product = scale * res_norm * res_norm;
  185. }
  186. return inner_product;
  187. }
  188. void btDeformableBodySolver::updateState()
  189. {
  190. updateVelocity();
  191. updateTempPosition();
  192. }
  193. void btDeformableBodySolver::updateDv(btScalar scale)
  194. {
  195. for (int i = 0; i < m_numNodes; ++i)
  196. {
  197. m_dv[i] += scale * m_ddv[i];
  198. }
  199. }
  200. void btDeformableBodySolver::computeStep(TVStack& ddv, const TVStack& residual)
  201. {
  202. if (m_useProjection)
  203. m_cg.solve(*m_objective, ddv, residual, false);
  204. else
  205. m_cr.solve(*m_objective, ddv, residual, false);
  206. }
  207. void btDeformableBodySolver::reinitialize(const btAlignedObjectArray<btSoftBody*>& softBodies, btScalar dt)
  208. {
  209. m_softBodies.copyFromArray(softBodies);
  210. bool nodeUpdated = updateNodes();
  211. if (nodeUpdated)
  212. {
  213. m_dv.resize(m_numNodes, btVector3(0, 0, 0));
  214. m_ddv.resize(m_numNodes, btVector3(0, 0, 0));
  215. m_residual.resize(m_numNodes, btVector3(0, 0, 0));
  216. m_backupVelocity.resize(m_numNodes, btVector3(0, 0, 0));
  217. }
  218. // need to setZero here as resize only set value for newly allocated items
  219. for (int i = 0; i < m_numNodes; ++i)
  220. {
  221. m_dv[i].setZero();
  222. m_ddv[i].setZero();
  223. m_residual[i].setZero();
  224. }
  225. if (dt > 0)
  226. {
  227. m_dt = dt;
  228. }
  229. m_objective->reinitialize(nodeUpdated, dt);
  230. updateSoftBodies();
  231. }
  232. void btDeformableBodySolver::setConstraints(const btContactSolverInfo& infoGlobal)
  233. {
  234. BT_PROFILE("setConstraint");
  235. m_objective->setConstraints(infoGlobal);
  236. }
  237. btScalar btDeformableBodySolver::solveContactConstraints(btCollisionObject** deformableBodies, int numDeformableBodies, const btContactSolverInfo& infoGlobal)
  238. {
  239. BT_PROFILE("solveContactConstraints");
  240. btScalar maxSquaredResidual = m_objective->m_projection.update(deformableBodies, numDeformableBodies, infoGlobal);
  241. return maxSquaredResidual;
  242. }
  243. void btDeformableBodySolver::updateVelocity()
  244. {
  245. int counter = 0;
  246. for (int i = 0; i < m_softBodies.size(); ++i)
  247. {
  248. btSoftBody* psb = m_softBodies[i];
  249. psb->m_maxSpeedSquared = 0;
  250. if (!psb->isActive())
  251. {
  252. counter += psb->m_nodes.size();
  253. continue;
  254. }
  255. for (int j = 0; j < psb->m_nodes.size(); ++j)
  256. {
  257. // set NaN to zero;
  258. if (m_dv[counter] != m_dv[counter])
  259. {
  260. m_dv[counter].setZero();
  261. }
  262. if (m_implicit)
  263. {
  264. psb->m_nodes[j].m_v = m_backupVelocity[counter] + m_dv[counter];
  265. }
  266. else
  267. {
  268. psb->m_nodes[j].m_v = m_backupVelocity[counter] + m_dv[counter] - psb->m_nodes[j].m_splitv;
  269. }
  270. psb->m_maxSpeedSquared = btMax(psb->m_maxSpeedSquared, psb->m_nodes[j].m_v.length2());
  271. ++counter;
  272. }
  273. }
  274. }
  275. void btDeformableBodySolver::updateTempPosition()
  276. {
  277. int counter = 0;
  278. for (int i = 0; i < m_softBodies.size(); ++i)
  279. {
  280. btSoftBody* psb = m_softBodies[i];
  281. if (!psb->isActive())
  282. {
  283. counter += psb->m_nodes.size();
  284. continue;
  285. }
  286. for (int j = 0; j < psb->m_nodes.size(); ++j)
  287. {
  288. psb->m_nodes[j].m_q = psb->m_nodes[j].m_x + m_dt * (psb->m_nodes[j].m_v + psb->m_nodes[j].m_splitv);
  289. ++counter;
  290. }
  291. psb->updateDeformation();
  292. }
  293. }
  294. void btDeformableBodySolver::backupVelocity()
  295. {
  296. int counter = 0;
  297. for (int i = 0; i < m_softBodies.size(); ++i)
  298. {
  299. btSoftBody* psb = m_softBodies[i];
  300. for (int j = 0; j < psb->m_nodes.size(); ++j)
  301. {
  302. m_backupVelocity[counter++] = psb->m_nodes[j].m_v;
  303. }
  304. }
  305. }
  306. void btDeformableBodySolver::setupDeformableSolve(bool implicit)
  307. {
  308. int counter = 0;
  309. for (int i = 0; i < m_softBodies.size(); ++i)
  310. {
  311. btSoftBody* psb = m_softBodies[i];
  312. if (!psb->isActive())
  313. {
  314. counter += psb->m_nodes.size();
  315. continue;
  316. }
  317. for (int j = 0; j < psb->m_nodes.size(); ++j)
  318. {
  319. if (implicit)
  320. {
  321. // setting the initial guess for newton, need m_dv = v_{n+1} - v_n for dofs that are in constraint.
  322. if (psb->m_nodes[j].m_v == m_backupVelocity[counter])
  323. m_dv[counter].setZero();
  324. else
  325. m_dv[counter] = psb->m_nodes[j].m_v - psb->m_nodes[j].m_vn;
  326. m_backupVelocity[counter] = psb->m_nodes[j].m_vn;
  327. }
  328. else
  329. {
  330. m_dv[counter] = psb->m_nodes[j].m_v + psb->m_nodes[j].m_splitv - m_backupVelocity[counter];
  331. }
  332. psb->m_nodes[j].m_v = m_backupVelocity[counter];
  333. ++counter;
  334. }
  335. }
  336. }
  337. void btDeformableBodySolver::revertVelocity()
  338. {
  339. int counter = 0;
  340. for (int i = 0; i < m_softBodies.size(); ++i)
  341. {
  342. btSoftBody* psb = m_softBodies[i];
  343. for (int j = 0; j < psb->m_nodes.size(); ++j)
  344. {
  345. psb->m_nodes[j].m_v = m_backupVelocity[counter++];
  346. }
  347. }
  348. }
  349. bool btDeformableBodySolver::updateNodes()
  350. {
  351. int numNodes = 0;
  352. for (int i = 0; i < m_softBodies.size(); ++i)
  353. numNodes += m_softBodies[i]->m_nodes.size();
  354. if (numNodes != m_numNodes)
  355. {
  356. m_numNodes = numNodes;
  357. return true;
  358. }
  359. return false;
  360. }
  361. void btDeformableBodySolver::predictMotion(btScalar solverdt)
  362. {
  363. // apply explicit forces to velocity
  364. if (m_implicit)
  365. {
  366. for (int i = 0; i < m_softBodies.size(); ++i)
  367. {
  368. btSoftBody* psb = m_softBodies[i];
  369. if (psb->isActive())
  370. {
  371. for (int j = 0; j < psb->m_nodes.size(); ++j)
  372. {
  373. psb->m_nodes[j].m_q = psb->m_nodes[j].m_x + psb->m_nodes[j].m_v * solverdt;
  374. }
  375. }
  376. }
  377. }
  378. m_objective->applyExplicitForce(m_residual);
  379. for (int i = 0; i < m_softBodies.size(); ++i)
  380. {
  381. btSoftBody* psb = m_softBodies[i];
  382. /* Clear contacts */
  383. psb->m_nodeRigidContacts.resize(0);
  384. psb->m_faceRigidContacts.resize(0);
  385. psb->m_faceNodeContacts.resize(0);
  386. if (psb->isActive())
  387. {
  388. // predict motion for collision detection
  389. predictDeformableMotion(psb, solverdt);
  390. }
  391. }
  392. }
  393. void btDeformableBodySolver::predictDeformableMotion(btSoftBody* psb, btScalar dt)
  394. {
  395. BT_PROFILE("btDeformableBodySolver::predictDeformableMotion");
  396. int i, ni;
  397. /* Update */
  398. if (psb->m_bUpdateRtCst)
  399. {
  400. psb->m_bUpdateRtCst = false;
  401. psb->updateConstants();
  402. psb->m_fdbvt.clear();
  403. if (psb->m_cfg.collisions & btSoftBody::fCollision::SDF_RD)
  404. {
  405. psb->initializeFaceTree();
  406. }
  407. }
  408. /* Prepare */
  409. psb->m_sst.sdt = dt * psb->m_cfg.timescale;
  410. psb->m_sst.isdt = 1 / psb->m_sst.sdt;
  411. psb->m_sst.velmrg = psb->m_sst.sdt * 3;
  412. psb->m_sst.radmrg = psb->getCollisionShape()->getMargin();
  413. psb->m_sst.updmrg = psb->m_sst.radmrg * (btScalar)0.25;
  414. /* Bounds */
  415. psb->updateBounds();
  416. /* Integrate */
  417. // do not allow particles to move more than the bounding box size
  418. btScalar max_v = (psb->m_bounds[1] - psb->m_bounds[0]).norm() / dt;
  419. for (i = 0, ni = psb->m_nodes.size(); i < ni; ++i)
  420. {
  421. btSoftBody::Node& n = psb->m_nodes[i];
  422. // apply drag
  423. n.m_v *= (1 - psb->m_cfg.drag);
  424. // scale velocity back
  425. if (m_implicit)
  426. {
  427. n.m_q = n.m_x;
  428. }
  429. else
  430. {
  431. if (n.m_v.norm() > max_v)
  432. {
  433. n.m_v.safeNormalize();
  434. n.m_v *= max_v;
  435. }
  436. n.m_q = n.m_x + n.m_v * dt;
  437. }
  438. n.m_splitv.setZero();
  439. n.m_constrained = false;
  440. }
  441. /* Nodes */
  442. psb->updateNodeTree(true, true);
  443. if (!psb->m_fdbvt.empty())
  444. {
  445. psb->updateFaceTree(true, true);
  446. }
  447. /* Optimize dbvt's */
  448. // psb->m_ndbvt.optimizeIncremental(1);
  449. // psb->m_fdbvt.optimizeIncremental(1);
  450. }
  451. void btDeformableBodySolver::updateSoftBodies()
  452. {
  453. BT_PROFILE("updateSoftBodies");
  454. for (int i = 0; i < m_softBodies.size(); i++)
  455. {
  456. btSoftBody* psb = (btSoftBody*)m_softBodies[i];
  457. if (psb->isActive())
  458. {
  459. psb->updateNormals();
  460. }
  461. }
  462. }
  463. void btDeformableBodySolver::setImplicit(bool implicit)
  464. {
  465. m_implicit = implicit;
  466. m_objective->setImplicit(implicit);
  467. }
  468. void btDeformableBodySolver::setLineSearch(bool lineSearch)
  469. {
  470. m_lineSearch = lineSearch;
  471. }