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.
gid_mesh_container.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 // Janosch Stascheit
12 // Pooyan Dadvand
13 //
14 
15 #if !defined(KRATOS_GID_MESH_CONTAINER_H_INCLUDED)
16 #define KRATOS_GID_MESH_CONTAINER_H_INCLUDED
17 // System includes
18 #include <string>
19 #include <iostream>
20 #include <fstream>
21 #include <sstream>
22 #include <cstddef>
23 // External includes
24 #include "gidpost/source/gidpost.h"
25 // Project includes
26 #include "includes/define.h"
29 
30 
31 namespace Kratos
32 {
46 {
47 public:
50  GiD_ElementType elementType, const char* mesh_title )
51  :mGeometryType (geometryType), mGidElementType (elementType), mMeshTitle (mesh_title) {}
53  {
55  if ( pElemIt->GetGeometry().GetGeometryType() == mGeometryType )
56  {
57  mMeshElements.push_back ( * (pElemIt.base() ) );
58  Geometry<Node >&geom = pElemIt->GetGeometry();
59  for ( Element::GeometryType::iterator it = geom.begin(); it != geom.end(); it++)
60  {
61  mMeshNodes.push_back ( * (it.base() ) );
62  }
63  return true;
64  }
65  else
66  return false;
67  KRATOS_CATCH ("")
68  }
70  {
72  if ( pCondIt->GetGeometry().GetGeometryType() == mGeometryType )
73  {
74  mMeshConditions.push_back ( * (pCondIt.base() ) );
75  Geometry<Node >&geom = pCondIt->GetGeometry();
76  for ( Condition::GeometryType::iterator it = geom.begin(); it != geom.end(); it++)
77  {
78  mMeshNodes.push_back ( * (it.base() ) );
79  }
80  return true;
81  }
82  else
83  return false;
84  KRATOS_CATCH ("")
85  }
87  {
88  if ( mMeshElements.size() != 0 )
89  {
90  mMeshNodes.Unique();
91  }
92  if ( mMeshConditions.size() != 0 )
93  {
94  mMeshNodes.Unique();
95  }
96  }
97  void WriteMesh (GiD_FILE MeshFile, bool deformed)
98  {
100 
101  bool nodes_written = false;
102  if ( mMeshElements.size() != 0 )
103  {
104  //compute number of layers
105  int max_id = 0;
106  for ( ModelPart::ElementsContainerType::iterator it = mMeshElements.begin();
107  it != mMeshElements.end(); ++it )
108  {
109  KRATOS_DEBUG_ERROR_IF_NOT((it)->HasProperties()) << "Element #"
110  << (it)->Id() << " does not have Properties and hence cannot "
111  << "be used with the GiD-Output" << std::endl;
112 
113  int prop_id = (it)->GetProperties().Id();
114  if (max_id < prop_id) max_id = prop_id;
115  }
116  if (max_id > 10000)
117  std::cout<< "a property Id > 10000 found. Are u sure you need so many properties?" << std::endl;
118  std::vector<int> elements_per_layer (max_id+1,0);
119  //KRATOS_WATCH(max_id);
120 
121  //fill layer list
122  for ( ModelPart::ElementsContainerType::iterator it = mMeshElements.begin();
123  it != mMeshElements.end(); ++it )
124  {
125  int prop_id = (it)->GetProperties().Id();
126  elements_per_layer[prop_id] += 1;
127  }
128  //std::cout << "start printing elements" <<std::endl;
129  for (unsigned int current_layer = 0; current_layer < elements_per_layer.size(); current_layer++)
130  {
131  if (elements_per_layer[current_layer] > 0)
132  {
133  //create an appropriate name
134  std::stringstream current_layer_name (std::stringstream::in | std::stringstream::out);
135  current_layer_name << mMeshTitle << "_" << current_layer ;
136  if ( mMeshElements.begin()->GetGeometry().WorkingSpaceDimension() == 2 )
137  {
138  //std::cout << " -print element 2D mesh: layer ["<<current_layer<<"]-"<<std::endl;
139  GiD_fBeginMesh ( MeshFile, (char *) (current_layer_name.str() ).c_str(), GiD_2D, mGidElementType,mMeshElements.begin()->GetGeometry().size() );
140  }
141  else if ( mMeshElements.begin()->GetGeometry().WorkingSpaceDimension() == 3 )
142  {
143  //std::cout << " -print element 3D mesh: layer ["<<current_layer<<"]-"<<std::endl;
144  GiD_fBeginMesh ( MeshFile, (char *) (current_layer_name.str() ).c_str(), GiD_3D, mGidElementType,mMeshElements.begin()->GetGeometry().size() );
145  }
146  else
147  KRATOS_THROW_ERROR (std::logic_error,"check working space dimension of model","");
148  //printing nodes
149  if(nodes_written == false)
150  {
151  GiD_fBeginCoordinates(MeshFile);
152  for ( ModelPart::NodesContainerType::iterator it = mMeshNodes.begin();
153  it != mMeshNodes.end(); ++it )
154  {
155  if ( deformed )
156  GiD_fWriteCoordinates ( MeshFile, (it)->Id(), (it)->X(),
157  (it)->Y(), (it)->Z() );
158  else
159  GiD_fWriteCoordinates ( MeshFile, (it)->Id(), (it)->X0(),
160  (it)->Y0(), (it)->Z0() );
161  }
162  GiD_fEndCoordinates(MeshFile);
163 
164  nodes_written = true;
165  }
166  //printing elements
167  GiD_fBeginElements(MeshFile);
168  int* nodes_id = new int[mMeshElements.begin()->GetGeometry().size() + 1];
169  for ( ModelPart::ElementsContainerType::iterator it = mMeshElements.begin();
170  it != mMeshElements.end(); ++it )
171  {
172  for ( unsigned int i=0; i< (it)->GetGeometry().size(); i++ )
173  nodes_id[i] = (it)->GetGeometry() [i].Id();
174 
176  {
177  nodes_id[0] = (it)->GetGeometry() [0].Id();
178  nodes_id[1] = (it)->GetGeometry() [1].Id();
179  nodes_id[2] = (it)->GetGeometry() [2].Id();
180  }
181  nodes_id[ (it)->GetGeometry().size()]= (it)->GetProperties().Id()+1;
182 
183  if (it->IsActive())
184  if ((it)->GetProperties().Id()==current_layer)
185  GiD_fWriteElementMat ( MeshFile, (it)->Id(), nodes_id);
186 
187  }
188  delete [] nodes_id;
189  GiD_fEndElements(MeshFile);
190  GiD_fEndMesh(MeshFile);
191  }
192  }
193  //std::cout << "end printing elements" <<std::endl;
194  }
195  if ( mMeshConditions.size() != 0 )
196  {
197  //compute number of layers
198  int max_id = 0;
199  for ( ModelPart::ConditionsContainerType::iterator it = mMeshConditions.begin();
200  it != mMeshConditions.end(); ++it )
201  {
202  int prop_id = (it)->GetProperties().Id();
203  if (max_id < prop_id) max_id = prop_id;
204  }
205  if (max_id > 10000)
206  std::cout<< "a property Id > 10000 found. Are u sure you need so many properties?" << std::endl;
207  std::vector<int> conditions_per_layer (max_id+1,0);
208  //fill layer list
209  for ( ModelPart::ConditionsContainerType::iterator it = mMeshConditions.begin();
210  it != mMeshConditions.end(); ++it )
211  {
212  KRATOS_DEBUG_ERROR_IF_NOT((it)->HasProperties()) << "Condition #"
213  << (it)->Id() << " does not have Properties and hence cannot "
214  << "be used with the GiD-Output" << std::endl;
215 
216  int prop_id = (it)->GetProperties().Id();
217  conditions_per_layer[prop_id] += 1;
218  }
219  //std::cout << "start printing conditions" <<std::endl;
220  for (unsigned int current_layer = 0; current_layer < conditions_per_layer.size(); current_layer++)
221  {
222  if (conditions_per_layer[current_layer] > 0)
223  {
224  std::stringstream current_layer_name (std::stringstream::in | std::stringstream::out);
225  current_layer_name << mMeshTitle << "_" << current_layer ;
226 
227  if ( mMeshConditions.begin()->GetGeometry().WorkingSpaceDimension() == 2 )
228  {
229  //std::cout << " -print condition 2D mesh: layer ["<<current_layer<<"]-"<<std::endl;
230  GiD_fBeginMesh ( MeshFile, (char *) (current_layer_name.str() ).c_str(), GiD_2D, mGidElementType,
231  mMeshConditions.begin()->GetGeometry().size() );
232  }
233  else if ( mMeshConditions.begin()->GetGeometry().WorkingSpaceDimension() == 3 )
234  {
235  //std::cout << " -print condition 3D mesh: layer ["<<current_layer<<"]-"<<std::endl;
236  GiD_fBeginMesh ( MeshFile, (char *) (current_layer_name.str() ).c_str(), GiD_3D, mGidElementType,
237  mMeshConditions.begin()->GetGeometry().size() );
238  }
239  else
240  KRATOS_THROW_ERROR (std::logic_error,"check working space dimension of model","");
241  //printing nodes
242  if(nodes_written == false)
243  {
244  GiD_fBeginCoordinates(MeshFile);
245  for ( ModelPart::NodesContainerType::iterator it = mMeshNodes.begin();
246  it != mMeshNodes.end(); ++it )
247  {
248  if ( deformed )
249  GiD_fWriteCoordinates ( MeshFile, (it)->Id(), (it)->X(),
250  (it)->Y(), (it)->Z() );
251  else
252  GiD_fWriteCoordinates ( MeshFile, (it)->Id(), (it)->X0(),
253  (it)->Y0(), (it)->Z0() );
254  }
255  GiD_fEndCoordinates(MeshFile);
256  nodes_written = true;
257  }
258  else //printing these headers avoids the exception raised by GidPost. There's an Assert stopping the program if we don't put this here.
259  {
260  GiD_fBeginCoordinates(MeshFile);
261  GiD_fEndCoordinates(MeshFile);
262  }
263  //printing elements
264  GiD_fBeginElements(MeshFile);
265  int* nodes_id = new int[mMeshConditions.begin()->GetGeometry().size() + 1];
266  for ( ModelPart::ConditionsContainerType::iterator it = mMeshConditions.begin( );
267  it != mMeshConditions.end(); ++it )
268  {
269  for ( unsigned int i=0; i< (it)->GetGeometry().size(); i++ )
270  nodes_id[i] = (it)->GetGeometry() [i].Id();
271  //workaround: reordering node ids for Hexahedra20 elements
273  {
274  nodes_id[12] = (it)->GetGeometry() [16].Id();
275  nodes_id[13] = (it)->GetGeometry() [17].Id();
276  nodes_id[14] = (it)->GetGeometry() [18].Id();
277  nodes_id[15] = (it)->GetGeometry() [19].Id();
278  nodes_id[16] = (it)->GetGeometry() [12].Id();
279  nodes_id[17] = (it)->GetGeometry() [13].Id();
280  nodes_id[18] = (it)->GetGeometry() [14].Id();
281  nodes_id[19] = (it)->GetGeometry() [15].Id();
282  }
283  nodes_id[ (it)->GetGeometry().size()]= (it)->GetProperties().Id()+1;
284 
285  if (it->IsActive())
286  if ((it)->GetProperties().Id()==current_layer)
287  GiD_fWriteElementMat ( MeshFile, (it)->Id(), nodes_id);
288  }
289  delete [] nodes_id;
290  GiD_fEndElements(MeshFile);
291  GiD_fEndMesh(MeshFile);
292  }
293  }
294  //std::cout << "end printing conditions" <<std::endl;
295  }
296  KRATOS_CATCH ("")
297  }
298  void Reset()
299  {
300  mMeshNodes.clear();
301  mMeshElements.clear();
302  mMeshConditions.clear();
303  }
305  {
306  return mMeshNodes;
307  }
308 protected:
311  GiD_ElementType mGidElementType;
315  const char* mMeshTitle;
316 };//class GidMeshContainer
317 }// namespace Kratos.
318 #endif // KRATOS_GID_MESH_CONTAINER_H_INCLUDED defined
KratosGeometryType
Definition: geometry_data.h:110
IntegrationMethod
Definition: geometry_data.h:76
KratosGeometryFamily
Definition: geometry_data.h:91
Geometry base class.
Definition: geometry.h:71
PointsArrayType::iterator iterator
PointsArrayType typedefs.
Definition: geometry.h:213
iterator begin()
Definition: geometry.h:465
iterator end()
Definition: geometry.h:473
Definition: gid_mesh_container.h:46
void WriteMesh(GiD_FILE MeshFile, bool deformed)
Definition: gid_mesh_container.h:97
ModelPart::NodesContainerType GetMeshNodes()
Definition: gid_mesh_container.h:304
GeometryData::KratosGeometryType mGeometryType
member variables
Definition: gid_mesh_container.h:310
ModelPart::ConditionsContainerType mMeshConditions
Definition: gid_mesh_container.h:314
GiD_ElementType mGidElementType
Definition: gid_mesh_container.h:311
ModelPart::ElementsContainerType mMeshElements
Definition: gid_mesh_container.h:313
bool AddElement(const ModelPart::ElementConstantIterator pElemIt)
Definition: gid_mesh_container.h:52
const char * mMeshTitle
Definition: gid_mesh_container.h:315
ModelPart::NodesContainerType mMeshNodes
Definition: gid_mesh_container.h:312
void Reset()
Definition: gid_mesh_container.h:298
void FinalizeMeshCreation()
Definition: gid_mesh_container.h:86
bool AddCondition(const ModelPart::ConditionConstantIterator pCondIt)
Definition: gid_mesh_container.h:69
GidMeshContainer(GeometryData::KratosGeometryType geometryType, GiD_ElementType elementType, const char *mesh_title)
Constructor.
Definition: gid_mesh_container.h:49
MeshType::ConditionsContainerType ConditionsContainerType
Condintions container. A vector set of Conditions with their Id's as key.
Definition: model_part.h:183
MeshType::ElementsContainerType ElementsContainerType
Element container. A vector set of Elements with their Id's as key.
Definition: model_part.h:168
MeshType::ConditionConstantIterator ConditionConstantIterator
Definition: model_part.h:195
MeshType::NodesContainerType NodesContainerType
Nodes container. Which is a vector set of nodes with their Id's as key.
Definition: model_part.h:128
MeshType::ElementConstantIterator ElementConstantIterator
Definition: model_part.h:180
#define KRATOS_THROW_ERROR(ExceptionType, ErrorMessage, MoreInfo)
Definition: define.h:77
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_DEBUG_ERROR_IF_NOT(conditional)
Definition: exception.h:172
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
GeometryData::IntegrationMethod IntegrationMethodType
Definition: gid_gauss_point_container.h:44
ModelPart::ConditionsContainerType ConditionsArrayType
Definition: gid_gauss_point_container.h:43
ModelPart::NodesContainerType NodesArrayType
Definition: gid_gauss_point_container.h:42
ModelPart::ElementsContainerType ElementsArrayType
Definition: gid_gauss_point_container.h:41
GeometryData::KratosGeometryFamily KratosGeometryFamily
Definition: gid_gauss_point_container.h:45
out
Definition: isotropic_damage_automatic_differentiation.py:200
integer i
Definition: TensorModule.f:17