btDeformableBodySolver.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_DEFORMABLE_BODY_SOLVERS_H
  15. #define BT_DEFORMABLE_BODY_SOLVERS_H
  16. #include "btSoftBodySolvers.h"
  17. #include "btDeformableBackwardEulerObjective.h"
  18. #include "btDeformableMultiBodyDynamicsWorld.h"
  19. #include "BulletDynamics/Featherstone/btMultiBodyLinkCollider.h"
  20. #include "BulletDynamics/Featherstone/btMultiBodyConstraint.h"
  21. #include "btConjugateResidual.h"
  22. #include "btConjugateGradient.h"
  23. struct btCollisionObjectWrapper;
  24. // class btDeformableBackwardEulerObjective;
  25. // class btDeformableMultiBodyDynamicsWorld;
  26. class btDeformableBodySolver : public btSoftBodySolver
  27. {
  28. typedef btAlignedObjectArray<btVector3> TVStack;
  29. protected:
  30. int m_numNodes; // total number of deformable body nodes
  31. TVStack m_dv; // v_{n+1} - v_n
  32. TVStack m_backup_dv; // backed up dv
  33. TVStack m_ddv; // incremental dv
  34. TVStack m_residual; // rhs of the linear solve
  35. btAlignedObjectArray<btSoftBody*> m_softBodies; // all deformable bodies
  36. TVStack m_backupVelocity; // backed up v, equals v_n for implicit, equals v_{n+1}^* for explicit
  37. btScalar m_dt; // dt
  38. btConjugateGradient<btDeformableBackwardEulerObjective> m_cg; // CG solver
  39. btConjugateResidual<btDeformableBackwardEulerObjective> m_cr; // CR solver
  40. bool m_implicit; // use implicit scheme if true, explicit scheme if false
  41. int m_maxNewtonIterations; // max number of newton iterations
  42. btScalar m_newtonTolerance; // stop newton iterations if f(x) < m_newtonTolerance
  43. bool m_lineSearch; // If true, use newton's method with line search under implicit scheme
  44. bool m_reducedSolver; // flag for reduced soft body solver
  45. public:
  46. // handles data related to objective function
  47. btDeformableBackwardEulerObjective* m_objective;
  48. bool m_useProjection;
  49. btDeformableBodySolver();
  50. virtual ~btDeformableBodySolver();
  51. virtual SolverTypes getSolverType() const
  52. {
  53. return DEFORMABLE_SOLVER;
  54. }
  55. // update soft body normals
  56. virtual void updateSoftBodies();
  57. virtual btScalar solveContactConstraints(btCollisionObject** deformableBodies, int numDeformableBodies, const btContactSolverInfo& infoGlobal);
  58. // solve the momentum equation
  59. virtual void solveDeformableConstraints(btScalar solverdt);
  60. // set gravity (get from deformable world)
  61. virtual void setGravity(const btVector3& gravity)
  62. {
  63. // for full deformable object, we don't store gravity in the solver
  64. // this function is overriden in the reduced deformable object
  65. }
  66. // resize/clear data structures
  67. virtual void reinitialize(const btAlignedObjectArray<btSoftBody*>& softBodies, btScalar dt);
  68. // set up contact constraints
  69. virtual void setConstraints(const btContactSolverInfo& infoGlobal);
  70. // add in elastic forces and gravity to obtain v_{n+1}^* and calls predictDeformableMotion
  71. virtual void predictMotion(btScalar solverdt);
  72. // move to temporary position x_{n+1}^* = x_n + dt * v_{n+1}^*
  73. // x_{n+1}^* is stored in m_q
  74. void predictDeformableMotion(btSoftBody* psb, btScalar dt);
  75. // save the current velocity to m_backupVelocity
  76. void backupVelocity();
  77. // set m_dv and m_backupVelocity to desired value to prepare for momentum solve
  78. virtual void setupDeformableSolve(bool implicit);
  79. // set the current velocity to that backed up in m_backupVelocity
  80. void revertVelocity();
  81. // set velocity to m_dv + m_backupVelocity
  82. void updateVelocity();
  83. // update the node count
  84. bool updateNodes();
  85. // calculate the change in dv resulting from the momentum solve
  86. void computeStep(TVStack& ddv, const TVStack& residual);
  87. // calculate the change in dv resulting from the momentum solve when line search is turned on
  88. btScalar computeDescentStep(TVStack& ddv, const TVStack& residual, bool verbose = false);
  89. virtual void copySoftBodyToVertexBuffer(const btSoftBody* const softBody, btVertexBufferDescriptor* vertexBuffer) {}
  90. // process collision between deformable and rigid
  91. virtual void processCollision(btSoftBody* softBody, const btCollisionObjectWrapper* collisionObjectWrap)
  92. {
  93. softBody->defaultCollisionHandler(collisionObjectWrap);
  94. }
  95. // process collision between deformable and deformable
  96. virtual void processCollision(btSoftBody* softBody, btSoftBody* otherSoftBody)
  97. {
  98. softBody->defaultCollisionHandler(otherSoftBody);
  99. }
  100. // If true, implicit time stepping scheme is used.
  101. // Otherwise, explicit time stepping scheme is used
  102. void setImplicit(bool implicit);
  103. // If true, newton's method with line search is used when implicit time stepping scheme is turned on
  104. void setLineSearch(bool lineSearch);
  105. // set temporary position x^* = x_n + dt * v
  106. // update the deformation gradient at position x^*
  107. void updateState();
  108. // set dv = dv + scale * ddv
  109. void updateDv(btScalar scale = 1);
  110. // set temporary position x^* = x_n + dt * v^*
  111. void updateTempPosition();
  112. // save the current dv to m_backup_dv;
  113. void backupDv();
  114. // set dv to the backed-up value
  115. void revertDv();
  116. // set dv = dv + scale * ddv
  117. // set v^* = v_n + dv
  118. // set temporary position x^* = x_n + dt * v^*
  119. // update the deformation gradient at position x^*
  120. void updateEnergy(btScalar scale);
  121. // calculates the appropriately scaled kinetic energy in the system, which is
  122. // 1/2 * dv^T * M * dv
  123. // used in line search
  124. btScalar kineticEnergy();
  125. // add explicit force to the velocity in the objective class
  126. virtual void applyExplicitForce();
  127. // execute position/velocity update and apply anchor constraints in the integrateTransforms from the Dynamics world
  128. virtual void applyTransforms(btScalar timeStep);
  129. virtual void setStrainLimiting(bool opt)
  130. {
  131. m_objective->m_projection.m_useStrainLimiting = opt;
  132. }
  133. virtual void setPreconditioner(int opt)
  134. {
  135. switch (opt)
  136. {
  137. case btDeformableBackwardEulerObjective::Mass_preconditioner:
  138. m_objective->m_preconditioner = m_objective->m_massPreconditioner;
  139. break;
  140. case btDeformableBackwardEulerObjective::KKT_preconditioner:
  141. m_objective->m_preconditioner = m_objective->m_KKTPreconditioner;
  142. break;
  143. default:
  144. btAssert(false);
  145. break;
  146. }
  147. }
  148. virtual btAlignedObjectArray<btDeformableLagrangianForce*>* getLagrangianForceArray()
  149. {
  150. return &(m_objective->m_lf);
  151. }
  152. virtual const btAlignedObjectArray<btSoftBody::Node*>* getIndices()
  153. {
  154. return m_objective->getIndices();
  155. }
  156. virtual void setProjection()
  157. {
  158. m_objective->m_projection.setProjection();
  159. }
  160. virtual void setLagrangeMultiplier()
  161. {
  162. m_objective->m_projection.setLagrangeMultiplier();
  163. }
  164. virtual bool isReducedSolver()
  165. {
  166. return m_reducedSolver;
  167. }
  168. virtual void deformableBodyInternalWriteBack() {}
  169. // unused functions
  170. virtual void optimize(btAlignedObjectArray<btSoftBody*>& softBodies, bool forceUpdate = false) {}
  171. virtual void solveConstraints(btScalar dt) {}
  172. virtual bool checkInitialized() { return true; }
  173. virtual void copyBackToSoftBodies(bool bMove = true) {}
  174. };
  175. #endif /* btDeformableBodySolver_h */