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_dense_jacobi_svd_decomposition.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: Ruben Zorrilla
11 //
12 
13 #if !defined(KRATOS_EIGEN_DENSE_JACOBI_SVD_H_INCLUDED)
14 #define KRATOS_EIGEN_DENSE_JACOBI_SVD_H_INCLUDED
15 
16 // External includes
17 #include <Eigen/SVD>
18 
19 // Project includes
20 #include "includes/define.h"
21 #include "linear_solvers_define.h"
24 
25 namespace Kratos {
26 
29 
33 
37 
41 
45 
46 template<class TDenseSpace>
48 {
49 public:
50 
53 
56 
57  typedef typename TDenseSpace::DataType DataType;
60 
63  using DecompositionOptions = Eigen::DecompositionOptions;
64 
68 
69  EigenDenseJacobiSVD() = default;
70 
74 
75 
79 
80  static std::string Name()
81  {
82  return "dense_jacobi_singular_value_decomposition";
83  }
84 
85  void Compute(
86  MatrixType& rInputMatrix,
87  Parameters Settings) override
88  {
89  // Check user defined options
90  Parameters default_settings = Parameters(R"(
91  {
92  "compute_u" : true,
93  "compute_v" : true,
94  "compute_thin_u" : true,
95  "compute_thin_v" : true
96  })");
97  Settings.ValidateAndAssignDefaults(default_settings);
98 
99  // Set the Eigen decomposition options
100  int decomposition_options = 0;
101  if (Settings["compute_u"].GetBool()) {
102  decomposition_options |= Settings["compute_thin_u"].GetBool() ? Eigen::ComputeThinU : Eigen::ComputeFullU;
103  }
104  if (Settings["compute_v"].GetBool()) {
105  decomposition_options |= Settings["compute_thin_v"].GetBool() ? Eigen::ComputeThinV : Eigen::ComputeFullV;
106  }
107 
108  // Compute the Bidiagonal Divide and Conquer Singular Value Decomposition (BDCSVD)
109  Eigen::Map<EigenMatrix> eigen_input_matrix_map(rInputMatrix.data().begin(), rInputMatrix.size1(), rInputMatrix.size2());
110  mJacobiSVD.compute(eigen_input_matrix_map, decomposition_options);
111  }
112 
113  void Compute(
114  MatrixType& rInputMatrix,
115  VectorType& rVectorS,
116  MatrixType& rMatrixU,
117  MatrixType& rMatrixV,
118  Parameters Settings) override
119  {
120  Compute(rInputMatrix, Settings);
121  SingularValues(rVectorS);
122  MatrixU(rMatrixU);
123  MatrixV(rMatrixV);
124  }
125 
126  void MatrixU(MatrixType& rMatrixU) override
127  {
128  KRATOS_ERROR_IF_NOT(mJacobiSVD.computeU()) << "Matrix U has not been computed. Switch \'compute_u\' to \'true\' in the \'Compute\' input settings." << std::endl;
129 
130  const auto& r_matrix_U = mJacobiSVD.matrixU();
131  const std::size_t m = r_matrix_U.rows();
132  const std::size_t n = r_matrix_U.cols();
133  if (rMatrixU.size1() != m || rMatrixU.size2() != n) {
134  rMatrixU.resize(m,n);
135  }
136 
137  Eigen::Map<EigenMatrix> matrix_u_map(rMatrixU.data().begin(), rMatrixU.size1(), rMatrixU.size2());
138  matrix_u_map = r_matrix_U;
139  }
140 
141  void MatrixV(MatrixType& rMatrixV) override
142  {
143  KRATOS_ERROR_IF_NOT(mJacobiSVD.computeV()) << "Matrix V has not been computed. Switch \'compute_v\' to \'true\' in the \'Compute\' input settings." << std::endl;
144 
145  const auto& r_matrix_V = mJacobiSVD.matrixV();
146  const std::size_t m = r_matrix_V.rows();
147  const std::size_t n = r_matrix_V.cols();
148  if (rMatrixV.size1() != m || rMatrixV.size2() != n) {
149  rMatrixV.resize(m,n);
150  }
151 
152  Eigen::Map<EigenMatrix> matrix_v_map(rMatrixV.data().begin(), rMatrixV.size1(), rMatrixV.size2());
153  matrix_v_map = r_matrix_V;
154  }
155 
156  void SingularValues(VectorType& rVectorS) override
157  {
158  const auto& r_vector_S = mJacobiSVD.singularValues();
159  const std::size_t m = r_vector_S.rows();
160  if (rVectorS.size() != m) {
161  rVectorS.resize(m);
162  }
163 
164  Eigen::Map<EigenVector> vector_s_map(rVectorS.data().begin(), rVectorS.size());
165  vector_s_map = r_vector_S;
166  }
167 
168  std::size_t NonZeroSingularValues() override
169  {
170  return mJacobiSVD.nonzeroSingularValues();
171  }
172 
173  void SetThreshold(const double RelTolerance) override
174  {
175  mJacobiSVD.setThreshold(RelTolerance);
176  }
177 
178  std::size_t Rank() override
179  {
180  return mJacobiSVD.rank();
181  }
182 
183  void PrintInfo(std::ostream &rOStream) const override
184  {
185  rOStream << "EigenDecomposition <" << Name() << "> finished.";
186  }
187 
191 
192 
196 
197 
201 
202 
206 
207 
209 private:
212 
213 
217 
218  Eigen::JacobiSVD<EigenMatrix> mJacobiSVD;
219 
223 
224 
228 
229 
233 
234 
238 
239 
243 
244 
248 
249 
251 };
252 
255 
256 
260 
261 
263 } // namespace Kratos
264 
265 #endif // defined(KRATOS_EIGEN_DENSE_JACOBI_SVD_H_INCLUDED)
Definition: dense_svd_decomposition.h:45
Definition: eigen_dense_jacobi_svd_decomposition.h:48
void SingularValues(VectorType &rVectorS) override
Definition: eigen_dense_jacobi_svd_decomposition.h:156
std::size_t Rank() override
Rank of the provided array Calculates and returns the rank of the array decomposed with the SVD.
Definition: eigen_dense_jacobi_svd_decomposition.h:178
TDenseSpace::MatrixType MatrixType
Definition: eigen_dense_jacobi_svd_decomposition.h:59
static std::string Name()
Definition: eigen_dense_jacobi_svd_decomposition.h:80
Kratos::EigenDynamicVector< DataType > EigenVector
Definition: eigen_dense_jacobi_svd_decomposition.h:61
void SetThreshold(const double RelTolerance) override
Set the relative threshold tolerance This method sets the relative threshold tolerance to consider si...
Definition: eigen_dense_jacobi_svd_decomposition.h:173
std::size_t NonZeroSingularValues() override
Number of non-zero singular values This method returns the number of non-zero singular values.
Definition: eigen_dense_jacobi_svd_decomposition.h:168
KRATOS_CLASS_POINTER_DEFINITION(EigenDenseJacobiSVD)
Definition of the shared pointer of the class.
void MatrixU(MatrixType &rMatrixU) override
Definition: eigen_dense_jacobi_svd_decomposition.h:126
Kratos::EigenDynamicMatrix< DataType > EigenMatrix
Definition: eigen_dense_jacobi_svd_decomposition.h:62
TDenseSpace::VectorType VectorType
Definition: eigen_dense_jacobi_svd_decomposition.h:58
void MatrixV(MatrixType &rMatrixV) override
Definition: eigen_dense_jacobi_svd_decomposition.h:141
void PrintInfo(std::ostream &rOStream) const override
SVD information Outputs the SVD class information.
Definition: eigen_dense_jacobi_svd_decomposition.h:183
Eigen::DecompositionOptions DecompositionOptions
Definition: eigen_dense_jacobi_svd_decomposition.h:63
void Compute(MatrixType &rInputMatrix, VectorType &rVectorS, MatrixType &rMatrixU, MatrixType &rMatrixV, Parameters Settings) override
Definition: eigen_dense_jacobi_svd_decomposition.h:113
void Compute(MatrixType &rInputMatrix, Parameters Settings) override
Definition: eigen_dense_jacobi_svd_decomposition.h:85
TDenseSpace::DataType DataType
Definition: eigen_dense_jacobi_svd_decomposition.h:57
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
void ValidateAndAssignDefaults(const Parameters &rDefaultParameters)
This function is designed to verify that the parameters under testing match the form prescribed by th...
Definition: kratos_parameters.cpp:1306
bool GetBool() const
This method returns the boolean contained in the current Parameter.
Definition: kratos_parameters.cpp:675
#define KRATOS_ERROR_IF_NOT(conditional)
Definition: exception.h:163
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
Eigen::Matrix< _Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > EigenDynamicMatrix
Definition: linear_solvers_define.h:32
Eigen::Matrix< _Scalar, Eigen::Dynamic, 1 > EigenDynamicVector
Definition: linear_solvers_define.h:33
int n
manufactured solution and derivatives (u=0 at z=0 dudz=0 at z=domain_height)
Definition: ode_solve.py:402
int m
Definition: run_marine_rain_substepping.py:8