MOAB: Mesh Oriented datABase  (version 5.2.1)
ReadIDEAS.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 #include <vector>
00004 #include <cstdlib>
00005 #include <sstream>
00006 #include "assert.h"
00007 
00008 #include "ReadIDEAS.hpp"
00009 #include "moab/Interface.hpp"
00010 #include "Internals.hpp"
00011 #include "moab/ReadUtilIface.hpp"
00012 #include "FileTokenizer.hpp"
00013 #include "MBTagConventions.hpp"
00014 #include "moab/Range.hpp"
00015 #include "moab/CN.hpp"
00016 
00017 namespace moab
00018 {
00019 
00020 ReaderIface* ReadIDEAS::factory( Interface* iface )
00021 {
00022     return new ReadIDEAS( iface );
00023 }
00024 
00025 ReadIDEAS::ReadIDEAS( Interface* impl ) : MBI( impl )
00026 {
00027     impl->query_interface( readMeshIface );
00028 }
00029 
00030 ErrorCode ReadIDEAS::read_tag_values( const char* /* file_name */, const char* /* tag_name */,
00031                                       const FileOptions& /* opts */, std::vector< int >& /* tag_values_out */,
00032                                       const SubsetList* /* subset_list */ )
00033 {
00034     return MB_NOT_IMPLEMENTED;
00035 }
00036 
00037 ErrorCode ReadIDEAS::load_file( const char* fname, const EntityHandle*, const FileOptions& /*options*/,
00038                                 const ReaderIface::SubsetList* subset_list, const Tag* file_id_tag )
00039 {
00040     if( subset_list ) { MB_SET_ERR( MB_UNSUPPORTED_OPERATION, "Reading subset of files not supported for IDEAS" ); }
00041 
00042     file.open( fname );
00043     if( !file.good() ) { MB_SET_ERR( MB_FILE_DOES_NOT_EXIST, "Failed to open file: " << fname ); }
00044 
00045     ErrorCode rval;
00046 
00047     char line[10000];
00048     file.getline( line, 10000 );
00049     char* liter = line;
00050     while( *liter && isspace( *liter ) )
00051         ++liter;
00052     if( *liter != '-' ) return MB_FAILURE;
00053     ++liter;
00054     if( *liter != '1' ) return MB_FAILURE;
00055     while( *++liter )
00056         if( !isspace( *liter ) ) return MB_FAILURE;
00057 
00058     EntityHandle first_vertex = 0;
00059     while( !file.eof() )
00060     {
00061         file.getline( line, 10000 );
00062         unsigned int header_id = (unsigned int)strtol( line, NULL, 10 );
00063 
00064         // Create vertices
00065         if( DOUBLE_PRECISION_NODES0 == header_id || DOUBLE_PRECISION_NODES1 == header_id )
00066         {
00067             if( first_vertex )  // multiple vertex blocks?
00068                 return MB_FAILURE;
00069             rval = create_vertices( first_vertex, file_id_tag );MB_CHK_SET_ERR( rval, "Failed to read vertices" );
00070         }
00071         // Create elements
00072         else if( ELEMENTS0 == header_id || ELEMENTS1 == header_id || ELEMENTS2 == header_id )
00073         {
00074             if( !first_vertex )  // Need to read vertices first
00075                 return MB_FAILURE;
00076             rval = create_elements( first_vertex, file_id_tag );MB_CHK_SET_ERR( rval, "Failed to read elements" );
00077         }
00078         // Skip everything else
00079         else
00080         {
00081             rval = skip_header();
00082             if( MB_SUCCESS != rval ) return MB_FAILURE;
00083         }
00084     }
00085 
00086     file.close();
00087     return MB_SUCCESS;
00088 }
00089 
00090 ErrorCode ReadIDEAS::skip_header()
00091 {
00092     // Go until finding a pair of -1 lines
00093     char* ctmp;
00094     char line[10000];
00095     std::string s;
00096 
00097     int end_of_block = 0;
00098 
00099     long int il;
00100 
00101     while( file.getline( line, 10000 ) )
00102     {
00103         il = std::strtol( line, &ctmp, 10 );
00104         if( il == -1 )
00105         {
00106             s = ctmp;
00107             if( s.empty() ) end_of_block++;
00108         }
00109         else
00110             end_of_block = 0;
00111 
00112         if( end_of_block >= 2 ) return MB_SUCCESS;
00113     }
00114 
00115     return MB_SUCCESS;
00116 }
00117 
00118 ErrorCode ReadIDEAS::create_vertices( EntityHandle& first_vertex, const Tag* file_id_tag )
00119 {
00120     // Read two lines: first has some data, second has coordinates
00121     char line1[10000], line2[10000];
00122     int il1, il2;
00123     char *ctmp1, *ctmp2;
00124     std::string s1, s2;
00125 
00126     ErrorCode rval;
00127 
00128     std::streampos top_of_block = file.tellg();
00129     unsigned int num_verts      = 0;
00130 
00131     for( ;; )
00132     {
00133         // Read both lines
00134         if( !file.getline( line1, 10000 ) ) return MB_FAILURE;
00135         if( !file.getline( line2, 10000 ) ) return MB_FAILURE;
00136 
00137         // Check if we are at the end of the block
00138         il1 = std::strtol( line1, &ctmp1, 10 );
00139         il2 = std::strtol( line2, &ctmp2, 10 );
00140         if( ( il1 == -1 ) && ( il2 == -1 ) )
00141         {
00142             s1 = ctmp1;
00143             s2 = ctmp2;
00144             if( ( s1.empty() ) && ( s2.empty() ) ) break;
00145         }
00146         num_verts++;
00147     }
00148 
00149     file.seekg( top_of_block );
00150 
00151     std::vector< double* > arrays;
00152     rval = readMeshIface->get_node_coords( 3, num_verts, 0, first_vertex, arrays );
00153     if( MB_SUCCESS != rval ) return rval;
00154 
00155     Range verts;
00156     verts.insert( first_vertex, first_vertex + num_verts - 1 );
00157 
00158     double* x = arrays[0];
00159     double* y = arrays[1];
00160     double* z = arrays[2];
00161 
00162     // For now, assume ids are sequential and begin with 1
00163     Tag id_tag                  = MBI->globalId_tag();
00164     const int beginning_node_id = 1;
00165     int node_id                 = beginning_node_id;
00166 
00167     for( unsigned int i = 0; i < num_verts; i++ )
00168     {
00169         if( !file.getline( line1, 10000 ) ) return MB_FAILURE;
00170         if( !file.getline( line2, 10000 ) ) return MB_FAILURE;
00171 
00172         // Get the id out of the 1st line. Check the assumption that node ids are
00173         // sequential and begin with 1.
00174         if( node_id != std::strtol( line1, &ctmp1, 10 ) )
00175             MB_SET_ERR( MB_FAILURE, "node_id " << node_id << " line2:" << line2 << " ctmp1:" << ctmp1 );
00176         else
00177             ++node_id;
00178 
00179         // Get the doubles out of the 2nd line
00180         x[i] = std::strtod( line2, &ctmp2 );
00181         y[i] = std::strtod( ctmp2 + 1, &ctmp2 );
00182         z[i] = std::strtod( ctmp2 + 1, NULL );
00183     }
00184 
00185     if( !file.getline( line1, 10000 ) ) MB_SET_ERR( MB_FAILURE, " expect more lines" );
00186     if( !file.getline( line2, 10000 ) ) MB_SET_ERR( MB_FAILURE, " expect more lines 2" );
00187 
00188     // Tag the nodes with ids
00189     rval = readMeshIface->assign_ids( id_tag, verts, beginning_node_id );MB_CHK_SET_ERR( rval, "Failed to assign IDs" );
00190     if( file_id_tag )
00191     {
00192         rval = readMeshIface->assign_ids( *file_id_tag, verts, beginning_node_id );MB_CHK_SET_ERR( rval, "Failed to assign file IDs" );
00193     }
00194 
00195     return MB_SUCCESS;
00196 }
00197 
00198 ErrorCode ReadIDEAS::create_elements( EntityHandle vstart, const Tag* file_id_tag )
00199 {
00200     char line1[10000], line2[10000];
00201     int il1, il2;
00202     char *ctmp1, *ctmp2;
00203     std::string s1, s2;
00204     ErrorCode rval;
00205     EntityHandle handle;
00206 
00207     Tag mat_tag, phys_tag, id_tag;
00208     rval = MBI->tag_get_handle( MAT_PROP_TABLE_TAG, 1, MB_TYPE_INTEGER, mat_tag, MB_TAG_DENSE | MB_TAG_CREAT );
00209     if( MB_SUCCESS != rval && MB_ALREADY_ALLOCATED != rval ) return rval;
00210     rval = MBI->tag_get_handle( PHYS_PROP_TABLE_TAG, 1, MB_TYPE_INTEGER, phys_tag, MB_TAG_DENSE | MB_TAG_CREAT );
00211     if( MB_SUCCESS != rval && MB_ALREADY_ALLOCATED != rval ) return rval;
00212     id_tag = MBI->globalId_tag();
00213 
00214     for( ;; )
00215     {
00216         if( !file.getline( line1, 10000 ) || !file.getline( line2, 10000 ) ) return MB_FAILURE;
00217 
00218         // Check if we are at the end of the block
00219         il1 = std::strtol( line1, &ctmp1, 10 );
00220         il2 = std::strtol( line2, &ctmp2, 10 );
00221         if( ( il1 == -1 ) && ( il2 == -1 ) )
00222         {
00223             s1 = ctmp1;
00224             s2 = ctmp2;
00225             if( ( s1.empty() ) && ( s2.empty() ) ) return MB_SUCCESS;
00226         }
00227 
00228         // The first line describes attributes of the element other than connectivity.
00229         const int element_id = strtol( line1 + 1, &ctmp1, 10 );
00230         const int ideas_type = strtol( line1 + 11, &ctmp1, 10 );
00231         const int phys_table = strtol( line1 + 21, &ctmp1, 10 );
00232         const int mat_table  = strtol( line1 + 31, &ctmp1, 10 );
00233 
00234         // Determine the element type.
00235         EntityType mb_type;
00236         if( TRI0 == ideas_type || TRI1 == ideas_type )
00237             mb_type = MBTRI;
00238         else if( QUAD0 == ideas_type || QUAD1 == ideas_type )
00239             mb_type = MBQUAD;
00240         else if( TET == ideas_type )
00241             mb_type = MBTET;
00242         else if( HEX == ideas_type )
00243             mb_type = MBHEX;
00244         else if( WEDGE == ideas_type )
00245             mb_type = MBPRISM;
00246         else
00247         {
00248             std::cout << "IDEAS element type not yet added to MOAB reader." << std::endl;
00249             return MB_NOT_IMPLEMENTED;
00250         }
00251 
00252         // Get the connectivity out of the 2nd line
00253         std::stringstream ss( line2 );
00254         const int n_conn = CN::VerticesPerEntity( mb_type );
00255         EntityHandle conn[CN::MAX_NODES_PER_ELEMENT];
00256         EntityHandle vert;
00257         for( int i = 0; i < n_conn; ++i )
00258         {
00259             ss >> vert;
00260             conn[i] = vstart + vert - 1;
00261         }
00262 
00263         // Make the element. According to the Gmsh 2.2.3 source code, the IDEAS
00264         // canonical numbering is the same as MBCN.
00265         rval = MBI->create_element( mb_type, conn, n_conn, handle );MB_CHK_SET_ERR( rval, "can't create elements of type " << mb_type );
00266 
00267         // If the phys set does not already exist, create it.
00268         Range phys_sets;
00269         EntityHandle phys_set;
00270         const void* const phys_set_id_val[] = { &phys_table };
00271         rval = MBI->get_entities_by_type_and_tag( 0, MBENTITYSET, &phys_tag, phys_set_id_val, 1, phys_sets );MB_CHK_SET_ERR( rval, "can't get phys sets" );
00272         if( phys_sets.empty() )
00273         {
00274             rval = MBI->create_meshset( MESHSET_SET, phys_set );MB_CHK_SET_ERR( rval, "can't create phys set" );
00275             rval = MBI->tag_set_data( phys_tag, &phys_set, 1, &phys_table );MB_CHK_SET_ERR( rval, "can't set tag to phys set" );
00276         }
00277         else if( 1 == phys_sets.size() )
00278         {
00279             phys_set = phys_sets.front();
00280         }
00281         else
00282         {
00283             return MB_MULTIPLE_ENTITIES_FOUND;
00284         }
00285         rval = MBI->add_entities( phys_set, &handle, 1 );MB_CHK_SET_ERR( rval, "can't add entities to phys set" );
00286 
00287         // If the material set does not already exist, create it.
00288         Range mat_sets;
00289         EntityHandle mat_set;
00290         const void* const mat_set_id_val[] = { &mat_table };
00291         rval = MBI->get_entities_by_type_and_tag( 0, MBENTITYSET, &mat_tag, mat_set_id_val, 1, mat_sets );
00292         if( MB_SUCCESS != rval ) return rval;
00293         if( mat_sets.empty() )
00294         {
00295             rval = MBI->create_meshset( MESHSET_SET, mat_set );
00296             if( MB_SUCCESS != rval ) return rval;
00297             rval = MBI->tag_set_data( mat_tag, &mat_set, 1, &mat_table );
00298             if( MB_SUCCESS != rval ) return rval;
00299         }
00300         else if( 1 == mat_sets.size() )
00301         {
00302             mat_set = mat_sets.front();
00303         }
00304         else
00305         {
00306             return MB_MULTIPLE_ENTITIES_FOUND;
00307         }
00308         rval = MBI->add_entities( mat_set, &handle, 1 );
00309         if( MB_SUCCESS != rval ) return rval;
00310 
00311         // Tag the element with its id
00312         rval = MBI->tag_set_data( id_tag, &handle, 1, &element_id );MB_CHK_SET_ERR( rval, "Failed to assign IDs" );
00313         if( file_id_tag )
00314         {
00315             rval = MBI->tag_set_data( *file_id_tag, &handle, 1, &element_id );MB_CHK_SET_ERR( rval, "Failed to assign file IDs" );
00316         }
00317     }
00318 
00319     return MB_SUCCESS;
00320 }
00321 
00322 }  // namespace moab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines