From 4b6eaeed4cdd02664b13c45b75fabb7c150872a2 Mon Sep 17 00:00:00 2001 From: jingyuc Date: Thu, 15 Jul 2021 17:30:08 -0400 Subject: [PATCH] kinematic tested. specified delta_v as a cosine function. --- examples/ExampleBrowser/main.cpp | 2 +- examples/ReducedDeformableDemo/BasicTest.cpp | 39 +++++++----------- .../btDeformableBackwardEulerObjective.cpp | 40 +++++++++++-------- .../btDeformableBackwardEulerObjective.h | 11 ----- .../btDeformableMultiBodyDynamicsWorld.cpp | 10 +---- .../btDeformableMultiBodyDynamicsWorld.h | 3 -- src/BulletSoftBody/btSoftBody.h | 3 +- 7 files changed, 43 insertions(+), 65 deletions(-) diff --git a/examples/ExampleBrowser/main.cpp b/examples/ExampleBrowser/main.cpp index 6138da267..f306c3f02 100644 --- a/examples/ExampleBrowser/main.cpp +++ b/examples/ExampleBrowser/main.cpp @@ -96,7 +96,7 @@ int main(int argc, char* argv[]) else { clock.reset(); - exampleBrowser->update(1.0); + exampleBrowser->update(deltaTimeInSeconds); } } while (!exampleBrowser->requestedExit() && !interrupted); } diff --git a/examples/ReducedDeformableDemo/BasicTest.cpp b/examples/ReducedDeformableDemo/BasicTest.cpp index db2469380..b7af85543 100644 --- a/examples/ReducedDeformableDemo/BasicTest.cpp +++ b/examples/ReducedDeformableDemo/BasicTest.cpp @@ -53,24 +53,16 @@ class BasicTest : public CommonDeformableBodyBase void mapToReducedDofs(btSoftBody* psb) { btAssert(psb->m_reducedDofs.size() == m_nReduced); - for (int j = 0; j < m_nReduced; ++j) { - if (j == selected_mode) - psb->m_reducedDofs[j] = sin(psb->m_eigenvalues[j] * sim_time); - else - psb->m_reducedDofs[j] = 0; + for (int j = 0; j < m_nReduced; ++j) + { + for (int i = 0; i < m_nFull; ++i) + for (int k = 0; k < 3; ++k) + { + int idx_f = 3 * i + k; + psb->m_reducedDofs[j] = psb->m_modes[j][idx_f] * (psb->m_nodes[i].m_x[k] - psb->m_x0[idx_f]); + // psb->m_reducedVelocity[j] = psb->m_modes[j][idx_f] * psb->m_nodes[i].m_v[k]; + } } - // for (int j = 0; j < m_nReduced; ++j) - // { - // // if (j != selected_mode) - // // continue; - // for (int i = 0; i < m_nFull; ++i) - // for (int k = 0; k < 3; ++k) - // { - // int idx_f = 3 * i + k; - // psb->m_reducedNodes[j] = psb->m_modes[j][idx_f] * (psb->m_nodes[i].m_x[k] - psb->m_x0[idx_f]); - // psb->m_reducedVelocity[j] = psb->m_modes[j][idx_f] * psb->m_nodes[i].m_v[k]; - // } - // } } // compute full degree of freedoms @@ -79,14 +71,12 @@ class BasicTest : public CommonDeformableBodyBase btAssert(psb->m_nodes.size() == m_nFull); for (int j = 0; j < m_nReduced; ++j) { - // if (j != selected_mode) - // continue; for (int i = 0; i < m_nFull; ++i) for (int k = 0; k < 3; ++k) { int idx_f = 3 * i + k; psb->m_nodes[i].m_x[k] = psb->m_x0[idx_f] + psb->m_modes[j][idx_f] * psb->m_reducedDofs[j]; - psb->m_nodes[i].m_v[k] = psb->m_modes[j][idx_f] * psb->m_reducedVelocity[j]; + // psb->m_nodes[i].m_v[k] = psb->m_modes[j][idx_f] * psb->m_reducedVelocity[j]; } } } @@ -150,6 +140,7 @@ public: { getDeformedShape(psb, 0); first_step = false; + mapToReducedDofs(psb); } // compute reduced dofs @@ -157,10 +148,10 @@ public: psb->m_reducedVelocity.resize(m_nReduced); sim_time += deltaTime; // std::cout << psb->m_eigenvalues[0] << "\t" << sim_time << "\t" << deltaTime << "\t" << sin(psb->m_eigenvalues[0] * sim_time) << "\n"; - mapToReducedDofs(psb); - float internalTimeStep = 1. / 60.f; - m_dynamicsWorld->stepSimulation(deltaTime, 1, internalTimeStep); + // float internalTimeStep = 1. / 60.f; + float internalTimeStep = 1; + m_dynamicsWorld->stepSimulation(1, 1, internalTimeStep); // for (int i = 0; i < m_nReduced; ++i) // std::cout << psb->m_reducedDofs[i] << "\t"; @@ -213,6 +204,7 @@ void BasicTest::initPhysics() std::string filename("../../../examples/SoftDemo/mesh.vtk"); btSoftBody* psb = btSoftBodyHelpers::CreateFromVtkFile(getDeformableDynamicsWorld()->getWorldInfo(), filename.c_str()); m_nFull = psb->m_nodes.size(); + psb->m_reducedModel = true; // read in eigenmodes, stiffness and mass matrices std::string eigenvalues_file("../../../examples/SoftDemo/eigenvalues.bin"); @@ -295,7 +287,6 @@ void BasicTest::initPhysics() getDeformableDynamicsWorld()->addForce(psb, m_massSpring); m_forces.push_back(m_massSpring); } - getDeformableDynamicsWorld()->setReducedModelFlag(false); getDeformableDynamicsWorld()->setImplicit(false); getDeformableDynamicsWorld()->setLineSearch(false); getDeformableDynamicsWorld()->setUseProjection(true); diff --git a/src/BulletSoftBody/btDeformableBackwardEulerObjective.cpp b/src/BulletSoftBody/btDeformableBackwardEulerObjective.cpp index 10c9fb25a..6f3c46ec0 100644 --- a/src/BulletSoftBody/btDeformableBackwardEulerObjective.cpp +++ b/src/BulletSoftBody/btDeformableBackwardEulerObjective.cpp @@ -23,7 +23,6 @@ btDeformableBackwardEulerObjective::btDeformableBackwardEulerObjective(btAligned m_massPreconditioner = new MassPreconditioner(m_softBodies); m_KKTPreconditioner = new KKTPreconditioner(m_softBodies, m_projection, m_lf, m_dt, m_implicit); m_preconditioner = m_KKTPreconditioner; - m_reducedModel = false; } btDeformableBackwardEulerObjective::~btDeformableBackwardEulerObjective() @@ -137,6 +136,8 @@ void btDeformableBackwardEulerObjective::updateVelocity(const TVStack& dv) void btDeformableBackwardEulerObjective::applyForce(TVStack& force, bool setZero) { + static btScalar sim_time = 0; + size_t counter = 0; for (int i = 0; i < m_softBodies.size(); ++i) { @@ -148,7 +149,7 @@ void btDeformableBackwardEulerObjective::applyForce(TVStack& force, bool setZero } if (m_implicit) { - if (m_reducedModel) + if (psb->m_reducedModel) { // for (int j = 0; j < psb->m_reducedNodes.size(); ++j) // TODO: reduced soft body // { @@ -171,25 +172,30 @@ void btDeformableBackwardEulerObjective::applyForce(TVStack& force, bool setZero } else { - if (m_reducedModel) + if (psb->m_reducedModel) { // get reduced force - // btAlignedObjectArray reduced_force; - // reduced_force.resize(psb->m_reducedNodes.size()); - // for (int r = 0; r < psb->m_reducedNodes.size(); ++r) - // reduced_force[r] = psb->m_Kr[r] * psb->m_reducedNodes[r]; + btAlignedObjectArray reduced_force; + reduced_force.resize(psb->m_reducedDofs.size()); + for (int r = 0; r < psb->m_reducedDofs.size(); ++r) + reduced_force[r] = psb->m_Kr[r] * psb->m_reducedDofs[r]; - // // update reduced velocity - // for (int r = 0; r < psb->m_reducedNodes.size(); ++r) // TODO: reduced soft body - // { - // btScalar mass_inv = (psb->m_Mr[r] == 0) ? 0 : 1.0 / psb->m_Mr[r]; - // // btScalar delta_v = m_dt * mass_inv * reduced_force[r]; - // btScalar delta_v = 1.0; + // update reduced velocity + for (int r = 0; r < psb->m_reducedDofs.size(); ++r) // TODO: reduced soft body + { + btScalar mass_inv = (psb->m_Mr[r] == 0) ? 0 : 1.0 / psb->m_Mr[r]; + // btScalar delta_v = m_dt * mass_inv * reduced_force[r]; - // psb->m_reducedVelocity[r] += delta_v; - // std::cout << psb->m_reducedVelocity[r] << "\t"; - // } - // std::cout << "\n"; + sim_time += m_dt; + // btScalar delta_v = 1; + // btScalar delta_v = 0.02 * cos(psb->m_eigenvalues[r] * sim_time); + btScalar delta_v = 0.02 * cos(psb->m_eigenvalues[r] * sim_time); + + // psb->m_reducedVelocity[r] = sin(psb->m_eigenvalues[r] * sim_time); + psb->m_reducedVelocity[r] -= delta_v; + + // std::cout << sim_time << "\t" << psb->m_reducedDofs[r] << "\t" << psb->m_reducedVelocity[r] << "\t" << delta_v << "\n"; + } } else { diff --git a/src/BulletSoftBody/btDeformableBackwardEulerObjective.h b/src/BulletSoftBody/btDeformableBackwardEulerObjective.h index ae4ed8690..eb05b9f01 100644 --- a/src/BulletSoftBody/btDeformableBackwardEulerObjective.h +++ b/src/BulletSoftBody/btDeformableBackwardEulerObjective.h @@ -40,7 +40,6 @@ public: const TVStack& m_backupVelocity; btAlignedObjectArray m_nodes; bool m_implicit; - bool m_reducedModel; MassPreconditioner* m_massPreconditioner; KKTPreconditioner* m_KKTPreconditioner; @@ -131,16 +130,6 @@ public: m_implicit = implicit; } - void setReducedModel(bool reduced_model) - { - m_reducedModel = reduced_model; - } - - bool getReducedModelFlag() const - { - return m_reducedModel; - } - // Calculate the total potential energy in the system btScalar totalEnergy(btScalar dt); diff --git a/src/BulletSoftBody/btDeformableMultiBodyDynamicsWorld.cpp b/src/BulletSoftBody/btDeformableMultiBodyDynamicsWorld.cpp index 97bec6062..4e42e166e 100644 --- a/src/BulletSoftBody/btDeformableMultiBodyDynamicsWorld.cpp +++ b/src/BulletSoftBody/btDeformableMultiBodyDynamicsWorld.cpp @@ -72,12 +72,6 @@ btDeformableMultiBodyDynamicsWorld::~btDeformableMultiBodyDynamicsWorld() delete m_solverDeformableBodyIslandCallback; } -void btDeformableMultiBodyDynamicsWorld::setReducedModelFlag(bool reduced_model) -{ - m_reducedModel = reduced_model; - m_deformableBodySolver->m_objective->setReducedModel(reduced_model); -} - void btDeformableMultiBodyDynamicsWorld::internalSingleStepSimulation(btScalar timeStep) { BT_PROFILE("internalSingleStepSimulation"); @@ -306,10 +300,10 @@ void btDeformableMultiBodyDynamicsWorld::integrateTransforms(btScalar timeStep) for (int i = 0; i < m_softBodies.size(); ++i) { btSoftBody* psb = m_softBodies[i]; - if (m_reducedModel) + if (psb->m_reducedModel) { for (int r = 0; r < psb->m_reducedDofs.size(); ++r) - psb->m_reducedDofs[r] += timeStep * psb->m_reducedVelocity[r]; + psb->m_reducedDofs[r] = timeStep * psb->m_reducedVelocity[r]; } else { diff --git a/src/BulletSoftBody/btDeformableMultiBodyDynamicsWorld.h b/src/BulletSoftBody/btDeformableMultiBodyDynamicsWorld.h index 405b335f9..4b7069aac 100644 --- a/src/BulletSoftBody/btDeformableMultiBodyDynamicsWorld.h +++ b/src/BulletSoftBody/btDeformableMultiBodyDynamicsWorld.h @@ -48,7 +48,6 @@ class btDeformableMultiBodyDynamicsWorld : public btMultiBodyDynamicsWorld btScalar m_internalTime; int m_ccdIterations; bool m_implicit; - bool m_reducedModel; bool m_lineSearch; bool m_useProjection; DeformableBodyInplaceSolverIslandCallback* m_solverDeformableBodyIslandCallback; @@ -155,8 +154,6 @@ public: void softBodySelfCollision(); - void setReducedModelFlag(bool reduced_model); - void setImplicit(bool implicit) { m_implicit = implicit; diff --git a/src/BulletSoftBody/btSoftBody.h b/src/BulletSoftBody/btSoftBody.h index 7e91dde3b..fc320c73c 100644 --- a/src/BulletSoftBody/btSoftBody.h +++ b/src/BulletSoftBody/btSoftBody.h @@ -855,7 +855,8 @@ public: btScalar m_restLengthScale; - btAlignedObjectArray m_reducedDofs; // Reduced degree of freedom + bool m_reducedModel; // Reduced deformable model flag + btAlignedObjectArray m_reducedDofs; // Reduced degree of freedom btAlignedObjectArray m_reducedVelocity; // Reduced velocity array btAlignedObjectArray m_x0; // Rest position btAlignedObjectArray m_eigenvalues; // eigenvalues of the reduce deformable model