iMSTK
Interactive Medical Simulation Toolkit
imstkReducedStVKBodyModel.h
1 /*=========================================================================
2 
3  Library: iMSTK
4 
5  Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
6  & Imaging in Medicine, Rensselaer Polytechnic Institute.
7 
8  Licensed under the Apache License, Version 2.0 (the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0.txt
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 =========================================================================*/
21 
22 #pragma once
23 
24 #include <memory>
25 
26 #include "imstkDynamicalModel.h"
27 #include "imstkVectorizedState.h"
28 #include "imstkNonLinearSystem.h"
29 #include "imstkLogger.h"
30 
31 namespace vega
32 {
33 class VolumetricMesh;
34 class ReducedStVKForceModel;
35 class StVKReducedInternalForces;
36 class ModalMatrix;
37 } // namespace vega
38 
39 namespace imstk
40 {
41 class InternalForceModel;
42 class SolverBase;
43 class TaskNode;
44 class TimeIntegrator;
45 class VegaMeshIO;
46 
48 {
49  std::string m_cubicPolynomialFilename;
50  std::string m_modesFileName;
51  // int r;
52 
53  double m_dampingMassCoefficient = 0.1;
54  double m_dampingStiffnessCoefficient = 0.01;
55  double m_dampingLaplacianCoefficient = 0.0;
56  double m_deformationCompliance = 1.0;
57  double m_gravity = 9.81;
58 
59  int m_numberOfThreads = 4;
60 };
61 
62 class ReducedStVK : public DynamicalModel<FeDeformBodyState>
63 {
65 // using Matrixd = Eigen::MatrixXd;
66 public:
70  ReducedStVK();
71 
75  virtual ~ReducedStVK() override;
76 
77 public:
81  void configure(const std::string& configFileName);
82  void configure(std::shared_ptr<ReducedStVKConfig> config = std::make_shared<ReducedStVKConfig>());
83 
87  bool initialize() override;
88 
92  void setForceModelConfiguration(std::shared_ptr<ReducedStVKConfig> config);
93  std::shared_ptr<ReducedStVKConfig> getForceModelConfiguration() const;
94 
99  void setInternalForceModel(std::shared_ptr<vega::StVKReducedInternalForces> fm);
100  std::shared_ptr<imstk::InternalForceModel> getInternalForceModel() const;
101 
105  void setTimeIntegrator(std::shared_ptr<TimeIntegrator> timeIntegrator);
106  std::shared_ptr<TimeIntegrator> getTimeIntegrator() const;
107 
111  void loadInitialStates();
112 
116  bool initializeForceModel();
117 
121  bool initializeMassMatrix();
122 
126  bool initializeDampingMatrix();
127 
131  bool initializeTangentStiffness();
132 
136  bool initializeGravityForce();
137 
141  bool initializeExplicitExternalForces();
142 
146  static void initializeEigenMatrixFromStdVector(const std::vector<double>& v,
147  Matrixd& eigenMatrix);
148 
152  void computeImplicitSystemRHS(kinematicState& prevState,
153  kinematicState& newState,
154  const StateUpdateType updateType);
155 
159  void computeSemiImplicitSystemRHS(kinematicState& stateAtT,
160  kinematicState& newState,
161  const StateUpdateType updateType);
162 
166  void computeImplicitSystemLHS(const kinematicState& prevState,
167  kinematicState& newState,
168  const StateUpdateType updateType);
169 
173  void updateDampingMatrix();
174 
179  void updateMassMatrix();
180 
184  void applyBoundaryConditions(Matrixd& M, const bool withCompliance = false) const;
185  void applyBoundaryConditions(Vectord& x) const;
186 
190  void updatePhysicsGeometry() override;
191 
195  void updateBodyStates(const Vectord& solution, const StateUpdateType updateType);
196  void updateBodyIntermediateStates(const Vectord& solution, const StateUpdateType updateType);
197 
201  void updateBodyPreviousStates();
202 
207  NonLinearSystem<Matrixd>::VectorFunctionType getFunction();
208 
212  NonLinearSystem<Matrixd>::UpdateFunctionType getUpdateFunction();
213  NonLinearSystem<Matrixd>::UpdatePrevStateFunctionType getUpdatePrevStateFunction();
214 
219  NonLinearSystem<Matrixd>::MatrixFunctionType getFunctionGradient();
220 
224  Vectord& getContactForce();
225 
229  Vectord& getUnknownVec()
230  {
231  return m_qSol;
232  }
233 
237  void setUpdateType(const StateUpdateType& updateType)
238  {
239  m_updateType = updateType;
240  }
241 
242  const StateUpdateType& getUpdateType() const
243  {
244  return m_updateType;
245  }
246 
249  std::vector<std::size_t>& getFixNodeIds()
250  {
251  return m_fixedNodeIds;
252  }
253 
257  virtual void setTimeStep(const double timeStep);
258 
262  virtual double getTimeStep() const;
263 
267  void setFixedSizeTimeStepping();
268 
273  {
274  LOG(WARNING) << "Boundary conditions are not allowed to change";
275  m_implementFixedBC = true;
276  };
277  void disableFixedBC()
278  {
279  // LOG(FATAL, "Boundary conditions are not allowed to change");
280  m_implementFixedBC = false;
281  };
282  bool isFixedBCImplemented() const
283  {
284  return m_implementFixedBC;
285  };
286 
290  void prolongate(const Vectord& uReduced, Vectord& u) const;
291 
295  void prolongate(kinematicState& uReduced, kinematicState& u) const;
296 
300  void project(const Vectord& u, Vectord& uReduced) const;
305  void readModalMatrix(const std::string& fname);
306 
307  std::shared_ptr<TaskNode> getSolveNode() const { return m_solveNode; }
308 
309  std::shared_ptr<SolverBase> getSolver() const { return m_solver; }
310  void setSolver(std::shared_ptr<SolverBase> solver) { this->m_solver = solver; }
311 
312 protected:
316  void initGraphEdges(std::shared_ptr<TaskNode> source, std::shared_ptr<TaskNode> sink) override;
317 
318 protected:
319  std::shared_ptr<SolverBase> m_solver = nullptr;
320  std::shared_ptr<vega::StVKReducedInternalForces> m_internalForceModel;
321  std::shared_ptr<vega::ReducedStVKForceModel> m_forceModel;
322  std::shared_ptr<TimeIntegrator> m_timeIntegrator;
323  std::shared_ptr<NonLinearSystem<Matrixd>> m_nonLinearSystem;
324  std::shared_ptr<vega::ModalMatrix> m_modalMatrix;
325 
326  std::shared_ptr<ReducedStVKConfig> m_config;
327 
333 
335  // full-space vectors
336  Vectord m_Fcontact;
337  Vectord m_Fgravity;
339  Vectord m_qSol;
340 
341  // reduced-space vectors
342  Vectord m_Feff;
343  Vectord m_Finternal;
344  Vectord m_qSolReduced;
346  Vectord m_FgravityReduced;
349 
350  std::shared_ptr<vega::VolumetricMesh> m_vegaPhysicsMesh;
351  std::vector<double> m_massMatrix;
352  std::vector<double> m_stiffnessMatrix;
353  std::vector<double> m_dampingMatrix;
354 
355  std::vector<std::size_t> m_fixedNodeIds;
356 
357  StateUpdateType m_updateType = StateUpdateType::DeltaVelocity;
358 
359  bool m_damped = false;
360 
361  // If this is true, the tangent stiffness and force vector will be modified to
362  // accommodate (the rows and columns will be nullified) the fixed boundary conditions
363  bool m_implementFixedBC = false;
364 
365  std::shared_ptr<kinematicState> m_initialStateReduced;
366  std::shared_ptr<kinematicState> m_previousStateReduced;
367  std::shared_ptr<kinematicState> m_currentStateReduced;
368 
369 private:
370  std::shared_ptr<TaskNode> m_solveNode = nullptr;
371 };
372 } // namespace imstk
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrixd
A dynamic size matrix of doubles.
Definition: imstkMath.h:97
Matrixd m_Keff
Tangent (derivative of internal force w.r.t displacements) stiffness matrix
Matrixd m_K
Damping coefficient matrix
std::vector< std::size_t > & getFixNodeIds()
Returns the unknown vectors.
std::shared_ptr< NonLinearSystem< Matrixd > > m_nonLinearSystem
Time integrator
std::shared_ptr< vega::ReducedStVKForceModel > m_forceModel
Mathematical model for intenal forces
std::shared_ptr< vega::ModalMatrix > m_modalMatrix
Nonlinear system resulting from TI and force model
std::vector< std::size_t > m_fixedNodeIds
Vega Laplacian damping matrix
Matrixd m_M
Matrices typical to a elastodynamics and 2nd order analogous systems.
Compound Geometry.
Vectord m_FexplicitExternal
Vector of gravity forces
Vectord m_qSol
Vector of explicitly defined external forces
void setUpdateType(const StateUpdateType &updateType)
Set/Get the update type.
std::vector< double > m_massMatrix
Mesh used for Physics
This class stores the state of the unknown field variable in vectorized form.
Vectord & getUnknownVec()
Returns the unknown vectors.
Vectord m_qSolReduced
Vector of internal forces
Vectord m_Feff
Vector to maintain solution at each iteration of nonlinear solver
Vectord m_Finternal
Vector of effective forces
Vectord m_FexplicitExternalReduced
Vector of gravity forces
std::vector< double > m_stiffnessMatrix
Vega mass matrix
Vectord m_Fgravity
Vector of contact forces
StateUpdateType
Type of the update of the state of the body.
Vectord m_FcontactReduced
Vector to maintain solution at each iteration of nonlinear solver
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
void enableFixedBC()
Set the fixed BC implementation state.
Vectord m_Fcontact
integrator)