Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
WriteCGNS.cpp
Go to the documentation of this file.
1 #include "WriteCGNS.hpp"
2 #include "moab/CN.hpp"
3 #include "MBTagConventions.hpp"
5 #include "moab/Interface.hpp"
6 #include "moab/Range.hpp"
8 #include "moab/FileOptions.hpp"
9 #include "GmshUtil.hpp"
10 
11 #include <fstream>
12 #include <map>
13 #include <set>
14 
15 #include <iostream>
16 
17 namespace moab
18 {
19 
21 {
22  return new WriteCGNS( iface );
23 }
24 
26  : mbImpl( impl ), fileName( NULL ), IndexFile( 0 ), BaseName( NULL ), IndexBase( 0 ), ZoneName( NULL ),
27  IndexZone( 0 ), IndexSection( 0 ), celldim( 0 ), physdim( 0 ), VrtSize( 0 ), EdgeSize( 0 ), FaceSize( 0 ),
28  CellSize( 0 )
29 {
31  IndexCoord[0] = IndexCoord[1] = IndexCoord[2] = 0;
32  isize[0] = isize[1] = isize[2] = 0;
33 }
34 
36 {
38 }
39 
40 //! writes out a file
41 ErrorCode WriteCGNS::write_file( const char* file_name,
42  const bool overwrite,
43  const FileOptions& /*options*/,
44  const EntityHandle* /*output_list*/,
45  const int /*num_sets*/,
46  const std::vector< std::string >&,
47  const Tag*,
48  int,
49  int )
50 {
51  ErrorCode rval;
52 
53  if( !overwrite )
54  {
55  rval = mWriteIface->check_doesnt_exist( file_name );
56  if( MB_SUCCESS != rval ) return rval;
57  }
58  std::cout << "THE CGNS CONVERSION ONLY WORKS FOR ENTITIES CITED BELOW:";
59  std::cout << "\n -MBVERTEX\n -MBEDGE\n -MBTRI\n -MBQUAD";
60  std::cout << "\n -MBTET\n -MBPYRAMID\n -MBHEX\n";
61 
62  // Get entities to write
63  // Get and count vertex entities
65  if( rval != MB_SUCCESS )
66  {
67  return rval;
68  }
69  // Get and count edge entities
70  rval = get_edge_entities( EdgeSize, Edges );
71  if( rval != MB_SUCCESS )
72  {
73  return rval;
74  }
75  // Get and count face entities
76  rval = get_face_entities( FaceSize, Faces );
77  if( rval != MB_SUCCESS )
78  {
79  return rval;
80  }
81  // Get and count cell entities
82  rval = get_cell_entities( CellSize, Cells );
83  if( rval != MB_SUCCESS )
84  {
85  return rval;
86  }
87  std::cout << "\nThe Number of Vertex is " << VrtSize << ".\n";
88  std::cout << "The Number of Edges is " << EdgeSize << ".\n";
89  std::cout << "The Number of Faces is " << FaceSize << ".\n";
90  std::cout << "The Number of Cells is " << CellSize << ".\n\n";
91 
92  // save filename to member variable so we don't need to pass as an argument
93  // to called functions
94  fileName = file_name;
95  std::cout << fileName << " file is a " << physdim << "-D mesh.\n";
96 
97  // Open file
98  IndexFile = 0;
99 
100  // open the cgns file
101  // filename: (input) Name of the CGNS file, including path name if necessary. There is no
102  // limit on the
103  // length of this character variable.
104  // CG_MODE_WRITE: (input) Mode used for opening the file. The modes currently supported are
105  // CG_MODE_READ,
106  // CG_MODE_WRITE, and CG_MODE_MODIFY.
107  // filePtr: (output) CGNS file index number.
108  if( cg_open( fileName, CG_MODE_WRITE, &IndexFile ) )
109  {
110  std::cout << "Error opening file\n";
111  cg_error_exit();
112  }
113  // Give a base name
114  BaseName = "Cgns Base";
115  if( cg_base_write( IndexFile, BaseName, celldim, physdim, &IndexBase ) )
116  {
117  std::cout << "Error creating CGNS base";
118  }
119  // Give a zone name
120  ZoneName = "Cgns Zone";
121  // isize array contains the total vertex size, cell size, and boundary
122  // vertex size for the zone
123  // Note that for unstructured zones, the index dimension is always 1
124  isize[0] = VrtSize; // isize[0] contains the total vertex size
125  isize[1] = CellSize; // isize[1] contains the total cell size
126  isize[2] = 0; // isize[2] = 0 for unsorted elements
127  // Create zone */
128  // ZoneType_t: Unstructured
129  if( cg_zone_write( IndexFile, IndexBase, ZoneName, isize, Unstructured, &IndexZone ) )
130  {
131  std::cout << "Error creating CGNS zone\n";
132  cg_error_exit();
133  }
134  // Write the vertex coordinates
135  rval = write_coord_cgns( Nodes );
136  if( rval != MB_SUCCESS )
137  {
138  return rval;
139  }
140 
141  // Create a vector to hold the Tags
142  std::vector< moab::Tag > TagHandles;
143  // Get Tags
144  rval = mbImpl->tag_get_tags( TagHandles );
145  if( rval != MB_SUCCESS )
146  {
147  return rval;
148  }
149  // Get the number of Tags in the mesh
150  int NbTags = TagHandles.size();
151  std::cout << "\nThe mesh has " << NbTags << " Tags.\n";
152 
153  // Create a vector of size NbTags
154  // Sets have informations about the entity set
155  std::vector< SetStruct > Sets;
156  Sets.reserve( NbTags );
157  // Fill Sets with all information needed
158  rval = set_tag_values( TagHandles, Edges, Faces, Cells, Sets );
159  if( rval != MB_SUCCESS )
160  {
161  std::cout << "Problem to set tag values\n";
162  return rval;
163  }
164 
165  // Create a matrix to hold connectivity
166  std::vector< std::vector< cgsize_t > > ConnTable;
167  ConnTable.resize( NbTags );
168 
169  std::vector< int > Begin( NbTags, 0 );
170  std::vector< int > End( NbTags, 0 );
171 
172  // Take the connectivity of higher dimension entities
173  cgsize_t BeginSetsIndex = 1;
174  cgsize_t EndSetsIndex;
175  switch( physdim )
176  {
177  case 1:
178  rval = get_conn_table( Edges, Begin, End, TagHandles, Sets, ConnTable );
179  if( rval != MB_SUCCESS )
180  {
181  std::cout << "Problem to fill the connectivity table for 1-D entities\n";
182  return rval;
183  }
184  for( int i = 0; i < NbTags; ++i )
185  {
186  if( Sets[i].IdSet != -1 )
187  {
188  const char* SectionName = Sets[i].TagName.c_str();
189  EndSetsIndex = BeginSetsIndex + Sets[i].NbEdges - 1;
190  // Write the section in CGNS file
191  if( cg_section_write( IndexFile, IndexBase, IndexBase, SectionName, Sets[i].CGNSType,
192  BeginSetsIndex, EndSetsIndex, 0, &ConnTable[i][0], &IndexSection ) )
193  {
194  std::cout << "Issue on writing connectivity - 3-D\n";
195  cg_error_exit();
196  }
197  BeginSetsIndex = EndSetsIndex + 1;
198  }
199  }
200  break;
201  case 2:
202  rval = get_conn_table( Edges, Begin, End, TagHandles, Sets, ConnTable );
203  if( rval != MB_SUCCESS )
204  {
205  std::cout << "Problem to fill the connectivity table for 1-D entities\n";
206  return rval;
207  }
208  rval = get_conn_table( Faces, Begin, End, TagHandles, Sets, ConnTable );
209  if( rval != MB_SUCCESS )
210  {
211  std::cout << "Problem to fill the connectivity table for 2-D entities\n";
212  return rval;
213  }
214  for( int i = 0; i < NbTags; ++i )
215  {
216  if( Sets[i].IdSet != -1 )
217  {
218  const char* SectionName = Sets[i].TagName.c_str();
219  EndSetsIndex = BeginSetsIndex + Sets[i].NbEdges + Sets[i].NbFaces - 1;
220  // Write the section in CGNS file
221  if( cg_section_write( IndexFile, IndexBase, IndexBase, SectionName, Sets[i].CGNSType,
222  BeginSetsIndex, EndSetsIndex, 0, &ConnTable[i][0], &IndexSection ) )
223  {
224  std::cout << "Issue on writing connectivity -- 2-D\n";
225  cg_error_exit();
226  }
227  BeginSetsIndex = EndSetsIndex + 1;
228  }
229  }
230  break;
231  case 3:
232  rval = get_conn_table( Faces, Begin, End, TagHandles, Sets, ConnTable );
233  if( rval != MB_SUCCESS )
234  {
235  std::cout << "Problem to fill the connectivity table for 2-D entities\n";
236  return rval;
237  }
238  rval = get_conn_table( Cells, Begin, End, TagHandles, Sets, ConnTable );
239  if( rval != MB_SUCCESS )
240  {
241  std::cout << "Problem to fill the connectivity table for 3-D entities\n";
242  return rval;
243  }
244  for( int i = 0; i < NbTags; ++i )
245  {
246  if( Sets[i].IdSet != -1 )
247  {
248  const char* SectionName = Sets[i].TagName.c_str();
249  EndSetsIndex = BeginSetsIndex + Sets[i].NbFaces + Sets[i].NbCells - 1;
250  std::cout << "BeginSetsIndex = " << BeginSetsIndex << "\tEndSetsIndex = " << EndSetsIndex << "\n";
251  // Write the section in CGNS file
252  if( cg_section_write( IndexFile, IndexBase, IndexBase, SectionName, Sets[i].CGNSType,
253  BeginSetsIndex, EndSetsIndex, 0, &ConnTable[i][0], &IndexSection ) )
254  {
255  std::cout << "Issue on writing connectivity -- 3-D\n";
256  cg_error_exit();
257  }
258  BeginSetsIndex = EndSetsIndex + 1;
259  }
260  }
261  break;
262  default:
263  std::cout << "Issue on Physical dimension\n";
264  return MB_FAILURE;
265  }
266 
267  // Close the CGNS mesh file
268  if( cg_close( IndexFile ) )
269  {
270  std::cout << "Error closing file\n";
271  cg_error_exit();
272  }
273  // done
274  return MB_SUCCESS;
275 }
276 
277 // Get and count vertex entities
278 ErrorCode WriteCGNS::get_vertex_entities( cgsize_t& VrtSize_, std::vector< moab::EntityHandle >& Nodes_ )
279 {
280  ErrorCode rval;
281  // Get vertex entities
282  // The first input is 0 because one queries the entire mesh.
283  // Retrieves all entities of dimension = 0 in "Nodes"
284  rval = mbImpl->get_entities_by_dimension( 0, 0, Nodes_, false );
285  if( Nodes.size() > 0 )
286  {
287  celldim = 0;
288  physdim = 0;
289  // get the amout of vertex
290  VrtSize_ = Nodes_.size();
291  }
292  else
293  {
294  std::cout << "The mesh has not node points.\n";
295  }
296  // done
297  return rval;
298 }
299 
300 // Get and count edge entities
301 ErrorCode WriteCGNS::get_edge_entities( cgsize_t& EdgeSize_, std::vector< moab::EntityHandle >& Edges_ )
302 {
303  ErrorCode rval;
304  // The first input is 0 because one queries the entire mesh.
305  // Get all entities of dimension = 1 in Edges
306  rval = mbImpl->get_entities_by_dimension( 0, 1, Edges_, false );
307  if( Edges_.size() > 0 )
308  {
309  celldim = 1;
310  physdim = 1;
311  // get the amout of edges
312  EdgeSize_ = Edges_.size();
313  }
314  // done
315  return rval;
316 }
317 
318 // Get and count face entities
319 ErrorCode WriteCGNS::get_face_entities( cgsize_t& FaceSize_, std::vector< moab::EntityHandle >& Faces_ )
320 {
321  ErrorCode rval;
322  // The first input is 0 because one queries the entire mesh.
323  // Get all entities of dimension = 2 in Faces
324  rval = mbImpl->get_entities_by_dimension( 0, 2, Faces_, false );
325  if( Faces_.size() )
326  {
327  celldim = 2;
328  physdim = 2;
329  // get the amout of faces
330  FaceSize_ = Faces_.size();
331  }
332  // done
333  return rval;
334 }
335 
336 // Get and count cell entities
337 ErrorCode WriteCGNS::get_cell_entities( cgsize_t& CellSize_, std::vector< moab::EntityHandle >& Cells_ )
338 {
339  ErrorCode rval;
340  // The first input is 0 because one queries the entire mesh.
341  // Get all entities of dimension = 3 in Cell
342  rval = mbImpl->get_entities_by_dimension( 0, 3, Cells_, false );
343  if( Cells_.size() )
344  {
345  celldim = 3;
346  physdim = 3;
347  // get the amout of volumes
348  CellSize_ = Cells_.size();
349  }
350  // done
351  return rval;
352 }
353 
354 ErrorCode WriteCGNS::write_coord_cgns( std::vector< moab::EntityHandle >& Nodes_ )
355 {
356  ErrorCode rval;
357 
358  const int num_entities = (int)Nodes_.size();
359 
360  // Moab works with one vector for the threee coordinates
361  std::vector< double > Coords( 3 * num_entities );
362  std::vector< double >::iterator c = Coords.begin();
363 
364  // CGNS uses one vector for each coordinate
365  std::vector< double > CoordX;
366  std::vector< double > CoordY;
367  std::vector< double > CoordZ;
368 
369  // Summ the values of all coordinates to be sure if it is not zero
370  double SumX = 0;
371  double SumY = 0;
372  double SumZ = 0;
373 
374  // Get the moab coordinates - Coords is the output
375  rval = mbImpl->get_coords( &Nodes_[0], num_entities, &Coords[0] );
376  if( MB_SUCCESS != rval )
377  {
378  std::cout << "Error getting coordinates from nodes.\n";
379  return rval;
380  }
381 
382  // Reserve the size of nodes
383  CoordX.reserve( Nodes_.size() );
384  CoordY.reserve( Nodes_.size() );
385  CoordZ.reserve( Nodes_.size() );
386 
387  for( std::vector< moab::EntityHandle >::iterator i = Nodes_.begin(); i != Nodes_.end(); ++i )
388  {
389  CoordX.push_back( *c ); // Put the X coordinate in CoordX vector
390  SumX += abs( *c ); // Sum all X coordinates
391  ++c; // Move to Y coordinate
392  CoordY.push_back( *c ); // Put the Y coordinate in CoordY vector
393  SumY += abs( *c ); // Sum all Y coordinates
394  ++c; // Move to Z coordinate
395  CoordZ.push_back( *c ); // Put the Z coordinate in CoordZ vector
396  SumZ += abs( *c ); // Sum all Z coordinates
397  ++c; // Move to X coordinate
398  }
399 
400  // If X coordinate is not empty then write CoordX (user must use SIDS-standard names here)
401  if( SumX != 0 )
402  {
403  if( cg_coord_write( IndexFile, IndexBase, IndexZone, RealDouble, "CoordinateX", &CoordX[0], &IndexCoord[0] ) )
404  {
405  std::cout << " Error writing X coordinates.\n";
406  cg_error_exit();
407  }
408  }
409  // If Y coordinate is not empty then write CoordY (user must use SIDS-standard names here)
410  if( SumY != 0 )
411  {
412  if( cg_coord_write( IndexFile, IndexBase, IndexZone, RealDouble, "CoordinateY", &CoordY[0], &IndexCoord[1] ) )
413  {
414  std::cout << " Error writing Y coordinates.\n";
415  cg_error_exit();
416  }
417  }
418  // If Z coordinate is not empty then write CoordZ (user must use SIDS-standard names here)
419  if( SumZ != 0 )
420  {
421  if( cg_coord_write( IndexFile, IndexBase, IndexZone, RealDouble, "CoordinateZ", &CoordZ[0], &IndexCoord[2] ) )
422  {
423  std::cout << " Error writing Z coordinates.\n";
424  cg_error_exit();
425  }
426  }
427 
428  // Clear vectors
429  Coords.clear();
430  CoordX.clear();
431  CoordY.clear();
432  CoordZ.clear();
433 
434  // done
435  return MB_SUCCESS;
436 }
437 
438 ErrorCode WriteCGNS::set_tag_values( std::vector< Tag >& TagHandles,
439  std::vector< moab::EntityHandle >& Edges_,
440  std::vector< moab::EntityHandle >& Faces_,
441  std::vector< moab::EntityHandle >& Cells_,
442  std::vector< WriteCGNS::SetStruct >& Sets )
443 {
444  ErrorCode rval;
445 
446  // Get the number of Tags in the mesh
447  int NbTags = TagHandles.size();
448 
449  // Loop over all Tags
450  for( int i = 0; i < NbTags; ++i )
451  {
452 
453  // Allocate another position in the vector of SetStruct using a default constructor
454  Sets.push_back( SetStruct() );
455 
456  // Get the Tag name
457  rval = mbImpl->tag_get_name( TagHandles[i], Sets[i].TagName );
458  if( rval != MB_SUCCESS )
459  {
460  std::cout << "Problem to get Tag Name\n";
461  return rval;
462  }
463  std::cout << "Tag name= " << Sets[i].TagName << "\n";
464 
465  // Count all entities by type and put in Sets[i].NbEntities vector
466  rval = get_set_entities( i, TagHandles, Sets );
467  if( rval != MB_SUCCESS )
468  {
469  std::cout << "Problem to get Set entities\n";
470  return rval;
471  }
472 
473  // Get the CGNSTYpe of the Set
474  rval = get_cgns_type( i, Sets );
475  if( rval != MB_SUCCESS )
476  {
477  std::cout << "Problem to get CGNSType\n";
478  return rval;
479  }
480  std::cout << "\tSets[" << i << "].CGNSType= " << Sets[i].CGNSType << "\n";
481 
482  // int Number;
483 
484  // Set a data index for Edges and TagHandles[i]
485  if( Sets[i].CGNSType == BAR_2 || Sets[i].CGNSType == TRI_3 || Sets[i].CGNSType == QUAD_4 ||
486  Sets[i].CGNSType == TETRA_4 || Sets[i].CGNSType == PYRA_5 || Sets[i].CGNSType == PENTA_6 ||
487  Sets[i].CGNSType == HEXA_8 || Sets[i].CGNSType == MIXED )
488  {
489 
490  if( Sets[i].NbEdges > 0 && physdim < 3 )
491  {
492  // Set a data index for Edges and TagHandles[i]
493  const std::vector< int > tag_values( Edges_.size(), i );
494  rval = mbImpl->tag_set_data( TagHandles[i], &Edges_[0], Edges_.size(), &tag_values[0] );
495  if( rval != MB_SUCCESS )
496  {
497  std::cout << "Problem to set data for 1-D entities\n";
498  return rval;
499  }
500  }
501  if( Sets[i].NbFaces > 0 && physdim > 1 )
502  {
503  // Set a data index for Faces and TagHandles[i]
504  const std::vector< int > tag_values( Faces.size(), i );
505  rval = mbImpl->tag_set_data( TagHandles[i], &Faces_[0], Faces_.size(), &tag_values[0] );
506  if( rval != MB_SUCCESS )
507  {
508  std::cout << "Problem to set data for 2-D entities\n";
509  return rval;
510  }
511  }
512  if( Sets[i].NbCells > 0 && physdim > 2 )
513  {
514  // Set a data index for Cells and TagHandles[i]
515  const std::vector< int > tag_values( Cells.size(), i );
516  rval = mbImpl->tag_set_data( TagHandles[i], &Cells_[0], Cells_.size(), &tag_values[0] );
517  if( rval != MB_SUCCESS )
518  {
519  std::cout << "Problem to set data for 3-D entities\n";
520  return rval;
521  }
522  }
523  // IdSet gets the Set Index indicating that it is not empty
524  Sets[i].IdSet = i;
525  }
526  std::cout << "\tSets[" << i << "].IdSet = " << Sets[i].IdSet << "\n\n";
527  }
528  return MB_SUCCESS;
529 }
530 
531 // Get Entities in the set
533  std::vector< Tag >& TagHandles,
534  std::vector< WriteCGNS::SetStruct >& Sets )
535 {
536  ErrorCode rval;
537 
538  // Get the number of MBEDGE entities
539  // NbEntities[0] holds the number of MBEDGE in the "Sets"
540  int Number = 0;
541  rval = mbImpl->get_number_entities_by_type_and_tag( 0, MBEDGE, &TagHandles[i], 0, 1, Number );
542  if( rval != MB_SUCCESS )
543  {
544  std::cout << "Problem to get the number of entities by type and tag\n";
545  return rval;
546  }
547  Sets[i].NbEntities.push_back( Number ); // MBEDGE == Sets[i].NbEntities[0]
548  Sets[i].NbEdges += Number;
549  std::cout << "\tNumber of MBEDGE = " << Number << "\n";
550 
551  // Get the number of MBTRI entities
552  // NbEntities[1] holds the number of MBTRI in the "Sets"
553  Number = 0;
554  rval = mbImpl->get_number_entities_by_type_and_tag( 0, MBTRI, &TagHandles[i], 0, 1, Number );
555  if( rval != MB_SUCCESS )
556  {
557  std::cout << "Problem to get the number of entities by type and tag\n";
558  return rval;
559  }
560  Sets[i].NbEntities.push_back( Number ); // MBTRI == Sets[i].NbEntities[1]
561  Sets[i].NbFaces += Number;
562  std::cout << "\tNumber of MBTRI = " << Number << "\n";
563 
564  // Get the number of MBQUAD entities
565  // NbEntities[2] holds the number of MBQUAD in the "Sets"
566  Number = 0;
567  rval = mbImpl->get_number_entities_by_type_and_tag( 0, MBQUAD, &TagHandles[i], 0, 1, Number );
568  if( rval != MB_SUCCESS )
569  {
570  std::cout << "Problem to get the number of entities by type and tag\n";
571  return rval;
572  }
573  Sets[i].NbEntities.push_back( Number ); // MBQUAD == Sets[i].NbEntities[2]
574  Sets[i].NbFaces += Number;
575  std::cout << "\tNumber of MBQUAD = " << Number << "\n";
576 
577  // Get the number of MBTET entities
578  // NbEntities[3] holds the number of MBTET in the "Sets"
579  Number = 0;
580  rval = mbImpl->get_number_entities_by_type_and_tag( 0, MBTET, &TagHandles[i], 0, 1, Number );
581  if( rval != MB_SUCCESS )
582  {
583  std::cout << "Problem to get the number of entities by type and tag\n";
584  return rval;
585  }
586  Sets[i].NbEntities.push_back( Number ); // MBTET == Sets[i].NbEntities[3]
587  Sets[i].NbCells += Number;
588  std::cout << "\tNumber of MBTET = " << Number << "\n";
589 
590  // Get the number of MBPYRAMID entities
591  // NbEntities[4] holds the number of MBPYRAMID in the "Sets"
592  Number = 0;
593  rval = mbImpl->get_number_entities_by_type_and_tag( 0, MBPYRAMID, &TagHandles[i], 0, 1, Number );
594  if( rval != MB_SUCCESS )
595  {
596  std::cout << "Problem to get the number of entities by type and tag\n";
597  return rval;
598  }
599  Sets[i].NbEntities.push_back( Number ); // MBPYRAMID == Sets[i].NbEntities[4]
600  Sets[i].NbCells += Number;
601  std::cout << "\tNumber of MBPYRAMID = " << Number << "\n";
602 
603  // Get the number of MBPRISM entities - MBPRISM == PENTA_6
604  // NbEntities[5] holds the number of MBPRISM in the "Sets"
605  Number = 0;
606  rval = mbImpl->get_number_entities_by_type_and_tag( 0, MBPRISM, &TagHandles[i], 0, 1, Number );
607  if( rval != MB_SUCCESS )
608  {
609  std::cout << "Problem to get the number of entities by type and tag\n";
610  return rval;
611  }
612  Sets[i].NbEntities.push_back( Number ); // MBPRISM == Sets[i].NbEntities[5]
613  Sets[i].NbCells += Number;
614  std::cout << "\tNumber of MBPRISM = " << Number << "\n";
615 
616  // Get the number of MBHEX entities
617  // NbEntities[6] holds the number of MBHEX in the "Sets"
618  Number = 0;
619  rval = mbImpl->get_number_entities_by_type_and_tag( 0, MBHEX, &TagHandles[i], 0, 1, Number );
620  if( rval != MB_SUCCESS )
621  {
622  std::cout << "Problem to get the number of entities by type and tag\n";
623  return rval;
624  }
625  Sets[i].NbEntities.push_back( Number ); // MBHEX == Sets[i].NbEntities[6]
626  Sets[i].NbCells += Number;
627  std::cout << "\tNumber of MBHEX = " << Number << "\n";
628 
629  std::cout << "\tTotal number of Edges = " << Sets[i].NbEdges << "\n";
630  std::cout << "\tTotal number of Faces = " << Sets[i].NbFaces << "\n";
631  std::cout << "\tTotal number of Cells = " << Sets[i].NbCells << "\n";
632 
633  return MB_SUCCESS;
634 }
635 
636 // Get CGNSType
637 ErrorCode WriteCGNS::get_cgns_type( int i, std::vector< WriteCGNS::SetStruct >& Sets )
638 {
639  std::vector< int > Test;
640  int Sum = 0;
641 
642  // NbEntities is a vector which has the number of entities of each type
643  // 0-MBEDGE | 1-MBTRI | 2-MBQUAD | 3-MBTET | 4-MBPYRAMID | 5-MBPRISM | 6-MBHEX
644  // if NbEntities[i]>0 then Test[i]=1
645  // else then Test[i]=0
646  for( int j = 0; j < (int)Sets[i].NbEntities.size(); ++j )
647  {
648  if( Sets[i].NbEntities[j] > 0 )
649  {
650  Test.push_back( 1 );
651  Sum++;
652  }
653  else
654  {
655  Test.push_back( 0 );
656  }
657  }
658 
659  // Test the Sum
660  // if Sum > 1 then the Set is MIXED
661  // if Sum = 0 then the Set is Homogeneous
662  // else then the Set is empty
663  if( Sum > 1 )
664  {
665  Sets[i].CGNSType = MIXED;
666  }
667  else if( Sum == 1 )
668  {
669  int j = 0;
670  std::cout << "Homogeneous Type\n";
671  while( j < (int)Sets[i].NbEntities.size() && Sets[i].NbEntities[j] != 1 )
672  {
673  ++j;
674  }
675  switch( j )
676  {
677  case 0:
678  Sets[i].CGNSType = BAR_2;
679  break;
680  case 1:
681  Sets[i].CGNSType = TRI_3;
682  break;
683  case 2:
684  Sets[i].CGNSType = QUAD_4;
685  break;
686  case 3:
687  Sets[i].CGNSType = TETRA_4;
688  break;
689  case 4:
690  Sets[i].CGNSType = PYRA_5;
691  break;
692  case 5:
693  Sets[i].CGNSType = PENTA_6;
694  break;
695  case 6:
696  Sets[i].CGNSType = HEXA_8;
697  break;
698  default:
699  std::cout << "It was not possible to identify the CGNSType\n";
700  return MB_FAILURE;
701  }
702  }
703  else
704  {
705  Sets[i].CGNSType = ElementTypeNull;
706  } // NOT SURE IF THAT'S THE RIGHT WAY.......
707 
708  // Clear the test vector
709  Test.clear();
710 
711  return MB_SUCCESS;
712 }
713 
714 // Fill the connectivity table
715 ErrorCode WriteCGNS::get_conn_table( std::vector< moab::EntityHandle >& Elements,
716  std::vector< int >& Begin,
717  std::vector< int >& End,
718  std::vector< moab::Tag >& TagHandles,
719  std::vector< WriteCGNS::SetStruct >& Sets,
720  std::vector< std::vector< cgsize_t > >& ConnTable )
721 {
722  ErrorCode rval;
723 
724  // int Begin = 0; // GOT TO WORK ON THIS
725  // int End;
726 
727  // Get the number of Tags in the mesh
728  int NbTags = TagHandles.size();
729 
730  // Test all Elements, get their ids and connectivity
731  // to fill ConnTable
732  for( std::vector< moab::EntityHandle >::iterator i = Elements.begin(); i != Elements.end(); ++i )
733  {
734  int id;
735  // Test all Tags
736  for( int j = 0; j < NbTags; ++j )
737  {
738  // Test if the Tag has data
739  if( Sets[j].IdSet != -1 )
740  {
741  // Try to get data from entity
742  rval = mbImpl->tag_get_data( TagHandles[j], &*i, 1, &id );
743  if( MB_SUCCESS != rval )
744  {
745  return rval;
746  }
747  // If successful id==j
748  if( id == j )
749  {
750  // Get the entity type of the EntityHandle wich points to Cells
751  int num_vtx; // Number of MeshVertices in array connectivity.
752  const EntityHandle* conn; // Array in which connectivity of entity_handle is returned.
753  // Gets a pointer to constant connectivity data of entity_handle
754  rval = mbImpl->get_connectivity( *i, conn, num_vtx );
755  if( MB_SUCCESS != rval )
756  {
757  return rval;
758  }
759  // If the Set is MIXED type
760  // push CGNS ENUM type of the entity
761  // before the connectivity
762  if( Sets[j].CGNSType == MIXED )
763  {
764  ConnTable[j].push_back( moab_cgns_conv( *i ) ); // moab_cgns_conv return an int which
765  // represents the CGNS type
766  Begin[j]++;
767  }
768  End[j] = Begin[j] + num_vtx;
769  // Push conn in ConnTable in which "j" is the Set Index
770  for( int k = Begin[j]; k < End[j]; ++k )
771  {
772  ConnTable[j].push_back( (cgsize_t)conn[k - Begin[j]] );
773  }
774  Begin[j] = End[j];
775  }
776  }
777  }
778  }
779  return MB_SUCCESS;
780 }
781 
782 // Read the Moab type and return CGNS type
784 {
785  EntityType MoabType = mbImpl->type_from_handle( handle );
786  switch( MoabType )
787  {
788  case MBEDGE: /**< Mesh Edge */
789  return BAR_2;
790  case MBTRI: /**< Triangular element (including shells) */
791  return TRI_3;
792  case MBQUAD: /**< Quadrilateral element (including shells) */
793  return QUAD_4;
794  case MBTET: /**< Tetrahedral element */
795  return TETRA_4;
796  case MBPYRAMID: /**< Pyramid element */
797  return PYRA_5;
798  case MBPRISM: /**< Wedge element */
799  return PENTA_6;
800  case MBHEX: /**< Hexahedral element */
801  return HEXA_8;
802  default:
803  std::cout << "It was not possible to identify the CGNSType\n";
804  return 0;
805  }
806 }
807 
808 } // namespace moab