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.
feast_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_FEAST_CONDITION_NUMBER_UTILITY )
14 #define KRATOS_FEAST_CONDITION_NUMBER_UTILITY
15 
16 // System includes
17 
18 // External includes
19 
20 // Project includes
21 #include "spaces/ublas_space.h"
23 #ifdef USE_EIGEN_FEAST
25 #endif
26 
27 namespace Kratos
28 {
29 
32 
36 
40 
44 
48 
50 
53 template<class TSparseSpace = UblasSpace<double, CompressedMatrix, Vector>,
54  class TDenseSpace = UblasSpace<double, Matrix, Vector>
55  >
57 {
58 public:
59 
62 
65 
67  typedef std::size_t SizeType;
68  typedef std::size_t IndexType;
69 
73 
77 
81 
85 
86 
90 
97  static inline double GetConditionNumber(const SparseMatrixType& InputMatrix)
98  {
99 #ifdef USE_EIGEN_FEAST
100  typedef FEASTEigensystemSolver<true, double, double> FEASTSolverType;
101 
102  Parameters this_params(R"(
103  {
104  "solver_type" : "feast",
105  "symmetric" : true,
106  "number_of_eigenvalues" : 3,
107  "search_lowest_eigenvalues" : true,
108  "search_highest_eigenvalues" : false,
109  "e_min" : 0.0,
110  "e_max" : 1.0,
111  "echo_level" : 0
112  })");
113 
114  const std::size_t size_matrix = InputMatrix.size1();
115 
116  const double normA = TSparseSpace::TwoNorm(InputMatrix);
117 
118  this_params["e_max"].SetDouble(normA);
119  this_params["e_min"].SetDouble(-normA);
120 // this_params["number_of_eigenvalues"].SetInt(size_matrix * 2/3 - 1);
121 // this_params["subspace_size"].SetInt(3/2 * size_matrix + 1);
122  SparseMatrixType copy_matrix = InputMatrix;
123  SparseMatrixType identity_matrix(size_matrix, size_matrix);
124  for (IndexType i = 0; i < size_matrix; ++i)
125  identity_matrix.push_back(i, i, 1.0);
126 
127  // Create the auxilary eigen system
128  DenseMatrixType eigen_vectors(size_matrix, 1);
129  DenseVectorType eigen_values(size_matrix);
130 
131  // Create the FEAST solver
132  FEASTSolverType feast_solver_lowest(this_params);
133 
134  // Solve the problem
135  feast_solver_lowest.Solve(copy_matrix, identity_matrix, eigen_values, eigen_vectors);
136 
137  // Size of the eigen values vector
138  int dim_eigen_values = eigen_values.size();
139 
140  // We get the moduli of the eigen values
141  #pragma omp parallel for
142  for (int i = 0; i < dim_eigen_values; i++) {
143  eigen_values[i] = std::abs(eigen_values[i]);
144  }
145 
146  // Now we sort the vector
147  std::sort(eigen_values.begin(), eigen_values.end());
148 
149  const double lowest_eigen_value = eigen_values[0];
150 
151  // Create the FEAST solver
152  this_params["search_lowest_eigenvalues"].SetBool(false);
153  this_params["search_highest_eigenvalues"].SetBool(true);
154  FEASTSolverType feast_solver_highest(this_params);
155 
156  // Solve the problem
157  copy_matrix = InputMatrix;
158  feast_solver_highest.Solve(copy_matrix, identity_matrix, eigen_values, eigen_vectors);
159 
160  // Size of the eigen values vector
161  dim_eigen_values = eigen_values.size();
162 
163  // We get the moduli of the eigen values
164  #pragma omp parallel for
165  for (int i = 0; i < dim_eigen_values; i++) {
166  eigen_values[i] = std::abs(eigen_values[i]);
167  }
168 
169  // Now we sort the vector
170  std::sort(eigen_values.begin(), eigen_values.end());
171 
172  const double highest_eigen_value = eigen_values[dim_eigen_values - 1];
173 
174  // We compute the eigen value
175  const double condition_number = highest_eigen_value/lowest_eigen_value;
176 #else
177  const double condition_number = 0.0;
178  KRATOS_ERROR << "YOU MUST COMPILE FEAST IN ORDER TO USE THIS UTILITY" << std::endl;
179 #endif
180 
181  return condition_number;
182  }
183 
187 
188 
192 
193 
197 
201 
202 private:
203 
206 
210 
214 
218 
222 
226 
230 
234 
236 
238 
239 }; /* Class FEASTConditionNumberUtility */
240 
243 
244 
248 
249 } /* namespace Kratos.*/
250 
251 #endif /* KRATOS_FEAST_CONDITION_NUMBER_UTILITY defined */
252 
This utility uses the FEAST solver to obtain (estimate) the the condition number of a regular matrix.
Definition: feast_condition_number_utility.h:57
TDenseSpace::MatrixType DenseMatrixType
Dense space.
Definition: feast_condition_number_utility.h:75
std::size_t SizeType
Indexes.
Definition: feast_condition_number_utility.h:67
TSparseSpace::VectorType SparseVectorType
Definition: feast_condition_number_utility.h:72
KRATOS_CLASS_POINTER_DEFINITION(FEASTConditionNumberUtility)
Definition of the shared pointer of the class.
TSparseSpace::MatrixType SparseMatrixType
Sparse space.
Definition: feast_condition_number_utility.h:71
static double GetConditionNumber(const SparseMatrixType &InputMatrix)
Computes the condition number using the maximum and minimum eigenvalue of the system (in moduli)
Definition: feast_condition_number_utility.h:97
TDenseSpace::VectorType DenseVectorType
Definition: feast_condition_number_utility.h:76
std::size_t IndexType
Definition: feast_condition_number_utility.h:68
Definition: feast_eigensystem_solver.h:185
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
void SetDouble(const double Value)
This method sets the double contained in the current Parameter.
Definition: kratos_parameters.cpp:787
void SetBool(const bool Value)
This method sets the bool contained in the current Parameter.
Definition: kratos_parameters.cpp:803
#define KRATOS_ERROR
Definition: exception.h:161
Vector VectorType
Definition: geometrical_transformation_utilities.h:56
Matrix MatrixType
Definition: geometrical_transformation_utilities.h:55
double TwoNorm(SparseSpaceType &dummy, SparseSpaceType::VectorType &x)
Definition: add_strategies_to_python.cpp:164
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
integer i
Definition: TensorModule.f:17