add internal force

This commit is contained in:
jingyuc
2021-07-16 15:01:44 -04:00
parent 4b6eaeed4c
commit c24f2a72ef
6 changed files with 63 additions and 22 deletions

View File

@@ -93,7 +93,6 @@ public:
BasicTest(struct GUIHelperInterface* helper)
: CommonDeformableBodyBase(helper)
{
// m_linearElasticity = 0;
m_massSpring = nullptr;
m_nFull = 0;
sim_time = 0;
@@ -138,9 +137,10 @@ public:
// TODO: remove this. very hacky way of adding initial deformation
if (first_step && !psb->m_bUpdateRtCst)
{
getDeformedShape(psb, 0);
getDeformedShape(psb, 0, 100);
first_step = false;
mapToReducedDofs(psb);
// std::cout << psb->m_reducedDofs[0] << "\n";
}
// compute reduced dofs
@@ -149,13 +149,10 @@ public:
sim_time += deltaTime;
// std::cout << psb->m_eigenvalues[0] << "\t" << sim_time << "\t" << deltaTime << "\t" << sin(psb->m_eigenvalues[0] * sim_time) << "\n";
// 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";
// std::cout << "\n";
float internalTimeStep = 1. / 60.f;
m_dynamicsWorld->stepSimulation(deltaTime, 1, internalTimeStep);
// float internalTimeStep = 1;
// m_dynamicsWorld->stepSimulation(1, 1, internalTimeStep);
// map reduced dof back to full
mapToFullDofs(psb);
@@ -217,7 +214,13 @@ void BasicTest::initPhysics()
btSoftBodyHelpers::readBinary(psb->m_Mr, m_startMode, m_nReduced, 3 * m_nFull, Mr_file.c_str());
std::string modes_file("../../../examples/SoftDemo/modes.bin");
btSoftBodyHelpers::readBinaryMat(psb->m_modes, m_startMode, m_nReduced, 3 * m_nFull, modes_file.c_str()); // default to 3D
btSoftBodyHelpers::readBinaryModes(psb->m_modes, m_startMode, m_nReduced, 3 * m_nFull, modes_file.c_str()); // default to 3D
// std::string Kr_dense_file("../../../examples/SoftDemo/Kr_dense.bin");
// btSoftBodyHelpers::readBinaryMat(psb->m_KrDense, m_startMode, m_nReduced, 3 * m_nFull, Kr_dense_file.c_str());
// std::string Mr_dense_file("../../../examples/SoftDemo/Mr_dense.bin");
// btSoftBodyHelpers::readBinaryMat(psb->m_MrDense, m_startMode, m_nReduced, 3 * m_nFull, Mr_dense_file.c_str());
// for (int i = 0; i < 3*m_nFull; ++i)
// std::cout << psb->m_modes[0][i] << '\n';

View File

@@ -177,24 +177,25 @@ void btDeformableBackwardEulerObjective::applyForce(TVStack& force, bool setZero
// get reduced force
btAlignedObjectArray<btScalar> 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];
for (int r = 0; r < psb->m_reducedDofs.size(); ++r) {
reduced_force[r] = 100 * psb->m_Kr[r] * psb->m_reducedDofs[r]; //TODO: increase stiffness
// std::cout << psb->m_Kr[r] << "\t" << psb->m_reducedDofs[r] << "\n";
}
// update reduced velocity
for (int r = 0; r < psb->m_reducedDofs.size(); ++r) // TODO: reduced soft body
for (int r = 0; r < psb->m_reducedDofs.size(); ++r)
{
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 = m_dt * mass_inv * reduced_force[r];
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);
// btScalar delta_v = 0.005 * 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";
// std::cout << sim_time << "\t" << psb->m_reducedDofs[r] << "\t" << psb->m_reducedVelocity[r] << "\t" << delta_v << "\t" << reduced_force[r] << "\n";
}
}
else

View File

