Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
ReadIDEAS.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <vector>
4 #include <cstdlib>
5 #include <sstream>
6 #include <cassert>
7 
8 #include "ReadIDEAS.hpp"
9 #include "moab/Interface.hpp"
10 #include "Internals.hpp"
11 #include "moab/ReadUtilIface.hpp"
12 #include "FileTokenizer.hpp"
13 #include "MBTagConventions.hpp"
14 #include "moab/Range.hpp"
15 #include "moab/CN.hpp"
16 
17 namespace moab
18 {
19 
21 {
22  return new ReadIDEAS( iface );
23 }
24 
26 {
28 }
29 
30 ErrorCode ReadIDEAS::read_tag_values( const char* /* file_name */,
31  const char* /* tag_name */,
32  const FileOptions& /* opts */,
33  std::vector< int >& /* tag_values_out */,
34  const SubsetList* /* subset_list */ )
35 {
36  return MB_NOT_IMPLEMENTED;
37 }
38 
39 ErrorCode ReadIDEAS::load_file( const char* fname,
40  const EntityHandle*,
41  const FileOptions& /*options*/,
42  const ReaderIface::SubsetList* subset_list,
43  const Tag* file_id_tag )
44 {
45  if( subset_list )
46  {
47  MB_SET_ERR( MB_UNSUPPORTED_OPERATION, "Reading subset of files not supported for IDEAS" );
48  }
49 
50  file.open( fname );
51  if( !file.good() )
52  {
53  MB_SET_ERR( MB_FILE_DOES_NOT_EXIST, "Failed to open file: " << fname );
54  }
55 
56  ErrorCode rval;
57 
58  char line[10000];
59  file.getline( line, 10000 );
60  char* liter = line;
61  while( *liter && isspace( *liter ) )
62  ++liter;
63  if( *liter != '-' ) return MB_FAILURE;
64  ++liter;
65  if( *liter != '1' ) return MB_FAILURE;
66  while( *++liter )
67  if( !isspace( *liter ) ) return MB_FAILURE;
68 
69  EntityHandle first_vertex = 0;
70  while( !file.eof() )
71  {
72  file.getline( line, 10000 );
73  unsigned int header_id = (unsigned int)strtol( line, NULL, 10 );
74 
75  // Create vertices
76  if( DOUBLE_PRECISION_NODES0 == header_id || DOUBLE_PRECISION_NODES1 == header_id )
77  {
78  if( first_vertex ) // multiple vertex blocks?
79  return MB_FAILURE;
80  MB_CHK_SET_ERR( create_vertices( first_vertex, file_id_tag ), "Failed to read vertices" );
81  }
82  // Create elements
83  else if( ELEMENTS0 == header_id || ELEMENTS1 == header_id || ELEMENTS2 == header_id )
84  {
85  if( !first_vertex ) // Need to read vertices first
86  return MB_FAILURE;
87  MB_CHK_SET_ERR( create_elements( first_vertex, file_id_tag ), "Failed to read elements" );
88  }
89  // Skip everything else
90  else
91  {
92  rval = skip_header();
93  if( MB_SUCCESS != rval ) return MB_FAILURE;
94  }
95  }
96 
97  file.close();
98  return MB_SUCCESS;
99 }
100 
102 {
103  // Go until finding a pair of -1 lines
104  char* ctmp;
105  char line[10000];
106  std::string s;
107 
108  int end_of_block = 0;
109 
110  long int il;
111 
112  while( file.getline( line, 10000 ) )
113  {
114  il = std::strtol( line, &ctmp, 10 );
115  if( il == -1 )
116  {
117  s = ctmp;
118  if( s.empty() ) end_of_block++;
119  }
120  else
121  end_of_block = 0;
122 
123  if( end_of_block >= 2 ) return MB_SUCCESS;
124  }
125 
126  return MB_SUCCESS;
127 }
128 
129 ErrorCode ReadIDEAS::create_vertices( EntityHandle& first_vertex, const Tag* file_id_tag )
130 {
131  // Read two lines: first has some data, second has coordinates
132  char line1[10000], line2[10000];
133  int il1, il2;
134  char *ctmp1, *ctmp2;
135  std::string s1, s2;
136 
137  ErrorCode rval;
138 
139  std::streampos top_of_block = file.tellg();
140  unsigned int num_verts = 0;
141 
142  for( ;; )
143  {
144  // Read both lines
145  if( !file.getline( line1, 10000 ) ) return MB_FAILURE;
146  if( !file.getline( line2, 10000 ) ) return MB_FAILURE;
147 
148  // Check if we are at the end of the block
149  il1 = std::strtol( line1, &ctmp1, 10 );
150  il2 = std::strtol( line2, &ctmp2, 10 );
151  if( ( il1 == -1 ) && ( il2 == -1 ) )
152  {
153  s1 = ctmp1;
154  s2 = ctmp2;
155  if( ( s1.empty() ) && ( s2.empty() ) ) break;
156  }
157  num_verts++;
158  }
159 
160  file.seekg( top_of_block );
161 
162  std::vector< double* > arrays;
163  rval = readMeshIface->get_node_coords( 3, num_verts, 0, first_vertex, arrays );
164  if( MB_SUCCESS != rval ) return rval;
165 
166  Range verts;
167  verts.insert( first_vertex, first_vertex + num_verts - 1 );
168 
169  double* x = arrays[0];
170  double* y = arrays[1];
171  double* z = arrays[2];
172 
173  // For now, assume ids are sequential and begin with 1
174  Tag id_tag = MBI->globalId_tag();
175  const int beginning_node_id = 1;
176  int node_id = beginning_node_id;
177 
178  for( unsigned int i = 0; i < num_verts; i++ )
179  {
180  if( !file.getline( line1, 10000 ) ) return MB_FAILURE;
181  if( !file.getline( line2, 10000 ) ) return MB_FAILURE;
182 
183  // Get the id out of the 1st line. Check the assumption that node ids are
184  // sequential and begin with 1.
185  if( node_id != std::strtol( line1, &ctmp1, 10 ) )
186  MB_SET_ERR( MB_FAILURE, "node_id " << node_id << " line2:" << line2 << " ctmp1:" << ctmp1 );
187  else
188  ++node_id;
189 
190  // Get the doubles out of the 2nd line
191  x[i] = std::strtod( line2, &ctmp2 );
192  y[i] = std::strtod( ctmp2 + 1, &ctmp2 );
193  z[i] = std::strtod( ctmp2 + 1, NULL );
194  }
195 
196  if( !file.getline( line1, 10000 ) ) MB_SET_ERR( MB_FAILURE, " expect more lines" );
197  if( !file.getline( line2, 10000 ) ) MB_SET_ERR( MB_FAILURE, " expect more lines 2" );
198 
199  // Tag the nodes with ids
200  MB_CHK_SET_ERR( readMeshIface->assign_ids( id_tag, verts, beginning_node_id ), "Failed to assign IDs" );
201  if( file_id_tag )
202  {
203  MB_CHK_SET_ERR( readMeshIface->assign_ids( *file_id_tag, verts, beginning_node_id ),
204  "Failed to assign file IDs" );
205  }
206 
207  return MB_SUCCESS;
208 }
209 
211 {
212  char line1[10000], line2[10000];
213  int il1, il2;
214  char *ctmp1, *ctmp2;
215  std::string s1, s2;
216  ErrorCode rval;
217  EntityHandle handle;
218 
219  Tag mat_tag, phys_tag, id_tag;
221  if( MB_SUCCESS != rval && MB_ALREADY_ALLOCATED != rval ) return rval;
223  if( MB_SUCCESS != rval && MB_ALREADY_ALLOCATED != rval ) return rval;
224  id_tag = MBI->globalId_tag();
225 
226  for( ;; )
227  {
228  if( !file.getline( line1, 10000 ) || !file.getline( line2, 10000 ) ) return MB_FAILURE;
229 
230  // Check if we are at the end of the block
231  il1 = std::strtol( line1, &ctmp1, 10 );
232  il2 = std::strtol( line2, &ctmp2, 10 );
233  if( ( il1 == -1 ) && ( il2 == -1 ) )
234  {
235  s1 = ctmp1;
236  s2 = ctmp2;
237  if( ( s1.empty() ) && ( s2.empty() ) ) return MB_SUCCESS;
238  }
239 
240  // The first line describes attributes of the element other than connectivity.
241  const int element_id = strtol( line1 + 1, &ctmp1, 10 );
242  const int ideas_type = strtol( line1 + 11, &ctmp1, 10 );
243  const int phys_table = strtol( line1 + 21, &ctmp1, 10 );
244  const int mat_table = strtol( line1 + 31, &ctmp1, 10 );
245 
246  // Determine the element type.
247  EntityType mb_type;
248  if( TRI0 == ideas_type || TRI1 == ideas_type )
249  mb_type = MBTRI;
250  else if( QUAD0 == ideas_type || QUAD1 == ideas_type )
251  mb_type = MBQUAD;
252  else if( TET == ideas_type )
253  mb_type = MBTET;
254  else if( HEX == ideas_type )
255  mb_type = MBHEX;
256  else if( WEDGE == ideas_type )
257  mb_type = MBPRISM;
258  else
259  {
260  std::cout << "IDEAS element type not yet added to MOAB reader." << std::endl;
261  return MB_NOT_IMPLEMENTED;
262  }
263 
264  // Get the connectivity out of the 2nd line
265  std::stringstream ss( line2 );
266  const int n_conn = CN::VerticesPerEntity( mb_type );
268  EntityHandle vert;
269  for( int i = 0; i < n_conn; ++i )
270  {
271  ss >> vert;
272  conn[i] = vstart + vert - 1;
273  }
274 
275  // Make the element. According to the Gmsh 2.2.3 source code, the IDEAS
276  // canonical numbering is the same as MBCN.
277  MB_CHK_SET_ERR( MBI->create_element( mb_type, conn, n_conn, handle ),
278  "can't create elements of type " << mb_type );
279 
280  // If the phys set does not already exist, create it.
281  Range phys_sets;
282  EntityHandle phys_set;
283  const void* const phys_set_id_val[] = { &phys_table };
284  MB_CHK_SET_ERR( MBI->get_entities_by_type_and_tag( 0, MBENTITYSET, &phys_tag, phys_set_id_val, 1, phys_sets ),
285  "can't get phys sets" );
286  if( phys_sets.empty() )
287  {
288  MB_CHK_SET_ERR( MBI->create_meshset( MESHSET_SET, phys_set ), "can't create phys set" );
289  MB_CHK_SET_ERR( MBI->tag_set_data( phys_tag, &phys_set, 1, &phys_table ), "can't set tag to phys set" );
290  }
291  else if( 1 == phys_sets.size() )
292  {
293  phys_set = phys_sets.front();
294  }
295  else
296  {
298  }
299  MB_CHK_SET_ERR( MBI->add_entities( phys_set, &handle, 1 ), "can't add entities to phys set" );
300 
301  // If the material set does not already exist, create it.
302  Range mat_sets;
303  EntityHandle mat_set;
304  const void* const mat_set_id_val[] = { &mat_table };
305  rval = MBI->get_entities_by_type_and_tag( 0, MBENTITYSET, &mat_tag, mat_set_id_val, 1, mat_sets );
306  if( MB_SUCCESS != rval ) return rval;
307  if( mat_sets.empty() )
308  {
309  rval = MBI->create_meshset( MESHSET_SET, mat_set );
310  if( MB_SUCCESS != rval ) return rval;
311  rval = MBI->tag_set_data( mat_tag, &mat_set, 1, &mat_table );
312  if( MB_SUCCESS != rval ) return rval;
313  }
314  else if( 1 == mat_sets.size() )
315  {
316  mat_set = mat_sets.front();
317  }
318  else
319  {
321  }
322  rval = MBI->add_entities( mat_set, &handle, 1 );
323  if( MB_SUCCESS != rval ) return rval;
324 
325  // Tag the element with its id
326  MB_CHK_SET_ERR( MBI->tag_set_data( id_tag, &handle, 1, &element_id ), "Failed to assign IDs" );
327  if( file_id_tag )
328  {
329  MB_CHK_SET_ERR( MBI->tag_set_data( *file_id_tag, &handle, 1, &element_id ), "Failed to assign file IDs" );
330  }
331  }
332 }
333 
334 } // namespace moab