iMSTK
Interactive Medical Simulation Toolkit
imstkConjugateGradient.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 "imstkIterativeLinearSolver.h"
10 
11 #include <Eigen/IterativeLinearSolvers>
12 
13 namespace imstk
14 {
15 class LinearProjectionConstraint;
16 
23 {
24 public:
26  ConjugateGradient(const SparseMatrixd& A, const Vectord& rhs);
27  ~ConjugateGradient() override = default;
28 
32  ConjugateGradient(const ConjugateGradient&) = delete;
33  ConjugateGradient(const ConjugateGradient&&) = delete;
34  ConjugateGradient& operator=(const ConjugateGradient&) = delete;
35  ConjugateGradient& operator=(const ConjugateGradient&&) = delete;
36 
40  //void iterate(Vectord& x, bool updateResidual = true) override {};
41 
45  void solve(Vectord& x) override;
46 
51  void solve(Vectord& x, const double tolerance);
52 
56  double getResidual(const Vectord& x) override;
57 
61  void setSystem(std::shared_ptr<LinearSystemType> newSystem) override;
62 
66  virtual void setMaxNumIterations(const size_t maxIter) override;
67 
71  void setTolerance(const double tolerance);
72 
76  void print() const override;
77 
81  void applyLinearProjectionFilter(Vectord& x, const std::vector<LinearProjectionConstraint>& linProj, const bool setVal);
82 
86  void setLinearProjectors(std::vector<LinearProjectionConstraint>* f)
87  {
88  m_FixedLinearProjConstraints = f;
89  }
90 
93  std::vector<LinearProjectionConstraint>& getLinearProjectors()
94  {
95  return *m_FixedLinearProjConstraints;
96  }
97 
100  void setDynamicLinearProjectors(std::vector<LinearProjectionConstraint>* f)
101  {
102  m_DynamicLinearProjConstraints = f;
103  }
104 
107  std::vector<LinearProjectionConstraint>& getDynamicLinearProjectors()
108  {
109  return *m_DynamicLinearProjConstraints;
110  }
111 
112 private:
116  void modifiedCGSolve(Vectord& x);
117 
119  Eigen::ConjugateGradient<SparseMatrixd> m_cgSolver;
120 
121  std::vector<LinearProjectionConstraint>* m_FixedLinearProjConstraints = nullptr;
122  std::vector<LinearProjectionConstraint>* m_DynamicLinearProjConstraints = nullptr;
123 };
124 } // namespace imstk
Base class for iterative linear solvers.
double getResidual(const Vectord &x) override
Return the error calculated by the solver.
void setDynamicLinearProjectors(std::vector< LinearProjectionConstraint > *f)
Get the vector denoting the filter.
Compound Geometry.
void setTolerance(const double tolerance)
Set solver tolerance.
virtual void setMaxNumIterations(const size_t maxIter) override
set/get the maximum number of iterations for the iterative solver.
void setLinearProjectors(std::vector< LinearProjectionConstraint > *f)
Get the vector denoting the filter.
void print() const override
Print solver information.
void setSystem(std::shared_ptr< LinearSystemType > newSystem) override
Sets the system. System of linear equations.
std::vector< LinearProjectionConstraint > & getDynamicLinearProjectors()
Get the vector denoting the filter.
void solve(Vectord &x) override
Do one iteration of the method.
void applyLinearProjectionFilter(Vectord &x, const std::vector< LinearProjectionConstraint > &linProj, const bool setVal)
Apply a filter to the vector supplied.
std::vector< LinearProjectionConstraint > & getLinearProjectors()
Get the vector denoting the filter.
Conjugate gradient sparse linear solver for Spd matrices.