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.
geometrical_projection_utilities.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 // Philipp Bucher
12 //
13 
14 #pragma once
15 
16 // System includes
17 
18 // External includes
19 
20 // Project includes
21 #include "geometries/geometry.h"
22 #include "includes/checks.h"
23 #include "includes/node.h"
24 #include "includes/variables.h"
25 
26 namespace Kratos
27 {
30 
38 class KRATOS_API(KRATOS_CORE) GeometricalProjectionUtilities
39 {
40 public:
43 
47  enum class DistanceComputed
48  {
49  NO_RADIUS,
50  PROJECTION_ERROR,
51  RADIUS_PROJECTED,
52  RADIUS_NOT_PROJECTED_OUTSIDE,
53  RADIUS_NOT_PROJECTED_INSIDE
54  };
55 
58 
59  // Some geometrical definitions
60  typedef Node NodeType;
61  typedef Point PointType;
62 
64  typedef std::size_t IndexType;
65 
67  typedef std::size_t SizeType;
68 
72 
74 
78 
90  template<class TGeometryType>
91  static inline double FastProjectDirection(
92  const TGeometryType& rGeom,
93  const PointType& rPointToProject,
94  PointType& rPointProjected,
95  const array_1d<double,3>& rNormal,
96  const array_1d<double,3>& rVector,
97  const SizeType EchoLevel = 0
98  )
99  {
100  // Zero tolerance
101  const double zero_tolerance = std::numeric_limits<double>::epsilon();
102 
103  // We define the distance
104  double distance = 0.0;
105 
106  const array_1d<double,3> vector_points = rGeom[0].Coordinates() - rPointToProject.Coordinates();
107 
108  if( norm_2( rVector ) < zero_tolerance && norm_2( rNormal ) > zero_tolerance ) {
109  distance = inner_prod(vector_points, rNormal)/norm_2(rNormal);
110  noalias(rPointProjected.Coordinates()) = rPointToProject.Coordinates() + rVector * distance;
111  KRATOS_WARNING_IF("GeometricalProjectionUtilities", EchoLevel > 0) << "WARNING:: Zero projection vector. Projection using the condition vector instead." << std::endl;
112  } else if (std::abs(inner_prod(rVector, rNormal) ) > zero_tolerance) {
113  distance = inner_prod(vector_points, rNormal)/inner_prod(rVector, rNormal);
114  noalias(rPointProjected.Coordinates()) = rPointToProject.Coordinates() + rVector * distance;
115  } else {
116  noalias(rPointProjected.Coordinates()) = rPointToProject.Coordinates();
117  KRATOS_WARNING_IF("GeometricalProjectionUtilities", EchoLevel > 0) << "WARNING:: The line and the plane are coplanar. Something wrong happened " << std::endl;
118  }
119 
120  return distance;
121  }
122 
134  template<class TPointClass1, class TPointClass2 = TPointClass1, class TPointClass3 = PointType>
135  static inline TPointClass3 FastProject(
136  const TPointClass1& rPointOrigin,
137  const TPointClass2& rPointToProject,
138  const array_1d<double,3>& rNormal,
139  double& rDistance
140  )
141  {
142  const array_1d<double,3> vector_points = rPointToProject - rPointOrigin;
143 
144  rDistance = inner_prod(vector_points, rNormal);
145 
146  TPointClass3 point_projected;
147 
148  noalias(point_projected) = rPointToProject - rNormal * rDistance;
149 
150  return point_projected;
151  }
152 
162  template<class TGeometryType>
163  static inline double FastProjectOnGeometry(const TGeometryType& rGeom,
164  const Point& rPointToProject,
165  PointType& rPointProjected,
166  const SizeType EchoLevel = 0)
167  {
168  // using the normal in the center of the geometry for the projection
169  array_1d<double,3> local_coords_center;
170  rGeom.PointLocalCoordinates(local_coords_center, rGeom.Center());
171  const array_1d<double,3> normal = rGeom.UnitNormal(local_coords_center);
172 
173  return FastProjectDirection(
174  rGeom,
175  rPointToProject,
176  rPointProjected,
177  normal,
178  normal,
179  EchoLevel);
180  }
181 
191  template<class TGeometryType>
192  static inline double FastProjectOnLine(const TGeometryType& rGeometry,
193  const PointType& rPointToProject,
194  PointType& rPointProjected)
195  {
196  const array_1d<double, 3>& r_p_a = rGeometry[0].Coordinates();
197  const array_1d<double, 3>& r_p_b = rGeometry[1].Coordinates();
198  const array_1d<double, 3> ab = r_p_b - r_p_a;
199 
200  const array_1d<double, 3>& p_c = rPointToProject.Coordinates();
201 
202  const double factor = (inner_prod(r_p_b, p_c) - inner_prod(r_p_a, p_c) - inner_prod(r_p_b, r_p_a) + inner_prod(r_p_a, r_p_a)) / inner_prod(ab, ab);
203 
204  rPointProjected.Coordinates() = r_p_a + factor * ab;
205 
206  return norm_2(rPointProjected.Coordinates()-p_c);
207  }
208 
209 
219  template<class TGeometryType>
220  static inline double FastMinimalDistanceOnLine(
221  const TGeometryType& rGeometry,
222  const PointType& rPoint,
223  const double Tolerance = 1.0e-9
224  )
225  {
226  PointType projected_point;
227  const double projected_distance = FastProjectOnLine(rGeometry, rPoint, projected_point);
228  typename TGeometryType::CoordinatesArrayType projected_local;
229  if (rGeometry.IsInside(projected_point.Coordinates(), projected_local, Tolerance)) {
230  return projected_distance;
231  } else {
232  const double distance_a = rPoint.Distance(rGeometry[0]);
233  const double distance_b = rPoint.Distance(rGeometry[1]);
234  return std::min(distance_a, distance_b);
235  }
236  }
237 
248  static DistanceComputed FastMinimalDistanceOnLineWithRadius(
249  double& rDistance,
250  const Geometry<Node>& rSegment,
251  const Point& rPoint,
252  const double Radius,
253  const double Tolerance = 1.0e-9
254  );
255 
266  template<class TGeometryType, class TPointClass1, class TPointClass2 = TPointClass1>
267  static inline double FastProjectOnLine2D(
268  const TGeometryType& rGeometry,
269  const TPointClass1& rPointToProject,
270  TPointClass2& rPointProjected
271  )
272  {
273  // Node coordinates
274  const array_1d<double, 3>& r_p_a = rGeometry[0];
275  const array_1d<double, 3>& r_p_b = rGeometry[1];
276  const array_1d<double, 3>& r_p_c = rPointToProject;
277 
278  // We compute the normal
279  array_1d<double,3> normal;
280  normal[0] = r_p_b[1] - r_p_a[1];
281  normal[1] = r_p_a[0] - r_p_b[0];
282  normal[2] = 0.0;
283 
284  const double norm_normal = norm_2(normal);
285  KRATOS_ERROR_IF(norm_normal <= std::numeric_limits<double>::epsilon()) << "Zero norm normal: X: " << normal[0] << "\tY: " << normal[1] << std::endl;
286  normal /= norm_normal;
287 
288  const array_1d<double,3> vector_points = r_p_a - r_p_c;
289  const double distance = inner_prod(vector_points, normal);
290  noalias(rPointProjected) = r_p_c + normal * distance;
291 
292  return distance;
293  }
294 
303  template<class TGeometryType>
304  static inline bool ProjectIterativeLine2D(
305  TGeometryType& rGeomOrigin,
306  const array_1d<double,3>& rPointDestiny,
307  array_1d<double,3>& rResultingPoint,
308  const array_1d<double, 3>& rNormal,
309  const double Tolerance = 1.0e-8,
310  double DeltaXi = 0.5
311  )
312  {
313 // rResultingPoint.clear();
314 
315  double old_delta_xi = 0.0;
316 
317  array_1d<double, 3> current_global_coords;
318 
319  KRATOS_DEBUG_CHECK_VARIABLE_IN_NODAL_DATA(NORMAL, rGeomOrigin[0])
320  KRATOS_DEBUG_CHECK_VARIABLE_IN_NODAL_DATA(NORMAL, rGeomOrigin[1])
321 
322  array_1d<array_1d<double, 3>, 2> normals;
323  normals[0] = rGeomOrigin[0].FastGetSolutionStepValue(NORMAL);
324  normals[1] = rGeomOrigin[1].FastGetSolutionStepValue(NORMAL);
325 
328  for(unsigned int i=0; i<2;++i) {
329  X(0,i) = rGeomOrigin[i].X();
330  X(1,i) = rGeomOrigin[i].Y();
331  }
332 
333  Matrix J = ZeroMatrix( 1, 1 );
334 
335  //Newton iteration:
336 
337  const unsigned int max_iter = 20;
338 
339  for ( unsigned int k = 0; k < max_iter; ++k ) {
340  array_1d<double, 2> N_origin;
341  N_origin[0] = 0.5 * ( 1.0 - rResultingPoint[0]);
342  N_origin[1] = 0.5 * ( 1.0 + rResultingPoint[0]);
343 
344  array_1d<double,3> normal_xi = N_origin[0] * normals[0] + N_origin[1] * normals[1];
345  normal_xi = normal_xi/norm_2(normal_xi);
346 
347  current_global_coords = ZeroVector(3);
348  for( IndexType i_node = 0; i_node < 2; ++i_node )
349  current_global_coords += N_origin[i_node] * rGeomOrigin[i_node].Coordinates();
350 
351  const array_1d<double,3> VectorPoints = rGeomOrigin.Center() - rPointDestiny;
352  const double distance = inner_prod(VectorPoints, rNormal)/inner_prod(-normal_xi, rNormal);
353  const array_1d<double, 3> current_destiny_global_coords = rPointDestiny - normal_xi * distance;
354 
355  // Derivatives of shape functions
356  Matrix ShapeFunctionsGradients;
357  ShapeFunctionsGradients = rGeomOrigin.ShapeFunctionsLocalGradients(ShapeFunctionsGradients, rResultingPoint );
358 
359  noalias(DN) = prod(X,ShapeFunctionsGradients);
360 
361  noalias(J) = prod(trans(DN),DN); // TODO: Add the non linearity concerning the normal
362 
363  const Vector RHS = prod(trans(DN),subrange(current_destiny_global_coords - current_global_coords,0,2));
364 
365  old_delta_xi = DeltaXi;
366  DeltaXi = RHS[0]/J(0, 0);
367 
368  rResultingPoint[0] += DeltaXi;
369 
370  if (rResultingPoint[0] <= -1.0)
371  rResultingPoint[0] = -1.0;
372  else if (rResultingPoint[0] >= 1.0)
373  rResultingPoint[0] = 1.0;
374 
375  if ( std::abs(DeltaXi - old_delta_xi) < Tolerance )
376  return true;
377  }
378 
379  return false;
380  }
381 
382 private:
383 };// class GeometricalProjectionUtilities
384 
386 
387 }
This is a class that provides auxiliar utilities for projections.
Definition: geometrical_projection_utilities.h:39
KRATOS_CLASS_POINTER_DEFINITION(GeometricalProjectionUtilities)
Pointer definition of GeometricalProjectionUtilities.
std::size_t SizeType
Size type definition.
Definition: geometrical_projection_utilities.h:67
static double FastProjectDirection(const TGeometryType &rGeom, const PointType &rPointToProject, PointType &rPointProjected, const array_1d< double, 3 > &rNormal, const array_1d< double, 3 > &rVector, const SizeType EchoLevel=0)
Project a point over a line/plane following an arbitrary direction.
Definition: geometrical_projection_utilities.h:91
Node NodeType
Definition: geometrical_projection_utilities.h:60
static double FastMinimalDistanceOnLine(const TGeometryType &rGeometry, const PointType &rPoint, const double Tolerance=1.0e-9)
Computes the minimal distance to a line.
Definition: geometrical_projection_utilities.h:220
static bool ProjectIterativeLine2D(TGeometryType &rGeomOrigin, const array_1d< double, 3 > &rPointDestiny, array_1d< double, 3 > &rResultingPoint, const array_1d< double, 3 > &rNormal, const double Tolerance=1.0e-8, double DeltaXi=0.5)
Projects iteratively to get the coordinate.
Definition: geometrical_projection_utilities.h:304
std::size_t IndexType
Index type definition.
Definition: geometrical_projection_utilities.h:64
static TPointClass3 FastProject(const TPointClass1 &rPointOrigin, const TPointClass2 &rPointToProject, const array_1d< double, 3 > &rNormal, double &rDistance)
Project a point over a plane (avoiding some steps)
Definition: geometrical_projection_utilities.h:135
DistanceComputed
How the distance is computed enum.
Definition: geometrical_projection_utilities.h:48
Point PointType
Definition: geometrical_projection_utilities.h:61
static double FastProjectOnLine2D(const TGeometryType &rGeometry, const TPointClass1 &rPointToProject, TPointClass2 &rPointProjected)
Project a point over a line (2D only)
Definition: geometrical_projection_utilities.h:267
static double FastProjectOnGeometry(const TGeometryType &rGeom, const Point &rPointToProject, PointType &rPointProjected, const SizeType EchoLevel=0)
Project a point over a line/plane (simplified since using the normal in the center)
Definition: geometrical_projection_utilities.h:163
static double FastProjectOnLine(const TGeometryType &rGeometry, const PointType &rPointToProject, PointType &rPointProjected)
Project a point over a line (2D or 3D)
Definition: geometrical_projection_utilities.h:192
Geometry base class.
Definition: geometry.h:71
Definition: amatrix_interface.h:41
This class defines the node.
Definition: node.h:65
Point class.
Definition: point.h:59
CoordinatesArrayType const & Coordinates() const
Definition: point.h:215
double Distance(const Point &rOtherPoint) const
This method computes the distance between this point and another one.
Definition: point.h:166
#define KRATOS_DEBUG_CHECK_VARIABLE_IN_NODAL_DATA(TheVariable, TheNode)
Definition: checks.h:242
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_WARNING_IF(label, conditional)
Definition: logger.h:266
static int EchoLevel
Definition: co_sim_EMPIRE_API.h:42
static double min(double a, double b)
Definition: GeometryFunctions.h:71
typename Point::CoordinatesArrayType CoordinatesArrayType
Definition: add_geometries_to_python.cpp:63
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
KratosZeroVector< double > ZeroVector
Definition: amatrix_interface.h:561
TExpressionType::data_type norm_2(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression)
Definition: amatrix_interface.h:625
KratosZeroMatrix< double > ZeroMatrix
Definition: amatrix_interface.h:559
AMatrix::MatrixProductExpression< TExpression1Type, TExpression2Type > prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:568
TExpression1Type::data_type inner_prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:592
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484
AMatrix::TransposeMatrix< const T > trans(const T &TheMatrix)
Definition: amatrix_interface.h:486
AMatrix::SubVector< const TExpressionType > subrange(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression, std::size_t From, std::size_t To)
Definition: amatrix_interface.h:662
DN
Definition: generate_convection_diffusion_explicit_element.py:98
int max_iter
Definition: hinsberg_optimization.py:139
int k
Definition: quadrature.py:595
J
Definition: sensitivityMatrix.py:58
integer i
Definition: TensorModule.f:17
float factor
for node in (self.combined_model_part).Nodes: pold = node.GetSolutionStepValue(PRESSURE,...
Definition: ulf_PGLASS.py:254