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.
structured_mesh_refinement_modeler.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 
14 
15 #if !defined(KRATOS_STRUCTURED_MESH_REFINEMENT_H_INCLUDED )
16 #define KRATOS_STRUCTURED_MESH_REFINEMENT_H_INCLUDED
17 
18 
19 
20 // System includes
21 #include <string>
22 #include <iostream>
23 
24 
25 // External includes
26 
27 
28 // Project includes
29 #include "includes/define.h"
30 #include "modeler/modeler.h"
32 
33 namespace Kratos
34 {
35 
38 
42 
46 
50 
54 
56 
59 {
60 public:
63 
66 
67  typedef Modeler BaseType;
68 
69  typedef Node NodeType;
70 
72 
73  typedef std::size_t SizeType;
74 
78 
81 
84 
85 
89 
90 
94 
95  void GenerateMesh(ModelPart& rThisModelPart, Element const& rReferenceElement)
96  {
98  old_elements.swap(rThisModelPart.Elements());
99 
100  ModelPart::ConditionsContainerType old_conditions;
101  old_conditions.swap(rThisModelPart.Conditions());
102 
103  const double tolerance = 1e-9;
104 
105  // Creating a bins for searching the nodes to be collapsed
106  //typedef Bucket<3, NodeType, ModelPart::NodesContainerType::ContainerType> bucket_type;
108 
109  bins_type nodes_bins(rThisModelPart.Nodes().ptr_begin(), rThisModelPart.Nodes().ptr_end());
111 
112 
113  for(ModelPart::ElementIterator i_element = old_elements.begin() ; i_element != old_elements.end() ; i_element++)
114  {
115  // Getting the number of divisions;
116  vector<int>& number_of_divisions = i_element->GetValue(STRUCTURED_MESH_DIVISIONS);
117 
118  //int node_id = rThisModelPart.Nodes().size() + 1;
119  int node_id = rThisModelPart.Nodes().back().Id() + 1;
120  int start_element_id = 1;
121  if(rThisModelPart.Elements().size() != 0)
122  start_element_id = rThisModelPart.Elements().back().Id() + 1;
123 
124 
125  // Calcualting the size of the nodes after refining this element
126  SizeType nodes_vector_size = number_of_divisions[0] + 1;
127  for(SizeType i = 1 ; i < number_of_divisions.size() ; i++)
128  nodes_vector_size *= number_of_divisions[i] + 1;
129 
130  if(nodes_vector_size > i_element->GetGeometry().size()) // if we need to create more nodes than actual we have to refine
131  {
132  NodesVectorType nodes_vector;
133  NodesVectorType surface_nodes_vector;
134  GenerateNodes(*i_element, nodes_vector, surface_nodes_vector, number_of_divisions);
135  for(SizeType i = 0 ; i < nodes_vector.size() ; i++)
136  {
137  NodeType::Pointer p_node = nodes_vector(i);
138  if(nodes_bins.SearchInRadius(*p_node, tolerance, founded_nodes.begin(), 1) > 0) // It founds a node in the radius of tolerance so there was a node there
139  {
140  nodes_vector(i) = founded_nodes[0];
141  }
142  else
143  {
144  p_node->SetId(node_id++);
145  rThisModelPart.AddNode(p_node);
146  nodes_bins.AddPoint(p_node);
147  }
148  }
149  GenerateElements(rThisModelPart, *i_element, nodes_vector, surface_nodes_vector, number_of_divisions, start_element_id);
150 
151  }
152  else
153  {
154  i_element->SetId(start_element_id);
155  rThisModelPart.Elements().push_back(*(i_element.base()));
156  }
157 
158  }
159 
160  for(ModelPart::ConditionIterator i_condition = old_conditions.begin() ; i_condition != old_conditions.end() ; i_condition++)
161  {
162  // Getting the number of divisions;
163  vector<int>& number_of_divisions = i_condition->GetValue(STRUCTURED_MESH_DIVISIONS);
164 
165  int node_id = rThisModelPart.Nodes().size() + 1;
166  int start_condition_id = rThisModelPart.Conditions().size() + 1;
167  // Calcualting the size of the nodes after refining this condition
168  SizeType nodes_vector_size = number_of_divisions[0] + 1;
169  for(SizeType i = 1 ; i < i_condition->GetGeometry().LocalSpaceDimension() ; i++)
170  nodes_vector_size *= number_of_divisions[i] + 1;
171 
172  if(nodes_vector_size > i_condition->GetGeometry().size()) // if we need to create more nodes than actual we have to refine
173  {
174  NodesVectorType nodes_vector;
175  NodesVectorType surface_nodes_vector;
176  GenerateNodes(*i_condition, nodes_vector, surface_nodes_vector, number_of_divisions);
177  for(SizeType i = 0 ; i < nodes_vector.size() ; i++)
178  {
179  NodeType::Pointer p_node = nodes_vector(i);
180  if(nodes_bins.SearchInRadius(*p_node, tolerance, founded_nodes.begin(), 1) > 0) // It founds a node in the radius of tolerance so there was a node there
181  {
182  nodes_vector(i) = founded_nodes[0];
183  }
184  else
185  {
186  p_node->SetId(node_id++);
187  rThisModelPart.AddNode(p_node);
188  nodes_bins.AddPoint(p_node);
189  }
190  }
191  GenerateConditions(rThisModelPart, *i_condition, nodes_vector, surface_nodes_vector, number_of_divisions, start_condition_id);
192 
193  }
194  else
195  {
196  i_condition->SetId(start_condition_id);
197  rThisModelPart.Conditions().push_back(*(i_condition.base()));
198  }
199  }
200  }
201 
202  template<class ComponentType>
203  void GenerateNodes(ComponentType& rThisComponent, NodesVectorType& VolumeNodesVector, NodesVectorType& SurfaceNodesVector, vector<int>& number_of_divisions)
204  {
205  SizeType local_dimension = rThisComponent.GetGeometry().LocalSpaceDimension();
206  Element::GeometryType::CoordinatesArrayType local_coordinates(local_dimension);
208 
209  if(local_dimension == 2)
210  {
211  for(int i = 0 ; i <= number_of_divisions[0] ; i++)
212  {
213  local_coordinates[0] = double(-1.00) + double(2.00 * i) / number_of_divisions[0];
214  for(int j = 0 ; j <= number_of_divisions[1] ; j++)
215  {
216  local_coordinates[1] = double(-1.00) + double(2.00 * j) / number_of_divisions[1];
217  GenerateNode(rThisComponent, VolumeNodesVector, SurfaceNodesVector, local_coordinates);
218  }
219  }
220  }
221  if(local_dimension == 3)
222  {
223  for(int i = 0 ; i <= number_of_divisions[0] ; i++)
224  {
225  local_coordinates[0] = double(-1.00) + double(2.00 * i) / number_of_divisions[0];
226  for(int j = 0 ; j <= number_of_divisions[1] ; j++)
227  {
228  local_coordinates[1] = double(-1.00) + double(2.00 * j) / number_of_divisions[1];
229  for(int k = 0 ; k <= number_of_divisions[2] ; k++)
230  {
231  local_coordinates[2] = double(-1.00) + double(2.00 * k) / number_of_divisions[2];
232  GenerateNode(rThisComponent, VolumeNodesVector, SurfaceNodesVector, local_coordinates);
233  }
234  }
235  }
236  }
237  }
238 
239  template<class ComponentType>
240  void GenerateNode(ComponentType& rThisComponent, NodesVectorType& VolumeNodesVector, NodesVectorType& SurfaceNodesVector, Element::GeometryType::CoordinatesArrayType& rLocalCoordinates)
241  {
242  SizeType local_dimension = rThisComponent.GetGeometry().LocalSpaceDimension();
243  typename ComponentType::GeometryType::CoordinatesArrayType global_coordinates;
244 
245  typename ComponentType::GeometryType& r_geometry = rThisComponent.GetGeometry();
246 
247  const SizeType components_nodes_number = r_geometry.size();
248 
249  Vector shape_functions_values(components_nodes_number);
250 
251  // Getting the shape function value for given local coordinates
252  for(SizeType h = 0 ; h < components_nodes_number ; h++)
253  shape_functions_values[h] = r_geometry.ShapeFunctionValue(h, rLocalCoordinates);
254 
255 
256  // Calculating the GlobalCoordinates
257  r_geometry.GlobalCoordinates(global_coordinates, rLocalCoordinates);
258 
259 
260  // Interpolating the Nodal data
261  SizeType ndoal_data_size = r_geometry[0].SolutionStepData().TotalDataSize() / sizeof(double);
262 
264 
265  block_type* nodal_data = new block_type[ndoal_data_size];
266 
267  // Initializing to zero
268  for(SizeType i = 0 ; i < ndoal_data_size ; i++)
269  nodal_data[i] = block_type();
270 
271  for(SizeType i = 0 ; i < components_nodes_number ; i++)
272  {
273  block_type* p_data = r_geometry[i].SolutionStepData().Data();
274  for(SizeType j = 0 ; j < ndoal_data_size ; j++)
275  {
276  nodal_data[j] += shape_functions_values[i] * p_data[j];
277  }
278  }
279 
280  // Creating the new node
281  NodeType::Pointer p_node(new NodeType(0, global_coordinates[0], global_coordinates[1], global_coordinates[2], r_geometry[0].pGetVariablesList(), nodal_data, r_geometry[0].GetBufferSize()));
282 
283  SizeType number_of_dofs = r_geometry[0].GetDofs().size();
284 
285  for(NodeType::DofsContainerType::iterator i_dof = r_geometry[0].GetDofs().begin() ; i_dof != r_geometry[0].GetDofs().end() ; i_dof++)
286  {
287  VariableData const& r_dof_variable = i_dof->GetVariable();
288  double dof_is_fixed = double();
289 
290  for(SizeType i = 0 ; i < components_nodes_number ; i++)
291  {
292  dof_is_fixed += shape_functions_values[i] * static_cast<double>(r_geometry[i].IsFixed(r_dof_variable));
293  }
294  if(dof_is_fixed > 0.999999)
295  {
296  p_node->pAddDof(*i_dof)->FixDof();
297  }
298  }
299 
300 
301 
302  VolumeNodesVector.push_back(p_node);
303  }
304 
305  void GenerateElements(ModelPart& rThisModelPart, Element& rThisElement, NodesVectorType& VolumeNodesVector, NodesVectorType& SurfaceNodesVector, vector<int>& number_of_divisions, SizeType StartElementId)
306  {
307  SizeType local_dimension = rThisElement.GetGeometry().LocalSpaceDimension();
308  Element::NodesArrayType element_nodes(rThisElement.GetGeometry().size());
309  SizeType nodes_number_i = number_of_divisions[0] + 1; // Number of nodes in i direction
310  SizeType nodes_number_j = number_of_divisions[1] + 1; // Number of nodes in j direction
311  SizeType nodes_number_k = number_of_divisions[2] + 1; // Number of nodes in k direction
312 
313  if(local_dimension == 3)
314  {
315  for(int i = 0 ; i < number_of_divisions[0] ; i++)
316  {
317  for(int j = 0 ; j < number_of_divisions[1] ; j++)
318  {
319  for(int k = 0 ; k < number_of_divisions[2] ; k++)
320  {
321  // This is done only for Hexahedra
322  SizeType local_id = i * nodes_number_k * nodes_number_j + j * nodes_number_k + k;
323  element_nodes(0) = VolumeNodesVector(local_id);
324  element_nodes(1) = VolumeNodesVector(local_id + nodes_number_j * nodes_number_k);
325  element_nodes(2) = VolumeNodesVector(local_id + nodes_number_j * nodes_number_k + nodes_number_k);
326  element_nodes(3) = VolumeNodesVector(local_id + nodes_number_k);
327  element_nodes(4) = VolumeNodesVector(local_id + 1);
328  element_nodes(5) = VolumeNodesVector(local_id + nodes_number_j * nodes_number_k + 1);
329  element_nodes(6) = VolumeNodesVector(local_id + nodes_number_j * nodes_number_k + nodes_number_k + 1);
330  element_nodes(7) = VolumeNodesVector(local_id + nodes_number_k + 1);
331 
332  rThisModelPart.Elements().push_back(rThisElement.Create(local_id + StartElementId, element_nodes, rThisElement.pGetProperties()));
333 
334 
335  }
336  }
337  }
338  }
339  }
340 
341  void GenerateConditions(ModelPart& rThisModelPart, Condition& rThisCondition, NodesVectorType& VolumeNodesVector, NodesVectorType& SurfaceNodesVector, vector<int>& number_of_divisions, SizeType StartConditionId)
342  {
343  SizeType local_dimension = rThisCondition.GetGeometry().LocalSpaceDimension();
344  Condition::NodesArrayType condition_nodes(rThisCondition.GetGeometry().size());
345  SizeType nodes_number_i = number_of_divisions[0] + 1; // Number of nodes in i direction
346  SizeType nodes_number_j = number_of_divisions[1] + 1; // Number of nodes in j direction
347 
348  if(local_dimension == 2)
349  {
350  for(int i = 0 ; i < number_of_divisions[0] ; i++)
351  {
352  for(int j = 0 ; j < number_of_divisions[1] ; j++)
353  {
354  // This is done only for quadrilateral
355  SizeType local_id = i * nodes_number_j + j;
356  condition_nodes(0) = VolumeNodesVector(local_id);
357  condition_nodes(1) = VolumeNodesVector(local_id + nodes_number_j);
358  condition_nodes(2) = VolumeNodesVector(local_id + nodes_number_j + 1);
359  condition_nodes(3) = VolumeNodesVector(local_id + 1);
360 
361  rThisModelPart.Conditions().push_back(rThisCondition.Create(local_id + StartConditionId, condition_nodes, rThisCondition.pGetProperties()));
362 
363  }
364  }
365  }
366  }
367 
368  void GenerateNodes(ModelPart& ThisModelPart, SizeType NumberOfSegments)
369  {
370  }
371 
372  /*
373  void Interpolate(Element& rThisElement
374  ModelPart::ElementsContainerType::iterator el_it,
375  const array_1d<double,3>& N,
376  int step_data_size,
377  Node::Pointer pnode)
378  {
379  //Geometry element of the rOrigin_ModelPart
380  Geometry< Node >& geom = el_it->GetGeometry();
381 
382  unsigned int buffer_size = pnode->GetBufferSize();
383 
384  for(unsigned int step = 0; step<buffer_size; step++)
385  {
386  //getting the data of the solution step
387  double* step_data = (pnode)->SolutionStepData().Data(step);
388 
389  double* node0_data = geom[0].SolutionStepData().Data(step);
390  double* node1_data = geom[1].SolutionStepData().Data(step);
391  double* node2_data = geom[2].SolutionStepData().Data(step);
392 
393  //copying this data in the position of the vector we are interested in
394  for(int j= 0; j< step_data_size; j++)
395  {
396  step_data[j] = N[0]*node0_data[j] + N[1]*node1_data[j] + N[2]*node2_data[j];
397  }
398  }
399  }
400 
401  */
402 
403 
404 
405 
406 
407 
408 
409 
410 
414 
415 
419 
420 
424 
426  virtual std::string Info() const
427  {
428  return "StructuredMeshRefinementModeler";
429  }
430 
432  virtual void PrintInfo(std::ostream& rOStream) const
433  {
434  rOStream << Info();
435  }
436 
438  virtual void PrintData(std::ostream& rOStream) const
439  {
440  }
441 
442 
446 
447 
449 
450 protected:
453 
454 
458 
459 
463 
464 
468 
469 
473 
474 
478 
479 
483 
484 
486 
487 private:
490 
491 
495 
496 
500 
501 
505 
506 
507  void GenerateNodes(ModelPart& ThisModelPart, GeometryType& rGeometry, SizeType NumberOfSegments, SizeType StartNodeId)
508  {
509  double x1 = rGeometry[0][0];
510  double y1 = rGeometry[0][1];
511  double z1 = rGeometry[0][2];
512  double x2 = rGeometry[1][0];
513  double y2 = rGeometry[1][1];
514  double z2 = rGeometry[1][2];
515 
516  double dx = (x2 - x1) / NumberOfSegments;
517  double dy = (y2 - y1) / NumberOfSegments;
518  double dz = (z2 - z1) / NumberOfSegments;
519 
520  for(SizeType i = 1 ; i < NumberOfSegments - 1 ; i++)
521  {
522  ThisModelPart.CreateNewNode(StartNodeId++, x1 + i * dx, y1 + i * dy, z1 + i * dz);
523  }
524  }
525 
529 
530 
534 
535 
539 
542 
545 
546 
548 
549 }; // Class StructuredMeshRefinementModeler
550 
552 
555 
556 
560 
561 
563 inline std::istream& operator >> (std::istream& rIStream,
565 
567 inline std::ostream& operator << (std::ostream& rOStream,
568  const StructuredMeshRefinementModeler& rThis)
569 {
570  rThis.PrintInfo(rOStream);
571  rOStream << std::endl;
572  rThis.PrintData(rOStream);
573 
574  return rOStream;
575 }
577 
578 
579 } // namespace Kratos.
580 
581 #endif // KRATOS_STRUCTURED_MESH_REFINEMENT_H_INCLUDED defined
582 
583 
A dynamic binning data structure template for organizing and querying points in multi-dimensional spa...
Definition: bins_dynamic.h:57
Base class for all Conditions.
Definition: condition.h:59
virtual Pointer Create(IndexType NewId, NodesArrayType const &ThisNodes, PropertiesType::Pointer pProperties) const
It creates a new condition pointer.
Definition: condition.h:205
PropertiesType::Pointer pGetProperties()
returns the pointer to the property of the condition. Does not throw an error, to allow copying of co...
Definition: condition.h:964
Base class for all Elements.
Definition: element.h:60
virtual Pointer Create(IndexType NewId, NodesArrayType const &ThisNodes, PropertiesType::Pointer pProperties) const
It creates a new element pointer.
Definition: element.h:202
PropertiesType::Pointer pGetProperties()
returns the pointer to the property of the element. Does not throw an error, to allow copying of elem...
Definition: element.h:1014
GeometryType & GetGeometry()
Returns the reference of the geometry.
Definition: geometrical_object.h:158
Geometry base class.
Definition: geometry.h:71
SizeType size() const
Definition: geometry.h:518
SizeType LocalSpaceDimension() const
Definition: geometry.h:1300
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
MeshType::ConditionsContainerType ConditionsContainerType
Condintions container. A vector set of Conditions with their Id's as key.
Definition: model_part.h:183
NodeType::Pointer CreateNewNode(int Id, double x, double y, double z, VariablesList::Pointer pNewVariablesList, IndexType ThisIndex=0)
Definition: model_part.cpp:270
void AddNode(NodeType::Pointer pNewNode, IndexType ThisIndex=0)
Definition: model_part.cpp:211
MeshType::ElementsContainerType ElementsContainerType
Element container. A vector set of Elements with their Id's as key.
Definition: model_part.h:168
ConditionsContainerType & Conditions(IndexType ThisIndex=0)
Definition: model_part.h:1381
ElementsContainerType & Elements(IndexType ThisIndex=0)
Definition: model_part.h:1189
MeshType::ElementIterator ElementIterator
Definition: model_part.h:174
MeshType::ConditionIterator ConditionIterator
Definition: model_part.h:189
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
Modeler to interact with ModelParts.
Definition: modeler.h:39
std::size_t SizeType
Definition: modeler.h:47
This class defines the node.
Definition: node.h:65
PointerVector is a container like stl vector but using a vector to store pointers to its data.
Definition: pointer_vector.h:72
size_type size() const
Definition: pointer_vector.h:255
void push_back(const TPointerType &x)
Definition: pointer_vector.h:270
Short class definition.
Definition: structured_mesh_refinement_modeler.h:59
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: structured_mesh_refinement_modeler.h:432
Modeler BaseType
Definition: structured_mesh_refinement_modeler.h:67
Node NodeType
Definition: structured_mesh_refinement_modeler.h:69
virtual ~StructuredMeshRefinementModeler()
Destructor.
Definition: structured_mesh_refinement_modeler.h:83
KRATOS_CLASS_POINTER_DEFINITION(StructuredMeshRefinementModeler)
Pointer definition of StructuredMeshRefinementModeler.
PointerVector< NodeType > NodesVectorType
Definition: structured_mesh_refinement_modeler.h:71
void GenerateMesh(ModelPart &rThisModelPart, Element const &rReferenceElement)
Definition: structured_mesh_refinement_modeler.h:95
void GenerateNodes(ComponentType &rThisComponent, NodesVectorType &VolumeNodesVector, NodesVectorType &SurfaceNodesVector, vector< int > &number_of_divisions)
Definition: structured_mesh_refinement_modeler.h:203
std::size_t SizeType
Definition: structured_mesh_refinement_modeler.h:73
void GenerateConditions(ModelPart &rThisModelPart, Condition &rThisCondition, NodesVectorType &VolumeNodesVector, NodesVectorType &SurfaceNodesVector, vector< int > &number_of_divisions, SizeType StartConditionId)
Definition: structured_mesh_refinement_modeler.h:341
virtual std::string Info() const
Turn back information as a string.
Definition: structured_mesh_refinement_modeler.h:426
void GenerateNodes(ModelPart &ThisModelPart, SizeType NumberOfSegments)
Definition: structured_mesh_refinement_modeler.h:368
void GenerateElements(ModelPart &rThisModelPart, Element &rThisElement, NodesVectorType &VolumeNodesVector, NodesVectorType &SurfaceNodesVector, vector< int > &number_of_divisions, SizeType StartElementId)
Definition: structured_mesh_refinement_modeler.h:305
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: structured_mesh_refinement_modeler.h:438
void GenerateNode(ComponentType &rThisComponent, NodesVectorType &VolumeNodesVector, NodesVectorType &SurfaceNodesVector, Element::GeometryType::CoordinatesArrayType &rLocalCoordinates)
Definition: structured_mesh_refinement_modeler.h:240
StructuredMeshRefinementModeler()
Default constructor.
Definition: structured_mesh_refinement_modeler.h:80
This class is the base of variables and variable's components which contains their common data.
Definition: variable_data.h:49
VariablesList::BlockType BlockType
The block type definition.
Definition: variables_list_data_value_container.h:70
Short class definition.
Definition: array_1d.h:61
end
Definition: DEM_benchmarks.py:180
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
Geometry< Node > GeometryType
The definition of the geometry.
Definition: mortar_classes.h:37
std::size_t SizeType
The definition of the size type.
Definition: mortar_classes.h:43
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
TABLE_NUMBER_ANGULAR_VELOCITY TABLE_NUMBER_MOMENT I33 BEAM_INERTIA_ROT_UNIT_LENGHT_Y KRATOS_DEFINE_APPLICATION_VARIABLE(DEM_APPLICATION, double, BEAM_INERTIA_ROT_UNIT_LENGHT_Z) typedef std double
Definition: DEM_application_variables.h:182
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
h
Definition: generate_droplet_dynamics.py:91
x2
Definition: generate_frictional_mortar_condition.py:122
x1
Definition: generate_frictional_mortar_condition.py:121
int k
Definition: quadrature.py:595
int j
Definition: quadrature.py:648
int node_id
Definition: read_stl.py:12
integer i
Definition: TensorModule.f:17
e
Definition: run_cpp_mpi_tests.py:31
Configure::ContainerType ContainerType
Definition: transfer_utility.h:247