btDeformableLinearElasticityForce.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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_LINEAR_ELASTICITY_H
  15. #define BT_LINEAR_ELASTICITY_H
  16. #include "btDeformableLagrangianForce.h"
  17. #include "LinearMath/btQuickprof.h"
  18. #include "btSoftBodyInternals.h"
  19. #define TETRA_FLAT_THRESHOLD 0.01
  20. class btDeformableLinearElasticityForce : public btDeformableLagrangianForce
  21. {
  22. public:
  23. typedef btAlignedObjectArray<btVector3> TVStack;
  24. btScalar m_mu, m_lambda;
  25. btScalar m_E, m_nu; // Young's modulus and Poisson ratio
  26. btScalar m_damping_alpha, m_damping_beta;
  27. btDeformableLinearElasticityForce() : m_mu(1), m_lambda(1), m_damping_alpha(0.01), m_damping_beta(0.01)
  28. {
  29. updateYoungsModulusAndPoissonRatio();
  30. }
  31. btDeformableLinearElasticityForce(btScalar mu, btScalar lambda, btScalar damping_alpha = 0.01, btScalar damping_beta = 0.01) : m_mu(mu), m_lambda(lambda), m_damping_alpha(damping_alpha), m_damping_beta(damping_beta)
  32. {
  33. updateYoungsModulusAndPoissonRatio();
  34. }
  35. void updateYoungsModulusAndPoissonRatio()
  36. {
  37. // conversion from Lame Parameters to Young's modulus and Poisson ratio
  38. // https://en.wikipedia.org/wiki/Lam%C3%A9_parameters
  39. m_E = m_mu * (3 * m_lambda + 2 * m_mu) / (m_lambda + m_mu);
  40. m_nu = m_lambda * 0.5 / (m_mu + m_lambda);
  41. }
  42. void updateLameParameters()
  43. {
  44. // conversion from Young's modulus and Poisson ratio to Lame Parameters
  45. // https://en.wikipedia.org/wiki/Lam%C3%A9_parameters
  46. m_mu = m_E * 0.5 / (1 + m_nu);
  47. m_lambda = m_E * m_nu / ((1 + m_nu) * (1 - 2 * m_nu));
  48. }
  49. void setYoungsModulus(btScalar E)
  50. {
  51. m_E = E;
  52. updateLameParameters();
  53. }
  54. void setPoissonRatio(btScalar nu)
  55. {
  56. m_nu = nu;
  57. updateLameParameters();
  58. }
  59. void setDamping(btScalar damping_alpha, btScalar damping_beta)
  60. {
  61. m_damping_alpha = damping_alpha;
  62. m_damping_beta = damping_beta;
  63. }
  64. void setLameParameters(btScalar mu, btScalar lambda)
  65. {
  66. m_mu = mu;
  67. m_lambda = lambda;
  68. updateYoungsModulusAndPoissonRatio();
  69. }
  70. virtual void addScaledForces(btScalar scale, TVStack& force)
  71. {
  72. addScaledDampingForce(scale, force);
  73. addScaledElasticForce(scale, force);
  74. }
  75. virtual void addScaledExplicitForce(btScalar scale, TVStack& force)
  76. {
  77. addScaledElasticForce(scale, force);
  78. }
  79. // The damping matrix is calculated using the time n state as described in https://www.math.ucla.edu/~jteran/papers/GSSJT15.pdf to allow line search
  80. virtual void addScaledDampingForce(btScalar scale, TVStack& force)
  81. {
  82. if (m_damping_alpha == 0 && m_damping_beta == 0)
  83. return;
  84. btScalar mu_damp = m_damping_beta * m_mu;
  85. btScalar lambda_damp = m_damping_beta * m_lambda;
  86. int numNodes = getNumNodes();
  87. btAssert(numNodes <= force.size());
  88. btVector3 grad_N_hat_1st_col = btVector3(-1, -1, -1);
  89. for (int i = 0; i < m_softBodies.size(); ++i)
  90. {
  91. btSoftBody* psb = m_softBodies[i];
  92. if (!psb->isActive())
  93. {
  94. continue;
  95. }
  96. for (int j = 0; j < psb->m_tetras.size(); ++j)
  97. {
  98. bool close_to_flat = (psb->m_tetraScratches[j].m_J < TETRA_FLAT_THRESHOLD);
  99. btSoftBody::Tetra& tetra = psb->m_tetras[j];
  100. btSoftBody::Node* node0 = tetra.m_n[0];
  101. btSoftBody::Node* node1 = tetra.m_n[1];
  102. btSoftBody::Node* node2 = tetra.m_n[2];
  103. btSoftBody::Node* node3 = tetra.m_n[3];
  104. size_t id0 = node0->index;
  105. size_t id1 = node1->index;
  106. size_t id2 = node2->index;
  107. size_t id3 = node3->index;
  108. btMatrix3x3 dF = DsFromVelocity(node0, node1, node2, node3) * tetra.m_Dm_inverse;
  109. if (!close_to_flat)
  110. {
  111. dF = psb->m_tetraScratches[j].m_corotation.transpose() * dF;
  112. }
  113. btMatrix3x3 I;
  114. I.setIdentity();
  115. btMatrix3x3 dP = (dF + dF.transpose()) * mu_damp + I * ((dF[0][0] + dF[1][1] + dF[2][2]) * lambda_damp);
  116. btMatrix3x3 df_on_node123 = dP * tetra.m_Dm_inverse.transpose();
  117. if (!close_to_flat)
  118. {
  119. df_on_node123 = psb->m_tetraScratches[j].m_corotation * df_on_node123;
  120. }
  121. btVector3 df_on_node0 = df_on_node123 * grad_N_hat_1st_col;
  122. // damping force differential
  123. btScalar scale1 = scale * tetra.m_element_measure;
  124. force[id0] -= scale1 * df_on_node0;
  125. force[id1] -= scale1 * df_on_node123.getColumn(0);
  126. force[id2] -= scale1 * df_on_node123.getColumn(1);
  127. force[id3] -= scale1 * df_on_node123.getColumn(2);
  128. }
  129. for (int j = 0; j < psb->m_nodes.size(); ++j)
  130. {
  131. const btSoftBody::Node& node = psb->m_nodes[j];
  132. size_t id = node.index;
  133. if (node.m_im > 0)
  134. {
  135. force[id] -= scale * node.m_v / node.m_im * m_damping_alpha;
  136. }
  137. }
  138. }
  139. }
  140. virtual double totalElasticEnergy(btScalar dt)
  141. {
  142. double energy = 0;
  143. for (int i = 0; i < m_softBodies.size(); ++i)
  144. {
  145. btSoftBody* psb = m_softBodies[i];
  146. if (!psb->isActive())
  147. {
  148. continue;
  149. }
  150. for (int j = 0; j < psb->m_tetraScratches.size(); ++j)
  151. {
  152. btSoftBody::Tetra& tetra = psb->m_tetras[j];
  153. btSoftBody::TetraScratch& s = psb->m_tetraScratches[j];
  154. energy += tetra.m_element_measure * elasticEnergyDensity(s);
  155. }
  156. }
  157. return energy;
  158. }
  159. // The damping energy is formulated as in https://www.math.ucla.edu/~jteran/papers/GSSJT15.pdf to allow line search
  160. virtual double totalDampingEnergy(btScalar dt)
  161. {
  162. double energy = 0;
  163. int sz = 0;
  164. for (int i = 0; i < m_softBodies.size(); ++i)
  165. {
  166. btSoftBody* psb = m_softBodies[i];
  167. if (!psb->isActive())
  168. {
  169. continue;
  170. }
  171. for (int j = 0; j < psb->m_nodes.size(); ++j)
  172. {
  173. sz = btMax(sz, psb->m_nodes[j].index);
  174. }
  175. }
  176. TVStack dampingForce;
  177. dampingForce.resize(sz + 1);
  178. for (int i = 0; i < dampingForce.size(); ++i)
  179. dampingForce[i].setZero();
  180. addScaledDampingForce(0.5, dampingForce);
  181. for (int i = 0; i < m_softBodies.size(); ++i)
  182. {
  183. btSoftBody* psb = m_softBodies[i];
  184. for (int j = 0; j < psb->m_nodes.size(); ++j)
  185. {
  186. const btSoftBody::Node& node = psb->m_nodes[j];
  187. energy -= dampingForce[node.index].dot(node.m_v) / dt;
  188. }
  189. }
  190. return energy;
  191. }
  192. double elasticEnergyDensity(const btSoftBody::TetraScratch& s)
  193. {
  194. double density = 0;
  195. btMatrix3x3 epsilon = (s.m_F + s.m_F.transpose()) * 0.5 - btMatrix3x3::getIdentity();
  196. btScalar trace = epsilon[0][0] + epsilon[1][1] + epsilon[2][2];
  197. density += m_mu * (epsilon[0].length2() + epsilon[1].length2() + epsilon[2].length2());
  198. density += m_lambda * trace * trace * 0.5;
  199. return density;
  200. }
  201. virtual void addScaledElasticForce(btScalar scale, TVStack& force)
  202. {
  203. int numNodes = getNumNodes();
  204. btAssert(numNodes <= force.size());
  205. btVector3 grad_N_hat_1st_col = btVector3(-1, -1, -1);
  206. for (int i = 0; i < m_softBodies.size(); ++i)
  207. {
  208. btSoftBody* psb = m_softBodies[i];
  209. if (!psb->isActive())
  210. {
  211. continue;
  212. }
  213. btScalar max_p = psb->m_cfg.m_maxStress;
  214. for (int j = 0; j < psb->m_tetras.size(); ++j)
  215. {
  216. btSoftBody::Tetra& tetra = psb->m_tetras[j];
  217. btMatrix3x3 P;
  218. firstPiola(psb->m_tetraScratches[j], P);
  219. #if USE_SVD
  220. if (max_p > 0)
  221. {
  222. // since we want to clamp the principal stress to max_p, we only need to
  223. // calculate SVD when sigma_0^2 + sigma_1^2 + sigma_2^2 > max_p * max_p
  224. btScalar trPTP = (P[0].length2() + P[1].length2() + P[2].length2());
  225. if (trPTP > max_p * max_p)
  226. {
  227. btMatrix3x3 U, V;
  228. btVector3 sigma;
  229. singularValueDecomposition(P, U, sigma, V);
  230. sigma[0] = btMin(sigma[0], max_p);
  231. sigma[1] = btMin(sigma[1], max_p);
  232. sigma[2] = btMin(sigma[2], max_p);
  233. sigma[0] = btMax(sigma[0], -max_p);
  234. sigma[1] = btMax(sigma[1], -max_p);
  235. sigma[2] = btMax(sigma[2], -max_p);
  236. btMatrix3x3 Sigma;
  237. Sigma.setIdentity();
  238. Sigma[0][0] = sigma[0];
  239. Sigma[1][1] = sigma[1];
  240. Sigma[2][2] = sigma[2];
  241. P = U * Sigma * V.transpose();
  242. }
  243. }
  244. #endif
  245. // btVector3 force_on_node0 = P * (tetra.m_Dm_inverse.transpose()*grad_N_hat_1st_col);
  246. btMatrix3x3 force_on_node123 = psb->m_tetraScratches[j].m_corotation * P * tetra.m_Dm_inverse.transpose();
  247. btVector3 force_on_node0 = force_on_node123 * grad_N_hat_1st_col;
  248. btSoftBody::Node* node0 = tetra.m_n[0];
  249. btSoftBody::Node* node1 = tetra.m_n[1];
  250. btSoftBody::Node* node2 = tetra.m_n[2];
  251. btSoftBody::Node* node3 = tetra.m_n[3];
  252. size_t id0 = node0->index;
  253. size_t id1 = node1->index;
  254. size_t id2 = node2->index;
  255. size_t id3 = node3->index;
  256. // elastic force
  257. btScalar scale1 = scale * tetra.m_element_measure;
  258. force[id0] -= scale1 * force_on_node0;
  259. force[id1] -= scale1 * force_on_node123.getColumn(0);
  260. force[id2] -= scale1 * force_on_node123.getColumn(1);
  261. force[id3] -= scale1 * force_on_node123.getColumn(2);
  262. }
  263. }
  264. }
  265. virtual void buildDampingForceDifferentialDiagonal(btScalar scale, TVStack& diagA) {}
  266. // The damping matrix is calculated using the time n state as described in https://www.math.ucla.edu/~jteran/papers/GSSJT15.pdf to allow line search
  267. virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack& dv, TVStack& df)
  268. {
  269. if (m_damping_alpha == 0 && m_damping_beta == 0)
  270. return;
  271. btScalar mu_damp = m_damping_beta * m_mu;
  272. btScalar lambda_damp = m_damping_beta * m_lambda;
  273. int numNodes = getNumNodes();
  274. btAssert(numNodes <= df.size());
  275. btVector3 grad_N_hat_1st_col = btVector3(-1, -1, -1);
  276. for (int i = 0; i < m_softBodies.size(); ++i)
  277. {
  278. btSoftBody* psb = m_softBodies[i];
  279. if (!psb->isActive())
  280. {
  281. continue;
  282. }
  283. for (int j = 0; j < psb->m_tetras.size(); ++j)
  284. {
  285. bool close_to_flat = (psb->m_tetraScratches[j].m_J < TETRA_FLAT_THRESHOLD);
  286. btSoftBody::Tetra& tetra = psb->m_tetras[j];
  287. btSoftBody::Node* node0 = tetra.m_n[0];
  288. btSoftBody::Node* node1 = tetra.m_n[1];
  289. btSoftBody::Node* node2 = tetra.m_n[2];
  290. btSoftBody::Node* node3 = tetra.m_n[3];
  291. size_t id0 = node0->index;
  292. size_t id1 = node1->index;
  293. size_t id2 = node2->index;
  294. size_t id3 = node3->index;
  295. btMatrix3x3 dF = Ds(id0, id1, id2, id3, dv) * tetra.m_Dm_inverse;
  296. if (!close_to_flat)
  297. {
  298. dF = psb->m_tetraScratches[j].m_corotation.transpose() * dF;
  299. }
  300. btMatrix3x3 I;
  301. I.setIdentity();
  302. btMatrix3x3 dP = (dF + dF.transpose()) * mu_damp + I * ((dF[0][0] + dF[1][1] + dF[2][2]) * lambda_damp);
  303. btMatrix3x3 df_on_node123 = dP * tetra.m_Dm_inverse.transpose();
  304. if (!close_to_flat)
  305. {
  306. df_on_node123 = psb->m_tetraScratches[j].m_corotation * df_on_node123;
  307. }
  308. btVector3 df_on_node0 = df_on_node123 * grad_N_hat_1st_col;
  309. // damping force differential
  310. btScalar scale1 = scale * tetra.m_element_measure;
  311. df[id0] -= scale1 * df_on_node0;
  312. df[id1] -= scale1 * df_on_node123.getColumn(0);
  313. df[id2] -= scale1 * df_on_node123.getColumn(1);
  314. df[id3] -= scale1 * df_on_node123.getColumn(2);
  315. }
  316. for (int j = 0; j < psb->m_nodes.size(); ++j)
  317. {
  318. const btSoftBody::Node& node = psb->m_nodes[j];
  319. size_t id = node.index;
  320. if (node.m_im > 0)
  321. {
  322. df[id] -= scale * dv[id] / node.m_im * m_damping_alpha;
  323. }
  324. }
  325. }
  326. }
  327. virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack& dx, TVStack& df)
  328. {
  329. int numNodes = getNumNodes();
  330. btAssert(numNodes <= df.size());
  331. btVector3 grad_N_hat_1st_col = btVector3(-1, -1, -1);
  332. for (int i = 0; i < m_softBodies.size(); ++i)
  333. {
  334. btSoftBody* psb = m_softBodies[i];
  335. if (!psb->isActive())
  336. {
  337. continue;
  338. }
  339. for (int j = 0; j < psb->m_tetras.size(); ++j)
  340. {
  341. btSoftBody::Tetra& tetra = psb->m_tetras[j];
  342. btSoftBody::Node* node0 = tetra.m_n[0];
  343. btSoftBody::Node* node1 = tetra.m_n[1];
  344. btSoftBody::Node* node2 = tetra.m_n[2];
  345. btSoftBody::Node* node3 = tetra.m_n[3];
  346. size_t id0 = node0->index;
  347. size_t id1 = node1->index;
  348. size_t id2 = node2->index;
  349. size_t id3 = node3->index;
  350. btMatrix3x3 dF = psb->m_tetraScratches[j].m_corotation.transpose() * Ds(id0, id1, id2, id3, dx) * tetra.m_Dm_inverse;
  351. btMatrix3x3 dP;
  352. firstPiolaDifferential(psb->m_tetraScratches[j], dF, dP);
  353. // btVector3 df_on_node0 = dP * (tetra.m_Dm_inverse.transpose()*grad_N_hat_1st_col);
  354. btMatrix3x3 df_on_node123 = psb->m_tetraScratches[j].m_corotation * dP * tetra.m_Dm_inverse.transpose();
  355. btVector3 df_on_node0 = df_on_node123 * grad_N_hat_1st_col;
  356. // elastic force differential
  357. btScalar scale1 = scale * tetra.m_element_measure;
  358. df[id0] -= scale1 * df_on_node0;
  359. df[id1] -= scale1 * df_on_node123.getColumn(0);
  360. df[id2] -= scale1 * df_on_node123.getColumn(1);
  361. df[id3] -= scale1 * df_on_node123.getColumn(2);
  362. }
  363. }
  364. }
  365. void firstPiola(const btSoftBody::TetraScratch& s, btMatrix3x3& P)
  366. {
  367. btMatrix3x3 corotated_F = s.m_corotation.transpose() * s.m_F;
  368. btMatrix3x3 epsilon = (corotated_F + corotated_F.transpose()) * 0.5 - btMatrix3x3::getIdentity();
  369. btScalar trace = epsilon[0][0] + epsilon[1][1] + epsilon[2][2];
  370. P = epsilon * btScalar(2) * m_mu + btMatrix3x3::getIdentity() * m_lambda * trace;
  371. }
  372. // Let P be the first piola stress.
  373. // This function calculates the dP = dP/dF * dF
  374. void firstPiolaDifferential(const btSoftBody::TetraScratch& s, const btMatrix3x3& dF, btMatrix3x3& dP)
  375. {
  376. btScalar trace = (dF[0][0] + dF[1][1] + dF[2][2]);
  377. dP = (dF + dF.transpose()) * m_mu + btMatrix3x3::getIdentity() * m_lambda * trace;
  378. }
  379. // Let Q be the damping stress.
  380. // This function calculates the dP = dQ/dF * dF
  381. void firstPiolaDampingDifferential(const btSoftBody::TetraScratch& s, const btMatrix3x3& dF, btMatrix3x3& dP)
  382. {
  383. btScalar mu_damp = m_damping_beta * m_mu;
  384. btScalar lambda_damp = m_damping_beta * m_lambda;
  385. btScalar trace = (dF[0][0] + dF[1][1] + dF[2][2]);
  386. dP = (dF + dF.transpose()) * mu_damp + btMatrix3x3::getIdentity() * lambda_damp * trace;
  387. }
  388. virtual void addScaledHessian(btScalar scale)
  389. {
  390. btVector3 grad_N_hat_1st_col = btVector3(-1, -1, -1);
  391. for (int i = 0; i < m_softBodies.size(); ++i)
  392. {
  393. btSoftBody* psb = m_softBodies[i];
  394. if (!psb->isActive())
  395. {
  396. continue;
  397. }
  398. for (int j = 0; j < psb->m_tetras.size(); ++j)
  399. {
  400. btSoftBody::Tetra& tetra = psb->m_tetras[j];
  401. btMatrix3x3 P;
  402. firstPiola(psb->m_tetraScratches[j], P); // make sure scratch is evaluated at x_n + dt * vn
  403. btMatrix3x3 force_on_node123 = psb->m_tetraScratches[j].m_corotation * P * tetra.m_Dm_inverse.transpose();
  404. btVector3 force_on_node0 = force_on_node123 * grad_N_hat_1st_col;
  405. btSoftBody::Node* node0 = tetra.m_n[0];
  406. btSoftBody::Node* node1 = tetra.m_n[1];
  407. btSoftBody::Node* node2 = tetra.m_n[2];
  408. btSoftBody::Node* node3 = tetra.m_n[3];
  409. btScalar scale1 = scale * (scale + m_damping_beta) * tetra.m_element_measure; // stiff and stiffness-damping terms;
  410. node0->m_effectiveMass += OuterProduct(force_on_node0, force_on_node0) * scale1;
  411. node1->m_effectiveMass += OuterProduct(force_on_node123.getColumn(0), force_on_node123.getColumn(0)) * scale1;
  412. node2->m_effectiveMass += OuterProduct(force_on_node123.getColumn(1), force_on_node123.getColumn(1)) * scale1;
  413. node3->m_effectiveMass += OuterProduct(force_on_node123.getColumn(2), force_on_node123.getColumn(2)) * scale1;
  414. }
  415. for (int j = 0; j < psb->m_nodes.size(); ++j)
  416. {
  417. btSoftBody::Node& node = psb->m_nodes[j];
  418. if (node.m_im > 0)
  419. {
  420. btMatrix3x3 I;
  421. I.setIdentity();
  422. node.m_effectiveMass += I * (scale * (1.0 / node.m_im) * m_damping_alpha);
  423. }
  424. }
  425. }
  426. }
  427. virtual btDeformableLagrangianForceType getForceType()
  428. {
  429. return BT_LINEAR_ELASTICITY_FORCE;
  430. }
  431. };
  432. #endif /* BT_LINEAR_ELASTICITY_H */