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.
search_structure.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: clabra
11 //
12 
13 #pragma once
14 
15 // System includes
16 #include <vector>
17 #include <cfloat>
18 // External includes
19 
20 // Project includes
21 
22 #ifndef DBL_MAX
23 #define DBL_MAX (1.0/DBL_EPSILON)
24 #endif
25 
26 namespace Kratos
27 {
30 
34 
38 
42 
46 
47 
49 
51 template< class T, std::size_t TDimension >
52 class Tvector
53 {
54 private:
55  T data[TDimension];
56 public:
58  {
59  for(std::size_t i = 0; i < TDimension; i++)
60  data[i] = T(0);
61  }
62  explicit Tvector( T const& value )
63  {
64  for(std::size_t i = 0; i < TDimension; i++)
65  data[i] = value;
66  }
68  {
69  for(std::size_t i = 0; i < TDimension; i++)
70  data[i] = Other[i];
71  }
72  ~Tvector() {}
73  T& operator[](std::size_t index)
74  {
75  return data[index];
76  }
77  T const& operator[](std::size_t index) const
78  {
79  return data[index];
80  }
81 };
82 
83 
85 
86 namespace SearchUtils
87 {
88 
89 
90 template< class TPointerType >
91 std::size_t PointerDistance( TPointerType const& PointerBegin, TPointerType const& PointerEnd )
92 {
93  //return std::distance(PointerBegin,PointerEnd);
94  return ( PointerEnd - PointerBegin ); // required for SUN Compiler
95 }
96 
97 
98 template< std::size_t TDimension, class TPointType >
99 bool PointInBox( TPointType const& BoxMinPoint, TPointType const& BoxMaxPoint, TPointType const& ThisPoint )
100 {
101  for(std::size_t i = 0 ; i < TDimension ; i++)
102  if( ThisPoint[i] < BoxMinPoint[i] || ThisPoint[i] > BoxMaxPoint[i] )
103  return false;
104  return true;
105 }
106 
107 template< std::size_t TDimension, class TPointType >
109 {
110 public:
111  double operator()( TPointType const& p1, TPointType const& p2 )
112  {
113  double tmp = p1[0] - p2[0];
114  double dist = tmp*tmp;
115  for( std::size_t i = 1 ; i < TDimension ; i++)
116  {
117  tmp = p1[i] - p2[i];
118  dist += tmp*tmp;
119  }
120  return dist;
121  }
122 };
123 
124 template< class TIteratorType, class TSizeType, class TResultIteratorType = TIteratorType >
126 {
127 public:
128  void operator()( TIteratorType const& RangeBegin, TIteratorType const& RangeEnd, TResultIteratorType& Results,
129  TSizeType& NumberOfResults, TSizeType const& MaxNumberOfResults )
130  {
131  for( TIteratorType Point = RangeBegin; (Point != RangeEnd)&&(NumberOfResults < MaxNumberOfResults); Point++)
132  {
133  *Results = *Point;
134  Results++;
135  NumberOfResults++;
136  }
137  }
138 };
139 
140 template< class TPointType, class TIteratorType, class TSizeType, std::size_t TDimension, class TResultIteratorType = TIteratorType >
142 {
143 public:
144  void operator()( TPointType const& MinBoxPoint, TPointType const& MaxBoxPoint, TIteratorType const& RangeBegin, TIteratorType const& RangeEnd,
145  TResultIteratorType& Results, TSizeType& NumberOfResults, TSizeType const& MaxNumberOfResults )
146  {
147  for( TIteratorType Point = RangeBegin; (Point != RangeEnd)&&(NumberOfResults < MaxNumberOfResults); Point++)
148  if ( PointInBox<TDimension,TPointType>(MinBoxPoint,MaxBoxPoint,**Point) )
149  {
150  *Results = *Point;
151  Results++;
152  NumberOfResults++;
153  }
154  }
155 };
156 
157 
158 template< class TPointType, class TPointerType, class TIteratorType, class TDistanceFunction, class TCoordinateType >
160 {
161 public:
162  void operator()( const TIteratorType& RangeBegin, const TIteratorType& RangeEnd, const TPointType& ThisPoint, TPointerType& Result, TCoordinateType& Distance )
163  {
164  TCoordinateType NewDistance;
165  for(TIteratorType Point = RangeBegin ; Point != RangeEnd ; Point++)
166  {
167  NewDistance = TDistanceFunction()(**Point,ThisPoint);
168  if( NewDistance < Distance )
169  {
170  Result = *Point;
171  Distance = NewDistance;
172  }
173  }
174  }
175  void operator()( const TIteratorType& RangeBegin, const TIteratorType& RangeEnd, const TPointType& ThisPoint, TPointerType& Result, TCoordinateType& Distance, bool& Found )
176  {
177  TCoordinateType NewDistance;
178  for(TIteratorType Point = RangeBegin ; Point != RangeEnd ; Point++)
179  {
180  NewDistance = TDistanceFunction()(**Point,ThisPoint);
181  if( NewDistance < Distance )
182  {
183  Result = *Point;
184  Distance = NewDistance;
185  Found = true;
186  }
187  }
188  }
189 };
190 
191 
192 template< class TPointType, class TIteratorType, class TDistanceIteratorType, class TDistanceFunction, class TSizeType, class TCoordinateType, class TResultIteratorType = TIteratorType >
194 {
195 public:
196 
197  void operator()( TIteratorType const& RangeBegin, TIteratorType const& RangeEnd, TPointType const& ThisPoint, TCoordinateType const& Radius,
198  TResultIteratorType& Results, TSizeType& NumberOfResults, TSizeType const& MaxNumberOfResults )
199  {
200  TCoordinateType distance;
201  for(TIteratorType Point = RangeBegin ; (Point != RangeEnd) && (NumberOfResults < MaxNumberOfResults) ; Point++)
202  {
203  distance = TDistanceFunction()(**Point,ThisPoint); // squared distance function
204  if( distance < Radius )
205  {
206  *Results = *Point;
207  Results++;
208  NumberOfResults++;
209  }
210  }
211  }
212 
213  void operator()( TIteratorType const& RangeBegin, TIteratorType const& RangeEnd, TPointType const& ThisPoint, TCoordinateType const& Radius,
214  TResultIteratorType& Results, TDistanceIteratorType& Distances, TSizeType& NumberOfResults, TSizeType const& MaxNumberOfResults )
215  {
216  TCoordinateType distance;
217  for(TIteratorType Point = RangeBegin ; (Point != RangeEnd) && (NumberOfResults < MaxNumberOfResults) ; Point++)
218  {
219  distance = TDistanceFunction()(**Point,ThisPoint); // squared distance function
220  if( distance < Radius )
221  {
222  *Results = *Point;
223  Results++;
224  *Distances = distance;
225  Distances++;
226  NumberOfResults++;
227  }
228  }
229  }
230 };
231 
232 
233 }
234 
235 
236 
238 
239 template< class IndexType, class SizeType>
241 {
242 public:
247 
248  SubBinAxis() : Min(0), Max(0), MaxSize(0), Block(1) {}
249 
250  SubBinAxis(IndexType const& Min_, IndexType const& Max_, IndexType const& MaxSize_, IndexType const& Block_)
251  {
252  Set(Min_,Max_,MaxSize_,Block_);
253  }
255  void Set(IndexType const& iCell, IndexType const& MaxSize_, IndexType const& Block_)
256  {
257  Set(iCell,iCell,MaxSize_,Block_);
258  }
259  void Set(IndexType const& Min_, IndexType const& Max_, IndexType const& MaxSize_, IndexType const& Block_)
260  {
261  MaxSize = MaxSize_;
262  Block = Block_;
263  Min = std::max( Min_, static_cast<IndexType>(0) );
264  Max = std::min( Max_, MaxSize-1 );
265  }
267  {
268  return Min*Block;
269  }
271  {
272  return Max*Block;
273  }
275  {
276  return Min;
277  }
279  {
280  return Max;
281  }
283  {
284  return static_cast<SizeType>(Max-Min);
285  }
287  {
288  if( Min > static_cast<IndexType>(0) ) Min--;
289  if( Max < MaxSize-1 ) Max++;
290  return *this;
291  }
293  {
294  Min++;
295  Max--;
296  return *this;
297  }
298 };
299 
300 
301 template<
302 class IndexType,
303  class SizeType,
304  class CoordinateType,
305  class IteratorType,
306  class IteratorIteratorType,
307  std::size_t Dimension >
309 {
310 public:
313 
316 
319 
321 
322 public:
323 
324  // Bin
327  IteratorIteratorType RowBegin;
328  IteratorIteratorType RowEnd;
329  IteratorIteratorType DataBegin;
330 
331  // KDTree
332  CoordinateType distance_to_partition;
333  CoordinateType distance_to_partition2;
334  CoordinateType residual_distance[Dimension];
336 
337 public:
338 
340 
341  SearchStructure( IndexVector const& Min_, IndexVector const& Max_, SizeVector const& MaxSize_, IteratorIteratorType const& IteratorBegin )
342  {
343  Set(Min_,Max_,MaxSize_,IteratorBegin);
344  }
345 
346  SearchStructure( IndexVector const& IndexCell, SizeVector const& MaxSize_, IteratorIteratorType const& IteratorBegin )
347  {
348  Set(IndexCell,IndexCell,MaxSize_,IteratorBegin);
349  }
350 
351  SearchStructure( IndexVector const& Min_, IndexVector const& Max_, SizeVector const& MaxSize_ )
352  {
353  Set(Min_,Max_,MaxSize_);
354  }
355 
356  SearchStructure( IndexVector const& IndexCell, SizeVector const& MaxSize_ )
357  {
358  Set(IndexCell,IndexCell,MaxSize_);
359  }
360 
362 
363  void Set( IndexVector const& IndexCell, SizeVector const& _MaxSize, IteratorIteratorType const& IteratorBegin )
364  {
365  Set( IndexCell, IndexCell, _MaxSize, IteratorBegin );
366  }
367 
368  void Set( IndexVector const& Min_, IndexVector const& Max_, SizeVector const& MaxSize_, IteratorIteratorType const& IteratorBegin )
369  {
370  IndexType Block = 1;
371  Axis[0].Set(Min_[0],Max_[0],MaxSize_[0],Block);
372  for(SizeType i = 1; i < Dimension; i++)
373  {
374  Block *= MaxSize_[i-1];
375  Axis[i].Set(Min_[i],Max_[i],MaxSize_[i],Block);
376  }
377 
378  hasIterators = true;
379 
380  DataBegin = IteratorBegin;
381 
382  RowBegin = DataBegin + Axis[0].Min;
383  RowEnd = DataBegin + Axis[0].Max + 1;
384  }
385 
386  void Set( IndexVector const& IndexCell, SizeVector const& MaxSize_ )
387  {
388  Set(IndexCell,IndexCell,MaxSize_);
389  }
390 
391  void Set( IndexVector const& Min_, IndexVector const& Max_, SizeVector const& MaxSize_ )
392  {
393  IndexType Block = 1;
394  Axis[0].Set(Min_[0],Max_[0],MaxSize_[0],Block);
395  for(SizeType i = 1; i < Dimension; i++)
396  {
397  Block *= MaxSize_[i-1];
398  Axis[i].Set(Min_[i],Max_[i],MaxSize_[i],Block);
399  }
400 
401  hasIterators = false;
402  }
403 
405  {
406  return Idx + Axis[0].Min;
407  }
408 
410  {
411  return Idx + Axis[0].Max+1;
412  }
413 
415  {
416  for(SizeType i = 0; i < Dimension; i++)
417  ++(Axis[i]);
418 
419  // Update the row iterators only if the DataBegin iterator exists
420  if(hasIterators) {
421  RowBegin = DataBegin + Axis[0].Min;
422  RowEnd = DataBegin + Axis[0].Max + 1;
423  }
424 
425  return *this;
426  }
427 
429  {
430  for(SizeType i = 0; i < Dimension; i++)
431  (Axis[i])--;
432 
433  // Update the row iterators only if the DataBegin iterator exists
434  if(hasIterators) {
435  RowBegin = DataBegin + Axis[0].Min;
436  RowEnd = DataBegin + Axis[0].Max + 1;
437  }
438 
439  return *this;
440  }
441 
442 };
443 
444 } // namespace Kratos.
Point class.
Definition: point.h:59
Definition: search_structure.h:309
void Set(IndexVector const &Min_, IndexVector const &Max_, SizeVector const &MaxSize_, IteratorIteratorType const &IteratorBegin)
Definition: search_structure.h:368
SearchStructure(IndexVector const &Min_, IndexVector const &Max_, SizeVector const &MaxSize_)
Definition: search_structure.h:351
void Set(IndexVector const &IndexCell, SizeVector const &_MaxSize, IteratorIteratorType const &IteratorBegin)
Definition: search_structure.h:363
IndexType EndRow(IndexType const &Idx)
Definition: search_structure.h:409
SearchStructure const & operator++()
Definition: search_structure.h:414
CoordinateType distance_to_partition
Definition: search_structure.h:332
Tvector< IndexType, Dimension > IndexVector
Definition: search_structure.h:317
KRATOS_CLASS_POINTER_DEFINITION(SearchStructure)
Pointer definition of SearchStructure.
~SearchStructure()
Definition: search_structure.h:361
SizeType BucketCounter
Definition: search_structure.h:335
SearchStructure(IndexVector const &IndexCell, SizeVector const &MaxSize_, IteratorIteratorType const &IteratorBegin)
Definition: search_structure.h:346
SearchStructure const & operator--()
Definition: search_structure.h:428
CoordinateType residual_distance[Dimension]
Definition: search_structure.h:334
void Set(IndexVector const &Min_, IndexVector const &Max_, SizeVector const &MaxSize_)
Definition: search_structure.h:391
IteratorIteratorType DataBegin
Definition: search_structure.h:329
bool hasIterators
Definition: search_structure.h:325
void Set(IndexVector const &IndexCell, SizeVector const &MaxSize_)
Definition: search_structure.h:386
SearchStructure(IndexVector const &Min_, IndexVector const &Max_, SizeVector const &MaxSize_, IteratorIteratorType const &IteratorBegin)
Definition: search_structure.h:341
SearchStructure()
Definition: search_structure.h:339
SearchStructure(IndexVector const &IndexCell, SizeVector const &MaxSize_)
Definition: search_structure.h:356
IndexType BeginRow(IndexType const &Idx)
Definition: search_structure.h:404
IteratorIteratorType RowBegin
Definition: search_structure.h:327
SubBinAxis< IndexType, SizeType > Axis[3]
Definition: search_structure.h:326
IteratorIteratorType RowEnd
Definition: search_structure.h:328
Tvector< SizeType, Dimension > SizeVector
Definition: search_structure.h:318
SearchStructure< IndexType, SizeType, CoordinateType, IteratorType, IteratorIteratorType, Dimension > ThisType
Definition: search_structure.h:320
CoordinateType distance_to_partition2
Definition: search_structure.h:333
Definition: search_structure.h:126
void operator()(TIteratorType const &RangeBegin, TIteratorType const &RangeEnd, TResultIteratorType &Results, TSizeType &NumberOfResults, TSizeType const &MaxNumberOfResults)
Definition: search_structure.h:128
Definition: search_structure.h:142
void operator()(TPointType const &MinBoxPoint, TPointType const &MaxBoxPoint, TIteratorType const &RangeBegin, TIteratorType const &RangeEnd, TResultIteratorType &Results, TSizeType &NumberOfResults, TSizeType const &MaxNumberOfResults)
Definition: search_structure.h:144
Definition: search_structure.h:160
void operator()(const TIteratorType &RangeBegin, const TIteratorType &RangeEnd, const TPointType &ThisPoint, TPointerType &Result, TCoordinateType &Distance)
Definition: search_structure.h:162
void operator()(const TIteratorType &RangeBegin, const TIteratorType &RangeEnd, const TPointType &ThisPoint, TPointerType &Result, TCoordinateType &Distance, bool &Found)
Definition: search_structure.h:175
Definition: search_structure.h:194
void operator()(TIteratorType const &RangeBegin, TIteratorType const &RangeEnd, TPointType const &ThisPoint, TCoordinateType const &Radius, TResultIteratorType &Results, TSizeType &NumberOfResults, TSizeType const &MaxNumberOfResults)
Definition: search_structure.h:197
void operator()(TIteratorType const &RangeBegin, TIteratorType const &RangeEnd, TPointType const &ThisPoint, TCoordinateType const &Radius, TResultIteratorType &Results, TDistanceIteratorType &Distances, TSizeType &NumberOfResults, TSizeType const &MaxNumberOfResults)
Definition: search_structure.h:213
Definition: search_structure.h:109
double operator()(TPointType const &p1, TPointType const &p2)
Definition: search_structure.h:111
TOOLS UTILS ///.
Definition: search_structure.h:241
SubBinAxis(IndexType const &Min_, IndexType const &Max_, IndexType const &MaxSize_, IndexType const &Block_)
Definition: search_structure.h:250
SubBinAxis const & operator++()
Definition: search_structure.h:286
IndexType Max
Definition: search_structure.h:244
IndexType Min
Definition: search_structure.h:243
IndexType EndIndex()
Definition: search_structure.h:278
void Set(IndexType const &Min_, IndexType const &Max_, IndexType const &MaxSize_, IndexType const &Block_)
Definition: search_structure.h:259
SubBinAxis()
Definition: search_structure.h:248
IndexType MaxSize
Definition: search_structure.h:245
SizeType Size()
Definition: search_structure.h:282
void Set(IndexType const &iCell, IndexType const &MaxSize_, IndexType const &Block_)
Definition: search_structure.h:255
~SubBinAxis()
Definition: search_structure.h:254
IndexType BeginIndex()
Definition: search_structure.h:274
IndexType Block
Definition: search_structure.h:246
IndexType Begin()
Definition: search_structure.h:266
IndexType End()
Definition: search_structure.h:270
SubBinAxis const & operator--()
Definition: search_structure.h:292
Short class definition.
Definition: search_structure.h:53
~Tvector()
Definition: search_structure.h:72
Tvector()
Definition: search_structure.h:57
Tvector(T const &value)
Definition: search_structure.h:62
Tvector(Tvector< T, TDimension > const &Other)
Definition: search_structure.h:67
T const & operator[](std::size_t index) const
Definition: search_structure.h:77
T & operator[](std::size_t index)
Definition: search_structure.h:73
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
static double max(double a, double b)
Definition: GeometryFunctions.h:79
static double min(double a, double b)
Definition: GeometryFunctions.h:71
std::size_t PointerDistance(TPointerType const &PointerBegin, TPointerType const &PointerEnd)
Definition: search_structure.h:91
bool PointInBox(TPointType const &BoxMinPoint, TPointType const &BoxMaxPoint, TPointType const &ThisPoint)
Definition: search_structure.h:99
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::size_t SizeType
The definition of the size type.
Definition: mortar_classes.h:43
float dist
Definition: edgebased_PureConvection.py:89
tuple tmp
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:98
integer i
Definition: TensorModule.f:17
Configure::IteratorType IteratorType
Definition: transfer_utility.h:249