@@ -303,7 +303,7 @@ void btDeformableMultiBodyDynamicsWorld::integrateTransforms(btScalar timeStep)
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
{

View File

@@ -798,6 +798,7 @@ public:
typedef btAlignedObjectArray<Material*> tMaterialArray;
typedef btAlignedObjectArray<Joint*> tJointArray;
typedef btAlignedObjectArray<btSoftBody*> tSoftBodyArray;
typedef btAlignedObjectArray<btAlignedObjectArray<btScalar> > tDenseMatrix;
//
// Fields
@@ -860,7 +861,9 @@ public:
btAlignedObjectArray<btScalar> m_reducedVelocity; // Reduced velocity array
btAlignedObjectArray<btScalar> m_x0; // Rest position
btAlignedObjectArray<btScalar> m_eigenvalues; // eigenvalues of the reduce deformable model
btAlignedObjectArray<btAlignedObjectArray<btScalar> > m_modes; // modes of the reduced deformable model. Each inner array is a mode, outer array size = n_modes
tDenseMatrix m_modes; // modes of the reduced deformable model. Each inner array is a mode, outer array size = n_modes
tDenseMatrix m_KrDense; // reduced stiffness matrix (dense)
tDenseMatrix m_MrDense; // reduced mass matrix (dense)
btAlignedObjectArray<btScalar> m_Kr; // reduced stiffness matrix
btAlignedObjectArray<btScalar> m_Mr; // reduced mass matrix //TODO: do we need this?
btAlignedObjectArray<btScalar> m_M; // full mass matrix //TODO: maybe don't need this?

View File

@@ -1512,7 +1512,7 @@ void btSoftBodyHelpers::readBinary(btAlignedObjectArray<btScalar>& vec,
f_in.close();
}
void btSoftBodyHelpers::readBinaryMat(btAlignedObjectArray<btAlignedObjectArray<btScalar> >& mat,
void btSoftBodyHelpers::readBinaryMat(btSoftBody::tDenseMatrix& mat,
const unsigned int n_start, // starting mode index
const unsigned int n_modes, // #modes, outer array size
const unsigned int n_full, // inner array size
@@ -1524,6 +1524,38 @@ void btSoftBodyHelpers::readBinaryMat(btAlignedObjectArray<btAlignedObjectArray<
f_in.read((char*)&v_size, sizeof(uint32_t));
btAssert(v_size == n_full * n_full);
// read data
mat.resize(n_modes);
for (int i = 0; i < n_start + n_modes; ++i)
{
for (int j = 0; j < n_full; ++j)
{
double temp;
f_in.read((char*)&temp, sizeof(double));
if (i >= n_start && j >= n_start && i < n_start + n_modes && j < n_start + n_modes)
{
if (mat[i - n_start].size() != n_modes)
mat[i - n_start].resize(n_modes);
mat[i - n_start][j - n_start] = btScalar(temp);
}
}
}
f_in.close();
}
void btSoftBodyHelpers::readBinaryModes(btSoftBody::tDenseMatrix& mat,
const unsigned int n_start, // starting mode index
const unsigned int n_modes, // #modes, outer array size
const unsigned int n_full, // inner array size
const char* file)
{
std::ifstream f_in(file, std::ios::in | std::ios::binary);
// first get size
unsigned int v_size;
f_in.read((char*)&v_size, sizeof(uint32_t));
btAssert(v_size == n_full * n_full);
// read data
mat.resize(n_modes);
for (int i = 0; i < n_start + n_modes; ++i)

View File

@@ -146,8 +146,10 @@ struct btSoftBodyHelpers
// read in a binary vector
static void readBinary(btAlignedObjectArray<btScalar>& vec, const unsigned int n_start, const unsigned int n_modes, const unsigned int n_full, const char* file);
// read in a binary matrix (must provide matrix size)
static void readBinaryMat(btAlignedObjectArray<btAlignedObjectArray<btScalar> >& mat, const unsigned int n_start, const unsigned int n_modes, const unsigned int n_full, const char* file);
// read in a binary matrix
static void readBinaryMat(btSoftBody::tDenseMatrix& mat, const unsigned int n_start, const unsigned int n_modes, const unsigned int n_full, const char* file);
// read in modes file
static void readBinaryModes(btSoftBody::tDenseMatrix& mat, const unsigned int n_start, const unsigned int n_modes, const unsigned int n_full, const char* file);
static void writeObj(const char* file, const btSoftBody* psb);