iMSTK
Interactive Medical Simulation Toolkit
imstkDirectLinearSolver.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 "imstkDirectLinearSolver.h"
8 #include "imstkLogger.h"
9 
10 namespace imstk
11 {
12 DirectLinearSolver<Matrixd>::
13 DirectLinearSolver(const Matrixd& matrix, const Vectord& b)
14 {
15  m_linearSystem = std::make_shared<LinearSystem<Matrixd>>(matrix, b);
16  m_solver.compute(matrix);
17 }
18 
19 void
21 setSystem(std::shared_ptr<LinearSystem<Matrixd>> newSystem)
22 {
24  m_solver.compute(m_linearSystem->getMatrix());
25 }
26 
28 DirectLinearSolver(const SparseMatrixd& matrix, const Vectord& b)
29 {
30  m_linearSystem = std::make_shared<LinearSystem<SparseMatrixd>>(matrix, b);
31  m_solver.compute(matrix);
32 }
33 
34 void
36 setSystem(std::shared_ptr<LinearSystem<SparseMatrixd>> newSystem)
37 {
39  m_solver.compute(m_linearSystem->getMatrix());
40 }
41 
42 void
43 DirectLinearSolver<SparseMatrixd>::solve(const Vectord& rhs, Vectord& x)
44 {
45  if (!m_linearSystem)
46  {
47  LOG(FATAL) << "Linear system has not been set";
48  }
49  x = m_solver.solve(rhs);
50 }
51 
52 void
54 {
55  if (!m_linearSystem)
56  {
57  LOG(FATAL) << "Linear system has not been set";
58  }
59  x.setZero();
60 
61  auto b = m_linearSystem->getRHSVector();
62  x = m_solver.solve(b);
63 }
64 
65 void
66 DirectLinearSolver<Matrixd>::solve(const Vectord& rhs, Vectord& x)
67 {
68  if (!m_linearSystem)
69  {
70  LOG(FATAL) << "Linear system has not been set";
71  }
72  x = m_solver.solve(rhs);
73 }
74 
75 void
77 {
78  if (!m_linearSystem)
79  {
80  LOG(FATAL) << "Linear system has not been set";
81  }
82  x.setZero();
83 
84  auto b = m_linearSystem->getRHSVector();
85  x = m_solver.solve(b);
86 }
87 } // namespace imstk
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrixd
A dynamic size matrix of doubles.
Definition: imstkMath.h:97
Compound Geometry.
Represents the linear system of the form .
Dense direct solvers. Solves a dense system of equations using Cholesky decomposition.
void setSystem(std::shared_ptr< LinearSystemType > newSystem) override
Sets the system. System of linear equations.
virtual void setSystem(std::shared_ptr< LinearSystemType > newSystem)
Set/get the system. Replaces/Returns the stored linear system of equations.
DirectLinearSolver()=default
Default constructor/destructor.