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.
local_refine_triangle_mesh.hpp
Go to the documentation of this file.
1 // KRATOS __ __ _____ ____ _ _ ___ _ _ ____
2 // | \/ | ____/ ___|| | | |_ _| \ | |/ ___|
3 // | |\/| | _| \___ \| |_| || || \| | | _
4 // | | | | |___ ___) | _ || || |\ | |_| |
5 // |_| |_|_____|____/|_| |_|___|_| \_|\____| APPLICATION
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Nelson Lafontaine
11 // Jordi Cotela Dalmau
12 // Riccardo Rossi
13 // Co-authors: Vicente Mataix Ferrandiz
14 //
15 
16 #pragma once
17 
18 // NOTE: Before compute the remeshing it is necessary to compute the neighbours
19 
20 // System includes
21 
22 // External includes
23 
24 // Project includes
25 #include "geometries/line_2d_2.h"
26 #include "geometries/line_3d_2.h"
28 
29 namespace Kratos
30 {
45 
46 class LocalRefineTriangleMesh : public LocalRefineTriangleMeshGeneric<Element, ModelPart::ElementsContainerType>
47 {
48 public:
49 
55 
58  {
59 
60  }
61 
64  = default;
65 
69 
73 
74 
75  /***********************************************************************************/
76  /***********************************************************************************/
77 
87  ModelPart& this_model_part,
88  const compressed_matrix<int>& Coord,
89  PointerVector< Element >& New_Elements,
90  bool interpolate_internal_variables
91  ) override
92  {
94  this_model_part,
95  this_model_part.Elements(),
96  Coord,
97  New_Elements,
98  interpolate_internal_variables
99  );
100 
101  // Now update the elements in SubModelParts
102  if (New_Elements.size() > 0)
103  {
104  UpdateSubModelPartElements(this_model_part.GetRootModelPart(), New_Elements);
105  }
106 
107  }
108 
109  /***********************************************************************************/
110  /***********************************************************************************/
111 
119  ModelPart& this_model_part,
120  const compressed_matrix<int>& Coord
121  ) override
122  {
123  KRATOS_TRY;
124 
125  PointerVector< Condition > New_Conditions;
126 
127  ConditionsArrayType& rConditions = this_model_part.Conditions();
128 
129  if (rConditions.size() > 0)
130  {
131  ConditionsArrayType::iterator it_begin = rConditions.ptr_begin();
132  ConditionsArrayType::iterator it_end = rConditions.ptr_end();
133  unsigned int to_be_deleted = 0;
134  unsigned int large_id = (rConditions.end() - 1)->Id() * 7;
135 
136  const ProcessInfo& rCurrentProcessInfo = this_model_part.GetProcessInfo();
137 
138  const unsigned int dimension = it_begin->GetGeometry().WorkingSpaceDimension();
139 
140  unsigned int current_id = (rConditions.end() - 1)->Id() + 1;
141 
142  for (ConditionsArrayType::iterator it = it_begin; it != it_end; ++it)
143  {
144  Condition::GeometryType& geom = it->GetGeometry();
145 
146  if (geom.size() == 2)
147  {
148  int index_0 = mMapNodeIdToPos[geom[0].Id()];
149  int index_1 = mMapNodeIdToPos[geom[1].Id()];
150  int new_id;
151 
152  if (index_0 > index_1)
153  {
154  new_id = Coord(index_1, index_0);
155  }
156  else
157  {
158  new_id = Coord(index_0, index_1);
159  }
160 
161  if (new_id > 0) // We need to create a new condition
162  {
163  to_be_deleted++;
164  if (dimension == 2)
165  {
166  Line2D2<Node > newgeom1(
167  this_model_part.Nodes()(geom[0].Id()),
168  this_model_part.Nodes()(new_id)
169  );
170 
171  Line2D2<Node > newgeom2(
172  this_model_part.Nodes()(new_id),
173  this_model_part.Nodes()(geom[1].Id())
174  );
175 
176  Condition::Pointer pcond1 = it->Create(current_id++, newgeom1, it->pGetProperties());
177  Condition::Pointer pcond2 = it->Create(current_id++, newgeom2, it->pGetProperties());
178 
179  pcond1->GetData() = it->GetData();
180  pcond2->GetData() = it->GetData();
181 
182  New_Conditions.push_back(pcond1);
183  New_Conditions.push_back(pcond2);
184  }
185  else
186  {
187  Line3D2<Node > newgeom1(
188  this_model_part.Nodes()(geom[0].Id()),
189  this_model_part.Nodes()(new_id)
190  );
191 
192  Line3D2<Node > newgeom2(
193  this_model_part.Nodes()(new_id),
194  this_model_part.Nodes()(geom[1].Id())
195  );
196 
197  Condition::Pointer pcond1 = it->Create(current_id++, newgeom1, it->pGetProperties());
198  Condition::Pointer pcond2 = it->Create(current_id++, newgeom2, it->pGetProperties());
199 
200  pcond1->GetData() = it->GetData();
201  pcond2->GetData() = it->GetData();
202 
203  New_Conditions.push_back(pcond1);
204  New_Conditions.push_back(pcond2);
205  }
206 
207  it->SetId(large_id);
208  large_id++;
209  }
210  }
211  }
212 
213  /* All of the elements to be erased are at the end */
214  this_model_part.Conditions().Sort();
215 
216  /* Remove all of the "old" elements */
217  this_model_part.Conditions().erase(this_model_part.Conditions().end() - to_be_deleted, this_model_part.Conditions().end());
218 
219  unsigned int total_size = this_model_part.Conditions().size() + New_Conditions.size();
220  this_model_part.Conditions().reserve(total_size);
221 
222  /* Adding news elements to the model part */
223  for (PointerVector< Condition >::iterator it_new = New_Conditions.begin(); it_new != New_Conditions.end(); it_new++)
224  {
225  it_new->Initialize(rCurrentProcessInfo);
226  it_new->InitializeSolutionStep(rCurrentProcessInfo);
227  it_new->FinalizeSolutionStep(rCurrentProcessInfo);
228  this_model_part.Conditions().push_back(*(it_new.base()));
229  }
230 
231  /* Renumber */
232  unsigned int my_index = 1;
233  for (ModelPart::ConditionsContainerType::iterator it = this_model_part.ConditionsBegin(); it != this_model_part.ConditionsEnd(); it++)
234  {
235  it->SetId(my_index++);
236  }
237 
238  }
239 
240  KRATOS_CATCH("");
241  }
242 
248  void UpdateSubModelPartElements(ModelPart& this_model_part, PointerVector< Element >& NewElements)
249  {
250  for (ModelPart::SubModelPartIterator iSubModelPart = this_model_part.SubModelPartsBegin();
251  iSubModelPart != this_model_part.SubModelPartsEnd(); iSubModelPart++)
252  {
253  unsigned int to_be_deleted = 0;
254  NewElements.clear();
255 
256  // Create list of new elements in SubModelPart
257  // Count how many elements will be removed
258  for (ModelPart::ElementIterator iElem = iSubModelPart->ElementsBegin();
259  iElem != iSubModelPart->ElementsEnd(); iElem++)
260  {
261  if( iElem->GetValue(SPLIT_ELEMENT) )
262  {
263  to_be_deleted++;
264  GlobalPointersVector< Element >& rChildElements = iElem->GetValue(NEIGHBOUR_ELEMENTS);
265 
266  for ( auto iChild = rChildElements.ptr_begin();
267  iChild != rChildElements.ptr_end(); iChild++ )
268  {
269  NewElements.push_back((*iChild)->shared_from_this());
270  }
271  }
272  }
273 
274  // Add new elements to SubModelPart
275  iSubModelPart->Elements().reserve( iSubModelPart->Elements().size() + NewElements.size() );
276  for (PointerVector< Element >::iterator it_new = NewElements.begin();
277  it_new != NewElements.end(); it_new++)
278  {
279  iSubModelPart->Elements().push_back(*(it_new.base()));
280  }
281 
282  // Delete old elements
283  iSubModelPart->Elements().Sort();
284  iSubModelPart->Elements().erase(iSubModelPart->Elements().end() - to_be_deleted, iSubModelPart->Elements().end());
285  /*
286  KRATOS_WATCH(iSubModelPart->Info());
287  KRATOS_WATCH(to_be_deleted);
288  KRATOS_WATCH(iSubModelPart->Elements().size());
289  KRATOS_WATCH(this_model_part.Elements().size());
290  */
291 
292  //NEXT LEVEL
293  if (NewElements.size() > 0)
294  {
295  ModelPart &rSubModelPart = *iSubModelPart;
296  UpdateSubModelPartElements(rSubModelPart,NewElements);
297  }
298 
299 
300  }
301  }
302  /***********************************************************************************/
303  /***********************************************************************************/
304 
305 
306 };
307 
308 } // namespace Kratos.
Geometry base class.
Definition: geometry.h:71
SizeType size() const
Definition: geometry.h:518
IndexType const & Id() const
Id of this Geometry.
Definition: geometry.h:964
ptr_iterator ptr_begin()
Definition: global_pointers_vector.h:253
ptr_iterator ptr_end()
Definition: global_pointers_vector.h:261
An two node 2D line geometry with linear shape functions.
Definition: line_2d_2.h:65
An two node 3D line geometry with linear shape functions.
Definition: line_3d_2.h:64
std::unordered_map< std::size_t, unsigned int > mMapNodeIdToPos
The current refinement level.
Definition: local_refine_geometry_mesh.hpp:240
ModelPart::ConditionsContainerType ConditionsArrayType
Definition: local_refine_geometry_mesh.hpp:56
Definition: local_refine_triangle_mesh_generic.hpp:47
void EraseOldObjetcsAndCreateNewObjects(ModelPart &rThisModelPart, ModelPart::ElementsContainerType &rObjects, const compressed_matrix< int > &rCoord, PointerVector< Element > &rNewObjects, bool InterpolateInternalVariables)
Definition: local_refine_triangle_mesh_generic.hpp:81
Definition: local_refine_triangle_mesh.hpp:47
void UpdateSubModelPartElements(ModelPart &this_model_part, PointerVector< Element > &NewElements)
Definition: local_refine_triangle_mesh.hpp:248
void EraseOldElementAndCreateNewElement(ModelPart &this_model_part, const compressed_matrix< int > &Coord, PointerVector< Element > &New_Elements, bool interpolate_internal_variables) override
Definition: local_refine_triangle_mesh.hpp:86
~LocalRefineTriangleMesh() override=default
Destructor.
LocalRefineTriangleMesh(ModelPart &model_part)
Default constructors.
Definition: local_refine_triangle_mesh.hpp:57
void EraseOldConditionsAndCreateNew(ModelPart &this_model_part, const compressed_matrix< int > &Coord) override
Definition: local_refine_triangle_mesh.hpp:118
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
SubModelPartIterator SubModelPartsEnd()
Definition: model_part.h:1708
ConditionIterator ConditionsBegin(IndexType ThisIndex=0)
Definition: model_part.h:1361
SubModelPartIterator SubModelPartsBegin()
Definition: model_part.h:1698
SubModelPartsContainerType::iterator SubModelPartIterator
Iterator over the sub model parts of this model part.
Definition: model_part.h:258
ConditionsContainerType & Conditions(IndexType ThisIndex=0)
Definition: model_part.h:1381
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
ModelPart & GetRootModelPart()
Definition: model_part.cpp:510
ElementsContainerType & Elements(IndexType ThisIndex=0)
Definition: model_part.h:1189
MeshType::ElementIterator ElementIterator
Definition: model_part.h:174
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
ConditionIterator ConditionsEnd(IndexType ThisIndex=0)
Definition: model_part.h:1371
PointerVector is a container like stl vector but using a vector to store pointers to its data.
Definition: pointer_vector.h:72
void clear()
Definition: pointer_vector.h:309
size_type size() const
Definition: pointer_vector.h:255
iterator end()
Definition: pointer_vector.h:177
boost::indirect_iterator< typename TContainerType::iterator > iterator
Definition: pointer_vector.h:89
iterator begin()
Definition: pointer_vector.h:169
void push_back(const TPointerType &x)
Definition: pointer_vector.h:270
ProcessInfo holds the current value of different solution parameters.
Definition: process_info.h:59
#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
model_part
Definition: face_heat.py:14
int dimension
Definition: isotropic_damage_automatic_differentiation.py:123
int current_id
Output settings end ####.
Definition: script.py:194