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.
dem_fem_search.h
Go to the documentation of this file.
1 //
2 // Project Name: Kratos
3 // Last Modified by: $Author: msantasusana, croig $
4 // Date: $Date: 2015-10-26 19:37:47 $
5 // Revision: $Revision: 1.2 $
6 //
7 //
8 
9 #if !defined(KRATOS_DEM_FEM_SEARCH_H_INCLUDED )
10 #define KRATOS_DEM_FEM_SEARCH_H_INCLUDED
11 
12 // System includes
13 #include <string>
14 #include <iostream>
15 
16 // include kratos definitions
17 #include "includes/define.h"
18 
19 // Project includes
20 #include "utilities/openmp_utils.h"
21 
22 // Configures
24 // Search
27 
28 namespace Kratos
29 {
30 
33 
37 
41 
45 
49 
51 
54 class KRATOS_API(DEM_APPLICATION) DEM_FEM_Search : public SpatialSearch
55 {
56  public:
59 
62 
64  typedef std::vector<PtrPointType>* PointVector;
65  typedef std::vector<PtrPointType>::iterator PointIterator;
66 
67  typedef double* DistanceVector;
68  typedef double* DistanceIterator;
69 
70 // //Configure Types
71 // typedef RigidFaceConfigure<3> ElementConfigureType; //Element
72 // //Bin Types
73 // typedef BinsObjectDynamic<ElementConfigureType> BinsType;
74 //
75  //Configure Types
77  //Bin Types
79  //typedef PointerVectorSet<GeometricalObject, IndexedObject> GeometricalObjectType;
81 
82  //typedef PointerVector<GeometricalObject> GeometricalObjectType;
83 
84 
88 
91 
92  mBins = NULL;
93 
94  }
95 
98  }
99 
100 
102  ElementsContainerType const& rElements,
103  ConditionsContainerType const& rConditions,
105  VectorDistanceType& rResultsDistance)
106  {
107  KRATOS_TRY
108 
109  /*
110  STEPS:
111  ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
112  1. INITIALIZE
113  2. CALCULATE THE DEM BBX
114  3. GATHER DEM BOUNDING BOX
115  4. FIND THE FE INSIDE DEM_BB TO BUILD THE BINS AND CONSTRUCT THE GLOBAL BBX
116  5. GATHER FEM ELEMENTS AND THE GLOBAL BBX
117  6. BUILD THE BINS
118  7. PERFORM THE SEARCH FOR THE SPHERES INSIDE THE BBX (amplified by the radius of each particle)
119  */
120 
121  //1. INITIALIZE
122 
123  int MaxNumberOfElements = rConditions.size();
124 
125  ElementsContainerType::ContainerType& elements_sear = const_cast<ElementsContainerType::ContainerType&> (rElements.GetContainer());
126  ConditionsContainerType::ContainerType& conditions_bins = const_cast<ConditionsContainerType::ContainerType&>(rConditions.GetContainer());
127 
128  //GeometricalObjectType::ContainerType SearElementPointerToGeometricalObjecPointerTemporalVector;
129  GeometricalObjectType::ContainerType BinsConditionPointerToGeometricalObjecPointerTemporalVector;
130  RadiusArrayType Radius_out;
131 
132  int num_of_threads = ParallelUtilities::GetNumThreads();
133  std::vector<unsigned int> total_fem_partition_index;
134  OpenMPUtils::CreatePartition(num_of_threads, conditions_bins.size(), total_fem_partition_index);
135 
136  //std::vector<GeometricalObjectType::ContainerType> Vector_SearElementPointerToGeometricalObjecPointerTemporalVector(num_of_threads);
137  std::vector<GeometricalObjectType::ContainerType> Vector_BinsConditionPointerToGeometricalObjecPointerTemporalVector(num_of_threads);
138 
139  std::vector<array_1d<double, 3> > Vector_DEM_BB_LowPoint(num_of_threads); std::vector <array_1d<double, 3 > > Vector_DEM_BB_HighPoint(num_of_threads);
140  std::vector<array_1d<double, 3> > Vector_GLOBAL_BB_LowPoint(num_of_threads); std::vector <array_1d<double, 3 > > Vector_GLOBAL_BB_HighPoint(num_of_threads);
141 
142  std::vector<double> Vector_Ref_Radius(num_of_threads);
143  std::vector<RadiusArrayType> Vector_Radius_out(num_of_threads);
144 
145  double Global_Ref_Radius = 0.0;
146  double inf = std::numeric_limits<double>::infinity();
147 
148  for (std::size_t i = 0; i < 3; i++) {
149  DEM_BB_LowPoint[i] = inf;
150  DEM_BB_HighPoint[i] = -inf;
151 
152  mGlobal_BB_LowPoint[i] = inf;
153  mGlobal_BB_HighPoint[i] = -inf;
154  }
155 
156  typedef ElementsContainerType::ContainerType::iterator Elem_iter;
157  typedef ConditionsContainerType::ContainerType::iterator Cond_iter;
158 
159  //2. CALCULATE THE DEM BBX
160 
161  #pragma omp parallel
162  {
163  double radius = 0.0;
164  int k = OpenMPUtils::ThisThread();
165 
166  for(std::size_t i = 0; i < 3; i++) {
167  Vector_DEM_BB_LowPoint[k][i] = inf;
168  Vector_DEM_BB_HighPoint[k][i] = -inf;
169  }
170 
171  #pragma omp for
172  for (int p = 0; p <(int) elements_sear.size(); p++) {
173 
174  Elem_iter it = elements_sear.begin() + p;
175  GeometryType& pGeometry = (*it)->GetGeometry();
176  const array_1d<double, 3 >& aux_coor = pGeometry[0].Coordinates();
177 
178  SphericParticle* p_particle = dynamic_cast<SphericParticle*>((*it).get());
179  radius = p_particle->GetSearchRadius();
180 
181  Vector_Ref_Radius[k] = (Vector_Ref_Radius[k] < radius) ? radius : Vector_Ref_Radius[k] ;
182 
183  for(std::size_t i = 0; i < 3; i++) {
184  Vector_DEM_BB_LowPoint[k][i] = (Vector_DEM_BB_LowPoint[k][i] > aux_coor[i]) ? aux_coor[i] : Vector_DEM_BB_LowPoint[k][i];
185  Vector_DEM_BB_HighPoint[k][i] = (Vector_DEM_BB_HighPoint[k][i] < aux_coor[i]) ? aux_coor[i] : Vector_DEM_BB_HighPoint[k][i];
186  }
187  } //pragma
188  }//pragma parallel
189 
190 
191  //3. GATHER DEM BOUNDING BOX
192  for(int k = 0; k < num_of_threads; k++) {
193  for(std::size_t i = 0; i < 3; i++) {
194  DEM_BB_LowPoint[i] = (DEM_BB_LowPoint[i] > Vector_DEM_BB_LowPoint[k][i]) ? Vector_DEM_BB_LowPoint[k][i] : DEM_BB_LowPoint[i];
195  DEM_BB_HighPoint[i] = (DEM_BB_HighPoint[i] < Vector_DEM_BB_HighPoint[k][i]) ? Vector_DEM_BB_HighPoint[k][i] : DEM_BB_HighPoint[i];
196  }
197 
198  Global_Ref_Radius = (Global_Ref_Radius < Vector_Ref_Radius[k]) ? Vector_Ref_Radius[k] : Global_Ref_Radius;
199  }
200 
201  for(std::size_t i = 0; i < 3; i++) {
202  DEM_BB_LowPoint[i] -= 1.00f * Global_Ref_Radius;
203  DEM_BB_HighPoint[i] += 1.00f * Global_Ref_Radius;
204  }
205 
206 
207  //4. FIND THE FE INSIDE DEM_BB TO BUILD THE BINS AND CONSTRUCT THE GLOBAL BBX
208 
209  #pragma omp parallel
210  {
211  int k = OpenMPUtils::ThisThread();
212  Vector_BinsConditionPointerToGeometricalObjecPointerTemporalVector[k].reserve(total_fem_partition_index[k+1]);
213 
214  for(std::size_t i = 0; i < 3; i++) {
215  Vector_GLOBAL_BB_LowPoint[k][i] = inf;
216  Vector_GLOBAL_BB_HighPoint[k][i] = -inf;
217  }
218 
219  array_1d<double, 3> rHighPoint;
220  array_1d<double, 3> rLowPoint;
221 
222  #pragma omp for private(rHighPoint,rLowPoint)
223  for (int c = 0; c < (int)conditions_bins.size(); c++) {
224 
225  Cond_iter it = conditions_bins.begin() + c;
226 
227  const GeometryType& pGeometry = (*it)->GetGeometry();
228  noalias(rLowPoint) = pGeometry[0];
229  noalias(rHighPoint) = pGeometry[0];
230 
231  for(unsigned int point = 1; point < pGeometry.size(); point++ ) {
232  for(unsigned int i = 0; i < 3; i++ ) {
233  rHighPoint[i] = ( rHighPoint[i] < pGeometry[point][i] ) ? pGeometry[point][i] : rHighPoint[i];
234  rLowPoint[i] = ( rLowPoint[i] > pGeometry[point][i] ) ? pGeometry[point][i] : rLowPoint[i];
235  }
236  }
237 
238  bool add = true;
239 
240  for(unsigned int i = 0; i < 3; i++) {
241  if(( rHighPoint[i] < DEM_BB_LowPoint[i] ) || ( rLowPoint[i] > DEM_BB_HighPoint[i] )) {
242  add = false;
243  break;
244  }
245  }
246 
247  if(add) {
248  for(unsigned int i = 0; i < 3; i++ ) {
249  Vector_GLOBAL_BB_LowPoint[k][i] = (Vector_GLOBAL_BB_LowPoint[k][i] > rLowPoint[i]) ? rLowPoint[i] : Vector_GLOBAL_BB_LowPoint[k][i];
250  Vector_GLOBAL_BB_HighPoint[k][i] = (Vector_GLOBAL_BB_HighPoint[k][i] < rHighPoint[i]) ? rHighPoint[i] : Vector_GLOBAL_BB_HighPoint[k][i];
251  }
252  Vector_BinsConditionPointerToGeometricalObjecPointerTemporalVector[k].push_back(*it);
253  }
254  }//parallel for
255 
256  }//parallel omp
257 
258  //5. GATHER FEM ELEMENTS AND THE GLOBAL BBX
259  int fem_total_size = 0;
260  for(int k = 0; k < num_of_threads; k++) {
261  fem_total_size += Vector_BinsConditionPointerToGeometricalObjecPointerTemporalVector[k].size();
262  }
263 
264  BinsConditionPointerToGeometricalObjecPointerTemporalVector.reserve(fem_total_size);
265 
266  for(int k = 0; k < num_of_threads; k++) {
267  BinsConditionPointerToGeometricalObjecPointerTemporalVector.insert(
268  BinsConditionPointerToGeometricalObjecPointerTemporalVector.end(),
269  Vector_BinsConditionPointerToGeometricalObjecPointerTemporalVector[k].begin(),
270  Vector_BinsConditionPointerToGeometricalObjecPointerTemporalVector[k].end()
271  );
272 
273  for(std::size_t i = 0; i < 3; i++) {
274  mGlobal_BB_LowPoint[i] = (mGlobal_BB_LowPoint[i] > Vector_GLOBAL_BB_LowPoint[k][i]) ? Vector_GLOBAL_BB_LowPoint[k][i] : mGlobal_BB_LowPoint[i];
275  mGlobal_BB_HighPoint[i] = (mGlobal_BB_HighPoint[i] < Vector_GLOBAL_BB_HighPoint[k][i]) ? Vector_GLOBAL_BB_HighPoint[k][i] : mGlobal_BB_HighPoint[i];
276  }
277  }
278 
279  if(BinsConditionPointerToGeometricalObjecPointerTemporalVector.size() >0 ) {
280 
281  //6. CREATE THE BINS
282 
283  //if (mBins) free(mBins);
284  delete mBins;
285  mBins = new GeometricalBinsType(BinsConditionPointerToGeometricalObjecPointerTemporalVector.begin(), BinsConditionPointerToGeometricalObjecPointerTemporalVector.end());
286 
287  //7. PERFORM THE SEARCH ON THE SPHERES
288  #pragma omp parallel
289  {
290  GeometricalObjectType::ContainerType localResults(MaxNumberOfElements);
291 
292  DistanceType localResultsDistances(MaxNumberOfElements);
293  std::size_t NumberOfResults = 0;
294 
295  #pragma omp for schedule(dynamic, 100)
296  for (int p = 0; p < (int)elements_sear.size(); p++) {
297 
298  Elem_iter it = elements_sear.begin() + p;
299 
300  GeometricalObject::Pointer go_it(*it);
301  bool search_particle = true;
302 
303  array_1d<double, 3 > & aux_coor = go_it->GetGeometry()[0].Coordinates();
304 
305  SphericParticle* p_particle = dynamic_cast<SphericParticle*>((*it).get());
306  double Rad = p_particle->GetSearchRadius();
307 
308  for(unsigned int i = 0; i < 3; i++ ) {
309  search_particle &= !(aux_coor[i] < (mGlobal_BB_LowPoint[i] - Rad) ) || (aux_coor[i] > (mGlobal_BB_HighPoint[i] + Rad) ); //amplify the BBX with the radius for every particle
310  }
311 
312  if(search_particle) {
313 
314  auto ResultsPointer = localResults.begin();
315  DistanceType::iterator ResultsDistancesPointer = localResultsDistances.begin();
316 
317  NumberOfResults = (*mBins).SearchObjectsInRadiusExclusive(go_it,Rad,ResultsPointer,ResultsDistancesPointer,MaxNumberOfElements);
318 
319  rResults[p].reserve(NumberOfResults);
320 
321  for(auto c_it = localResults.begin(); c_it != localResults.begin() + NumberOfResults; c_it++) {
322  auto presult = *c_it;
323  Condition::Pointer condition = dynamic_pointer_cast<Condition>(presult);
324  //Condition::Pointer condition = Kratos::dynamic_pointer_cast<Condition>(*c_it);
325  rResults[p].push_back(condition);
326  }
327 
328  rResultsDistance[p].insert(rResultsDistance[p].begin(),localResultsDistances.begin(),localResultsDistances.begin()+NumberOfResults);
329 
330  }
331 
332  } //loop elements
333  } //parallel
334 
335  }//if Bins is not empty--> search
336 
337  KRATOS_CATCH("")
338  }
339 
340 
342  return (mGlobal_BB_HighPoint);
343  }
345  return (mGlobal_BB_LowPoint);
346  }
347 
349  virtual std::string Info() const override
350  {
351  std::stringstream buffer;
352  buffer << "DEM_FEM_Search" ;
353 
354  return buffer.str();
355  }
356 
358  virtual void PrintInfo(std::ostream& rOStream) const override {rOStream << "DEM_FEM_Search";}
359 
361  virtual void PrintData(std::ostream& rOStream) const override {}
362 
363 
367 
368 
370 
371  protected:
374 
375 
379 
380 
381 
385 
386 
390 
391 
395 
396 
400 
401 
405 
406 
408 
409  private:
410  array_1d<double, 3 > mGlobal_BB_HighPoint;
411  array_1d<double, 3 > mGlobal_BB_LowPoint;
412 
413  array_1d<double, 3 > DEM_BB_HighPoint;
414  array_1d<double, 3 > DEM_BB_LowPoint;
415  GeometricalBinsType* mBins;
416 
417 
418 
419 
422 
423 
427 
428 
432 
433 
437 
438 
442 
443 
447 
448 
452 
454  DEM_FEM_Search& operator=(DEM_FEM_Search const& rOther)
455  {
456  return *this;
457  }
458 
460  DEM_FEM_Search(DEM_FEM_Search const& rOther)
461  {
462  *this = rOther;
463  }
464 
465 
466 
467 
468  }; // Class DEM_FEMSearch
469 
470 
471 } // namespace Kratos.
472 
473 #endif // KRATOS_DEM_FEM_SEARCH_H_INCLUDED defined
PeriodicInterfaceProcess & operator=(const PeriodicInterfaceProcess &)=delete
Short class definition.
Definition: bins_dynamic_objects.h:57
Short class definition.
Definition: dem_fem_search.h:55
double * DistanceIterator
Definition: dem_fem_search.h:68
PointType * PtrPointType
Definition: dem_fem_search.h:63
std::vector< PtrPointType > * PointVector
Definition: dem_fem_search.h:64
BinsObjectDynamic< RigidFaceGeometricalConfigureType > GeometricalBinsType
Definition: dem_fem_search.h:78
~DEM_FEM_Search()
Destructor.
Definition: dem_fem_search.h:97
DEM_FEM_Search()
Default constructor.
Definition: dem_fem_search.h:90
void SearchRigidFaceForDEMInRadiusExclusiveImplementation(ElementsContainerType const &rElements, ConditionsContainerType const &rConditions, VectorResultConditionsContainerType &rResults, VectorDistanceType &rResultsDistance)
Definition: dem_fem_search.h:101
array_1d< double, 3 > GetBBLowPoint()
Definition: dem_fem_search.h:344
virtual void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: dem_fem_search.h:358
RigidFaceGeometricalObjectConfigure< 3 > RigidFaceGeometricalConfigureType
Definition: dem_fem_search.h:76
double * DistanceVector
Definition: dem_fem_search.h:67
KRATOS_CLASS_POINTER_DEFINITION(DEM_FEM_Search)
Pointer definition of OMP_DEMSearch.
virtual void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: dem_fem_search.h:361
std::vector< PtrPointType >::iterator PointIterator
Definition: dem_fem_search.h:65
array_1d< double, 3 > GetBBHighPoint()
Definition: dem_fem_search.h:341
virtual std::string Info() const override
Turn back information as a string.
Definition: dem_fem_search.h:349
RigidFaceGeometricalConfigureType::ElementsContainerType GeometricalObjectType
Definition: dem_fem_search.h:80
Geometry base class.
Definition: geometry.h:71
SizeType size() const
Definition: geometry.h:518
static int ThisThread()
Wrapper for omp_get_thread_num().
Definition: openmp_utils.h:108
static int GetNumThreads()
Returns the current number of threads.
Definition: parallel_utilities.cpp:34
Point class.
Definition: point.h:59
A sorted associative container similar to an STL set, but uses a vector to store pointers to its data...
Definition: pointer_vector_set.h:72
TContainerType ContainerType
Definition: pointer_vector_set.h:90
Definition: rigid_face_geometrical_object_configure.h:48
This class is used to search for elements, conditions and nodes in a given model part.
Definition: spatial_search.h:50
std::vector< ResultConditionsContainerType > VectorResultConditionsContainerType
Definition: spatial_search.h:92
std::vector< double > RadiusArrayType
Input/output types.
Definition: spatial_search.h:95
std::vector< DistanceType > VectorDistanceType
Definition: spatial_search.h:97
ModelPart::ElementsContainerType ElementsContainerType
Elements classes.
Definition: spatial_search.h:85
ModelPart::ConditionsContainerType ConditionsContainerType
Conditions classes.
Definition: spatial_search.h:90
std::vector< double > DistanceType
Definition: spatial_search.h:96
Definition: spheric_particle.h:31
virtual double GetSearchRadius()
Definition: spheric_particle.cpp:2201
BOOST_UBLAS_INLINE size_type size() const
Definition: array_1d.h:370
BOOST_UBLAS_INLINE const_iterator begin() const
Definition: array_1d.h:606
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484
REACTION_CHECK_STIFFNESS_FACTOR int
Definition: contact_structural_mechanics_application_variables.h:75
c
Definition: generate_weakly_compressible_navier_stokes_element.py:108
float radius
Definition: mesh_to_mdpa_converter.py:18
int k
Definition: quadrature.py:595
p
Definition: sensitivityMatrix.py:52
integer i
Definition: TensorModule.f:17
Configure::ContainerType ContainerType
Definition: transfer_utility.h:247