7 #include "imstkConjugateGradient.h" 8 #include "imstkLinearProjectionConstraint.h" 9 #include "imstkLogger.h" 13 ConjugateGradient::ConjugateGradient()
15 m_type = Type::ConjugateGradient;
20 ConjugateGradient::ConjugateGradient(
const SparseMatrixd& A,
const Vectord& rhs) : ConjugateGradient()
22 this->setSystem(std::make_shared<LinearSystem<SparseMatrixd>>(A, rhs));
28 for (
auto& localProjector : linProj)
30 const auto threeI = 3 * localProjector.getNodeId();
31 Vec3d p = localProjector.getProjector() * Vec3d(x(threeI), x(threeI + 1), x(threeI + 2));
35 p += (Mat3d::Identity() - localProjector.getProjector()) * localProjector.getValue();
39 x(threeI + 1) = p.y();
40 x(threeI + 2) = p.z();
49 LOG(WARNING) <<
"Linear system is not supplied for CG solver!";
53 if (!(m_FixedLinearProjConstraints || m_DynamicLinearProjConstraints))
55 x = m_cgSolver.solve(m_linearSystem->getRHSVector());
59 this->modifiedCGSolve(x);
64 ConjugateGradient::modifiedCGSolve(Vectord& x)
66 const auto& b = m_linearSystem->getRHSVector();
67 const auto& A = m_linearSystem->getMatrix();
71 if (m_DynamicLinearProjConstraints)
73 applyLinearProjectionFilter(x, *m_DynamicLinearProjConstraints,
true);
76 if (m_FixedLinearProjConstraints)
78 applyLinearProjectionFilter(x, *m_FixedLinearProjConstraints,
true);
82 if (m_DynamicLinearProjConstraints)
84 applyLinearProjectionFilter(res, *m_DynamicLinearProjConstraints,
false);
86 if (m_FixedLinearProjConstraints)
88 applyLinearProjectionFilter(res, *m_FixedLinearProjConstraints,
false);
91 auto delta = res.dot(res);
92 const auto eps = m_tolerance * m_tolerance * delta;
100 if (m_DynamicLinearProjConstraints)
102 applyLinearProjectionFilter(q, *m_DynamicLinearProjConstraints,
false);
104 if (m_FixedLinearProjConstraints)
106 applyLinearProjectionFilter(q, *m_FixedLinearProjConstraints,
false);
108 double dotval = c.dot(q);
111 alpha = delta / dotval;
115 LOG(WARNING) <<
"Warning: denominator zero. Terminating MCG iteration!";
120 const double deltaPrev = delta;
121 delta = res.dot(res);
122 c *= delta / deltaPrev;
124 if (m_DynamicLinearProjConstraints)
126 applyLinearProjectionFilter(c, *m_DynamicLinearProjConstraints,
false);
128 if (m_FixedLinearProjConstraints)
130 applyLinearProjectionFilter(c, *m_FixedLinearProjConstraints,
false);
133 if (++iterNum >= m_maxIterations)
144 return m_cgSolver.error();
151 m_cgSolver.setTolerance(epsilon);
158 m_cgSolver.setMaxIterations(maxIter);
165 m_cgSolver.compute(m_linearSystem->getMatrix());
173 LOG(INFO) <<
"Solver: Conjugate gradient";
174 LOG(INFO) <<
"Tolerance: " << m_tolerance;
175 LOG(INFO) <<
"max. iterations: " << m_maxIterations;
181 this->setTolerance(tolerance);
void setTolerance(const double tolerance)
Set solver tolerance.
double getResidual(const Vectord &x) override
Return the error calculated by the solver.
size_t m_maxIterations
Maximum number of iterations to be performed.
double m_tolerance
default tolerance
void setTolerance(const double tolerance)
Set solver tolerance.
virtual void setMaxNumIterations(const size_t maxIter)
Do one iteration of the method.
virtual void setMaxNumIterations(const size_t maxIter) override
set/get the maximum number of iterations for the iterative solver.
void print() const override
Print solver information.
Represents the linear system of the form .
void print() const override
Print solver information.
void setSystem(std::shared_ptr< LinearSystemType > newSystem) override
Sets the system. System of linear equations.
void solve(Vectord &x) override
Do one iteration of the method.
Type m_type
Type of the scene object.
void applyLinearProjectionFilter(Vectord &x, const std::vector< LinearProjectionConstraint > &linProj, const bool setVal)
Apply a filter to the vector supplied.
virtual void setSystem(std::shared_ptr< LinearSystemType > newSystem)
Set/get the system. Replaces/Returns the stored linear system of equations.