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.
condition_number_utility.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Vicente Mataix Ferrandiz
11 //
12 
13 #if !defined(KRATOS_COND_NUMBER_UTILITY )
14 #define KRATOS_COND_NUMBER_UTILITY
15 
16 /* System includes */
17 
18 /* External includes */
19 
20 /* Project includes */
21 #include "spaces/ublas_space.h"
22 #include "utilities/math_utils.h"
23 
24 // Linear solvers
29 
30 namespace Kratos
31 {
32 
35 
39 
43 
47 
51 
60 {
61 public:
62 
65 
68 
70  typedef std::size_t SizeType;
71 
73  typedef std::size_t IndexType;
74 
77 
80 
83 
86 
89 
92 
95 
98 
101 
104 
107 
111 
115 
121  {
122  // definition of empty parameters
123  Parameters empty_parameters = Parameters(R"({})" );
124  LinearSolverType::Pointer psolver = LinearSolverType::Pointer( new SkylineLUFactorizationSolverType() );
125  mpEigenSolverMax = LinearSolverType::Pointer( new PowerIterationHighestEigenvalueSolverType(empty_parameters, psolver) );
126  mpEigenSolverMin = LinearSolverType::Pointer( new PowerIterationEigenvalueSolverType(empty_parameters, psolver) );
127  }
128 
135  LinearSolverType::Pointer pEigenSolverMax,
136  LinearSolverType::Pointer pEigenSolverMin
137  ) :mpEigenSolverMax(pEigenSolverMax),
138  mpEigenSolverMin(pEigenSolverMin)
139  {}
140 
143 
147 
153  double GetConditionNumber(SparseMatrixType& rInputMatrix)
154  {
155  KRATOS_ERROR_IF(mpEigenSolverMax == nullptr || mpEigenSolverMin == nullptr) << "ERROR:: PLEASE DEFINE THE EigenSolvers" << std::endl;
156  return GetConditionNumber(rInputMatrix, mpEigenSolverMax, mpEigenSolverMin);
157  }
158 
167  SparseMatrixType& rInputMatrix,
168  LinearSolverType::Pointer pEigenSolverMax,
169  LinearSolverType::Pointer pEigenSolverMin
170  )
171  {
172  // The eigen system
173  DenseVectorType eigen_values;
174  DenseMatrixType eigen_vectors;
175 
176  const SizeType size_matrix = rInputMatrix.size1();
177 
178  SparseMatrixType identity_matrix(size_matrix, size_matrix);
179  for (IndexType i = 0; i < size_matrix; ++i)
180  identity_matrix.push_back(i, i, 1.0);
181 
182  pEigenSolverMax->Solve(rInputMatrix, identity_matrix, eigen_values, eigen_vectors);
183  const double max_lambda = eigen_values[0];
184 
185  pEigenSolverMin->Solve(rInputMatrix, identity_matrix, eigen_values, eigen_vectors);
186  const double min_lambda = eigen_values[0];
187 
188  KRATOS_ERROR_IF(min_lambda < std::numeric_limits<double>::epsilon()) << "ERROR:: NOT POSSIBLE TO COMPUTE CONDITION NUMBER. ZERO EIGENVALUE" << std::endl;
189 
190  const double condition_number = std::abs(max_lambda)/std::abs(min_lambda);
191 
192  return condition_number;
193  }
194 
198 
199 
203 
204 
208 
212 
213 private:
214 
217 
221 
222  LinearSolverType::Pointer mpEigenSolverMax;
223  LinearSolverType::Pointer mpEigenSolverMin;
224 
228 
232 
236 
240 
244 
248 
249 }; /* Class ConditionNumberUtility */
250 
253 
254 
258 
259 } /* namespace Kratos.*/
260 
261 #endif /* KRATOS_COND_NUMBER_UTILITY defined */
262 
Utility to compute the condition number.
Definition: condition_number_utility.h:60
std::size_t IndexType
The index type.
Definition: condition_number_utility.h:73
PowerIterationHighestEigenvalueSolver< SparseSpaceType, LocalSpaceType, LinearSolverType > PowerIterationHighestEigenvalueSolverType
Power iteration solver for the highest eigenvalue.
Definition: condition_number_utility.h:103
SkylineLUFactorizationSolver< SparseSpaceType, LocalSpaceType, ReordererType > SkylineLUFactorizationSolverType
Skyline solver definion.
Definition: condition_number_utility.h:100
ConditionNumberUtility()
Default constructor.
Definition: condition_number_utility.h:120
SparseSpaceType::VectorType VectorType
The vector considered.
Definition: condition_number_utility.h:85
KRATOS_CLASS_POINTER_DEFINITION(ConditionNumberUtility)
Pointer definition of ConditionNumberUtility.
virtual ~ConditionNumberUtility()
Destructor.
Definition: condition_number_utility.h:142
PowerIterationEigenvalueSolver< SparseSpaceType, LocalSpaceType, LinearSolverType > PowerIterationEigenvalueSolverType
Power iteration solver for the lowest eigenvalue.
Definition: condition_number_utility.h:106
LinearSolver< SparseSpaceType, LocalSpaceType > LinearSolverType
The definion of the linear solver.
Definition: condition_number_utility.h:94
SparseSpaceType::MatrixType SparseMatrixType
The compressed matrix.
Definition: condition_number_utility.h:82
ConditionNumberUtility(LinearSolverType::Pointer pEigenSolverMax, LinearSolverType::Pointer pEigenSolverMin)
Default constructor.
Definition: condition_number_utility.h:134
UblasSpace< double, Matrix, Vector > LocalSpaceType
The dense space considered.
Definition: condition_number_utility.h:79
double GetConditionNumber(SparseMatrixType &rInputMatrix)
This function computes using the inverse power method the minimal eigenvalue.
Definition: condition_number_utility.h:153
Reorderer< SparseSpaceType, LocalSpaceType > ReordererType
The reorder considered.
Definition: condition_number_utility.h:97
LocalSpaceType::VectorType DenseVectorType
The dense vector.
Definition: condition_number_utility.h:91
UblasSpace< double, CompressedMatrix, boost::numeric::ublas::vector< double > > SparseSpaceType
The sparse space considered (the one containing the compressed matrix)
Definition: condition_number_utility.h:76
LocalSpaceType::MatrixType DenseMatrixType
The dense matrix.
Definition: condition_number_utility.h:88
std::size_t SizeType
The sisze type.
Definition: condition_number_utility.h:70
double GetConditionNumber(SparseMatrixType &rInputMatrix, LinearSolverType::Pointer pEigenSolverMax, LinearSolverType::Pointer pEigenSolverMin)
This function computes using the inverse power method the minimal eigenvalue.
Definition: condition_number_utility.h:166
Base class for all the linear solvers in Kratos.
Definition: linear_solver.h:65
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
This class uses the inverted power iteration method to obtain the lowest eigenvalue of a system.
Definition: power_iteration_eigenvalue_solver.h:64
This class uses the inverted power iteration method to obtain the lowest eigenvalue of a system.
Definition: power_iteration_highest_eigenvalue_solver.h:63
Base class for all reorderer objects in Kratos used in linear solvers.
Definition: reorderer.h:60
Definition: skyline_lu_factorization_solver.h:562
A class template for handling data types, matrices, and vectors in a Ublas space.
Definition: ublas_space.h:121
TMatrixType MatrixType
The matrix type considered.
Definition: ublas_space.h:133
TVectorType VectorType
The vector type considered.
Definition: ublas_space.h:136
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
integer i
Definition: TensorModule.f:17