iMSTK
Interactive Medical Simulation Toolkit
imstkSOR.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 "imstkSOR.h"
8 #include "imstkLogger.h"
9 #include "imstkLinearProjectionConstraint.h"
10 
11 namespace imstk
12 {
13 SOR::SOR(const SparseMatrixd& A, const Vectord& rhs) : SOR()
14 {
15  this->setSystem(std::make_shared<LinearSystem<SparseMatrixd>>(A, rhs));
16 }
17 
18 void
19 SOR::solve(Vectord& x)
20 {
21  if (!m_linearSystem)
22  {
23  LOG(WARNING) << "SOR::solve: Linear system is not supplied for Gauss-Seidel solver!";
24  return;
25  }
26 
27  if (m_FixedLinearProjConstraints->size() == 0)
28  {
29  this->SORSolve(x);
30  }
31  else
32  {
33  // Do nothing for now!
34  }
35 }
36 
37 void
38 SOR::solve(Vectord& x, const double tolerance)
39 {
40  this->setTolerance(tolerance);
41  this->solve(x);
42 }
43 
44 void
45 SOR::SORSolve(Vectord& x)
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() * x[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  x *= m_relaxationFactor;
76  x += (1. - m_relaxationFactor) * xOld;
77 
78  if ((x - xOld).norm() < 1.0e-4)
79  {
80  return;
81  }
82  xOld = x;
83  iterNum++;
84  }
85 }
86 
87 double
88 SOR::getResidual(const Vectord&)
89 {
90  return 0;
91 }
92 
93 void
94 SOR::setTolerance(const double epsilon)
95 {
97 }
98 
99 void
100 SOR::setMaxNumIterations(const size_t maxIter)
101 {
103 }
104 
105 void
106 SOR::setSystem(std::shared_ptr<LinearSystem<SparseMatrixd>> newSystem)
107 {
109 }
110 
111 void
112 SOR::print() const
113 {
115 
116  LOG(INFO) << "Solver: SOR";
117  LOG(INFO) << "Tolerance: " << m_tolerance;
118  LOG(INFO) << "max. iterations: " << m_maxIterations;
119 }
120 } // namespace imstk
void setTolerance(const double tolerance)
Set solver tolerance.
void solve(Vectord &x) override
Solve the system of equations.
Definition: imstkSOR.cpp:19
void setTolerance(const double tolerance)
Set solver tolerance.
Definition: imstkSOR.cpp:94
Compound Geometry.
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 .
void print() const override
Print solver information.
Definition: imstkSOR.cpp:112
double getResidual(const Vectord &x) override
Return the error calculated by the solver.
Definition: imstkSOR.cpp:88
void SORSolve(Vectord &x)
Do one iteration of the method.
Definition: imstkSOR.cpp:45
void setMaxNumIterations(const size_t maxIter) override
set/get the maximum number of iterations for the iterative solver.
Definition: imstkSOR.cpp:100
virtual void setSystem(std::shared_ptr< LinearSystemType > newSystem)
Set/get the system. Replaces/Returns the stored linear system of equations.
void setSystem(std::shared_ptr< LinearSystemType > newSystem) override
Sets the system. System of linear equations.
Definition: imstkSOR.cpp:106