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.
trigen_mesh_suite_refine.h
Go to the documentation of this file.
1 //
2 // Project Name: Kratos
3 // Last Modified by: $Author: rrossi $
4 // Date: $Date: 2008-03-11 10:29:26 $
5 // Revision: $Revision: 1.2 $
6 //
7 //
8 
9 
10 #if !defined(KRATOS_TRIGEN_MODELER_H_INCLUDED )
11 #define KRATOS_TRIGEN_MODELER_H_INCLUDED
12 
13 
14 
15 // System includes
16 #include <string>
17 #include <iostream>
18 #include <cstdlib>
19 
20 // External includes
21 #include "triangle.h"
22 
23 // Project includes
24 #include "includes/define.h"
25 #include "includes/model_part.h"
28 
29 
30 
31 
32 namespace Kratos
33 {
34 extern "C" {
35  void triangulate(char *, struct triangulateio *, struct triangulateio *,struct triangulateio *);
36  //void trifree();
37 }
38 
39 
40 
43 
47 
51 
55 
59 
61 
64 {
65 public:
68 
71 
75 
78  mJ(ZeroMatrix(2,2)), //local jacobian
79  mJinv(ZeroMatrix(2,2)), //inverse jacobian
80  mC(ZeroVector(2)), //dimension = number of nodes
81  mRhs(ZeroVector(2)) {} //dimension = number of nodes
82 
84  virtual ~TriGenModeler() {}
85 
86 
90 
91 
95 
96 
97  //*******************************************************************************************
98  //*******************************************************************************************
99  void RefineMesh(
100  ModelPart& ThisModelPart ,
101  Element const& rReferenceElement,
102  Condition const& rReferenceBoundaryCondition,
103  double my_alpha = 1.4)
104  {
105 
106  KRATOS_TRY
107 
108  struct triangulateio in;
109  struct triangulateio out;
110  struct triangulateio vorout;
111 
112  //sizing array of coordinates
113  in.numberofpoints = ThisModelPart.Nodes().size();
114  in.pointlist = (REAL *) malloc(in.numberofpoints * 2 * sizeof(REAL));
115 
116  //set the array of attributes
117  in.numberofpointattributes = 0;
118  in.pointattributelist = (REAL *) NULL; //in my opinion this is not needed
119 
120  //reserving memory for the element list
121  in.numberoftriangles = ThisModelPart.Elements().size();
122  in.trianglelist = (int *) malloc(in.numberoftriangles * 3 * sizeof(int) ];
123 
124  //reserving area for volume constraints
125  in.trianglearealist = new REAL[in.numberoftetrahedra];
126 
127  in.pointmarkerlist = (int *) NULL;
128  in.regionlist = (REAL *) NULL; //in my opinion not needed
129 
130  out.pointattributelist = (REAL *) NULL; //in my opinion not needed
131  out.trianglelist = (int *) NULL;
132  out.triangleattributelist = (REAL *) NULL; //in my opinion not needed
133  out.neighborlist = (int *) NULL; //in my opinion not needed
134  //*******************************************************************
135 
136  //writing the points coordinates in a vector
137  ModelPart::NodesContainerType::iterator nodes_begin = ThisModelPart.NodesBegin();
138  for(unsigned int i = 0; i<ThisModelPart.Nodes().size(); i++)
139  {
140  int base = i*2;
141  //int base = ((nodes_begin + i)->Id() - 1 ) * 2;
142 
143  //from now on it is consecutive
144  (nodes_begin + i)->Id() = i+1;
145 
146  in.pointlist[base] = (nodes_begin + i)->X();
147  in.pointlist[base+1] = (nodes_begin + i)->Y();
148 
149  //providing to the mesher the nodal attributea
150  ...
151  }
152 
153  //provide the list of triangles and the area desired to the mesher
154  unsigned int i = 0;
155  for(ModelPart::ElementsContainerType iel = ThisModelPart.ElementsBegin(); iel!= ThisModelPart.ElementsEnd(); iel++)
156  {
157  Geometry< Node >& geom = iel->GetGeometry();
158  int base = i*3;
159  in.trianglelist[base] = geom[0]->Id();
160  in.trianglelist[base+1] = geom[1]->Id();
161  in.trianglelist[base+2] = geom[2]->Id();
162 
163  double h = geom[0].FastGetSolutionStepValue(NODAL_H);
164  h += geom[1].FastGetSolutionStepValue(NODAL_H);
165  h += geom[2].FastGetSolutionStepValue(NODAL_H);
166  h *= 0.33333333333333;
167 
168  //a given triangle (equilatera) of edge h has area A = sqrt(3)/4* h*h = 0.43301*h*h
169  double desired_area = 0.43301 * h*h;
170 
171  //set the desired area in the list
172  in.trianglearealist[i] = desired_area;
173 
174  //update counter
175  i = i + 1;
176  }
177 
178  //erase elements and conditions
179  ThisModelPart.Elements().clear();
180  ThisModelPart.Conditions().clear();
181 
182  //perform the meshing - it also finds the neighbours
183  triangulate("rNPBn", &in, &out, &vorout); //version with no elemental neighbours
184 
185  //generate the new node list
186  unsigned int original_size = ThisModelPart.Nodes().size();
187  if( out.numberofpoints > original_size; )
188  {
189  for(unsigned int i = original_size; i < out.numberofpoints; i++)
190  {
191  int id = original_size+1
192  int base = i*3;
193  double& x = out.pointlist[base];
194  double& y = out.pointlist[base+1];
195  double& z = out.pointlist[base+2];
196 
197  //create new node with the x,y,z corresponding and the attribute list given
198  ThisModelPart.AddNode(id,x,y,z, attributelist);
199  }
200  }
201 
202  //properties to be used in the generation of the elements
203  Properties::Pointer properties = ThisModelPart.GetMesh().pGetProperties(1);
204 
205  //generate Kratos triangle
206  int el_number = out.numberoftriangles;
207 
208 
209  //note that the node list can not be changed starting from here
210  ModelPart::NodesContainerType& ModelNodes = ThisModelPart.Nodes();
211 
212  for(int el = 0; el< el_number; el++)
213  {
214  int id = el + 1;
215  int base = el * 3;
216 
217  //generating the geometry
219  temp.push_back( *((ModelNodes).find( out.trianglelist[base]).base() ) );
220  temp.push_back( *((ModelNodes).find( out.trianglelist[base+1]).base() ) );
221  temp.push_back( *((ModelNodes).find( out.trianglelist[base+2]).base() ) );
222 
223  //generating a new element
224  Element::Pointer p_element = rReferenceElement.Create(id, temp, properties);
225  ThisModelPart.Elements().push_back(p_element);
226  }
227  KRATOS_WATCH("trigenmodeler qua")
228 
229  ThisModelPart.Elements().Unique();
230 
231  for(ModelPart::ElementsContainerType::iterator iii = ThisModelPart.ElementsBegin(); iii != ThisModelPart.ElementsEnd(); iii++)
232  {
233  int base = ( iii->Id() - 1 )*3;
234 
235  ModelPart::ElementsContainerType::iterator el_neighb;
236  /*each face is opposite to the corresponding node number so
237  0 ----- 1 2
238  1 ----- 2 0
239  2 ----- 0 1
240  */
241 
243  //
244  //********************************************************************
245  //first face
246  el_neighb = (ThisModelPart.Elements()).find( out.neighborlist[base] );
247  if( el_neighb == elements_end )
248  {
249  //Generate condition
251  temp.reserve(2);
252  temp.push_back(iii->GetGeometry()(1));
253  temp.push_back(iii->GetGeometry()(2));
254 
257  int id = (iii->Id()-1)*3;
258 
259  Condition::Pointer p_cond =
260  Condition::Pointer(new Condition(id, cond, properties) );
261 
262  ThisModelPart.Conditions().push_back(p_cond);
263 
264  }
265 
266  //********************************************************************
267  //second face
268  el_neighb = (ThisModelPart.Elements()).find( out.neighborlist[base+1] );
269  //if( el != ThisModelPart.Elements().end() )
270  if( el_neighb == elements_end )
271  {
272  //Generate condition
274  temp.reserve(2);
275  temp.push_back(iii->GetGeometry()(2));
276  temp.push_back(iii->GetGeometry()(0));
277 
280  int id = (iii->Id()-1)*3+1;
281  //
282  Condition::Pointer p_cond =
283  Condition::Pointer(new Condition(id, cond, properties) );
284 
285  ThisModelPart.Conditions().push_back(p_cond);
286 
287 
288  }
289 
290  //********************************************************************
291  //third face
292  el_neighb = (ThisModelPart.Elements()).find( out.neighborlist[base+2] );
293  if( el_neighb == elements_end )
294  {
295 // Generate condition
297  temp.reserve(2);
298  temp.push_back(iii->GetGeometry()(0));
299  temp.push_back(iii->GetGeometry()(1));
302  int id = (iii->Id()-1)*3+2;
303 
304  Condition::Pointer p_cond =
305  Condition::Pointer(new Condition(id, cond, properties) );
306 
307  ThisModelPart.Conditions().push_back(p_cond);
308 
309 
310  }
311 
312 
313  }
314 
315  KRATOS_WATCH("free surface identified")
316 
317  free( in.pointlist );
318  free( in.numberoftriangles );
319  free( in.trianglelist );
320  free( in.trianglearealist );
321 
322  /* free( in.pointmarkerlist);
323  free( in.pointmarkerlist );
324  free( in.regionlist );*/
325 
326  free( out.pointattributelist );
327  free( out.trianglelist );
328 // free( out.triangleattributelist );
329  free( out.neighborlist );
330  KRATOS_WATCH("everything freed")
331 
332  KRATOS_CATCH("")
333  }
334 
335 
336 
340 
341 
345 
346 
350 
352  virtual std::string Info() const
353  {
354  return "";
355  }
356 
358  virtual void PrintInfo(std::ostream& rOStream) const {}
359 
361  virtual void PrintData(std::ostream& rOStream) const {}
362 
363 
367 
368 
370 
371 protected:
374 
375 
379 
380 
384 
385 
389 
390 
394 
395 
399 
400 
404 
405 
407 
408 private:
411 
412 
416  boost::numeric::ublas::bounded_matrix<double,2,2> mJ; //local jacobian
417  boost::numeric::ublas::bounded_matrix<double,2,2> mJinv; //inverse jacobian
418  array_1d<double,2> mC; //center pos
419  array_1d<double,2> mRhs; //center pos
420 
421 
422 
426  //returns false if it should be removed
427  bool AlphaShape(double alpha_param, Geometry<Node >& pgeom)
428  //bool AlphaShape(double alpha_param, Triangle2D<Node >& pgeom)
429  {
430  KRATOS_TRY
431 
432 
433  double x0 = pgeom[0].X();
434  double x1 = pgeom[1].X();
435  double x2 = pgeom[2].X();
436 
437  double y0 = pgeom[0].Y();
438  double y1 = pgeom[1].Y();
439  double y2 = pgeom[2].Y();
440 
441  mJ(0,0)=2.0*(x1-x0);
442  mJ(0,1)=2.0*(y1-y0);
443  mJ(1,0)=2.0*(x2-x0);
444  mJ(1,1)=2.0*(y2-y0);
445 
446 
447  double detJ = mJ(0,0)*mJ(1,1)-mJ(0,1)*mJ(1,0);
448 
449  mJinv(0,0) = mJ(1,1);
450  mJinv(0,1) = -mJ(0,1);
451  mJinv(1,0) = -mJ(1,0);
452  mJinv(1,1) = mJ(0,0);
453 
454  bounded_matrix<double,2,2> check;
455 
456 
457  if(detJ < 1e-12)
458  {
459  //std::cout << "detJ = " << detJ << std::endl;
461  pgeom[0].GetSolutionStepValue(IS_BOUNDARY) = 1;
462  pgeom[1].GetSolutionStepValue(IS_BOUNDARY) = 1;
463  pgeom[2].GetSolutionStepValue(IS_BOUNDARY) = 1;
464  return false;
465  }
466 
467  else
468  {
469 
470  double x0_2 = x0*x0 + y0*y0;
471  double x1_2 = x1*x1 + y1*y1;
472  double x2_2 = x2*x2 + y2*y2;
473 
474  //finalizing the calculation of the inverted matrix
475  //std::cout<<"MATR INV"<<MatrixInverse(mJ)<<std::endl;
476  mJinv /= detJ;
477  //calculating the RHS
478  mRhs[0] = (x1_2 - x0_2);
479  mRhs[1] = (x2_2 - x0_2);
480 
481  //calculate position of the center
482  noalias(mC) = prod(mJinv,mRhs);
483 
484  double radius = sqrt(pow(mC[0]-x0,2)+pow(mC[1]-y0,2));
485 
486  //calculate average h
487  double h;
488  h = pgeom[0].FastGetSolutionStepValue(NODAL_H);
489  h += pgeom[1].FastGetSolutionStepValue(NODAL_H);
490  h += pgeom[2].FastGetSolutionStepValue(NODAL_H);
491  h *= 0.333333333;
492  if (radius < h*alpha_param)
493  {
494  return true;
495  }
496  else
497  {
498  return false;
499  }
500  }
501 
502 
503  KRATOS_CATCH("")
504  }
505 
509 
510 
514 
515 
519 
520 
524 
526  TriGenModeler& operator=(TriGenModeler const& rOther);
527 
528 
530 
531 }; // Class TriGenModeler
532 
534 
537 
538 
542 
543 
545 inline std::istream& operator >> (std::istream& rIStream,
546  TriGenModeler& rThis);
547 
549 inline std::ostream& operator << (std::ostream& rOStream,
550  const TriGenModeler& rThis)
551 {
552  rThis.PrintInfo(rOStream);
553  rOStream << std::endl;
554  rThis.PrintData(rOStream);
555 
556  return rOStream;
557 }
559 
560 
561 } // namespace Kratos.
562 
563 #endif // KRATOS_TRIGEN_MODELER_H_INCLUDED defined
564 
565 
PeriodicInterfaceProcess & operator=(const PeriodicInterfaceProcess &)=delete
Base class for all Conditions.
Definition: condition.h:59
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
Geometry base class.
Definition: geometry.h:71
IndexType const & Id() const
Id of this Geometry.
Definition: geometry.h:964
Definition: amatrix_interface.h:497
Definition: amatrix_interface.h:530
PropertiesType::Pointer pGetProperties(IndexType PropertiesId)
Definition: mesh.h:394
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
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
NodeIterator NodesBegin(IndexType ThisIndex=0)
Definition: model_part.h:487
ConditionsContainerType & Conditions(IndexType ThisIndex=0)
Definition: model_part.h:1381
ElementsContainerType & Elements(IndexType ThisIndex=0)
Definition: model_part.h:1189
MeshType::NodesContainerType NodesContainerType
Nodes container. Which is a vector set of nodes with their Id's as key.
Definition: model_part.h:128
ElementIterator ElementsEnd(IndexType ThisIndex=0)
Definition: model_part.h:1179
MeshType & GetMesh(IndexType ThisIndex=0)
Definition: model_part.h:1791
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
PointerVector is a container like stl vector but using a vector to store pointers to its data.
Definition: pointer_vector.h:72
Short class definition.
Definition: trigen_mesh_suite_refine.h:64
void RefineMesh(ModelPart &ThisModelPart, Element const &rReferenceElement, Condition const &rReferenceBoundaryCondition, double my_alpha=1.4)
Definition: trigen_mesh_suite_refine.h:99
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: trigen_mesh_suite_refine.h:361
virtual std::string Info() const
Turn back information as a string.
Definition: trigen_mesh_suite_refine.h:352
KRATOS_CLASS_POINTER_DEFINITION(TriGenModeler)
Pointer definition of TriGenModeler.
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: trigen_mesh_suite_refine.h:358
virtual ~TriGenModeler()
Destructor.
Definition: trigen_mesh_suite_refine.h:84
TriGenModeler()
Default constructor.
Definition: trigen_mesh_suite_refine.h:77
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_WATCH(variable)
Definition: define.h:806
#define KRATOS_TRY
Definition: define.h:109
#define REAL
Definition: delaunator_utilities.cpp:16
z
Definition: GenerateWind.py:163
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
AMatrix::MatrixProductExpression< TExpression1Type, TExpression2Type > prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:568
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484
void triangulate(char *, struct triangulateio *, struct triangulateio *, struct triangulateio *)
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
y
Other simbols definition.
Definition: generate_axisymmetric_navier_stokes_element.py:54
h
Definition: generate_droplet_dynamics.py:91
x2
Definition: generate_frictional_mortar_condition.py:122
x1
Definition: generate_frictional_mortar_condition.py:121
out
Definition: isotropic_damage_automatic_differentiation.py:200
float radius
Definition: mesh_to_mdpa_converter.py:18
el
Definition: read_stl.py:25
float temp
Definition: rotating_cone.py:85
x
Definition: sensitivityMatrix.py:49
integer i
Definition: TensorModule.f:17
e
Definition: run_cpp_mpi_tests.py:31