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.
particles_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: Riccardo Rossi
11 //
12 
13 #pragma once
14 
15 // System includes
16 #include <string>
17 #include <iostream>
18 
19 // External includes
20 
21 // Project includes
22 #include "includes/define.h"
25 
26 namespace Kratos
27 {
30 
33 
37 
41 
45 
49 
57 {
58 public:
61 
64 
68 
70  ParticlesUtilities() = delete;
71 
80  template<unsigned int TDim, bool CounterHasHistory=false >
81  static void CountParticlesInNodes(
83  ModelPart& rVolumeModelPart,
84  const ModelPart& rParticlesModelPart,
85  const Variable<double>& rCounterVariable,
86  const double SearchTolerance=1e-5
87  )
88  {
89  //reset the counter
90  block_for_each(rVolumeModelPart.Nodes(), [&rCounterVariable](auto& rNode)
91  {
92  if constexpr (CounterHasHistory)
93  rNode.FastGetSolutionStepValue(rCounterVariable) = 0.0;
94  else
95  rNode.SetValue(rCounterVariable,0.0);
96  });
97 
98 
99  unsigned int max_results = 10000;
100  typename BinBasedFastPointLocator<TDim>::ResultContainerType TLS(max_results);
101 
102  //for every interface node (nodes in cut elements)
103  block_for_each(rParticlesModelPart.Nodes(), TLS, [&rLocator, &rCounterVariable, SearchTolerance](const auto& rNode, auto& rTLS)
104  {
105 
106  Vector shape_functions;
107  Element::Pointer p_element;
108  const bool is_found = rLocator.FindPointOnMesh(rNode.Coordinates(), shape_functions, p_element, rTLS.begin(), rTLS.size(), SearchTolerance);
109 
110  if(is_found)
111  {
112 
113  auto& r_geom = p_element->GetGeometry();
114  for(unsigned int i=0; i<r_geom.size(); ++i)
115  {
116  if constexpr (CounterHasHistory)
117  {
118  auto& rcounter = r_geom[i].FastGetSolutionStepValue(rCounterVariable);
119  AtomicAdd(rcounter, 1.0);
120  }
121  else
122  {
123  auto& rcounter = r_geom[i].GetValue(rCounterVariable);
124  AtomicAdd(rcounter, 1.0);
125  }
126 
127  }
128  }
129 
130  });
131 
132  }
133 
143  template<unsigned int TDim, class TScalarType, bool ParticleTypeVariableHasHistory=false>
146  ModelPart& rVolumeModelPart,
147  const ModelPart& rParticlesModelPart,
148  const int NumberOfTypes,
149  const Variable<TScalarType>& rParticleTypeVariable=AUX_INDEX,
150  const Variable<Vector>& rClassificationVectorVariable=MARKER_LABELS,
151  const double SearchTolerance=1e-5
152  )
153  {
154  //reset the counter
155  Vector zero = ZeroVector(NumberOfTypes);
156  block_for_each(rVolumeModelPart.Elements(), [&rClassificationVectorVariable, &zero](auto& rElement){
157  rElement.SetValue(rClassificationVectorVariable,zero);
158  });
159 
160 
161  unsigned int max_results = 10000;
162  auto TLS = std::make_pair(typename BinBasedFastPointLocator<TDim>::ResultContainerType(max_results), Vector());
163  //typename BinBasedFastPointLocator<TDim>::ResultContainerType TLS(max_results);
164 
165  //for every interface node (nodes in cut elements)
166  block_for_each(rParticlesModelPart.Nodes(),
167  TLS,
168  [&rLocator, &rParticleTypeVariable, &rClassificationVectorVariable, &NumberOfTypes, SearchTolerance]
169  (const auto& rNode, auto& rTLS)
170  {
171  auto& results = rTLS.first;
172  Vector& shape_functions = rTLS.second;
173  Element::Pointer p_element;
174  const bool is_found = rLocator.FindPointOnMesh(rNode.Coordinates(), shape_functions, p_element, results.begin(), results.size(), SearchTolerance);
175 
176  if(is_found)
177  {
178  int particle_type;
179  if constexpr (ParticleTypeVariableHasHistory)
180  particle_type = static_cast<int>(rNode.FastGetSolutionStepValue(rParticleTypeVariable));
181  else
182  particle_type = static_cast<int>(rNode.GetValue(rParticleTypeVariable));
183 
184  if(particle_type>=0 && particle_type<NumberOfTypes) //we will ignore particles identified by a marker <0 or >NumberOfTypes
185  {
186  auto& rclassification = p_element->GetValue(rClassificationVectorVariable);
187  AtomicAdd(rclassification[particle_type], 1.0);
188  }
189  }
190  });
191  }
192 
193 
201  template<unsigned int TDim, class TDataType, bool VariableHasHistory >
204  ModelPart& rParticlesModelPart,
205  const Variable<TDataType>& rVariable,
206  const TDataType& OutsiderValue,
207  const double SearchTolerance=1e-5
208  )
209  {
210  unsigned int max_results = 10000;
211  typename BinBasedFastPointLocator<TDim>::ResultContainerType TLS(max_results);
212 
213  //for every interface node (nodes in cut elements)
214  block_for_each(rParticlesModelPart.Nodes(), TLS, [&rLocator, &rVariable, &OutsiderValue, SearchTolerance](auto& rNode, auto& rTLS)
215  {
216 
217  Vector shape_functions;
218  Element::Pointer p_element;
219  const bool is_found = rLocator.FindPointOnMesh(rNode.Coordinates(), shape_functions, p_element, rTLS.begin(), rTLS.size(), SearchTolerance);
220 
221  if(!is_found)
222  {
223  if constexpr (VariableHasHistory)
224  rNode.FastGetSolutionStepValue(rVariable) = OutsiderValue;
225  else
226  rNode.SetValue(rVariable, OutsiderValue);
227  }
228  });
229  }
230 
242  template<unsigned int TDim, class TDataType, bool InterpolationVariableHasHistory>
243  static std::pair< DenseVector<bool>, std::vector<TDataType> > InterpolateValuesAtCoordinates(
245  const Matrix& rCoordinates,
246  const Variable<TDataType>& rInterpolationVariable,
247  const double SearchTolerance
248  )
249  {
250  unsigned int max_results = 10000;
251  typename BinBasedFastPointLocator<TDim>::ResultContainerType TLS(max_results);
252 
253  auto interpolations = std::make_pair(DenseVector<bool>(rCoordinates.size1()), std::vector<TDataType>(rCoordinates.size1()));
254 
255  // For every interface node (nodes in cut elements)
256  const auto zero = rInterpolationVariable.Zero();
257  IndexPartition(rCoordinates.size1()).for_each(TLS, [&rLocator, &rCoordinates, &interpolations, &rInterpolationVariable, &zero, SearchTolerance](const auto& i, auto& rTLS)
258  {
259  Vector shape_functions;
260  Element::Pointer p_element;
261  const bool is_found = rLocator.FindPointOnMesh(row(rCoordinates,i), shape_functions, p_element, rTLS.begin(), rTLS.size(), SearchTolerance);
262 
263  (interpolations.first)[i] = is_found;
264  if(is_found)
265  {
266  auto& r_geom = p_element->GetGeometry();
267  (interpolations.second)[i] = zero;
268  for(unsigned int k=0; k<r_geom.size(); ++k)
269  {
270  if constexpr (InterpolationVariableHasHistory)
271  (interpolations.second)[i] += shape_functions[k]*r_geom[k].FastGetSolutionStepValue(rInterpolationVariable);
272  else
273  (interpolations.second)[i] += shape_functions[k]*r_geom[k].GetValue(rInterpolationVariable);
274 
275  }
276  }
277  });
278 
279  return interpolations;
280  }
281 
285 
289 
293 
297 
301 
303  std::string Info() const
304  {
305  return std::string("ParticlesUtilities");
306  };
307 
309  void PrintInfo(std::ostream& rOStream) const {};
310 
312  void PrintData(std::ostream& rOStream) const {};
313 
314 
318 
320 private:
323 
327 
331 
335 
339 
343 
347 
349  ParticlesUtilities& operator=(ParticlesUtilities const& rOther) = delete;
350 
352  ParticlesUtilities(ParticlesUtilities const& rOther) = delete;
353 
355 }; // Class ParticlesUtilities
356 
358 
361 
365 
367 inline std::istream& operator >> (std::istream& rIStream,
368  ParticlesUtilities& rThis)
369 {
370  return rIStream;
371 }
372 
374 inline std::ostream& operator << (std::ostream& rOStream,
375  const ParticlesUtilities& rThis)
376 {
377  rThis.PrintInfo(rOStream);
378  rOStream << std::endl;
379  rThis.PrintData(rOStream);
380 
381  return rOStream;
382 }
384 
386 
387 } // namespace Kratos.
388 
PeriodicInterfaceProcess & operator=(const PeriodicInterfaceProcess &)=delete
This class is designed to allow the fast location of MANY points on the top of a 3D mesh.
Definition: binbased_fast_point_locator.h:68
ConfigureType::ResultContainerType ResultContainerType
Definition: binbased_fast_point_locator.h:81
This class is useful for index iteration over containers.
Definition: parallel_utilities.h:451
void for_each(TUnaryFunction &&f)
Definition: parallel_utilities.h:514
iterator begin()
Definition: amatrix_interface.h:241
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
ElementsContainerType & Elements(IndexType ThisIndex=0)
Definition: model_part.h:1189
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
Collection of utilities to compute statistic of particles.
Definition: particles_utilities.h:57
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: particles_utilities.h:309
static void MarkOutsiderParticles(BinBasedFastPointLocator< TDim > &rLocator, ModelPart &rParticlesModelPart, const Variable< TDataType > &rVariable, const TDataType &OutsiderValue, const double SearchTolerance=1e-5)
: this function looks if particle is found in the locator. If it is not, "rVariable" is marked with t...
Definition: particles_utilities.h:202
static void ClassifyParticlesInElements(BinBasedFastPointLocator< TDim > &rLocator, ModelPart &rVolumeModelPart, const ModelPart &rParticlesModelPart, const int NumberOfTypes, const Variable< TScalarType > &rParticleTypeVariable=AUX_INDEX, const Variable< Vector > &rClassificationVectorVariable=MARKER_LABELS, const double SearchTolerance=1e-5)
: this function takes all the nodes in rParticlesModelPart and finds in which element they fall withi...
Definition: particles_utilities.h:144
KRATOS_CLASS_POINTER_DEFINITION(ParticlesUtilities)
Pointer definition of ParticlesUtilities.
static std::pair< DenseVector< bool >, std::vector< TDataType > > InterpolateValuesAtCoordinates(BinBasedFastPointLocator< TDim > &rLocator, const Matrix &rCoordinates, const Variable< TDataType > &rInterpolationVariable, const double SearchTolerance)
This function takes a matrix of coordinates and gives the intrpolated value of the variable rInterpol...
Definition: particles_utilities.h:243
ParticlesUtilities()=delete
Default constructor.
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: particles_utilities.h:312
static void CountParticlesInNodes(BinBasedFastPointLocator< TDim > &rLocator, ModelPart &rVolumeModelPart, const ModelPart &rParticlesModelPart, const Variable< double > &rCounterVariable, const double SearchTolerance=1e-5)
: this function takes all the nodes in rParticlesModelPart and finds in which element they fall withi...
Definition: particles_utilities.h:81
std::string Info() const
Turn back information as a string.
Definition: particles_utilities.h:303
const TDataType & Zero() const
This method returns the zero value of the variable type.
Definition: variable.h:346
std::ostream & operator<<(std::ostream &rOStream, const ParticlesUtilities &rThis)
output stream function
Definition: particles_utilities.h:374
std::istream & operator>>(std::istream &rIStream, ParticlesUtilities &rThis)
input stream function
Definition: particles_utilities.h:367
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
KratosZeroVector< double > ZeroVector
Definition: amatrix_interface.h:561
Internals::Matrix< double, AMatrix::dynamic, 1 > Vector
Definition: amatrix_interface.h:472
void block_for_each(TIterator itBegin, TIterator itEnd, TFunction &&rFunction)
Execute a functor on all items of a range in parallel.
Definition: parallel_utilities.h:299
AMatrix::MatrixRow< const TExpressionType > row(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression, std::size_t RowIndex)
Definition: amatrix_interface.h:649
integer i
Definition: TensorModule.f:17
zero
Definition: test_pureconvectionsolver_benchmarking.py:94
e
Definition: run_cpp_mpi_tests.py:31