iMSTK
Interactive Medical Simulation Toolkit
imstkFemDeformableBodyModel.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 
9 #include "imstkDynamicalModel.h"
10 #include "imstkInternalForceModelTypes.h"
11 #include "imstkVectorizedState.h"
12 #include "imstkNonLinearSystem.h"
13 
14 #include <sparseMatrix.h>
15 
16 namespace vega
17 {
18 class VolumetricMesh;
19 } // namespace vega
20 
21 namespace imstk
22 {
23 class InternalForceModel;
24 class TimeIntegrator;
25 class SolverBase;
26 class VegaMeshIO;
27 template<typename T, int N> class VecDataArray;
28 
35 {
36  FeMethodType m_femMethod = FeMethodType::Invertible;
37  HyperElasticMaterialType m_hyperElasticMaterialType = HyperElasticMaterialType::StVK;
38 
39  // file names (remove from here?)
40  std::string m_fixedDOFFilename;
41  std::vector<std::size_t> m_fixedNodeIds;
42 
43  double m_dampingMassCoefficient = 0.1;
44  double m_dampingStiffnessCoefficient = 0.01;
45  double m_dampingLaplacianCoefficient = 0.0;
46  double m_deformationCompliance = 1.0;
47  double m_compressionResistance = 500.0;
48  double m_inversionThreshold = -std::numeric_limits<double>::max();
49  double m_gravity = 9.81;
50 };
51 
59 class FemDeformableBodyModel : public DynamicalModel<FeDeformBodyState>
60 {
61 public:
64 
65 public:
67  ~FemDeformableBodyModel() override;
68 
69 public:
73  void configure(const std::string& configFileName);
74  void configure(std::shared_ptr<FemModelConfig> config = std::make_shared<FemModelConfig>());
75 
79  bool initialize() override;
80 
84  void setForceModelConfiguration(std::shared_ptr<FemModelConfig> fmConfig) { this->m_FEModelConfig = fmConfig; }
85  std::shared_ptr<FemModelConfig> getForceModelConfiguration() const { return m_FEModelConfig; }
87 
91  void setInternalForceModel(std::shared_ptr<InternalForceModel> fm) { m_internalForceModel = fm; }
92  std::shared_ptr<InternalForceModel> getInternalForceModel() const { return m_internalForceModel; }
94 
98  void setTimeIntegrator(std::shared_ptr<TimeIntegrator> timeIntegrator) { this->m_timeIntegrator = timeIntegrator; }
99  std::shared_ptr<TimeIntegrator> getTimeIntegrator() const { return m_timeIntegrator; }
101 
105  void loadInitialStates();
106 
110  bool loadBoundaryConditions();
111 
115  bool initializeForceModel();
116 
120  bool initializeMassMatrix();
121 
125  bool initializeDampingMatrix();
126 
130  bool initializeTangentStiffness();
131 
135  bool initializeGravityForce();
136 
140  bool initializeExplicitExternalForces();
141 
145  static void initializeEigenMatrixFromVegaMatrix(const vega::SparseMatrix& vegaMatrix, SparseMatrixd& eigenMatrix);
146 
150  void computeImplicitSystemRHS(kinematicState& prevState, kinematicState& newState, const StateUpdateType updateType);
151 
155  void computeSemiImplicitSystemRHS(kinematicState& stateAtT, kinematicState& newState, const StateUpdateType updateType);
156 
160  void computeImplicitSystemLHS(const kinematicState& prevState, kinematicState& newState, const StateUpdateType updateType);
161 
165  void computeSemiImplicitSystemRHSAndLHS(kinematicState& prevState, kinematicState& newState, const StateUpdateType updateType);
166 
170  void computeImplicitSystemRHSAndLHS(kinematicState& prevState, kinematicState& newState, const StateUpdateType updateType);
171 
175  void updateDampingMatrix();
176 
180  void applyBoundaryConditions(SparseMatrixd& M, const bool withCompliance = false) const;
181  void applyBoundaryConditions(Vectord& x) const;
182 
187  void updateMassMatrix();
188 
192  void updatePhysicsGeometry() override;
193 
197  void updateBodyStates(const Vectord& solution, const StateUpdateType updateType);
198  void updateBodyIntermediateStates(const Vectord& solution, const StateUpdateType updateType);
199 
203  void updateBodyPreviousStates();
204 
209  System::VectorFunctionType getFunction();
210 
214  System::UpdateFunctionType getUpdateFunction();
215  System::UpdatePrevStateFunctionType getUpdatePrevStateFunction();
216 
221  System::MatrixFunctionType getFunctionGradient();
222 
227  System::VectorMatrixFunctionType getFunctionAndGradient();
228 
232  Vectord& getContactForce() { return m_Fcontact; }
233 
237  Vectord& getUnknownVec() { return m_qSol; }
238 
242  void setUpdateType(const StateUpdateType& updateType) { m_updateType = updateType; }
243  const StateUpdateType& getUpdateType() const { return m_updateType; }
245 
248  std::vector<std::size_t>& getFixNodeIds() { return m_fixedNodeIds; }
249 
253  virtual void setTimeStep(const double timeStep);
254 
258  virtual double getTimeStep() const;
259 
263  void setFixedSizeTimeStepping();
264 
268  void enableFixedBC() { m_implementFixedBC = true; };
269  void disableFixedBC() { m_implementFixedBC = false; };
270  bool isFixedBCImplemented() const { return m_implementFixedBC; };
271 
272  std::shared_ptr<TaskNode> getSolveNode() const { return m_solveNode; }
273 
277  std::shared_ptr<SolverBase> getSolver() const { return m_solver; }
278  void setSolver(std::shared_ptr<SolverBase> solver) { this->m_solver = solver; }
280 
281 protected:
285  void initGraphEdges(std::shared_ptr<TaskNode> source, std::shared_ptr<TaskNode> sink) override;
286 
287  std::shared_ptr<SolverBase> m_solver = nullptr;
288  std::shared_ptr<InternalForceModel> m_internalForceModel = nullptr;
289  std::shared_ptr<TimeIntegrator> m_timeIntegrator = nullptr;
290  std::shared_ptr<NonLinearSystem<SparseMatrixd>> m_nonLinearSystem = nullptr;
291 
292  std::shared_ptr<FemModelConfig> m_FEModelConfig = nullptr;
293 
295  SparseMatrixd m_M;
296  SparseMatrixd m_C;
297  SparseMatrixd m_K;
298  SparseMatrixd m_Keff;
299 
300  Vectord m_Finternal;
301  Vectord m_Feff;
302  Vectord m_Fcontact;
303  Vectord m_Fgravity;
305  Vectord m_qSol;
306 
307  std::shared_ptr<vega::VolumetricMesh> m_vegaPhysicsMesh = nullptr;
308  std::shared_ptr<vega::SparseMatrix> m_vegaMassMatrix = nullptr;
309  std::shared_ptr<vega::SparseMatrix> m_vegaTangentStiffnessMatrix = nullptr;
310  std::shared_ptr<vega::SparseMatrix> m_vegaDampingMatrix = nullptr;
311 
312  std::vector<std::size_t> m_fixedNodeIds;
313 
314  StateUpdateType m_updateType = StateUpdateType::DeltaVelocity;
315 
316  bool m_damped = false;
317 
318  // If this is true, the tangent stiffness and force vector will be modified to
319  // accommodate (the rows and columns will be nullified) the fixed boundary conditions
320  bool m_implementFixedBC = true;
321 
322 private:
323  std::shared_ptr<TaskNode> m_solveNode = nullptr;
324 };
325 } // namespace imstk
SparseMatrixd m_K
Tangent (derivative of internal force w.r.t displacements) stiffness matrix.
Vectord m_Fcontact
Vector of contact forces.
Compound Geometry.
Base class for a multi-variable nonlinear system.
Vectord m_Feff
Vector of effective forces.
SparseMatrixd m_Keff
Effective stiffness matrix (dependent on internal force model and time integrator) ...
Vectord & getContactForce()
Get the contact force vector.
std::vector< std::size_t > & getFixNodeIds()
Returns the unknown vectors.
SparseMatrixd m_M
Matrices typical to a elastodynamics and 2nd order analogous systems.
This class stores the state of the unknown field variable in vectorized form.
void setTimeIntegrator(std::shared_ptr< TimeIntegrator > timeIntegrator)
Get/Set time integrator.
Vectord m_Fgravity
Vector of gravity forces.
Vectord m_Finternal
Vector of internal forces.
Parameters for finite element model.
Vectord m_qSol
Vector to maintain solution at each iteration of nonlinear solver.
void enableFixedBC()
Set the fixed BC implementation state.
std::vector< std::size_t > m_fixedNodeIds
Nodal IDs of the nodes that are fixed.
StateUpdateType
Type of the update of the state of the body.
void setInternalForceModel(std::shared_ptr< InternalForceModel > fm)
Get/Set internal force model.
Base class for mathematical model of the physics governing the dynamic object.
Vectord m_FexplicitExternal
Vector of explicitly defined external forces.
std::shared_ptr< SolverBase > getSolver() const
Get/Set the solver pointer.
void setForceModelConfiguration(std::shared_ptr< FemModelConfig > fmConfig)
Get/Set force model configuration.
Mathematical model of the physics governing the dynamic deformable object.
SparseMatrixd m_C
Damping coefficient matrix.
void setUpdateType(const StateUpdateType &updateType)
Get/Set the update type.
Vectord & getUnknownVec()
Returns the unknown vectors.