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.
tetgen_volume_mesher.h
Go to the documentation of this file.
1 //
2 // Project Name: Kratos
3 // Last Modified by: $Author: pooyan $
4 // Date: $Date: 2006-11-27 16:07:33 $
5 // Revision: $Revision: 1.1.1.1 $
6 //
7 //
8 
9 
10 #if !defined(KRATOS_TETGEN_VOLUME_MESHER_H_INCLUDED )
11 #define KRATOS_TETGEN_VOLUME_MESHER_H_INCLUDED
12 
13 
14 
15 // System includes
16 #include <string>
17 #include <iostream>
18 
19 
20 // External includes
21 
22 #include "tetgen.h" // Defined tetgenio, tetrahedralize().
23 
24 // Project includes
25 #include "includes/define.h"
26 #include "utilities/geometry_utilities.h"
27 #include "includes/model_part.h"
30 
31 
32 namespace Kratos
33 {
36 
39 
43 
47 
51 
55 
57 
62 {
63 public:
66 
69 
73 
75 
77  :mrModelPart(rmodel_part)
78  {
79  }
80 
82 
84  = default;
85 
94  void AddHole(double x, double y, double z)
95  {
96  array_1d<double, 3 > hole_coordinates;
97  hole_coordinates[0] = x;
98  hole_coordinates[1] = y;
99  hole_coordinates[2] = z;
100  mholes.push_back(hole_coordinates);
101  }
102 
112  void GenerateMesh(std::string settings, std::string ElementName)
113  {
114  tetgenio in, out;
115 
116  if(mrModelPart.Conditions().size() == 0)
117  KRATOS_THROW_ERROR(std::logic_error,"model part does not have faces","")
118  if(mrModelPart.Elements().size() != 0)
119  KRATOS_THROW_ERROR(std::logic_error,"model part already has volume elements","")
120 
121  //reorder node Ids consecutively
122  ModelPart::NodesContainerType::iterator nodes_begin = mrModelPart.NodesBegin();
123  for (unsigned int i = 0; i < mrModelPart.Nodes().size(); i++)
124  {
125  (nodes_begin + i)->SetId(i + 1);
126  }
127 
128  // All indices start from 1.
129  in.initialize();
130  in.firstnumber = 1;
131 
132  in.numberofpoints = mrModelPart.Nodes().size();
133  in.pointlist = new REAL[in.numberofpoints * 3];
134 
135  //give the coordinates to the mesher
136 
137  for (unsigned int i = 0; i < mrModelPart.Nodes().size(); i++)
138  {
139  int base = i * 3;
140  in.pointlist[base] = (nodes_begin + i)->X();
141  in.pointlist[base + 1] = (nodes_begin + i)->Y();
142  in.pointlist[base + 2] = (nodes_begin + i)->Z();
143  }
144 
145  //prepare the list of faces
146  in.numberoffacets = mrModelPart.Conditions().size();
147  in.facetlist = new tetgenio::facet[in.numberoffacets];
148  in.facetmarkerlist = new int[in.numberoffacets];
149 
150  //give the surface connectivity to the mesher
151  ModelPart::ConditionsContainerType::iterator condition_begin = mrModelPart.ConditionsBegin();
152  for (unsigned int i = 0; i < mrModelPart.Conditions().size(); i++)
153  {
154  tetgenio::facet* f = &in.facetlist[i];
155  f->numberofpolygons = 1;
156  f->polygonlist = new tetgenio::polygon[f->numberofpolygons];
157  f->numberofholes = 0;
158  f->holelist = nullptr;
159  tetgenio::polygon* p = &f->polygonlist[0];
160  p->numberofvertices = 3;
161  p->vertexlist = new int[p->numberofvertices];
162  p->vertexlist[0] = (condition_begin + i)->GetGeometry()[0].Id();
163  p->vertexlist[1] = (condition_begin + i)->GetGeometry()[1].Id();
164  p->vertexlist[2] = (condition_begin + i)->GetGeometry()[2].Id();
165 
166  in.facetmarkerlist[i] = static_cast<int>( (condition_begin + i)->GetValue(FLAG_VARIABLE) );
167  }
168 
169  //give the hole list to the mesher
170  in.numberofholes = mholes.size();
171  in.holelist = new double[in.numberofholes*3];
172  for (unsigned int i = 0; i < mholes.size(); i++)
173  {
174  int base = i * 3;
175  in.holelist[base] = mholes[i][0];
176  in.holelist[base + 1] = mholes[i][1];
177  in.holelist[base + 2] = mholes[i][2];
178  }
179 
180 
181 // char tetgen_options[] = settings.c_str() ; //"pqYY";
182 // //perform meshing with tetgen
183 // tetrahedralize(tetgen_options, &in, &out);
184 
185  auto tmp=new char[settings.size()+1];
186  tmp[settings.size()]=0;
187  memcpy(tmp,settings.c_str(),settings.size());
188 
189  tetrahedralize(tmp, &in, &out);
190 
191  delete [] tmp;
192 
193 
194 
195 
196  //generate new nodes
197  Node ::DofsContainerType& reference_dofs = (mrModelPart.NodesBegin())->GetDofs();
198  for(int i=in.numberofpoints; i< out.numberofpoints; i++)
199  {
200  int base = i * 3;
201  double x = out.pointlist[base];
202  double y = out.pointlist[base+1];
203  double z = out.pointlist[base+2];
204 
205  Node::Pointer p_new_node = Node::Pointer(new Node(i+1, x, y, z));
206 
207  // Giving model part's variables list to the node
208  p_new_node->SetSolutionStepVariablesList(&(mrModelPart.GetNodalSolutionStepVariablesList()));
209  p_new_node->SetBufferSize(mrModelPart.NodesBegin()->GetBufferSize());
210  for (Node ::DofsContainerType::iterator iii = reference_dofs.begin(); iii != reference_dofs.end(); iii++)
211  {
212  Node ::DofType &rDof = **iii;
213  Node ::DofType::Pointer p_new_dof = p_new_node->pAddDof(rDof);
214  (p_new_dof)->FreeDof(); //the variables are left as free for the internal node
215  }
216 
217  mrModelPart.Nodes().push_back(p_new_node);
218  }
219 
220  //generate new Elements
221  Element const& rReferenceElement = KratosComponents<Element>::Get(ElementName);
222  nodes_begin = mrModelPart.NodesBegin();
223  Properties::Pointer properties = mrModelPart.GetMesh().pGetProperties(0);
224 
225  for(int i=0; i!=out.numberoftetrahedra; i++)
226  {
227  int base=i*4;
228 
230  *( (nodes_begin + out.tetrahedronlist[base]-1).base() ),
231  *( (nodes_begin + out.tetrahedronlist[base+1]-1).base() ),
232  *( (nodes_begin + out.tetrahedronlist[base+2]-1).base() ),
233  *( (nodes_begin + out.tetrahedronlist[base+3]-1).base() )
234  );
235 
236  Element::Pointer p_element = rReferenceElement.Create(i+1, geom, properties);
237  (mrModelPart.Elements()).push_back(p_element);
238  }
239 
240  in.deinitialize();
241  out.deinitialize();
242  in.initialize(); //better deinitialize and initialize again
243  out.initialize();
244 
245 
246  }
247 
251 
252 
256 
257 
261 
262 
266 
267 
271 
273 
274  virtual std::string Info() const
275  {
276  std::stringstream buffer;
277  buffer << "TetgenVolumeMesher";
278  return buffer.str();
279  }
280 
282 
283  virtual void PrintInfo(std::ostream& rOStream) const
284  {
285  rOStream << "TetgenVolumeMesher";
286  }
287 
289 
290  virtual void PrintData(std::ostream& rOStream) const
291  {
292  }
293 
294 
298 
299 
301 
302 protected:
305 
306 
310 
311 
315 
316 
320 
321 
325 
326 
330 
331 
335 
336 
338 
339 private:
342 
343 
347  std::vector< array_1d<double, 3 > > mholes;
348  ModelPart& mrModelPart;
349 
350 
354 
355 
359 
360 
364 
365 
369 
370 
374 
376 
377  TetgenVolumeMesher & operator=(TetgenVolumeMesher const& rOther)
378  {
379  return *this;
380  }
381 
383 
384  TetgenVolumeMesher(TetgenVolumeMesher const& rOther):
385  mrModelPart(rOther.mrModelPart)
386  {
387  }
388 
389 
391 
392 }; // Class TetgenVolumeMesher
393 
395 
398 
399 
403 
404 
406 
407 inline std::istream & operator >>(std::istream& rIStream,
408  TetgenVolumeMesher& rThis)
409 {
410  return rIStream;
411 }
412 
414 
415 inline std::ostream & operator <<(std::ostream& rOStream,
416  const TetgenVolumeMesher& rThis)
417 {
418  rThis.PrintInfo(rOStream);
419  rOStream << std::endl;
420  rThis.PrintData(rOStream);
421 
422  return rOStream;
423 }
425 
427 
428 } // namespace Kratos.
429 
430 #endif // KRATOS_TETGEN_VOLUME_MESHER_H_INCLUDED defined
431 
432 
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
static const TComponentType & Get(const std::string &rName)
Retrieves a component with the specified name.
Definition: kratos_components.h:114
PropertiesType::Pointer pGetProperties(IndexType PropertiesId)
Definition: mesh.h:394
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
ConditionIterator ConditionsBegin(IndexType ThisIndex=0)
Definition: model_part.h:1361
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 & GetMesh(IndexType ThisIndex=0)
Definition: model_part.h:1791
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
VariablesList & GetNodalSolutionStepVariablesList()
Definition: model_part.h:549
This class defines the node.
Definition: node.h:65
Dof< double > DofType
Dof type.
Definition: node.h:83
std::vector< std::unique_ptr< Dof< double > >> DofsContainerType
The DoF container type definition.
Definition: node.h:92
This class performs the (constrained) Delaunay meshing of the internal volume given a surface discret...
Definition: tetgen_volume_mesher.h:62
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: tetgen_volume_mesher.h:290
void AddHole(double x, double y, double z)
Definition: tetgen_volume_mesher.h:94
KRATOS_CLASS_POINTER_DEFINITION(TetgenVolumeMesher)
Pointer definition of TetgenVolumeMesher.
void GenerateMesh(std::string settings, std::string ElementName)
Definition: tetgen_volume_mesher.h:112
virtual std::string Info() const
Turn back information as a string.
Definition: tetgen_volume_mesher.h:274
TetgenVolumeMesher(ModelPart &rmodel_part)
Default constructor.
Definition: tetgen_volume_mesher.h:76
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: tetgen_volume_mesher.h:283
virtual ~TetgenVolumeMesher()=default
Destructor.
A four node tetrahedra geometry with linear shape functions.
Definition: tetrahedra_3d_4.h:59
#define KRATOS_THROW_ERROR(ExceptionType, ErrorMessage, MoreInfo)
Definition: define.h:77
#define REAL
Definition: delaunator_utilities.cpp:16
z
Definition: GenerateWind.py:163
Parameters GetValue(Parameters &rParameters, const std::string &rEntry)
Definition: add_kratos_parameters_to_python.cpp:53
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
y
Other simbols definition.
Definition: generate_axisymmetric_navier_stokes_element.py:54
f
Definition: generate_convection_diffusion_explicit_element.py:112
tuple tmp
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:98
out
Definition: isotropic_damage_automatic_differentiation.py:200
p
Definition: sensitivityMatrix.py:52
x
Definition: sensitivityMatrix.py:49
integer i
Definition: TensorModule.f:17