G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsSolverUtils.h
Go to the documentation of this file.
1
14#pragma once
15
16namespace gismo
17{
18
19
23template<class T>
25{
26public:
27
37 static T convergenceRateLS(std::vector<T> const & error_list,
38 std::vector<T> const & h_list)
39 {
40 if (error_list.size() != h_list.size())
41 {
42 gsWarn << "The lists does not have same size!\n";
43 return 0;
44 }
45
46 const index_t Nsize = error_list.size();
47
48 // Define the matrix and vector for the least square problem
49 gsMatrix<T> A(Nsize,2);
50 A.col(1).setOnes();
51 A.col(0) = gsAsConstMatrix<T>(h_list, Nsize, 1) .array().log();
52 gsVector<T> y = gsAsConstMatrix<T>(error_list, Nsize, 1).array().log();
53
54 // Solve the least square problem
55 gsVector<T> rate = A.fullPivHouseholderQr().solve(y);
56
57 // Return the convergence rate
58 return rate(0);
59 }
60
70 static T conditionNumber(const gsMatrix<T> & matrix, bool removeSingularity = false)
71 {
72 GISMO_ENSURE(matrix.cols() == matrix.rows(), "Matrix must be square!");
73
74 unsigned N_size = matrix.cols();
75
76 if (N_size >= 10000)
77 gsWarn << "Matrix has dimension " << N_size <<". It might take long to find eigenvalues...\n";
78
79 // Compute eigenvalues
80 //gsMatrix<T> eigen_values = A.eigenvalues().real();
81 typename gsMatrix<T>::EigenSolver eigen_values;
82 eigen_values.compute(matrix, false);
83
84 T tmp = math::abs(eigen_values.eigenvalues()(0,0).real());
85
86 T eigen_low = tmp;
87 T eigen_high= tmp;
88
89 // Find lowest and highest eigenvalue
90 for (unsigned k=0; k< N_size; ++k)
91 {
92 tmp = math::abs(eigen_values.eigenvalues()(k,0).real());
93
94 // Remove the one zero eigenvalue from not using pressure avg.
95 if (tmp < 1e-13 && removeSingularity)
96 {
97 removeSingularity = false;
98 gsDebug << "Removed the eigen value: " << tmp << "\n";
99 // In case the first eigenvalue has value zero, we use the second eigenvalue.
100 if (k == 0)
101 {
102 tmp = math::abs(eigen_values.eigenvalues()(k+1,0).real());
103 eigen_low = tmp;
104 eigen_high= tmp;
105 }
106 continue;
107 }
108 // Store its absolute value if lower the eigen_low or higher then eigen_high
109 if (tmp < eigen_low ) {eigen_low = tmp;}
110 if (tmp > eigen_high) {eigen_high = tmp;}
111 }
112 //gsDebug << "Highest eigen value: " << eigen_high << " Lowest eigen value: " << eigen_low<< "\n";
113 // Return condition number
114 return eigen_high/eigen_low;
115 }
116
118 static T conditionNumber(const gsSparseMatrix<T> & matrix )
119 {
120 unsigned N_size = matrix.cols();
121
122 if (N_size >= 10000)
123 gsWarn << "Matrix has dimension " << N_size <<". It might take long to find eigenvalues...\n" << std::flush;
124
125 // Copy to a dense matrix
126 gsMatrix<T> matrixDense(matrix);
127
128 return conditionNumber(matrixDense);
129 }
130
131private:
132 gsSolverUtils() {} // No objects of this class
133
134};
135
136} // namespace gismo
Creates a mapped object or data pointer to a const matrix without copying data.
Definition gsAsMatrix.h:141
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41
Utility class for PDE's solver related utils.
Definition gsSolverUtils.h:25
static T conditionNumber(const gsMatrix< T > &matrix, bool removeSingularity=false)
Finds the spectral condition number of a small matrix.
Definition gsSolverUtils.h:70
static T conditionNumber(const gsSparseMatrix< T > &matrix)
Finds the spectral condition number of a small matrix.
Definition gsSolverUtils.h:118
static T convergenceRateLS(std::vector< T > const &error_list, std::vector< T > const &h_list)
Finds the convergence rate for a list of errors and a list with elements sizes by the least square me...
Definition gsSolverUtils.h:37
Sparse matrix class, based on gsEigen::SparseMatrix.
Definition gsSparseMatrix.h:139
A vector with arbitrary coefficient type and fixed or dynamic size.
Definition gsVector.h:37
#define index_t
Definition gsConfig.h:32
#define gsDebug
Definition gsDebug.h:61
#define gsWarn
Definition gsDebug.h:50
#define GISMO_ENSURE(cond, message)
Definition gsDebug.h:102
The G+Smo namespace, containing all definitions for the library.