28 #include "imstkMath.h" 29 #include "imstkNewtonSolver.h" 30 #include "imstkReducedStVKBodyModel.h" 31 #include "imstkTimeIntegrator.h" 32 #include "imstkVegaMeshIO.h" 33 #include "imstkVolumetricMesh.h" 34 #include "imstkTaskGraph.h" 35 #include "imstkNewtonSolver.h" 38 #include "StVKReducedInternalForces.h" 40 #include "modalMatrix.h" 41 #include "reducedStVKForceModel.h" 44 #pragma warning(disable : 4458) 45 #include "configFile.h" 56 m_solveNode = m_taskGraph->addFunction(
"FEMModel_Solve", [&]() { getSolver()->solve(); });
62 m_internalForceModel =
nullptr;
68 std::cerr <<
"not implemented yet" << std::endl;
70 m_config = std::make_shared<ReducedStVKConfig>();
76 vega::ConfigFile vegaConfigFileOptions;
79 vegaConfigFileOptions.addOptionOptional(
"dampingMassCoefficient",
80 &m_config->m_dampingMassCoefficient,
81 m_config->m_dampingMassCoefficient);
82 vegaConfigFileOptions.addOptionOptional(
"dampingStiffnessCoefficient",
83 &m_config->m_dampingStiffnessCoefficient,
84 m_config->m_dampingStiffnessCoefficient);
85 vegaConfigFileOptions.addOptionOptional(
"dampingLaplacianCoefficient",
86 &m_config->m_dampingLaplacianCoefficient,
87 m_config->m_dampingLaplacianCoefficient);
88 vegaConfigFileOptions.addOptionOptional(
"numberOfThreads",
89 &m_config->m_numberOfThreads,
90 m_config->m_numberOfThreads);
91 vegaConfigFileOptions.addOptionOptional(
"gravity", &m_config->m_gravity, m_config->m_gravity);
94 CHECK(vegaConfigFileOptions.parseOptions(configFileName.data()) == 0)
95 <<
"ForceModelConfig::parseConfig - Unable to load the configuration file";
121 std::shared_ptr<imstk::ReducedStVKConfig>
122 ReducedStVK::getForceModelConfiguration()
const 131 m_internalForceModel = fm;
134 std::shared_ptr<imstk::InternalForceModel>
135 ReducedStVK::getInternalForceModel()
const 138 LOG(FATAL) <<
"current implementation can not return an imstk::InternalForceModel.";
149 m_timeIntegrator = timeIntegrator;
152 std::shared_ptr<imstk::TimeIntegrator>
153 ReducedStVK::getTimeIntegrator()
const 155 return m_timeIntegrator;
162 CHECK(
m_geometry !=
nullptr && m_config !=
nullptr)
163 <<
"Physics mesh or force model configuration not set yet!";
166 if (m_solver ==
nullptr)
173 nlSystem->setUpdatePreviousStatesFunction(getUpdatePrevStateFunction());
176 auto nlSolver = std::make_shared<NewtonSolver<Matrixd>>();
177 nlSolver->setToSemiImplicit();
179 nlSolver->setMaxIterations(1);
180 nlSolver->setSystem(nlSystem);
190 CHECK(m_numDOF == m_vegaPhysicsMesh->getNumVertices() * 3);
221 std::vector<float> Ufloat;
223 vega::ReadMatrixFromDisk_(fname.c_str(), m, n, Ufloat);
226 std::vector<double> Udouble(Ufloat.size());
228 for (
size_t i = 0; i < Ufloat.size(); ++i)
230 Udouble[i] = Ufloat[i];
234 std::make_shared<vega::ModalMatrix>(m_numDOF / 3,
m_numDOFReduced, Udouble.data());
243 LOG(WARNING) <<
"Num. of degree of freedom is zero!";
251 m_initialStateReduced = std::make_shared<kinematicState>(
m_numDOFReduced);
252 m_previousStateReduced = std::make_shared<kinematicState>(
m_numDOFReduced);
253 m_currentStateReduced = std::make_shared<kinematicState>(
m_numDOFReduced);
259 const double g = m_config->m_gravity;
260 const bool isGravityPresent = (g > 0) ?
true :
false;
263 m_internalForceModel = std::make_shared<vega::StVKReducedInternalForces>(
264 m_config->m_cubicPolynomialFilename.c_str(),
266 m_forceModel = std::make_shared<vega::ReducedStVKForceModel>(m_internalForceModel.get());
275 <<
"Force model geometry not set!";
283 this->
m_M.resize(m_numDOFReduced, m_numDOFReduced);
294 auto cM = m_config->m_dampingMassCoefficient;
295 auto cK = m_config->m_dampingStiffnessCoefficient;
300 this->
m_C.resize(m_numDOFReduced, m_numDOFReduced);
311 <<
"Tangent stiffness cannot be initialized without force model";
314 m_forceModel->GetTangentStiffnessMatrix(m_initialStateReduced->getQ().data(),
316 this->
m_K.resize(m_numDOFReduced, m_numDOFReduced);
325 const double gravity = m_config->m_gravity;
327 m_vegaPhysicsMesh->computeGravity(
m_Fgravity.data(), gravity);
339 const auto& uPrev = stateAtT.
getQ();
340 const auto& vPrev = stateAtT.
getQDot();
341 auto& u = newState.
getQ();
342 const auto& v = newState.
getQDot();
348 const double dT = m_timeIntegrator->getTimestepSize();
352 case StateUpdateType::DeltaVelocity:
367 m_Feff += m_FgravityReduced;
375 LOG(WARNING) <<
"Update type not supported";
385 const auto& vPrev = stateAtT.
getQDot();
386 auto& u = newState.
getQ();
393 const double dT = m_timeIntegrator->getTimestepSize();
397 case StateUpdateType::DeltaVelocity:
411 m_Feff += m_FgravityReduced;
419 LOG(FATAL) <<
"Update type not supported";
428 const double dT = m_timeIntegrator->getTimestepSize();
432 case StateUpdateType::DeltaVelocity:
453 LOG(FATAL) <<
"Update type not supported";
476 const auto& dampingStiffnessCoefficient = m_config->m_dampingStiffnessCoefficient;
477 const auto& dampingMassCoefficient = m_config->m_dampingMassCoefficient;
479 if (dampingMassCoefficient > 0)
481 m_C = dampingMassCoefficient *
m_M;
483 if (dampingStiffnessCoefficient > 0)
485 m_C +=
m_K * dampingStiffnessCoefficient;
488 else if (dampingStiffnessCoefficient > 0)
490 m_C =
m_K * dampingStiffnessCoefficient;
505 LOG(WARNING) <<
"Boundary conditions are not not allowed to change";
518 auto& uReduced = m_currentStateReduced->getQ();
521 volMesh->setVertexDisplacements(uFull);
528 m_previousStateReduced->setU(m_currentStateReduced->getQ());
529 m_previousStateReduced->setV(m_currentStateReduced->getQDot());
537 this->updateBodyIntermediateStates(solution, updateType);
541 ReducedStVK::updateBodyIntermediateStates(
const Vectord& solution,
const StateUpdateType updateType)
543 const auto& uPrev = m_previousStateReduced->getQ();
545 const auto& v = m_currentStateReduced->getQDot();
546 const double dT = m_timeIntegrator->getTimestepSize();
550 case StateUpdateType::DeltaVelocity:
551 m_currentStateReduced->setV(v + solution);
552 m_currentStateReduced->setU(uPrev + dT * v);
556 case StateUpdateType::Velocity:
557 m_currentStateReduced->setV(solution);
558 m_currentStateReduced->setU(uPrev + dT * v);
563 LOG(FATAL) <<
"Unknown state update type";
569 NonLinearSystem<Matrixd>::VectorFunctionType
572 #pragma warning(push) 573 #pragma warning(disable : 4100) 576 return [&,
this](
const Vectord& q,
const bool semiImplicit) ->
const Vectord& {
578 *m_currentStateReduced,
581 *m_currentStateReduced,
589 NonLinearSystem<Matrixd>::MatrixFunctionType
592 #pragma warning(push) 593 #pragma warning(disable : 4100) 595 return [&,
this](
const Vectord& q) ->
const Matrixd& {
597 *m_currentStateReduced,
605 NonLinearSystem<Matrixd>::UpdateFunctionType
609 return [&,
this](
const Vectord& q,
const bool fullyImplicit) ->
void {
610 (fullyImplicit) ? this->updateBodyIntermediateStates(q,
m_updateType)
615 NonLinearSystem<Matrixd>::UpdatePrevStateFunctionType
616 ReducedStVK::getUpdatePrevStateFunction()
625 CHECK(eigenMatrix.rows() == eigenMatrix.cols());
626 CHECK(A.size() == eigenMatrix.rows() * eigenMatrix.cols());
629 for (
size_t j = 0; j < eigenMatrix.cols(); ++j)
631 size_t offset = j * eigenMatrix.rows();
632 for (
size_t i = 0; i < eigenMatrix.rows(); ++i)
634 eigenMatrix(i, j) = A[i + offset];
648 m_timeStepSizeType = TimeSteppingType::Fixed;
649 m_timeIntegrator->setTimestepSizeToDefault();
655 m_timeIntegrator->setTimestepSize(timeStep);
661 return m_timeIntegrator->getTimestepSize();
668 m_modalMatrix->AssembleVector(const_cast<double*>(uReduced.data()), u.data());
681 m_modalMatrix->ProjectVector(const_cast<double*>(u.data()), uReduced.data());
688 m_taskGraph->addEdge(source, m_solveNode);
689 m_taskGraph->addEdge(m_solveNode, sink);
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrixd
A dynamic size matrix of doubles.
void setFixedSizeTimeStepping()
Set the time step size to fixed size.
std::shared_ptr< FeDeformBodyState > m_initialState
Initial state.
Matrixd m_Keff
Tangent (derivative of internal force w.r.t displacements) stiffness matrix
Matrixd m_K
Damping coefficient matrix
std::shared_ptr< vega::ReducedStVKForceModel > m_forceModel
Mathematical model for intenal forces
void prolongate(const Vectord &uReduced, Vectord &u) const
prolongate reduced vector into full space, ie, u = U * uReduced
Vectord & getContactForce()
Get the contact force vector.
std::shared_ptr< Geometry > getModelGeometry() const
Gets the model geometry.
std::shared_ptr< vega::ModalMatrix > m_modalMatrix
Nonlinear system resulting from TI and force model
virtual ~ReducedStVK() override
Destructor.
void updateDampingMatrix()
Update damping Matrix.
Matrixd m_M
Matrices typical to a elastodynamics and 2nd order analogous systems.
ReducedStVK()
Constructor.
Vectord & getQ()
Get the state.
Vectord m_FexplicitExternal
Vector of gravity forces
Vectord m_qSol
Vector of explicitly defined external forces
void configure(const std::string &configFileName)
Configure the force model from external file.
void computeImplicitSystemRHS(kinematicState &prevState, kinematicState &newState, const StateUpdateType updateType)
Compute the RHS of the resulting linear system.
bool initializeGravityForce()
Initialize the gravity force.
DynamicalModelType
Type of the time dependent mathematical model.
std::shared_ptr< FeDeformBodyState > m_previousState
Previous state.
void applyBoundaryConditions(Matrixd &M, const bool withCompliance=false) const
Applies boundary conditions to matrix and a vector.
void updatePhysicsGeometry() override
Updates the Physics geometry.
std::vector< double > m_massMatrix
Mesh used for Physics
void setTimeIntegrator(std::shared_ptr< TimeIntegrator > timeIntegrator)
Set/Get time integrator.
static void initializeEigenMatrixFromStdVector(const std::vector< double > &v, Matrixd &eigenMatrix)
Initialize the Eigen matrix with data inside vega sparse matrix.
void project(const Vectord &u, Vectord &uReduced) const
project full-space vector into reduced space, uReduced = U^T u
This class stores the state of the unknown field variable in vectorized form.
Vectord & getUnknownVec()
Returns the unknown vectors.
void updateBodyStates(const Vectord &solution, const StateUpdateType updateType)
Update states.
void setInternalForceModel(std::shared_ptr< vega::StVKReducedInternalForces > fm)
Set/Get time integrator.
Vectord & getQDot()
Get the derivative of state w.r.t time.
bool initialize() override
Initialize the deformable body model.
Vectord m_qSolReduced
Vector of internal forces
bool initializeDampingMatrix()
Initialize the damping (combines structural and viscous damping) matrix.
void computeImplicitSystemLHS(const kinematicState &prevState, kinematicState &newState, const StateUpdateType updateType)
Compute the LHS of the resulting linear system.
virtual void setTimeStep(const double timeStep)
Set the time step size.
Vectord m_Feff
Vector to maintain solution at each iteration of nonlinear solver
bool initializeTangentStiffness()
Initialize the tangent stiffness matrix.
bool initializeExplicitExternalForces()
Initialize explicit external forces.
Vectord m_Finternal
Vector of effective forces
Vectord m_FexplicitExternalReduced
Vector of gravity forces
StateUpdateType m_updateType
Nodal IDs of the nodes that are fixed
std::vector< double > m_stiffnessMatrix
Vega mass matrix
bool m_damped
Update type of the model
std::set< std::string > m_validGeometryTypes
Valid geometry types of this model.
Vectord m_Fgravity
Vector of contact forces
bool initializeForceModel()
Initialize the force model.
StateUpdateType
Type of the update of the state of the body.
void computeSemiImplicitSystemRHS(kinematicState &stateAtT, kinematicState &newState, const StateUpdateType updateType)
Compute the RHS of the resulting linear system using semi-implicit scheme.
Vectord m_FcontactReduced
Vector to maintain solution at each iteration of nonlinear solver
void loadInitialStates()
Load the initial conditions of the deformable object.
virtual double getTimeStep() const
Returns the time step size.
std::vector< double > m_dampingMatrix
Vega Tangent stiffness matrix
Base class for mathematical model of the physics governing the dynamic object.
size_t m_numDOFReduced
Vector of explicitly defined external forces
std::shared_ptr< FeDeformBodyState > m_currentState
Current state.
void readModalMatrix(const std::string &fname)
Read in the basis file and create m_modalMatrix.
Base class for all volume mesh types.
NonLinearSystem< Matrixd >::UpdateFunctionType getUpdateFunction()
Get the function that updates the model given the solution.
void updateMassMatrix()
Update mass matrix Note: Not supported yet!
Vectord m_Fcontact
integrator)
bool initializeMassMatrix()
Initialize the mass matrix from the mesh.
NonLinearSystem< Matrixd >::VectorFunctionType getFunction()
Returns the "function" that evaluates the nonlinear function given the state vector.
std::shared_ptr< Geometry > m_geometry
Physics geometry of the model.
void updateBodyPreviousStates()
Update the previous states given the current state.
static std::shared_ptr< vega::VolumetricMesh > convertVolumetricMeshToVegaMesh(const std::shared_ptr< PointSet > volumeMesh)
Generate a vega volume mesh given volumetric mesh.
NonLinearSystem< Matrixd >::MatrixFunctionType getFunctionGradient()
Returns the "function" that evaluates the gradient of the nonlinear function given the state vector...
void setForceModelConfiguration(std::shared_ptr< ReducedStVKConfig > config)
Set/Get internal force model.
void initGraphEdges()
Initializes the edges of the graph.