iMSTK
Interactive Medical Simulation Toolkit
imstkJacobi.cpp
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 #include "imstkJacobi.h"
8 #include "imstkLinearProjectionConstraint.h"
9 #include "imstkLogger.h"
10 
11 namespace imstk
12 {
13 Jacobi::Jacobi(const SparseMatrixd& A, const Vectord& rhs) : Jacobi()
14 {
15  this->setSystem(std::make_shared<LinearSystem<SparseMatrixd>>(A, rhs));
16 }
17 
18 void
19 Jacobi::solve(Vectord& x)
20 {
21  if (!m_linearSystem)
22  {
23  LOG(WARNING) << "Jacobi::solve: Linear system is not supplied for Gauss-Seidel solver!";
24  return;
25  }
26 
27  if (m_FixedLinearProjConstraints->size() == 0)
28  {
29  this->JacobiSolve(x);
30  }
31  else
32  {
33  // Do nothing for now!
34  }
35 }
36 
37 void
38 Jacobi::solve(Vectord& x, const double tolerance)
39 {
40  this->setTolerance(tolerance);
41  this->solve(x);
42 }
43 
44 void
46 {
47  const auto& b = m_linearSystem->getRHSVector();
48  const auto& A = m_linearSystem->getMatrix();
49 
50  // Set the initial guess to zero
51  x.setZero();
52 
53  auto xOld = x;
54  size_t iterNum = 0;
55  while (iterNum < this->getMaxNumIterations())
56  {
57  for (auto k = 0; k < A.outerSize(); ++k)
58  {
59  double diagEle = 0.;
60  double aggregate = 0.;
61  for (SparseMatrixd::InnerIterator it(A, k); it; ++it)
62  {
63  auto col = it.col();
64  if (k != col)
65  {
66  aggregate += it.value() * xOld[col];
67  }
68  else
69  {
70  diagEle = it.value();
71  }
72  }
73  x[k] = (b[k] - aggregate) / diagEle; // div by zero is possible!
74  }
75 
76  if ((x - xOld).norm() < 1.0e-4)
77  {
78  return;
79  }
80  xOld = x;
81  iterNum++;
82  }
83 }
84 
85 double
86 Jacobi::getResidual(const Vectord&)
87 {
88  return 0;
89 }
90 
91 void
92 Jacobi::setTolerance(const double epsilon)
93 {
95 }
96 
97 void
98 Jacobi::setMaxNumIterations(const size_t maxIter)
99 {
101 }
102 
103 void
105 {
107 }
108 
109 void
111 {
113 
114  LOG(INFO) << "Solver: Jacobi";
115  LOG(INFO) << "Tolerance: " << m_tolerance;
116  LOG(INFO) << "max. iterations: " << m_maxIterations;
117 }
118 } // namespace imstk
void setTolerance(const double tolerance)
Set solver tolerance.
size_t m_maxIterations
Maximum number of iterations to be performed.
virtual void setMaxNumIterations(const size_t maxIter) override
set/get the maximum number of iterations for the iterative solver.
Definition: imstkJacobi.cpp:98
double m_tolerance
default tolerance
virtual void solve(Vectord &x) override
Solve the linear system using Gauss-Seidel iterations.
Compound Geometry.
void setTolerance(const double tolerance)
Set solver tolerance.
Definition: imstkJacobi.cpp:92
virtual void setMaxNumIterations(const size_t maxIter)
Do one iteration of the method.
void print() const override
Print solver information.
Represents the linear system of the form .
double getResidual(const Vectord &x) override
Return the error calculated by the solver.
Definition: imstkJacobi.cpp:86
void setSystem(std::shared_ptr< LinearSystemType > newSystem) override
Sets the system. System of linear equations.
void print() const override
Print solver information.
void JacobiSolve(Vectord &x)
Do one iteration of the method.
Definition: imstkJacobi.cpp:45
void solve(Vectord &x) override
Solve the system of equations.
Definition: imstkJacobi.cpp:19
virtual void setSystem(std::shared_ptr< LinearSystemType > newSystem)
Set/get the system. Replaces/Returns the stored linear system of equations.
std::shared_ptr< LinearSystemType > m_linearSystem
Linear system of equations.