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.
plane_approximation_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: Ruben Zorrilla
11 //
12 
13 #if !defined(KRATOS_PLANE_APPROXIMATION_UTILITY )
14 #define KRATOS_PLANE_APPROXIMATION_UTILITY
15 
16 /* System includes */
17 
18 /* External includes */
19 
20 /* Project includes */
21 #include "utilities/math_utils.h"
22 
23 namespace Kratos
24 {
25 
28 
32 
36 
40 
44 
57 template <unsigned int TDim>
59 {
60 public:
61 
64 
67 
71 
75 
80 
83 
87 
96  const std::vector< array_1d<double,3> > &rPointsCoords,
97  array_1d<double,3> &rPlaneBasePointCoords,
98  array_1d<double,3> &rPlaneNormal)
99  {
100  ComputeBasePoint(rPointsCoords,rPlaneBasePointCoords);
101  ComputePlaneNormal(rPointsCoords, rPlaneBasePointCoords, rPlaneNormal);
102  }
103 
107 
111 
115 
119 
120 private:
121 
124 
128 
132 
136 
142  static void ComputeBasePoint(
143  const std::vector< array_1d<double,3> > &rPointsCoords,
144  array_1d<double,3> &rBasePointCoords)
145  {
146  const unsigned int n_points = rPointsCoords.size();
147  KRATOS_ERROR_IF(n_points == 0) << "No base point can be computed. Points container is empty." << std::endl;
148 
149  noalias(rBasePointCoords) = ZeroVector(3);
150  for (unsigned int j = 0; j < n_points; ++j){
151  rBasePointCoords += rPointsCoords[j];
152  }
153  rBasePointCoords /= n_points;
154  }
155 
162  static void SetMatrixA(
163  const std::vector< array_1d< double,3 > > &rPointsCoords,
164  const array_1d<double,3> &rPlaneBasePointCoords,
165  BoundedMatrix<double,TDim,TDim> &rA)
166  {
167  noalias(rA) = ZeroMatrix(TDim,TDim);
168  const unsigned int n_points = rPointsCoords.size();
169 
170  for (unsigned int i = 0; i < TDim; ++i){
171  const double base_i = rPlaneBasePointCoords(i);
172  for (unsigned int j = 0; j < TDim; ++j){
173  const double base_j = rPlaneBasePointCoords(j);
174  for (unsigned int m = 0; m < n_points; ++m){
175  const double pt_m_i = rPointsCoords[m](i);
176  const double pt_m_j = rPointsCoords[m](j);
177  rA(i,j) += (base_i - pt_m_i)*(base_j - pt_m_j);
178  }
179  }
180  }
181  }
182 
189  static void ComputePlaneNormal(
190  const std::vector< array_1d<double,3> > &rPointsCoords,
191  const array_1d<double,3> &rPlaneBasePointCoords,
192  array_1d<double,3> &rPlaneNormal)
193  {
194  // Solve the A matrix eigenvalue problem
195  BoundedMatrix<double, TDim, TDim> a_mat, eigenval_mat, eigenvector_mat;
196  SetMatrixA(rPointsCoords, rPlaneBasePointCoords, a_mat);
197  bool converged = MathUtils<double>::GaussSeidelEigenSystem(a_mat, eigenvector_mat, eigenval_mat);
198  KRATOS_ERROR_IF(!converged) << "Plane normal can't be computed. Eigenvalue problem did not converge." << std::endl;
199 
200  // Find the minimum eigenvalue
201  double min_eigval = std::numeric_limits<double>::max();
202  unsigned int min_eigval_id = 0;
203  for (unsigned int i = 0; i < TDim; ++i){
204  if (eigenval_mat(i,i) < min_eigval){
205  min_eigval_id = i;
206  min_eigval = eigenval_mat(i,i);
207  }
208  }
209 
210  // Set as plane normal the eigenvector associated to the minimum eigenvalue
211  noalias(rPlaneNormal) = ZeroVector(3);
212  for (unsigned int i = 0; i < TDim; ++i){
213  rPlaneNormal(i) = eigenvector_mat(i, min_eigval_id);
214  }
215  }
216 
220 
224 
228 
232 
233 }; /* Class PlaneApproximationUtility */
234 
237 
238 
242 
243 } /* namespace Kratos.*/
244 
245 #endif /* KRATOS_PLANE_APPROXIMATION_UTILITY defined */
246 
static bool GaussSeidelEigenSystem(const TMatrixType1 &rA, TMatrixType2 &rEigenVectorsMatrix, TMatrixType2 &rEigenValuesMatrix, const double Tolerance=1.0e-18, const SizeType MaxIterations=20)
Calculates the eigenvectors and eigenvalues of given symmetric matrix.
Definition: math_utils.h:1587
Utility to compute an approximation plane for a set of points.
Definition: plane_approximation_utility.h:59
virtual ~PlaneApproximationUtility()
Destructor.
Definition: plane_approximation_utility.h:82
PlaneApproximationUtility()
Default constructor.
Definition: plane_approximation_utility.h:79
KRATOS_CLASS_POINTER_DEFINITION(PlaneApproximationUtility)
Pointer definition of PlaneApproximationUtility.
static void ComputePlaneApproximation(const std::vector< array_1d< double, 3 > > &rPointsCoords, array_1d< double, 3 > &rPlaneBasePointCoords, array_1d< double, 3 > &rPlaneNormal)
Provided a list of point coordinates, computes the best plane approximation in a least squares sense.
Definition: plane_approximation_utility.h:95
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
static double max(double a, double b)
Definition: GeometryFunctions.h:79
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
KratosZeroVector< double > ZeroVector
Definition: amatrix_interface.h:561
KratosZeroMatrix< double > ZeroMatrix
Definition: amatrix_interface.h:559
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484
int j
Definition: quadrature.py:648
int m
Definition: run_marine_rain_substepping.py:8
integer i
Definition: TensorModule.f:17