KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
eigen_direct_solver.h
Go to the documentation of this file.
1 /* KRATOS _ _ ____ _
2 // | | (_)_ __ ___ __ _ _ __/ ___| ___ | |_ _____ _ __ ___
3 // | | | | '_ \ / _ \/ _` | '__\___ \ / _ \| \ \ / / _ \ '__/ __|
4 // | |___| | | | | __/ (_| | | ___) | (_) | |\ V / __/ | \__ |
5 // |_____|_|_| |_|\___|\__,_|_| |____/ \___/|_| \_/ \___|_| |___/ Application
6 //
7 // Author: Thomas Oberbichler
8 */
9 
10 #if !defined(KRATOS_EIGEN_SOLVER_H_INCLUDED)
11 #define KRATOS_EIGEN_SOLVER_H_INCLUDED
12 
13 // External includes
14 #include <Eigen/Core>
15 #include <Eigen/Sparse>
16 
17 // Project includes
18 #include "includes/define.h"
24 #include "spaces/ublas_space.h"
25 
26 namespace Kratos {
27 
28 template <typename Scalar>
29 struct SpaceType;
30 
31 template <>
33 {
36 };
37 
38 template <>
39 struct SpaceType<std::complex<double>>
40 {
43 };
44 
45 template <
46  class TSolverType,
47  class TSparseSpaceType = typename SpaceType<typename TSolverType::Scalar>::Global,
48  class TDenseSpaceType = typename SpaceType<typename TSolverType::Scalar>::Local,
49  class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType>>
51  : public DirectSolver<TSparseSpaceType, TDenseSpaceType, TReordererType>
52 {
53 private:
54  TSolverType m_solver;
55 
56  EigenDirectSolver &operator=(const EigenDirectSolver &Other);
57 
59 
61 
62 public:
64 
66 
68 
70 
71  typedef typename TSparseSpaceType::DataType DataType;
72 
74 
76 
78 
79  EigenDirectSolver(Parameters settings) : BaseType(settings)
80  {
81  m_solver.Initialize(settings);
82  }
83 
84  ~EigenDirectSolver() override {}
85 
96  {
97  m_a_wrapper = UblasWrapper<DataType>(rA);
98 
99  const auto& a = m_a_wrapper.matrix();
100 
101  const bool success = m_solver.Compute(a);
102 
103  KRATOS_ERROR_IF(!success) << "Decomposition failed!" << std::endl;
104  }
105 
114  {
115  Eigen::Map<Kratos::EigenDynamicVector<DataType>> x(rX.data().begin(), rX.size());
116  Eigen::Map<Kratos::EigenDynamicVector<DataType>> b(rB.data().begin(), rB.size());
117 
118  const bool success = m_solver.Solve(b, x);
119 
120  KRATOS_ERROR_IF(!success) << "Solving failed!\n" << m_solver.GetSolverErrorMessages() << std::endl;
121  }
122 
130  bool Solve(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
131  {
132  InitializeSolutionStep(rA, rX, rB);
133  PerformSolutionStep(rA, rX, rB);
134 
135  return true;
136  }
137 
146  {
147  return false;
148  }
149 
153  void PrintInfo(std::ostream &rOStream) const override
154  {
155  m_solver.PrintInfo(rOStream);
156  }
157 
161  void PrintData(std::ostream &rOStream) const override
162  {
163  }
164 
166  {
168  }
169 }; // class EigenDirectSolver
170 
174 template<
175  class TSolverType,
176  class TSparseSpaceType,
177  class TDenseSpaceType,
178  class TReordererType>
179 inline std::istream &operator>>(
180  std::istream &rIStream,
182 {
183  return rIStream;
184 }
185 
189 template<
190  class TSolverType,
191  class TSparseSpaceType,
192  class TDenseSpaceType,
193  class TReordererType
194  >
195 inline std::ostream &operator<<(
196  std::ostream &rOStream,
198 {
199  rThis.PrintInfo(rOStream);
200  rOStream << std::endl;
201  rThis.PrintData(rOStream);
202 
203  return rOStream;
204 }
205 
206 } // namespace Kratos
207 
208 #endif // defined(KRATOS_EIGEN_SOLVER_H_INCLUDED)
Definition: direct_solver.h:48
Definition: eigen_direct_solver.h:52
bool Solve(SparseMatrixType &rA, DenseMatrixType &rX, DenseMatrixType &rB) override
Definition: eigen_direct_solver.h:145
TDenseSpaceType::MatrixType DenseMatrixType
Definition: eigen_direct_solver.h:73
EigenDirectSolver(Parameters settings)
Definition: eigen_direct_solver.h:79
TSparseSpaceType::VectorType VectorType
Definition: eigen_direct_solver.h:69
DirectSolver< TSparseSpaceType, TDenseSpaceType, TReordererType > BaseType
Definition: eigen_direct_solver.h:65
StandardLinearSolverFactory< TSparseSpaceType, TDenseSpaceType, EigenDirectSolver > FactoryType
Definition: eigen_direct_solver.h:75
TSparseSpaceType::DataType DataType
Definition: eigen_direct_solver.h:71
void PrintData(std::ostream &rOStream) const override
Definition: eigen_direct_solver.h:161
void InitializeSolutionStep(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: eigen_direct_solver.h:95
KRATOS_CLASS_POINTER_DEFINITION(EigenDirectSolver)
bool Solve(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: eigen_direct_solver.h:130
static StandardLinearSolverFactory< TSparseSpaceType, TDenseSpaceType, EigenDirectSolver > Factory()
Definition: eigen_direct_solver.h:165
EigenDirectSolver()
Definition: eigen_direct_solver.h:77
TSparseSpaceType::MatrixType SparseMatrixType
Definition: eigen_direct_solver.h:67
void PrintInfo(std::ostream &rOStream) const override
Definition: eigen_direct_solver.h:153
~EigenDirectSolver() override
Definition: eigen_direct_solver.h:84
void PerformSolutionStep(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: eigen_direct_solver.h:113
Definition: amatrix_interface.h:41
TSparseSpaceType::MatrixType SparseMatrixType
Definition: linear_solver.h:73
TDenseSpaceType::MatrixType DenseMatrixType
Definition: linear_solver.h:81
TSparseSpaceType::VectorType VectorType
Definition: linear_solver.h:77
virtual void Initialize(SparseMatrixType &rA, VectorType &rX, VectorType &rB)
Definition: linear_solver.h:131
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
Base class for all reorderer objects in Kratos used in linear solvers.
Definition: reorderer.h:60
Here we add the functions needed for the registration of linear solvers.
Definition: standard_linear_solver_factory.h:62
A class template for handling data types, matrices, and vectors in a Ublas space.
Definition: ublas_space.h:121
const Eigen::Map< const TEigenSparseMatrix > & matrix() const
Definition: ublas_wrapper.h:53
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
Vector VectorType
Definition: geometrical_transformation_utilities.h:56
Matrix MatrixType
Definition: geometrical_transformation_utilities.h:55
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
DenseMatrix< std::complex< double > > ComplexMatrix
Definition: ublas_complex_interface.h:58
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
TABLE_NUMBER_ANGULAR_VELOCITY TABLE_NUMBER_MOMENT I33 BEAM_INERTIA_ROT_UNIT_LENGHT_Y KRATOS_DEFINE_APPLICATION_VARIABLE(DEM_APPLICATION, double, BEAM_INERTIA_ROT_UNIT_LENGHT_Z) typedef std double
Definition: DEM_application_variables.h:182
compressed_matrix< std::complex< double > > ComplexCompressedMatrix
Definition: ublas_complex_interface.h:67
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
a
Definition: generate_stokes_twofluid_element.py:77
b
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:31
x
Definition: sensitivityMatrix.py:49
namespace
Definition: array_1d.h:793
Definition: eigen_direct_solver.h:29