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.
generate_new_elements_mesher_process.hpp
Go to the documentation of this file.
1 //
2 // Project Name: KratosDelaunayMeshingApplication $
3 // Created by: $Author: JMCarbonell $
4 // Last modified by: $Co-Author: $
5 // Date: $Date: April 2018 $
6 // Revision: $Revision: 0.0 $
7 //
8 //
9 
10 #if !defined(KRATOS_GENERATE_NEW_ELEMENTS_MESHER_PROCESS_H_INCLUDED)
11 #define KRATOS_GENERATE_NEW_ELEMENTS_MESHER_PROCESS_H_INCLUDED
12 
13 // System includes
14 
15 // External includes
16 
17 // Project includes
20 
22 //Data: MASTER_ELEMENTS(set), MASTER_NODES(set)
23 //StepData:
24 //Flags: (checked) TO_ERASE, TO_REFINE, CONTACT, NEW_ENTITY
25 // (set) BOUNDARY(set), [TO_REFINE(nodes), TO_ERASE(condition)]->locally to not preserve condition
26 // (modified)
27 // (reset)
28 // (set):=(set in this process)
29 
30 namespace Kratos
31 {
32 
35 
39 
43 
47 
51 
53 
56  : public MesherProcess
57  {
58  public:
61 
64 
69 
72 
76 
79  MesherUtilities::MeshingParameters &rRemeshingParameters,
80  int EchoLevel)
81  : mrModelPart(rModelPart),
82  mrRemesh(rRemeshingParameters)
83  {
84  mEchoLevel = EchoLevel;
85  }
86 
89  {
90  }
91 
95 
96  void operator()()
97  {
98  Execute();
99  }
100 
104 
105  void Execute() override
106  {
107  KRATOS_TRY
108 
109  if (mEchoLevel > 0)
110  std::cout << " [ GENERATE NEW ELEMENTS: " << std::endl;
111 
112  if (mrModelPart.Name() != mrRemesh.SubModelPartName)
113  std::cout << " ModelPart Supplied do not corresponds to the Meshing Domain: (" << mrModelPart.Name() << " != " << mrRemesh.SubModelPartName << ")" << std::endl;
114 
115  //*******************************************************************
116  //selecting elements
117 
118  if (!mrRemesh.MeshElementsSelectedFlag) //Select Mesh Elements not performed ... is needed to be done before building new elements
119  {
120  std::cout << " ERROR : no selection of elements performed before building the elements " << std::endl;
121  SelectElementsMesherProcess SelectElements(mrModelPart, mrRemesh, mEchoLevel);
122  SelectElements.Execute();
123  }
124 
125  //*******************************************************************
126  //setting new elements
127  //(mrModelPart.Elements()).reserve(mrRemesh.Info->NumberOfElements);
128 
129  //*******************************************************************
130  // mine 2016 TIP
131  // //All nodes in boundary element change
132  // if(mrRemesh.AvoidTipElementsFlag){ //is not working correctly some dispositions not considered
133  // if( mEchoLevel > 0 )
134  // std::cout<<"[ AVOID TIP ELEMENTS START ]"<<std::endl;
135 
136  // ChangeTipElementsUtilities TipElements;
137  // //TipElements.SwapDiagonals(mrModelPart,out,mrRemesh.PreservedElements);
138 
139  // if( mEchoLevel > 0 )
140  // std::cout<<"[ AVOID TIP ELEMENTS END ]"<<std::endl;
141  // }
142  //*******************************************************************
143 
144  //properties to be used in the generation
145  ModelPart::ElementsContainerType::iterator element_begin = mrModelPart.ElementsBegin();
146  ModelPart::NodesContainerType::iterator nodes_begin = mrModelPart.NodesBegin();
147 
148  // int number_properties = mrModelPart.GetParentModelPart().NumberOfProperties();
149  // if(number_properties<0)
150  // KRATOS_ERROR<<" number of properties is "<<number_properties<<std::endl;
151  // Properties::Pointer properties = mrModelPart.GetParentModelPart().GetMesh().pGetProperties(number_properties-1);
152  // properties->PrintData(std::cout);
153  // std::cout<<std::endl;
154 
155  MeshDataTransferUtilities DataTransferUtilities;
156 
157  const Element &rReferenceElement = mrRemesh.GetReferenceElement();
158 
159  const unsigned int nds = element_begin->GetGeometry().size();
160 
161  int &OutNumberOfElements = mrRemesh.OutMesh.GetNumberOfElements();
162  int *OutElementList = mrRemesh.OutMesh.GetElementList();
163 
164  std::vector<NodeType::Pointer> list_of_element_centers;
165  std::vector<Geometry<NodeType>> list_of_element_vertices; //is this list needed?
166  //find the center and "radius" of the element
167  double xc = 0;
168  double yc = 0;
169  double zc = 0;
170  double radius = 0;
171 
172  //generate kratos elements (conditions are not touched)
173  int id = 0;
174  // std::vector<std::vector<int> > EmptyNeighList;
175  // mrRemesh.NeighbourList.swap(EmptyNeighList);
176  // mrRemesh.NeighbourList.clear(); //destroy all elements
177 
178  for (int el = 0; el < OutNumberOfElements; ++el)
179  {
180  if (mrRemesh.PreservedElements[el])
181  {
182  Geometry<NodeType> vertices;
183  std::vector<int> neighbours(nds);
184 
185  for (unsigned int i = 0; i < nds; ++i)
186  {
187  //note that OutElementList, starts from node 1, not from node 0, it can be directly assigned to mrRemesh.NodalPreIds.
188  vertices.push_back(*(nodes_begin + OutElementList[el * nds + i] - 1).base());
189  //vertices.push_back(mrModelPart.pGetNode(OutElementList[el*3+pn]));
190 
191  if (vertices.back().Is(TO_ERASE))
192  std::cout << " WARNING:: mesh vertex RELEASED " << vertices.back().Id() << std::endl;
193  }
194 
195  id += 1;
196 
197  mrRemesh.PreservedElements[el] = id;
198 
199  //*******************************************************************
200  //1) Store Preserved elements in an array of vertices (Geometry<NodeType > vertices;)
201 
202  if (vertices.size() == 3)
203  {
204  DataTransferUtilities.CalculateCenterAndSearchRadius(vertices[0].X(), vertices[0].Y(),
205  vertices[1].X(), vertices[1].Y(),
206  vertices[2].X(), vertices[2].Y(),
207  xc, yc, radius);
208  }
209  else if (vertices.size() == 4)
210  {
211  DataTransferUtilities.CalculateCenterAndSearchRadius(vertices[0].X(), vertices[0].Y(), vertices[0].Z(),
212  vertices[1].X(), vertices[1].Y(), vertices[1].Z(),
213  vertices[2].X(), vertices[2].Y(), vertices[2].Z(),
214  vertices[3].X(), vertices[3].Y(), vertices[3].Z(),
215  xc, yc, zc, radius);
216  }
217 
218  //std::cout<<" XC ["<<id<<"]: ("<<xc<<" "<<yc<<") "<<std::endl;
219  //std::cout<<" vertices "<<vertices[0].X()<<" "<<vertices[2].X()<<std::endl;
220  //*******************************************************************
221 
222  NodeType::Pointer p_center = Kratos::make_intrusive<NodeType>(id, xc, yc, zc);
223 
224  //*******************************************************************
225  //2) Create list_of_centers
226 
227  list_of_element_centers.push_back(p_center);
228  list_of_element_vertices.push_back(vertices);
229 
230  //*******************************************************************
231 
232  // std::cout<<" list of centers "<<list_of_element_centers.back()->X()<<" "<<list_of_element_centers.back()->Y()<<std::endl;
233  // std::cout<<" list of vertices ";
234  // std::cout.flush();
235  // std::cout<<" vertices "<<list_of_element_vertices.back()[0].X()<<" "<<list_of_element_vertices.back()[2].X()<<std::endl;
236  // std::cout.flush();
237  }
238  else
239  {
240  mrRemesh.PreservedElements[el] = -1;
241  }
242 
243  // if(mrRemesh.PreservedElements[el])
244  // {
245  // std::cout<<" Neighbours ["<<id-1<<"] :";
246  // for(int pn=0; pn<3; ++pn)
247  // {
248  // std::cout<<" neighborlist ("<<pn<<") "<<mrRemesh.NeighbourList[id-1][pn]<<std::endl;
249  // }
250  // }
251 
252  //std::cout<<" NodalPreIds ["<<el<<"] :"<<NodalPreIds[el]<<std::endl;
253  }
254 
255  //*******************************************************************
256  //5) Laplacian Smoothing
257  // check geometrical smoothing is better for solid cutting problems commented July 2018 for fluid testing
258  //if( mrRemesh.Options.Is(MesherUtilities::MESH_SMOOTHING) && mrRemesh.Info->GeometricalSmoothingRequired ){
259  if (mrRemesh.Options.Is(MesherUtilities::MESH_SMOOTHING))
260  {
261  //Check Mesh Info to perform smoothing:
262  if (mrRemesh.Options.Is(MesherUtilities::REFINE))
263  mrRemesh.Info->CheckGeometricalSmoothing();
264  else
265  mrRemesh.Info->GeometricalSmoothingRequired = true;
266 
267  int &OutNumberOfPoints = mrRemesh.OutMesh.GetNumberOfPoints();
268  LaplacianSmoothing MeshGeometricSmoothing(mrModelPart);
269  MeshGeometricSmoothing.SetEchoLevel(mEchoLevel);
270  MeshGeometricSmoothing.ApplyMeshSmoothing(mrModelPart, mrRemesh.PreservedElements, OutElementList, OutNumberOfPoints);
271  }
272  //*******************************************************************
273 
274  //*******************************************************************
275  //6) Pass rReferenceElement and transfer variables
276  DataTransferUtilities.TransferData(mrModelPart, rReferenceElement, list_of_element_centers, list_of_element_vertices, MeshDataTransferUtilities::ELEMENT_TO_ELEMENT);
277  //*******************************************************************
278 
279  //*******************************************************************w
280  //std::cout<<" Number of Nodes "<<mrModelPart.Nodes().size()<<" Number Of Ids "<<mrRemesh.NodalPreIds.size()<<std::endl;
281 
282  //7) Restore global ID's
283  for (ModelPart::NodesContainerType::iterator in = mrModelPart.NodesBegin(); in != mrModelPart.NodesEnd(); ++in)
284  {
285  //std::cout<<" node (local:"<<in->Id()<<", global:"<<mrRemesh.NodalPreIds[ in->Id() ]<<")"<<std::endl;
286  in->SetId(mrRemesh.NodalPreIds[in->Id()]);
287  }
288  //*******************************************************************
289 
290  //*******************************************************************
291 
292  //8) Filling the neighbour list
293  SetElementNeighbours(mrModelPart);
294 
295  //*******************************************************************
296 
297  if (mEchoLevel > 0)
298  std::cout << " GENERATE NEW ELEMENTS ]; " << std::endl;
299 
300  KRATOS_CATCH(" ")
301  }
302 
306 
310 
314 
316  std::string Info() const override
317  {
318  return "GenerateNewElementsMesherProcess";
319  }
320 
322  void PrintInfo(std::ostream &rOStream) const override
323  {
324  rOStream << "GenerateNewElementsMesherProcess";
325  }
326 
328  void PrintData(std::ostream &rOStream) const override
329  {
330  }
331 
335 
337 
338  protected:
341 
345 
349 
353 
357 
361 
365 
367 
368  private:
371 
375  ModelPart &mrModelPart;
376 
378 
379  int mEchoLevel;
380 
384 
385  //**************************************************************************
386  //**************************************************************************
387 
388  void SetElementNeighbours(ModelPart &rModelPart)
389  {
390  KRATOS_TRY
391 
392  if (mEchoLevel > 0)
393  {
394  std::cout << " [ SET ELEMENT NEIGHBOURS : " << std::endl;
395  std::cout << " Initial Faces : " << rModelPart.Conditions().size() << std::endl;
396  }
397 
398  ModelPart::ElementsContainerType::iterator element_begin = rModelPart.ElementsBegin();
399 
400  const unsigned int nds = element_begin->GetGeometry().size();
401 
402  int *OutElementNeighbourList = mrRemesh.OutMesh.GetElementNeighbourList();
403 
404  int facecounter = 0;
405  int Id = 0;
406  for (auto i_elem(rModelPart.ElementsBegin()); i_elem != rModelPart.ElementsEnd(); ++i_elem)
407  {
408 
409  for (unsigned int i = 0; i < mrRemesh.PreservedElements.size(); ++i)
410  {
411  if (mrRemesh.PreservedElements[Id] == -1)
412  Id++;
413  else
414  break;
415  }
416 
417  //Id = i_elem->Id()-1;
418 
419  unsigned int number_of_faces = i_elem->GetGeometry().FacesNumber(); //defined for triangles and tetrahedra
420 
421  ElementWeakPtrVectorType &nElements = i_elem->GetValue(NEIGHBOUR_ELEMENTS);
422  nElements.resize(number_of_faces);
423 
424  int index = 0;
425  for (unsigned int iface = 0; iface < number_of_faces; ++iface)
426  {
427 
428  index = OutElementNeighbourList[Id * nds + iface];
429 
430  if (index > 0)
431  {
432  //std::cout<<" Element "<<Id<<" size "<<mrRemesh.PreservedElements.size()<<std::endl;
433  //std::cout<<" Index pre "<<index<<" size "<<mrRemesh.PreservedElements.size()<<std::endl;
434  index = mrRemesh.PreservedElements[index - 1];
435  //std::cout<<" Index post "<<index<<std::endl;
436  }
437 
438  if (index > 0)
439  {
440  nElements(iface) = *(element_begin + index - 1).base();
441  }
442  else
443  {
444  nElements(iface) = *i_elem.base();
445  facecounter++;
446  }
447  }
448 
449  Id++;
450  }
451 
452  if (mEchoLevel > 0)
453  {
454  std::cout << " Final Faces : " << facecounter << std::endl;
455  std::cout << " SET ELEMENT NEIGHBORS ]; " << std::endl;
456  }
457 
458  KRATOS_CATCH("")
459  }
460 
464 
468 
472 
476 
479 
481  //GenerateNewElementsMesherProcess(GenerateNewElementsMesherProcess const& rOther);
482 
484 
485  }; // Class GenerateNewElementsMesherProcess
486 
488 
491 
495 
497  inline std::istream &operator>>(std::istream &rIStream,
499 
501  inline std::ostream &operator<<(std::ostream &rOStream,
503  {
504  rThis.PrintInfo(rOStream);
505  rOStream << std::endl;
506  rThis.PrintData(rOStream);
507 
508  return rOStream;
509  }
511 
512 } // namespace Kratos.
513 
514 #endif // KRATOS_GENERATE_NEW_ELEMENTS_MESHER_PROCESS_H_INCLUDED defined
Base class for all Conditions.
Definition: condition.h:59
Base class for all Elements.
Definition: element.h:60
bool Is(Flags const &rOther) const
Definition: flags.h:274
Short class definition.
Definition: generate_new_elements_mesher_process.hpp:57
KRATOS_CLASS_POINTER_DEFINITION(GenerateNewElementsMesherProcess)
Pointer definition of GenerateNewElementsMesherProcess.
GlobalPointersVector< Element > ElementWeakPtrVectorType
Definition: generate_new_elements_mesher_process.hpp:71
void operator()()
Definition: generate_new_elements_mesher_process.hpp:96
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: generate_new_elements_mesher_process.hpp:322
GenerateNewElementsMesherProcess(ModelPart &rModelPart, MesherUtilities::MeshingParameters &rRemeshingParameters, int EchoLevel)
Default constructor.
Definition: generate_new_elements_mesher_process.hpp:78
ModelPart::ElementsContainerType ElementsContainerType
Definition: generate_new_elements_mesher_process.hpp:70
ModelPart::ConditionType ConditionType
Definition: generate_new_elements_mesher_process.hpp:66
std::string Info() const override
Turn back information as a string.
Definition: generate_new_elements_mesher_process.hpp:316
void Execute() override
Execute method is used to execute the Process algorithms.
Definition: generate_new_elements_mesher_process.hpp:105
ConditionType::GeometryType GeometryType
Definition: generate_new_elements_mesher_process.hpp:68
virtual ~GenerateNewElementsMesherProcess()
Destructor.
Definition: generate_new_elements_mesher_process.hpp:88
ModelPart::NodeType NodeType
Definition: generate_new_elements_mesher_process.hpp:65
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: generate_new_elements_mesher_process.hpp:328
ModelPart::PropertiesType PropertiesType
Definition: generate_new_elements_mesher_process.hpp:67
GeometryType & GetGeometry()
Returns the reference of the geometry.
Definition: geometrical_object.h:158
Geometry base class.
Definition: geometry.h:71
void push_back(PointPointerType x)
Definition: geometry.h:548
SizeType size() const
Definition: geometry.h:518
PointReferenceType back()
Definition: geometry.h:507
void resize(size_type new_dim) const
Definition: global_pointers_vector.h:366
Short class definition.
Definition: laplacian_smoothing.hpp:71
virtual void SetEchoLevel(int Level)
Definition: laplacian_smoothing.hpp:755
void ApplyMeshSmoothing(ModelPart &rModelPart, std::vector< int > &PreservedElements, const int *pElementsList, const int &NumberOfPoints)
Definition: laplacian_smoothing.hpp:115
Short class definition.
Definition: mesh_data_transfer_utilities.hpp:46
void TransferData(ModelPart &rModelPart, const Element &rReferenceElement, PointPointerVector &list_of_new_centers, std::vector< Geometry< Node > > &list_of_new_vertices, Flags Options)
Definition: mesh_data_transfer_utilities.cpp:30
void CalculateCenterAndSearchRadius(const std::vector< std::vector< double > > &rPointCoordinates, std::vector< double > &rCenter, double &rRadius)
Definition: mesh_data_transfer_utilities.hpp:334
The base class for processes passed to the solution scheme.
Definition: mesher_process.hpp:37
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
ElementIterator ElementsBegin(IndexType ThisIndex=0)
Definition: model_part.h:1169
MeshType::ElementsContainerType ElementsContainerType
Element container. A vector set of Elements with their Id's as key.
Definition: model_part.h:168
std::string & Name()
Definition: model_part.h:1811
NodeIterator NodesBegin(IndexType ThisIndex=0)
Definition: model_part.h:487
ConditionsContainerType & Conditions(IndexType ThisIndex=0)
Definition: model_part.h:1381
ElementIterator ElementsEnd(IndexType ThisIndex=0)
Definition: model_part.h:1179
NodeIterator NodesEnd(IndexType ThisIndex=0)
Definition: model_part.h:497
This class defines the node.
Definition: node.h:65
Properties encapsulates data shared by different Elements or Conditions. It can store any type of dat...
Definition: properties.h:69
Refine Mesh Elements Process 2D and 3D.
Definition: select_elements_mesher_process.hpp:47
void Execute() override
Execute method is used to execute the Process algorithms.
Definition: select_elements_mesher_process.hpp:95
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
static int EchoLevel
Definition: co_sim_EMPIRE_API.h:42
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
float radius
Definition: mesh_to_mdpa_converter.py:18
el
Definition: read_stl.py:25
float xc
Definition: rotating_cone.py:77
float yc
Definition: rotating_cone.py:78
integer i
Definition: TensorModule.f:17
int * GetElementNeighbourList()
Definition: mesher_utilities.hpp:180
int * GetElementList()
Definition: mesher_utilities.hpp:178
int & GetNumberOfPoints()
Definition: mesher_utilities.hpp:182
int & GetNumberOfElements()
Definition: mesher_utilities.hpp:183
Definition: mesher_utilities.hpp:631
MeshingInfoParameters::Pointer Info
Definition: mesher_utilities.hpp:681
Flags Options
Definition: mesher_utilities.hpp:645
std::vector< int > PreservedElements
Definition: mesher_utilities.hpp:669
bool MeshElementsSelectedFlag
Definition: mesher_utilities.hpp:662
MeshContainer OutMesh
Definition: mesher_utilities.hpp:675
std::string SubModelPartName
Definition: mesher_utilities.hpp:642
std::vector< int > NodalPreIds
Definition: mesher_utilities.hpp:668
Element const & GetReferenceElement()
Definition: mesher_utilities.hpp:843