MOAB: Mesh Oriented datABase  (version 5.2.1)
WriteNCDF.cpp
Go to the documentation of this file.
00001 /**
00002  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
00003  * storing and accessing finite element mesh data.
00004  *
00005  * Copyright 2004 Sandia Corporation.  Under the terms of Contract
00006  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
00007  * retains certain rights in this software.
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  */
00015 
00016 #ifdef WIN32
00017 #ifdef _DEBUG
00018 // turn off warnings that say they debugging identifier has been truncated
00019 // this warning comes up when using some STL containers
00020 #pragma warning( disable : 4786 )
00021 #endif
00022 #endif
00023 
00024 #include "WriteNCDF.hpp"
00025 
00026 #include "netcdf.h"
00027 #include <utility>
00028 #include <algorithm>
00029 #include <time.h>
00030 #include <string>
00031 #include <vector>
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <assert.h>
00035 
00036 #include "moab/Interface.hpp"
00037 #include "moab/Range.hpp"
00038 #include "moab/CN.hpp"
00039 #include "moab/FileOptions.hpp"
00040 #include "MBTagConventions.hpp"
00041 #include "Internals.hpp"
00042 #include "ExoIIUtil.hpp"
00043 #include "moab/WriteUtilIface.hpp"
00044 #include "exodus_order.h"
00045 
00046 #ifndef MOAB_HAVE_NETCDF
00047 #error Attempt to compile WriteNCDF with NetCDF support disabled
00048 #endif
00049 
00050 namespace moab
00051 {
00052 
00053 const int TIME_STR_LEN = 11;
00054 
00055 #define INS_ID( stringvar, prefix, id ) sprintf( stringvar, prefix, id )
00056 
00057 #define GET_DIM( ncdim, name, val )                                                                             \
00058     {                                                                                                           \
00059         int gdfail = nc_inq_dimid( ncFile, name, &ncdim );                                                      \
00060         if( NC_NOERR == gdfail )                                                                                \
00061         {                                                                                                       \
00062             size_t tmp_val;                                                                                     \
00063             gdfail = nc_inq_dimlen( ncFile, ncdim, &tmp_val );                                                  \
00064             if( NC_NOERR != gdfail ) { MB_SET_ERR( MB_FAILURE, "WriteNCDF:: couldn't get dimension length" ); } \
00065             else                                                                                                \
00066                 val = tmp_val;                                                                                  \
00067         }                                                                                                       \
00068         else                                                                                                    \
00069             val = 0;                                                                                            \
00070     }
00071 
00072 #define GET_DIMB( ncdim, name, varname, id, val ) \
00073     INS_ID( name, varname, id );                  \
00074     GET_DIM( ncdim, name, val );
00075 
00076 #define GET_VAR( name, id, dims )                                 \
00077     {                                                             \
00078         id         = -1;                                          \
00079         int gvfail = nc_inq_varid( ncFile, name, &id );           \
00080         if( NC_NOERR == gvfail )                                  \
00081         {                                                         \
00082             int ndims;                                            \
00083             gvfail = nc_inq_varndims( ncFile, id, &ndims );       \
00084             if( NC_NOERR == gvfail )                              \
00085             {                                                     \
00086                 dims.resize( ndims );                             \
00087                 gvfail = nc_inq_vardimid( ncFile, id, &dims[0] ); \
00088             }                                                     \
00089         }                                                         \
00090     }
00091 
00092 WriterIface* WriteNCDF::factory( Interface* iface )
00093 {
00094     return new WriteNCDF( iface );
00095 }
00096 
00097 WriteNCDF::WriteNCDF( Interface* impl )
00098     : mdbImpl( impl ), ncFile( 0 ), mCurrentMeshHandle( 0 ), mGeomDimensionTag( 0 ), repeat_face_blocks( 0 )
00099 {
00100     assert( impl != NULL );
00101 
00102     impl->query_interface( mWriteIface );
00103 
00104     // Initialize in case tag_get_handle fails below
00105     //! Get and cache predefined tag handles
00106     int negone = -1;
00107     impl->tag_get_handle( MATERIAL_SET_TAG_NAME, 1, MB_TYPE_INTEGER, mMaterialSetTag, MB_TAG_SPARSE | MB_TAG_CREAT,
00108                           &negone );
00109 
00110     impl->tag_get_handle( DIRICHLET_SET_TAG_NAME, 1, MB_TYPE_INTEGER, mDirichletSetTag, MB_TAG_SPARSE | MB_TAG_CREAT,
00111                           &negone );
00112 
00113     impl->tag_get_handle( NEUMANN_SET_TAG_NAME, 1, MB_TYPE_INTEGER, mNeumannSetTag, MB_TAG_SPARSE | MB_TAG_CREAT,
00114                           &negone );
00115 
00116     mGlobalIdTag = impl->globalId_tag();
00117 
00118     int dum_val_array[] = { -1, -1, -1, -1 };
00119     impl->tag_get_handle( HAS_MID_NODES_TAG_NAME, 4, MB_TYPE_INTEGER, mHasMidNodesTag, MB_TAG_SPARSE | MB_TAG_CREAT,
00120                           dum_val_array );
00121 
00122     impl->tag_get_handle( "distFactor", 0, MB_TYPE_DOUBLE, mDistFactorTag,
00123                           MB_TAG_SPARSE | MB_TAG_VARLEN | MB_TAG_CREAT );
00124 
00125     impl->tag_get_handle( "qaRecord", 0, MB_TYPE_OPAQUE, mQaRecordTag, MB_TAG_SPARSE | MB_TAG_VARLEN | MB_TAG_CREAT );
00126 
00127     impl->tag_get_handle( "WriteNCDF element mark", 1, MB_TYPE_BIT, mEntityMark, MB_TAG_CREAT );
00128 }
00129 
00130 WriteNCDF::~WriteNCDF()
00131 {
00132     mdbImpl->release_interface( mWriteIface );
00133 
00134     mdbImpl->tag_delete( mEntityMark );
00135 
00136     if( 0 != ncFile ) ncFile = 0;
00137 }
00138 
00139 void WriteNCDF::reset_block( std::vector< MaterialSetData >& block_info )
00140 {
00141     std::vector< MaterialSetData >::iterator iter;
00142 
00143     for( iter = block_info.begin(); iter != block_info.end(); ++iter )
00144     {
00145         iter->elements.clear();
00146     }
00147 }
00148 
00149 void WriteNCDF::time_and_date( char* time_string, char* date_string )
00150 {
00151     struct tm* local_time;
00152     time_t calendar_time;
00153 
00154     calendar_time = time( NULL );
00155     local_time    = localtime( &calendar_time );
00156 
00157     assert( NULL != time_string && NULL != date_string );
00158 
00159     strftime( time_string, TIME_STR_LEN, "%H:%M:%S", local_time );
00160     strftime( date_string, TIME_STR_LEN, "%m/%d/%Y", local_time );
00161 
00162     // Terminate with NULL character
00163     time_string[10] = (char)NULL;
00164     date_string[10] = (char)NULL;
00165 }
00166 
00167 ErrorCode WriteNCDF::write_file( const char* exodus_file_name, const bool overwrite, const FileOptions& opts,
00168                                  const EntityHandle* ent_handles, const int num_sets,
00169                                  const std::vector< std::string >& qa_records, const Tag*, int, int user_dimension )
00170 {
00171     assert( 0 != mMaterialSetTag && 0 != mNeumannSetTag && 0 != mDirichletSetTag );
00172 
00173     if( user_dimension == 0 ) mdbImpl->get_dimension( user_dimension );
00174 
00175     if( opts.get_null_option( "REPEAT_FACE_BLOCKS" ) == MB_SUCCESS ) repeat_face_blocks = 1;
00176 
00177     std::vector< EntityHandle > blocks, nodesets, sidesets, entities;
00178 
00179     // Separate into blocks, nodesets, sidesets
00180 
00181     if( num_sets == 0 )
00182     {
00183         // Default to all defined block, nodeset and sideset-type sets
00184         Range this_range;
00185         mdbImpl->get_entities_by_type_and_tag( 0, MBENTITYSET, &mMaterialSetTag, NULL, 1, this_range );
00186         std::copy( this_range.begin(), this_range.end(), std::back_inserter( blocks ) );
00187         this_range.clear();
00188         mdbImpl->get_entities_by_type_and_tag( 0, MBENTITYSET, &mDirichletSetTag, NULL, 1, this_range );
00189         std::copy( this_range.begin(), this_range.end(), std::back_inserter( nodesets ) );
00190         this_range.clear();
00191         mdbImpl->get_entities_by_type_and_tag( 0, MBENTITYSET, &mNeumannSetTag, NULL, 1, this_range );
00192         std::copy( this_range.begin(), this_range.end(), std::back_inserter( sidesets ) );
00193 
00194         // If there is nothing to write, write everything as one block.
00195         if( blocks.empty() && nodesets.empty() && sidesets.empty() )
00196         {
00197             this_range.clear();
00198             for( int d = user_dimension; d > 0 && this_range.empty(); --d )
00199                 mdbImpl->get_entities_by_dimension( 0, d, this_range, false );
00200             if( this_range.empty() ) return MB_FILE_WRITE_ERROR;
00201 
00202             EntityHandle block_handle;
00203             int block_id = 1;
00204             mdbImpl->create_meshset( MESHSET_SET, block_handle );
00205             mdbImpl->tag_set_data( mMaterialSetTag, &block_handle, 1, &block_id );
00206             mdbImpl->add_entities( block_handle, this_range );
00207             blocks.push_back( block_handle );
00208         }
00209     }
00210     else
00211     {
00212         int dummy;
00213         for( const EntityHandle* iter = ent_handles; iter < ent_handles + num_sets; ++iter )
00214         {
00215             if( MB_SUCCESS == mdbImpl->tag_get_data( mMaterialSetTag, &( *iter ), 1, &dummy ) && -1 != dummy )
00216                 blocks.push_back( *iter );
00217             else if( MB_SUCCESS == mdbImpl->tag_get_data( mDirichletSetTag, &( *iter ), 1, &dummy ) && -1 != dummy )
00218                 nodesets.push_back( *iter );
00219             else if( MB_SUCCESS == mdbImpl->tag_get_data( mNeumannSetTag, &( *iter ), 1, &dummy ) && -1 != dummy )
00220                 sidesets.push_back( *iter );
00221         }
00222     }
00223 
00224     // If there is nothing to write just return.
00225     if( blocks.empty() && nodesets.empty() && sidesets.empty() ) return MB_FILE_WRITE_ERROR;
00226 
00227     // Try to get mesh information
00228     ExodusMeshInfo mesh_info;
00229 
00230     std::vector< MaterialSetData > block_info;
00231     std::vector< NeumannSetData > sideset_info;
00232     std::vector< DirichletSetData > nodeset_info;
00233 
00234     mesh_info.num_dim = user_dimension;
00235 
00236     if( qa_records.empty() )
00237     {
00238         // qa records are empty - initialize some MB-standard ones
00239         mesh_info.qaRecords.push_back( "MB" );
00240         mesh_info.qaRecords.push_back( "0.99" );
00241         char string1[80], string2[80];
00242         time_and_date( string2, string1 );
00243         mesh_info.qaRecords.push_back( string2 );
00244         mesh_info.qaRecords.push_back( string1 );
00245     }
00246     else
00247     {
00248         // Constrained to multiples of 4 qa records
00249         assert( qa_records.size() % 4 == 0 );
00250 
00251         std::copy( qa_records.begin(), qa_records.end(), std::back_inserter( mesh_info.qaRecords ) );
00252     }
00253 
00254     block_info.clear();
00255     if( gather_mesh_information( mesh_info, block_info, sideset_info, nodeset_info, blocks, sidesets, nodesets ) !=
00256         MB_SUCCESS )
00257     {
00258         reset_block( block_info );
00259         return MB_FAILURE;
00260     }
00261 
00262     // Try to open the file after gather mesh info succeeds
00263     int fail = nc_create( exodus_file_name, overwrite ? NC_CLOBBER : NC_NOCLOBBER, &ncFile );
00264     if( NC_NOERR != fail )
00265     {
00266         reset_block( block_info );
00267         return MB_FAILURE;
00268     }
00269 
00270     if( write_header( mesh_info, block_info, sideset_info, nodeset_info, exodus_file_name ) != MB_SUCCESS )
00271     {
00272         reset_block( block_info );
00273         return MB_FAILURE;
00274     }
00275 
00276     {
00277         // write dummy time_whole
00278         double timev = 0.0;  // dummy, to make paraview happy
00279         size_t start = 0, count = 1;
00280         int nc_var;
00281         std::vector< int > dims;
00282         GET_VAR( "time_whole", nc_var, dims );
00283         fail = nc_put_vara_double( ncFile, nc_var, &start, &count, &timev );
00284         if( NC_NOERR != fail ) { MB_SET_ERR( MB_FAILURE, "Failed writing dist factor array" ); }
00285     }
00286 
00287     if( write_nodes( mesh_info.num_nodes, mesh_info.nodes, mesh_info.num_dim ) != MB_SUCCESS )
00288     {
00289         reset_block( block_info );
00290         return MB_FAILURE;
00291     }
00292 
00293     if( !mesh_info.polyhedronFaces.empty() )
00294     {
00295         if( write_poly_faces( mesh_info ) != MB_SUCCESS )
00296         {
00297             reset_block( block_info );
00298             return MB_FAILURE;
00299         }
00300     }
00301 
00302     if( write_elementblocks( mesh_info, block_info ) )
00303     {
00304         reset_block( block_info );
00305         return MB_FAILURE;
00306     }
00307 
00308     // Write the three maps
00309     if( write_global_node_order_map( mesh_info.num_nodes, mesh_info.nodes ) != MB_SUCCESS )
00310     {
00311         reset_block( block_info );
00312         return MB_FAILURE;
00313     }
00314 
00315     if( write_global_element_order_map( mesh_info.num_elements ) != MB_SUCCESS )
00316     {
00317         reset_block( block_info );
00318         return MB_FAILURE;
00319     }
00320 
00321     if( write_element_order_map( mesh_info.num_elements ) != MB_SUCCESS )
00322     {
00323         reset_block( block_info );
00324         return MB_FAILURE;
00325     }
00326 
00327     /*
00328      if (write_elementmap(mesh_info) != MB_SUCCESS)
00329        return MB_FAILURE;
00330     */
00331 
00332     if( write_BCs( sideset_info, nodeset_info ) != MB_SUCCESS )
00333     {
00334         reset_block( block_info );
00335         return MB_FAILURE;
00336     }
00337 
00338     if( write_qa_records( mesh_info.qaRecords ) != MB_SUCCESS ) return MB_FAILURE;
00339 
00340     // Copy the qa records into the argument
00341     // mesh_info.qaRecords.swap(qa_records);
00342     // Close the file
00343     fail = nc_close( ncFile );
00344     if( NC_NOERR != fail ) { MB_SET_ERR( MB_FAILURE, "Trouble closing file" ); }
00345 
00346     return MB_SUCCESS;
00347 }
00348 
00349 ErrorCode WriteNCDF::gather_mesh_information( ExodusMeshInfo& mesh_info, std::vector< MaterialSetData >& block_info,
00350                                               std::vector< NeumannSetData >& sideset_info,
00351                                               std::vector< DirichletSetData >& nodeset_info,
00352                                               std::vector< EntityHandle >& blocks,
00353                                               std::vector< EntityHandle >& sidesets,
00354                                               std::vector< EntityHandle >& nodesets )
00355 {
00356     ErrorCode rval;
00357     std::vector< EntityHandle >::iterator vector_iter, end_vector_iter;
00358 
00359     mesh_info.num_nodes            = 0;
00360     mesh_info.num_elements         = 0;
00361     mesh_info.num_elementblocks    = 0;
00362     mesh_info.num_polyhedra_blocks = 0;
00363 
00364     int id = 0;
00365 
00366     vector_iter     = blocks.begin();
00367     end_vector_iter = blocks.end();
00368 
00369     std::vector< EntityHandle > parent_meshsets;
00370 
00371     // Clean out the bits for the element mark
00372     rval = mdbImpl->tag_delete( mEntityMark );
00373     if( MB_SUCCESS != rval ) return rval;
00374     rval = mdbImpl->tag_get_handle( "WriteNCDF element mark", 1, MB_TYPE_BIT, mEntityMark, MB_TAG_CREAT );
00375     if( MB_SUCCESS != rval ) return rval;
00376 
00377     int highest_dimension_of_element_blocks = 0;
00378 
00379     for( vector_iter = blocks.begin(); vector_iter != blocks.end(); ++vector_iter )
00380     {
00381         MaterialSetData block_data;
00382 
00383         // For the purpose of qa records, get the parents of these blocks
00384         if( mdbImpl->get_parent_meshsets( *vector_iter, parent_meshsets ) != MB_SUCCESS ) return MB_FAILURE;
00385 
00386         // Get all Entity Handles in the mesh set
00387         Range dummy_range;
00388         rval = mdbImpl->get_entities_by_handle( *vector_iter, dummy_range, true );
00389         if( MB_SUCCESS != rval ) return rval;
00390 
00391         // Skip empty blocks
00392         if( dummy_range.empty() ) continue;
00393 
00394         // Get the block's id
00395         if( mdbImpl->tag_get_data( mMaterialSetTag, &( *vector_iter ), 1, &id ) != MB_SUCCESS )
00396         { MB_SET_ERR( MB_FAILURE, "Couldn't get block id from a tag for an element block" ); }
00397 
00398         block_data.id                = id;
00399         block_data.number_attributes = 0;
00400 
00401         // Wait a minute, we are doing some filtering here that doesn't make sense at this level CJS
00402 
00403         // Find the dimension of the last entity in this range
00404         int this_dim = CN::Dimension( TYPE_FROM_HANDLE( dummy_range.back() ) );
00405         if( this_dim > 3 ) { MB_SET_ERR( MB_TYPE_OUT_OF_RANGE, "Block " << id << " contains entity sets" ); }
00406         block_data.elements = dummy_range.subset_by_dimension( this_dim );
00407 
00408         // End of -- wait a minute, we are doing some filtering here that doesn't make sense at this
00409         // level CJS
00410 
00411         // Get the entity type for this block, verifying that it's the same for all elements
00412         EntityType entity_type = TYPE_FROM_HANDLE( block_data.elements.front() );
00413         if( !block_data.elements.all_of_type( entity_type ) )
00414         { MB_SET_ERR( MB_FAILURE, "Entities in block " << id << " not of common type" ); }
00415 
00416         int dimension = -1;
00417         if( entity_type == MBQUAD || entity_type == MBTRI )
00418             dimension = 2;  // Output shells by default
00419         else if( entity_type == MBEDGE )
00420             dimension = 1;
00421         else
00422             dimension = CN::Dimension( entity_type );
00423 
00424         if( dimension > highest_dimension_of_element_blocks ) highest_dimension_of_element_blocks = dimension;
00425 
00426         std::vector< EntityHandle > tmp_conn;
00427         rval = mdbImpl->get_connectivity( &( block_data.elements.front() ), 1, tmp_conn );
00428         if( MB_SUCCESS != rval ) return rval;
00429         block_data.element_type = ExoIIUtil::get_element_type_from_num_verts( tmp_conn.size(), entity_type, dimension );
00430 
00431         if( block_data.element_type == EXOII_MAX_ELEM_TYPE )
00432         { MB_SET_ERR( MB_FAILURE, "Element type in block " << id << " didn't get set correctly" ); }
00433 
00434         if( block_data.element_type == EXOII_POLYGON )
00435         {
00436             // get all poly connectivity
00437             int numconn = 0;
00438             for( Range::iterator eit = block_data.elements.begin(); eit != block_data.elements.end(); eit++ )
00439             {
00440                 EntityHandle polg        = *eit;
00441                 int nnodes               = 0;
00442                 const EntityHandle* conn = NULL;
00443                 rval                     = mdbImpl->get_connectivity( polg, conn, nnodes );MB_CHK_ERR( rval );
00444                 numconn += nnodes;
00445             }
00446             block_data.number_nodes_per_element = numconn;
00447         }
00448         else
00449             block_data.number_nodes_per_element = ExoIIUtil::VerticesPerElement[block_data.element_type];
00450 
00451         // Number of nodes for this block
00452         block_data.number_elements = block_data.elements.size();
00453 
00454         // Total number of elements
00455         mesh_info.num_elements += block_data.number_elements;
00456 
00457         // Get the nodes for the elements
00458         rval = mWriteIface->gather_nodes_from_elements( block_data.elements, mEntityMark, mesh_info.nodes );
00459         if( MB_SUCCESS != rval ) return rval;
00460 
00461         // if polyhedra block
00462         if( EXOII_POLYHEDRON == block_data.element_type )
00463         {
00464             rval = mdbImpl->get_connectivity( block_data.elements, mesh_info.polyhedronFaces );MB_CHK_ERR( rval );
00465             mesh_info.num_polyhedra_blocks++;
00466         }
00467 
00468         if( !sidesets.empty() )
00469         {
00470             // If there are sidesets, keep track of which elements are being written out
00471             for( Range::iterator iter = block_data.elements.begin(); iter != block_data.elements.end(); ++iter )
00472             {
00473                 unsigned char bit = 0x1;
00474                 rval              = mdbImpl->tag_set_data( mEntityMark, &( *iter ), 1, &bit );
00475                 if( MB_SUCCESS != rval ) return rval;
00476             }
00477         }
00478 
00479         block_info.push_back( block_data );
00480 
00481         const void* data = NULL;
00482         int size         = 0;
00483         if( MB_SUCCESS == mdbImpl->tag_get_by_ptr( mQaRecordTag, &( *vector_iter ), 1, &data, &size ) && NULL != data )
00484         {
00485             // There are qa records on this block - copy over to mesh qa records
00486             const char* qa_rec = static_cast< const char* >( data );
00487             int start          = 0;
00488             int count          = 0;
00489             for( int i = 0; i < size; i++ )
00490             {
00491                 if( qa_rec[i] == '\0' )
00492                 {
00493                     std::string qa_string( &qa_rec[start], i - start );
00494                     mesh_info.qaRecords.push_back( qa_string );
00495                     start = i + 1;
00496                     count++;
00497                 }
00498             }
00499 
00500             // Constrained to multiples of 4 qa records
00501             if( count > 0 ) assert( count % 4 == 0 );
00502         }
00503     }
00504 
00505     mesh_info.num_elementblocks = block_info.size();
00506 
00507     for( std::vector< MaterialSetData >::iterator blit = block_info.begin(); blit != block_info.end(); blit++ )
00508     {
00509         MaterialSetData& block = *blit;
00510         if( block.element_type != EXOII_POLYHEDRON )
00511         { mesh_info.polyhedronFaces = subtract( mesh_info.polyhedronFaces, block.elements ); }
00512     }
00513 
00514     // If user hasn't entered dimension, we figure it out
00515     if( mesh_info.num_dim == 0 )
00516     {
00517         // Never want 1 or zero dimensions
00518         if( highest_dimension_of_element_blocks < 2 )
00519             mesh_info.num_dim = 3;
00520         else
00521             mesh_info.num_dim = highest_dimension_of_element_blocks;
00522     }
00523 
00524     Range::iterator range_iter, end_range_iter;
00525     range_iter     = mesh_info.nodes.begin();
00526     end_range_iter = mesh_info.nodes.end();
00527 
00528     mesh_info.num_nodes = mesh_info.nodes.size();
00529 
00530     //------nodesets--------
00531 
00532     vector_iter     = nodesets.begin();
00533     end_vector_iter = nodesets.end();
00534 
00535     for( ; vector_iter != end_vector_iter; ++vector_iter )
00536     {
00537         DirichletSetData nodeset_data;
00538         nodeset_data.id           = 0;
00539         nodeset_data.number_nodes = 0;
00540 
00541         // Get the nodeset's id
00542         if( mdbImpl->tag_get_data( mDirichletSetTag, &( *vector_iter ), 1, &id ) != MB_SUCCESS )
00543         { MB_SET_ERR( MB_FAILURE, "Couldn't get id tag for nodeset " << id ); }
00544 
00545         nodeset_data.id = id;
00546 
00547         std::vector< EntityHandle > node_vector;
00548         // Get the nodes of the nodeset that are in mesh_info.nodes
00549         if( mdbImpl->get_entities_by_handle( *vector_iter, node_vector, true ) != MB_SUCCESS )
00550         { MB_SET_ERR( MB_FAILURE, "Couldn't get nodes in nodeset " << id ); }
00551 
00552         // Get the tag for distribution factors
00553         const double* dist_factor_vector;
00554         int dist_factor_size;
00555         const void* ptr = 0;
00556 
00557         int has_dist_factors = 0;
00558         if( mdbImpl->tag_get_by_ptr( mDistFactorTag, &( *vector_iter ), 1, &ptr, &dist_factor_size ) == MB_SUCCESS &&
00559             dist_factor_size )
00560             has_dist_factors = 1;
00561         dist_factor_size /= sizeof( double );
00562         dist_factor_vector = reinterpret_cast< const double* >( ptr );
00563         std::vector< EntityHandle >::iterator iter, end_iter;
00564         iter     = node_vector.begin();
00565         end_iter = node_vector.end();
00566 
00567         int j                     = 0;
00568         unsigned char node_marked = 0;
00569         ErrorCode result;
00570         for( ; iter != end_iter; ++iter )
00571         {
00572             if( TYPE_FROM_HANDLE( *iter ) != MBVERTEX ) continue;
00573             result = mdbImpl->tag_get_data( mEntityMark, &( *iter ), 1, &node_marked );MB_CHK_SET_ERR( result, "Couldn't get mark data" );
00574 
00575             if( 0x1 == node_marked )
00576             {
00577                 nodeset_data.nodes.push_back( *iter );
00578                 if( 0 != has_dist_factors )
00579                     nodeset_data.node_dist_factors.push_back( dist_factor_vector[j] );
00580                 else
00581                     nodeset_data.node_dist_factors.push_back( 1.0 );
00582             }
00583             j++;
00584         }
00585 
00586         nodeset_data.number_nodes = nodeset_data.nodes.size();
00587         nodeset_info.push_back( nodeset_data );
00588     }
00589 
00590     //------sidesets--------
00591     vector_iter     = sidesets.begin();
00592     end_vector_iter = sidesets.end();
00593 
00594     for( ; vector_iter != end_vector_iter; ++vector_iter )
00595     {
00596         NeumannSetData sideset_data;
00597 
00598         // Get the sideset's id
00599         if( mdbImpl->tag_get_data( mNeumannSetTag, &( *vector_iter ), 1, &id ) != MB_SUCCESS ) return MB_FAILURE;
00600 
00601         sideset_data.id              = id;
00602         sideset_data.mesh_set_handle = *vector_iter;
00603 
00604         // Get the sides in two lists, one forward the other reverse; starts with forward sense
00605         // by convention
00606         Range forward_elems, reverse_elems;
00607         if( get_sideset_elems( *vector_iter, 0, forward_elems, reverse_elems ) == MB_FAILURE ) return MB_FAILURE;
00608 
00609         ErrorCode result = get_valid_sides( forward_elems, mesh_info, 1, sideset_data );MB_CHK_SET_ERR( result, "Couldn't get valid sides data" );
00610         result = get_valid_sides( reverse_elems, mesh_info, -1, sideset_data );MB_CHK_SET_ERR( result, "Couldn't get valid sides data" );
00611 
00612         sideset_data.number_elements = sideset_data.elements.size();
00613         sideset_info.push_back( sideset_data );
00614     }
00615 
00616     return MB_SUCCESS;
00617 }
00618 
00619 ErrorCode WriteNCDF::get_valid_sides( Range& elems, ExodusMeshInfo& /*mesh_info*/, const int sense,
00620                                       NeumannSetData& sideset_data )
00621 {
00622     // This is where we see if underlying element of side set element is included in output
00623 
00624     // Get the sideset-based info for distribution factors
00625     const double* dist_factor_vector = 0;
00626     int dist_factor_size             = 0;
00627 
00628     // Initialize dist_fac_iter to get rid of compiler warning
00629     const double* dist_fac_iter = 0;
00630     const void* ptr             = 0;
00631     bool has_dist_factors       = false;
00632     if( mdbImpl->tag_get_by_ptr( mDistFactorTag, &( sideset_data.mesh_set_handle ), 1, &ptr, &dist_factor_size ) ==
00633             MB_SUCCESS &&
00634         dist_factor_size )
00635     {
00636         has_dist_factors   = true;
00637         dist_factor_vector = reinterpret_cast< const double* >( ptr );
00638         dist_fac_iter      = dist_factor_vector;
00639         dist_factor_size /= sizeof( double );
00640     }
00641 
00642     unsigned char element_marked = 0;
00643     ErrorCode result;
00644     for( Range::iterator iter = elems.begin(); iter != elems.end(); ++iter )
00645     {
00646         // Should insert here if "side" is a quad/tri on a quad/tri mesh
00647         result = mdbImpl->tag_get_data( mEntityMark, &( *iter ), 1, &element_marked );MB_CHK_SET_ERR( result, "Couldn't get mark data" );
00648 
00649         if( 0x1 == element_marked )
00650         {
00651             sideset_data.elements.push_back( *iter );
00652 
00653             // TJT TODO: the sense should really be # edges + 1or2
00654             sideset_data.side_numbers.push_back( ( sense == 1 ? 1 : 2 ) );
00655         }
00656         else
00657         {  // Then "side" is probably a quad/tri on a hex/tet mesh
00658             std::vector< EntityHandle > parents;
00659             int dimension = CN::Dimension( TYPE_FROM_HANDLE( *iter ) );
00660 
00661             // Get the adjacent parent element of "side"
00662             if( mdbImpl->get_adjacencies( &( *iter ), 1, dimension + 1, false, parents ) != MB_SUCCESS )
00663             {
00664 #if 0
00665         // This is not treated as an error, print warning messages for
00666         // debugging only
00667         fprintf(stderr, "[Warning]: Couldn't get adjacencies for sideset.\n");
00668 #endif
00669             }
00670 
00671             if( !parents.empty() )
00672             {
00673                 // Make sure the adjacent parent element will be output
00674                 for( unsigned int k = 0; k < parents.size(); k++ )
00675                 {
00676                     result = mdbImpl->tag_get_data( mEntityMark, &( parents[k] ), 1, &element_marked );MB_CHK_SET_ERR( result, "Couldn't get mark data" );
00677 
00678                     int side_no, this_sense, this_offset;
00679                     if( 0x1 == element_marked &&
00680                         mdbImpl->side_number( parents[k], *iter, side_no, this_sense, this_offset ) == MB_SUCCESS &&
00681                         this_sense == sense )
00682                     {
00683                         sideset_data.elements.push_back( parents[k] );
00684                         sideset_data.side_numbers.push_back( side_no + 1 );
00685                         break;
00686                     }
00687                 }
00688             }
00689             else
00690             {
00691 #if 0
00692         // This is not treated as an error, print warning messages for
00693         // debugging only
00694         fprintf(stderr, "[Warning]: No parent element exists for element in sideset %i\n", sideset_data.id);
00695 #endif
00696             }
00697         }
00698 
00699         if( sideset_data.elements.size() != 0 )
00700         {
00701             // Distribution factors
00702             int num_nodes = CN::VerticesPerEntity( TYPE_FROM_HANDLE( *iter ) );
00703             // put some dummy dist factors for polygons; why do we need them?
00704             if( TYPE_FROM_HANDLE( *iter ) == MBPOLYGON ) num_nodes = 1;  // dummy
00705             if( has_dist_factors )
00706             {
00707                 std::copy( dist_fac_iter, dist_fac_iter + num_nodes,
00708                            std::back_inserter( sideset_data.ss_dist_factors ) );
00709                 dist_fac_iter += num_nodes;
00710             }
00711             else
00712             {
00713                 for( int j = 0; j < num_nodes; j++ )
00714                     sideset_data.ss_dist_factors.push_back( 1.0 );
00715             }
00716         }
00717     }
00718 
00719     return MB_SUCCESS;
00720 }
00721 
00722 ErrorCode WriteNCDF::write_qa_records( std::vector< std::string >& qa_record_list )
00723 {
00724     int i = 0;
00725 
00726     for( std::vector< std::string >::iterator string_it = qa_record_list.begin(); string_it != qa_record_list.end(); )
00727     {
00728         for( int j = 0; j < 4; j++ )
00729             write_qa_string( ( *string_it++ ).c_str(), i, j );
00730         i++;
00731     }
00732 
00733     return MB_SUCCESS;
00734 }
00735 
00736 ErrorCode WriteNCDF::write_qa_string( const char* string, int record_number, int record_position )
00737 {
00738     // Get the variable id in the exodus file
00739 
00740     std::vector< int > dims;
00741     int temp_var = -1;
00742     GET_VAR( "qa_records", temp_var, dims );
00743     if( -1 == temp_var ) { MB_SET_ERR( MB_FAILURE, "WriteNCDF:: Problem getting qa record variable" ); }
00744     size_t count[3], start[3];
00745 
00746     // Write out the record
00747     start[0] = record_number;
00748     start[1] = record_position;
00749     start[2] = 0;
00750 
00751     count[0] = 1;
00752     count[1] = 1;
00753     count[2] = (long)strlen( string ) + 1;
00754     int fail = nc_put_vara_text( ncFile, temp_var, start, count, string );
00755     if( NC_NOERR != fail ) { MB_SET_ERR( MB_FAILURE, "Failed to position qa string variable" ); }
00756 
00757     return MB_SUCCESS;
00758 }
00759 
00760 ErrorCode WriteNCDF::write_nodes( int num_nodes, Range& nodes, int dimension )
00761 {
00762     // Write coordinates names
00763     int nc_var = -1;
00764     std::vector< int > dims;
00765     GET_VAR( "coor_names", nc_var, dims );
00766     if( -1 == nc_var ) { MB_SET_ERR( MB_FAILURE, "Trouble getting coordinate name variable" ); }
00767 
00768     size_t start[2] = { 0, 0 }, count[2] = { 1, ExoIIInterface::MAX_STR_LENGTH };
00769     char dum_str[ExoIIInterface::MAX_STR_LENGTH];
00770     strcpy( dum_str, "x" );
00771     int fail = nc_put_vara_text( ncFile, nc_var, start, count, dum_str );
00772     if( NC_NOERR != fail )
00773     { MB_SET_ERR( MB_FAILURE, "Trouble adding x coordinate name; netcdf message: " << nc_strerror( fail ) ); }
00774 
00775     start[0] = 1;
00776     strcpy( dum_str, "y" );
00777     fail = nc_put_vara_text( ncFile, nc_var, start, count, dum_str );
00778     if( NC_NOERR != fail )
00779     { MB_SET_ERR( MB_FAILURE, "Trouble adding y coordinate name; netcdf message: " << nc_strerror( fail ) ); }
00780 
00781     start[0] = 2;
00782     strcpy( dum_str, "z" );
00783     fail = nc_put_vara_text( ncFile, nc_var, start, count, dum_str );
00784     if( NC_NOERR != fail )
00785     { MB_SET_ERR( MB_FAILURE, "Trouble adding z coordinate name; netcdf message: " << nc_strerror( fail ) ); }
00786 
00787     // See if should transform coordinates
00788     ErrorCode result;
00789     Tag trans_tag;
00790     result                = mdbImpl->tag_get_handle( MESH_TRANSFORM_TAG_NAME, 16, MB_TYPE_DOUBLE, trans_tag );
00791     bool transform_needed = true;
00792     if( result == MB_TAG_NOT_FOUND ) transform_needed = false;
00793 
00794     int num_coords_to_fill = transform_needed ? 3 : dimension;
00795 
00796     std::vector< double* > coord_arrays( 3 );
00797     coord_arrays[0] = new double[num_nodes];
00798     coord_arrays[1] = new double[num_nodes];
00799     coord_arrays[2] = NULL;
00800 
00801     if( num_coords_to_fill == 3 ) coord_arrays[2] = new double[num_nodes];
00802 
00803     result = mWriteIface->get_node_coords( dimension, num_nodes, nodes, mGlobalIdTag, 1, coord_arrays );
00804     if( result != MB_SUCCESS )
00805     {
00806         delete[] coord_arrays[0];
00807         delete[] coord_arrays[1];
00808         if( coord_arrays[2] ) delete[] coord_arrays[2];
00809         return result;
00810     }
00811 
00812     if( transform_needed )
00813     {
00814         double trans_matrix[16];
00815         const EntityHandle mesh = 0;
00816         result                  = mdbImpl->tag_get_data( trans_tag, &mesh, 0, trans_matrix );MB_CHK_SET_ERR( result, "Couldn't get transform data" );
00817 
00818         for( int i = 0; i < num_nodes; i++ )
00819         {
00820             double vec1[3];
00821             double vec2[3];
00822 
00823             vec2[0] = coord_arrays[0][i];
00824             vec2[1] = coord_arrays[1][i];
00825             vec2[2] = coord_arrays[2][i];
00826 
00827             for( int row = 0; row < 3; row++ )
00828             {
00829                 vec1[row] = 0.0;
00830                 for( int col = 0; col < 3; col++ )
00831                     vec1[row] += ( trans_matrix[( row * 4 ) + col] * vec2[col] );
00832             }
00833 
00834             coord_arrays[0][i] = vec1[0];
00835             coord_arrays[1][i] = vec1[1];
00836             coord_arrays[2][i] = vec1[2];
00837         }
00838     }
00839 
00840     // Write the nodes
00841     nc_var = -1;
00842     GET_VAR( "coord", nc_var, dims );
00843     if( -1 == nc_var ) { MB_SET_ERR( MB_FAILURE, "Trouble getting coordinate variable" ); }
00844     start[0] = 0;
00845     count[1] = num_nodes;
00846     fail     = nc_put_vara_double( ncFile, nc_var, start, count, &( coord_arrays[0][0] ) );
00847     if( NC_NOERR != fail ) { MB_SET_ERR( MB_FAILURE, "Trouble writing x coordinate" ); }
00848 
00849     start[0] = 1;
00850     fail     = nc_put_vara_double( ncFile, nc_var, start, count, &( coord_arrays[1][0] ) );
00851     if( NC_NOERR != fail ) { MB_SET_ERR( MB_FAILURE, "Trouble writing y coordinate" ); }
00852 
00853     start[0] = 2;
00854     fail     = nc_put_vara_double( ncFile, nc_var, start, count, &( coord_arrays[2][0] ) );
00855     if( NC_NOERR != fail ) { MB_SET_ERR( MB_FAILURE, "Trouble writing z coordinate" ); }
00856 
00857     delete[] coord_arrays[0];
00858     delete[] coord_arrays[1];
00859     if( coord_arrays[2] ) delete[] coord_arrays[2];
00860 
00861     return MB_SUCCESS;
00862 }
00863 
00864 ErrorCode WriteNCDF::write_poly_faces( ExodusMeshInfo& mesh_info )
00865 {
00866     // write all polygons that are not in another element block;
00867     // usually they are nowhere else, but be sure, write in this block only ones that are not in the
00868     // other blocks
00869     Range pfaces = mesh_info.polyhedronFaces;
00870 
00871     /*
00872      * int fbconn1(num_nod_per_fa1) ;
00873                 fbconn1:elem_type = "nsided" ;
00874         int fbepecnt1(num_fa_in_blk1) ;
00875                 fbepecnt1:entity_type1 = "NODE" ;
00876                 fbepecnt1:entity_type2 = "FACE" ;
00877      */
00878     if( pfaces.empty() ) return MB_SUCCESS;
00879     char wname[80];
00880     int nc_var = -1;
00881     std::vector< int > dims;
00882 
00883     // write one for each element block, to make paraview and visit happy
00884     int num_faces_in_block = (int)pfaces.size();
00885     for( unsigned int bl = 0; bl < mesh_info.num_polyhedra_blocks; bl++ )
00886     {
00887         INS_ID( wname, "fbconn%u", bl + 1 );  // it is the first block
00888         GET_VAR( wname, nc_var, dims );       // fbconn# variable, 1 dimensional
00889 
00890         INS_ID( wname, "num_nod_per_fa%u", bl + 1 );
00891         int ncdim, num_nod_per_face;
00892         GET_DIM( ncdim, wname, num_nod_per_face );
00893         int* connectivity = new int[num_nod_per_face];
00894         int ixcon = 0, j = 0;
00895         std::vector< int > fbepe( num_faces_in_block );  // fbepecnt1
00896         for( Range::iterator eit = pfaces.begin(); eit != pfaces.end(); eit++ )
00897         {
00898             EntityHandle polyg       = *eit;
00899             int nnodes               = 0;
00900             const EntityHandle* conn = NULL;
00901             ErrorCode rval           = mdbImpl->get_connectivity( polyg, conn, nnodes );MB_CHK_ERR( rval );
00902             for( int k = 0; k < nnodes; k++ )
00903                 connectivity[ixcon++] = conn[k];
00904             fbepe[j++] = nnodes;
00905         }
00906         size_t start[1] = { 0 }, count[1] = { 0 };
00907         count[0] = ixcon;
00908         int fail = nc_put_vara_int( ncFile, nc_var, start, count, connectivity );
00909         if( NC_NOERR != fail )
00910         {
00911             delete[] connectivity;
00912             MB_SET_ERR( MB_FAILURE, "Couldn't write fbconn variable" );
00913         }
00914 
00915         INS_ID( wname, "fbepecnt%u", bl + 1 );
00916         GET_VAR( wname, nc_var, dims );  // fbconn# variable, 1 dimensional
00917         count[0] = num_faces_in_block;
00918 
00919         fail = nc_put_vara_int( ncFile, nc_var, start, count, &fbepe[0] );
00920         if( NC_NOERR != fail )
00921         {
00922             delete[] connectivity;
00923             MB_SET_ERR( MB_FAILURE, "Couldn't write fbepecnt variable" );
00924         }
00925 
00926         int id = bl + 1;
00927         if( write_exodus_integer_variable( "fa_prop1", &id, bl, 1 ) != MB_SUCCESS )
00928         { MB_SET_ERR_CONT( "Problem writing element block id " << id ); }
00929 
00930         int status = 1;
00931         if( write_exodus_integer_variable( "fa_status", &status, bl, 1 ) != MB_SUCCESS )
00932         { MB_SET_ERR( MB_FAILURE, "Problem writing face block status" ); }
00933 
00934         delete[] connectivity;
00935         if( 0 == repeat_face_blocks ) break;  // do not repeat face blocks
00936     }
00937 
00938     return MB_SUCCESS;
00939 }
00940 ErrorCode WriteNCDF::write_header( ExodusMeshInfo& mesh_info, std::vector< MaterialSetData >& block_info,
00941                                    std::vector< NeumannSetData >& sideset_info,
00942                                    std::vector< DirichletSetData >& nodeset_info, const char* filename )
00943 {
00944     // Get the date and time
00945     char time[TIME_STR_LEN];
00946     char date[TIME_STR_LEN];
00947     time_and_date( time, date );
00948 
00949     std::string title_string = "MOAB";
00950     title_string.append( "(" );
00951     title_string.append( filename );
00952     title_string.append( "): " );
00953     title_string.append( date );
00954     title_string.append( ": " );
00955     title_string.append( "time " );
00956 
00957     if( title_string.length() > ExoIIInterface::MAX_LINE_LENGTH )
00958         title_string.resize( ExoIIInterface::MAX_LINE_LENGTH );
00959 
00960     // Initialize the exodus file
00961 
00962     int result = initialize_exodus_file( mesh_info, block_info, sideset_info, nodeset_info, title_string.c_str() );
00963 
00964     if( result == MB_FAILURE ) return MB_FAILURE;
00965 
00966     return MB_SUCCESS;
00967 }
00968 
00969 ErrorCode WriteNCDF::write_elementblocks( ExodusMeshInfo& mesh_info, std::vector< MaterialSetData >& block_data )
00970 {
00971     unsigned int i;
00972     int block_index = 0;  // Index into block list, may != 1 if there are inactive blocks
00973     int exodus_id   = 1;
00974 
00975     for( i = 0; i < block_data.size(); i++ )
00976     {
00977         MaterialSetData& block = block_data[i];
00978 
00979         unsigned int num_nodes_per_elem = block.number_nodes_per_element;
00980 
00981         // Write out the id for the block
00982 
00983         int id         = block.id;
00984         int num_values = 1;
00985 
00986         if( write_exodus_integer_variable( "eb_prop1", &id, block_index, num_values ) != MB_SUCCESS )
00987         { MB_SET_ERR_CONT( "Problem writing element block id " << id ); }
00988 
00989         // Write out the block status
00990 
00991         int status = 1;
00992         if( 0 == block.number_elements ) { MB_SET_ERR( MB_FAILURE, "No elements in block " << id ); }
00993 
00994         if( write_exodus_integer_variable( "eb_status", &status, block_index, num_values ) != MB_SUCCESS )
00995         { MB_SET_ERR( MB_FAILURE, "Problem writing element block status" ); }
00996 
00997         //
00998         // Map the connectivity to the new nodes
00999         const unsigned int num_elem = block.number_elements;
01000         unsigned int num_nodes      = num_nodes_per_elem * num_elem;
01001         if( EXOII_POLYGON == block.element_type || EXOII_POLYHEDRON == block.element_type )
01002         { num_nodes = num_nodes_per_elem; }
01003         int* connectivity = new int[num_nodes];
01004 
01005         ErrorCode result = MB_SUCCESS;
01006         if( block.element_type != EXOII_POLYHEDRON )
01007             mWriteIface->get_element_connect( num_elem, num_nodes_per_elem, mGlobalIdTag, block.elements, mGlobalIdTag,
01008                                               exodus_id, connectivity );
01009         if( result != MB_SUCCESS )
01010         {
01011             delete[] connectivity;
01012             MB_SET_ERR( result, "Couldn't get element array to write from" );
01013         }
01014 
01015         // If necessary, convert from EXODUS to CN node order
01016         const EntityType elem_type = ExoIIUtil::ExoIIElementMBEntity[block.element_type];
01017         assert( block.elements.all_of_type( elem_type ) );
01018         const int* reorder = 0;
01019         if( block.element_type != EXOII_POLYHEDRON && block.element_type != EXOII_POLYGON )
01020             reorder = exodus_elem_order_map[elem_type][block.number_nodes_per_element];
01021         if( reorder )
01022             WriteUtilIface::reorder( reorder, connectivity, block.number_elements, block.number_nodes_per_element );
01023 
01024         char wname[80];
01025         int nc_var = -1;
01026         std::vector< int > dims;
01027         if( block.element_type != EXOII_POLYHEDRON )
01028         {
01029             exodus_id += num_elem;
01030             INS_ID( wname, "connect%u", i + 1 );
01031 
01032             GET_VAR( wname, nc_var, dims );
01033             if( -1 == nc_var )
01034             {
01035                 delete[] connectivity;
01036                 MB_SET_ERR( MB_FAILURE, "Couldn't get connectivity variable" );
01037             }
01038         }
01039 
01040         if( EXOII_POLYGON == block.element_type )
01041         {
01042             size_t start[1] = { 0 }, count[1] = { num_nodes_per_elem };
01043             int fail = nc_put_vara_int( ncFile, nc_var, start, count, connectivity );
01044             if( NC_NOERR != fail )
01045             {
01046                 delete[] connectivity;
01047                 MB_SET_ERR( MB_FAILURE, "Couldn't write connectivity variable" );
01048             }
01049             // now put also number ebepecnt1
01050             INS_ID( wname, "ebepecnt%u", i + 1 );
01051             GET_VAR( wname, nc_var, dims );
01052             count[0] = block.number_elements;
01053             start[0] = 0;
01054             // reuse connectivity array, to not allocate another one
01055             int j = 0;
01056             for( Range::iterator eit = block.elements.begin(); eit != block.elements.end(); j++, eit++ )
01057             {
01058                 EntityHandle polg        = *eit;
01059                 int nnodes               = 0;
01060                 const EntityHandle* conn = NULL;
01061                 ErrorCode rval           = mdbImpl->get_connectivity( polg, conn, nnodes );MB_CHK_ERR( rval );
01062                 connectivity[j] = nnodes;
01063             }
01064             fail = nc_put_vara_int( ncFile, nc_var, start, count, connectivity );
01065             if( NC_NOERR != fail )
01066             {
01067                 delete[] connectivity;
01068                 MB_SET_ERR( MB_FAILURE, "Couldn't write ebepecnt variable" );
01069             }
01070         }
01071         else if( block.element_type != EXOII_POLYHEDRON )
01072         {
01073             size_t start[2] = { 0, 0 }, count[2] = { num_elem, num_nodes_per_elem };
01074             int fail = nc_put_vara_int( ncFile, nc_var, start, count, connectivity );
01075             if( NC_NOERR != fail )
01076             {
01077                 delete[] connectivity;
01078                 MB_SET_ERR( MB_FAILURE, "Couldn't write connectivity variable" );
01079             }
01080         }
01081         else  // if (block.element_type == EXOII_POLYHEDRON)
01082         {
01083             /* write a lot of stuff // faconn
01084             num_fa_in_blk1 = 15 ;
01085             num_nod_per_fa1 = 58 ;
01086             num_el_in_blk1 = 3 ;
01087             num_fac_per_el1 = 17 ;
01088             int fbconn1(num_nod_per_fa1) ;
01089                     fbconn1:elem_type = "nsided" ;
01090             int fbepecnt1(num_fa_in_blk1) ;
01091                     fbepecnt1:entity_type1 = "NODE" ;
01092                     fbepecnt1:entity_type2 = "FACE" ;
01093             int facconn1(num_fac_per_el1) ;
01094                     facconn1:elem_type = "NFACED" ;
01095             int ebepecnt1(num_el_in_blk1) ;
01096                     ebepecnt1:entity_type1 = "FACE" ;
01097                     ebepecnt1:entity_type2 = "ELEM" ;
01098 
01099              */
01100             Range& block_faces = mesh_info.polyhedronFaces;
01101             // ErrorCode rval = mdbImpl->get_connectivity(block.elements, block_faces);
01102             // MB_CHK_ERR(rval);
01103 
01104             // reuse now connectivity for facconn1
01105             INS_ID( wname, "facconn%u", i + 1 );
01106             GET_VAR( wname, nc_var, dims );  // fbconn# variable, 1 dimensional
01107 
01108             std::vector< int > ebepe( block.elements.size() );  // ebepecnt1
01109             int ixcon = 0, j = 0;
01110             size_t start[1] = { 0 }, count[1] = { 0 };
01111 
01112             for( Range::iterator eit = block.elements.begin(); eit != block.elements.end(); eit++ )
01113             {
01114                 EntityHandle polyh       = *eit;
01115                 int nfaces               = 0;
01116                 const EntityHandle* conn = NULL;
01117                 ErrorCode rval           = mdbImpl->get_connectivity( polyh, conn, nfaces );MB_CHK_ERR( rval );
01118                 for( int k = 0; k < nfaces; k++ )
01119                 {
01120                     int index = block_faces.index( conn[k] );
01121                     if( index == -1 ) MB_SET_ERR( MB_FAILURE, "Couldn't find face in polyhedron" );
01122                     connectivity[ixcon++] = index + 1;
01123                 }
01124                 ebepe[j++] = nfaces;
01125                 // num_faces+=nfaces;
01126             }
01127             count[0] = ixcon;  // facconn1
01128             int fail = nc_put_vara_int( ncFile, nc_var, start, count, connectivity );
01129             if( NC_NOERR != fail )
01130             {
01131                 delete[] connectivity;
01132                 MB_SET_ERR( MB_FAILURE, "Couldn't write fbconn variable" );
01133             }
01134 
01135             INS_ID( wname, "ebepecnt%u", i + 1 );
01136             GET_VAR( wname, nc_var, dims );  // ebepecnt# variable, 1 dimensional
01137             count[0] = block.elements.size();
01138 
01139             fail = nc_put_vara_int( ncFile, nc_var, start, count, &ebepe[0] );
01140             if( NC_NOERR != fail )
01141             {
01142                 delete[] connectivity;
01143                 MB_SET_ERR( MB_FAILURE, "Couldn't write fbepecnt variable" );
01144             }
01145         }
01146         block_index++;
01147         delete[] connectivity;
01148     }
01149 
01150     return MB_SUCCESS;
01151 }
01152 
01153 ErrorCode WriteNCDF::write_global_node_order_map( int num_nodes, Range& nodes )
01154 {
01155     // Note: this routine bypasses the standard exodusII interface for efficiency!
01156 
01157     // Node order map
01158     int* map = new int[num_nodes];
01159 
01160     // For now, output a dummy map!
01161 
01162     Range::iterator range_iter, end_iter;
01163     range_iter = nodes.begin();
01164     end_iter   = nodes.end();
01165 
01166     int i = 0;
01167 
01168     for( ; range_iter != end_iter; ++range_iter )
01169     {
01170         // TODO -- do we really want to cast this to an int?
01171         map[i++] = (int)ID_FROM_HANDLE( *range_iter );
01172     }
01173 
01174     // Output array and cleanup
01175 
01176     int error = write_exodus_integer_variable( "node_num_map", map, 0, num_nodes );
01177 
01178     if( map ) delete[] map;
01179 
01180     if( error < 0 ) { MB_SET_ERR( MB_FAILURE, "Failed writing global node order map" ); }
01181 
01182     return MB_SUCCESS;
01183 }
01184 
01185 ErrorCode WriteNCDF::write_global_element_order_map( int num_elements )
01186 {
01187     // Allocate map array
01188     int* map = new int[num_elements];
01189 
01190     // Many Sandia codes assume this map is unique, and CUBIT does not currently
01191     // have unique ids for all elements. Therefore, to make sure nothing crashes,
01192     // insert a dummy map...
01193 
01194     for( int i = 0; i < num_elements; i++ )
01195         map[i] = i + 1;
01196 
01197     // Output array and cleanup
01198 
01199     int error = write_exodus_integer_variable( "elem_num_map", map, 0, num_elements );
01200 
01201     if( map ) delete[] map;
01202 
01203     if( error < 0 ) { MB_SET_ERR( MB_FAILURE, "Failed writing global element order map" ); }
01204 
01205     return MB_SUCCESS;
01206 }
01207 
01208 ErrorCode WriteNCDF::write_element_order_map( int num_elements )
01209 {
01210     // Note: this routine bypasses the standard exodusII interface for efficiency!
01211 
01212     // Element order map
01213     int* map = new int[num_elements];
01214 
01215     // For now, output a dummy map!
01216 
01217     for( int i = 0; i < num_elements; i++ )
01218     {
01219         map[i] = i + 1;
01220     }
01221 
01222     // Output array and cleanup
01223 
01224     int error = write_exodus_integer_variable( "elem_map", map, 0, num_elements );
01225 
01226     if( map ) delete[] map;
01227 
01228     if( error < 0 ) { MB_SET_ERR( MB_FAILURE, "Failed writing element map" ); }
01229 
01230     return MB_SUCCESS;
01231 }
01232 
01233 ErrorCode WriteNCDF::write_exodus_integer_variable( const char* variable_name, int* variable_array, int start_position,
01234                                                     int number_values )
01235 {
01236     // Note: this routine bypasses the standard exodusII interface for efficiency!
01237 
01238     // Write directly to netcdf interface for efficiency
01239 
01240     // Get the variable id of the element map
01241     int nc_var = -1;
01242     std::vector< int > dims;
01243     GET_VAR( variable_name, nc_var, dims );
01244     if( -1 == nc_var )
01245     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to locate variable " << variable_name << " in file" ); }
01246     // This contortion is necessary because netCDF is expecting nclongs;
01247     // fortunately it's necessary only when ints and nclongs aren't the same size
01248 
01249     size_t start[1], count[1];
01250     start[0] = start_position;
01251     count[0] = number_values;
01252 
01253     int fail = NC_NOERR;
01254     if( sizeof( int ) == sizeof( long ) ) { fail = nc_put_vara_int( ncFile, nc_var, start, count, variable_array ); }
01255     else
01256     {
01257         long* lptr = new long[number_values];
01258         for( int jj = 0; jj < number_values; jj++ )
01259             lptr[jj] = variable_array[jj];
01260         fail = nc_put_vara_long( ncFile, nc_var, start, count, lptr );
01261         delete[] lptr;
01262     }
01263 
01264     if( NC_NOERR != fail ) { MB_SET_ERR( MB_FAILURE, "Failed to store variable " << variable_name ); }
01265 
01266     return MB_SUCCESS;
01267 }
01268 
01269 ErrorCode WriteNCDF::write_BCs( std::vector< NeumannSetData >& sidesets, std::vector< DirichletSetData >& nodesets )
01270 {
01271     unsigned int i, j;
01272     int id;
01273     int ns_index = -1;
01274 
01275     for( std::vector< DirichletSetData >::iterator ns_it = nodesets.begin(); ns_it != nodesets.end(); ++ns_it )
01276     {
01277         // Get number of nodes in set
01278         int number_nodes = ( *ns_it ).number_nodes;
01279         if( 0 == number_nodes ) continue;
01280 
01281         // If we're here, we have a non-empty nodeset; increment the index
01282         ns_index++;
01283 
01284         // Get the node set id
01285         id = ( *ns_it ).id;
01286 
01287         // Build new array to old exodus ids
01288         int* exodus_id_array      = new int[number_nodes];
01289         double* dist_factor_array = new double[number_nodes];
01290 
01291         std::vector< EntityHandle >::iterator begin_iter, end_iter;
01292         std::vector< double >::iterator other_iter;
01293         begin_iter = ( *ns_it ).nodes.begin();
01294         end_iter   = ( *ns_it ).nodes.end();
01295         other_iter = ( *ns_it ).node_dist_factors.begin();
01296 
01297         j = 0;
01298         int exodus_id;
01299         ErrorCode result;
01300         // Fill up node array and dist. factor array at the same time
01301         for( ; begin_iter != end_iter; ++begin_iter )
01302         {
01303             result = mdbImpl->tag_get_data( mGlobalIdTag, &( *begin_iter ), 1, &exodus_id );MB_CHK_SET_ERR( result, "Problem getting id tag data" );
01304 
01305             exodus_id_array[j]   = exodus_id;
01306             dist_factor_array[j] = *( other_iter );
01307             ++other_iter;
01308             j++;
01309         }
01310 
01311         // Write out the id for the nodeset
01312 
01313         int num_values = 1;
01314 
01315         result = write_exodus_integer_variable( "ns_prop1", &id, ns_index, num_values );MB_CHK_SET_ERR_RET_VAL( result, "Problem writing node set id " << id, MB_FAILURE );
01316 
01317         // Write out the nodeset status
01318 
01319         int status = 1;
01320         if( !number_nodes ) status = 0;
01321 
01322         result = write_exodus_integer_variable( "ns_status", &status, ns_index, num_values );MB_CHK_SET_ERR_RET_VAL( result, "Problem writing node set status", MB_FAILURE );
01323 
01324         // Write it out
01325         char wname[80];
01326         int nc_var = -1;
01327         std::vector< int > dims;
01328         INS_ID( wname, "node_ns%d", ns_index + 1 );
01329         GET_VAR( wname, nc_var, dims );
01330         if( -1 == nc_var ) { MB_SET_ERR( MB_FAILURE, "Failed to get node_ns variable" ); }
01331 
01332         size_t start = 0, count = number_nodes;
01333         int fail = nc_put_vara_int( ncFile, nc_var, &start, &count, exodus_id_array );
01334         if( NC_NOERR != fail ) { MB_SET_ERR( MB_FAILURE, "Failed writing exodus id array" ); }
01335 
01336         // Write out nodeset distribution factors
01337         INS_ID( wname, "dist_fact_ns%d", ns_index + 1 );
01338         nc_var = -1;
01339         GET_VAR( wname, nc_var, dims );
01340         if( -1 == nc_var ) { MB_SET_ERR( MB_FAILURE, "Failed to get dist_fact variable" ); }
01341         fail = nc_put_vara_double( ncFile, nc_var, &start, &count, dist_factor_array );
01342         if( NC_NOERR != fail ) { MB_SET_ERR( MB_FAILURE, "Failed writing dist factor array" ); }
01343 
01344         delete[] dist_factor_array;
01345         delete[] exodus_id_array;
01346     }
01347 
01348     // Now do sidesets
01349     int ss_index = 0;  // Index of sideset - not the same as 'i' because
01350                        // only writing non-empty side sets
01351     for( i = 0; i < sidesets.size(); i++ )
01352     {
01353         NeumannSetData sideset_data = sidesets[i];
01354 
01355         // Get the side set id
01356         int side_set_id = sideset_data.id;
01357 
01358         // Get number of elements in set
01359         int number_elements = sideset_data.number_elements;
01360         if( 0 == number_elements ) continue;
01361 
01362         // Build new array to old exodus ids
01363         int* output_element_ids          = new int[number_elements];
01364         int* output_element_side_numbers = new int[number_elements];
01365 
01366         std::vector< EntityHandle >::iterator begin_iter, end_iter;
01367         begin_iter                             = sideset_data.elements.begin();
01368         end_iter                               = sideset_data.elements.end();
01369         std::vector< int >::iterator side_iter = sideset_data.side_numbers.begin();
01370 
01371         // Get the tag handle
01372         j = 0;
01373         int exodus_id;
01374 
01375         // For each "side"
01376         for( ; begin_iter != end_iter; ++begin_iter, ++side_iter )
01377         {
01378             ErrorCode result = mdbImpl->tag_get_data( mGlobalIdTag, &( *begin_iter ), 1, &exodus_id );MB_CHK_SET_ERR( result, "Problem getting exodus id for sideset element "
01379                                         << (long unsigned int)ID_FROM_HANDLE( *begin_iter ) );
01380 
01381             output_element_ids[j]            = exodus_id;
01382             output_element_side_numbers[j++] = *side_iter;
01383         }
01384 
01385         if( 0 != number_elements )
01386         {
01387             // Write out the id for the nodeset
01388 
01389             int num_values = 1;
01390 
01391             // ss_prop1[ss_index] = side_set_id
01392             ErrorCode result = write_exodus_integer_variable( "ss_prop1", &side_set_id, ss_index, num_values );MB_CHK_SET_ERR_RET_VAL( result, "Problem writing node set id " << id, MB_FAILURE );
01393 
01394             // FIXME : Something seems wrong here.  The we are within a block
01395             // started with if (0 != number_elements), so this condition is always
01396             // false.  This code seems to indicate that we want to write all
01397             // sidesets, not just empty ones.  But the code both here and in
01398             // initialize_exodus_file() skip empty side sets.
01399             //  - j.k. 2007-03-09
01400             int status = 1;
01401             if( 0 == number_elements ) status = 0;
01402 
01403             // ss_status[ss_index] = status
01404             result = write_exodus_integer_variable( "ss_status", &status, ss_index, num_values );MB_CHK_SET_ERR_RET_VAL( result, "Problem writing side set status", MB_FAILURE );
01405 
01406             // Increment ss_index now because we want a) we need to
01407             // increment it somewhere within the if (0 != number_elements)
01408             // block and b) the above calls need a zero-based index
01409             // while the following use a one-based index.
01410             ++ss_index;
01411 
01412             char wname[80];
01413             int nc_var;
01414             std::vector< int > dims;
01415             INS_ID( wname, "elem_ss%d", ss_index );
01416             GET_VAR( wname, nc_var, dims );
01417             if( -1 == nc_var ) { MB_SET_ERR( MB_FAILURE, "Failed to get elem_ss variable" ); }
01418             size_t start = 0, count = number_elements;
01419             int fail = nc_put_vara_int( ncFile, nc_var, &start, &count, output_element_ids );
01420             if( NC_NOERR != fail ) { MB_SET_ERR( MB_FAILURE, "Failed writing sideset element array" ); }
01421 
01422             INS_ID( wname, "side_ss%d", ss_index );
01423             nc_var = -1;
01424             GET_VAR( wname, nc_var, dims );
01425             if( -1 == nc_var ) { MB_SET_ERR( MB_FAILURE, "Failed to get side_ss variable" ); }
01426             fail = nc_put_vara_int( ncFile, nc_var, &start, &count, output_element_side_numbers );
01427             if( NC_NOERR != fail ) { MB_SET_ERR( MB_FAILURE, "Failed writing sideset side array" ); }
01428 
01429             INS_ID( wname, "dist_fact_ss%d", ss_index );
01430             nc_var = -1;
01431             GET_VAR( wname, nc_var, dims );
01432             if( -1 == nc_var ) { MB_SET_ERR( MB_FAILURE, "Failed to get sideset dist factors variable" ); }
01433             count = sideset_data.ss_dist_factors.size();
01434             fail  = nc_put_vara_double( ncFile, nc_var, &start, &count, &( sideset_data.ss_dist_factors[0] ) );
01435             if( NC_NOERR != fail ) { MB_SET_ERR( MB_FAILURE, "Failed writing sideset dist factors array" ); }
01436         }
01437 
01438         delete[] output_element_ids;
01439         delete[] output_element_side_numbers;
01440     }
01441 
01442     return MB_SUCCESS;
01443 }
01444 
01445 ErrorCode WriteNCDF::initialize_exodus_file( ExodusMeshInfo& mesh_info, std::vector< MaterialSetData >& block_data,
01446                                              std::vector< NeumannSetData >& sideset_data,
01447                                              std::vector< DirichletSetData >& nodeset_data, const char* title_string,
01448                                              bool write_maps, bool /* write_sideset_distribution_factors */ )
01449 {
01450     // This routine takes the place of the exodusii routine ex_put_init,
01451     // and additionally pre-defines variables such as qa, element blocks,
01452     // sidesets and nodesets in a single pass.
01453     //
01454     // This is necessary because of the way exodusII works.  Each time the
01455     // netcdf routine endef is called to take the file out of define mode,
01456     // the entire file is copied before the new information is added.
01457     //
01458     // With very large files, this is simply not workable.  This routine takes
01459     // the definition portions of all applicable exodus routines and puts them
01460     // in a single definition, preventing repeated copying of the file.
01461     //
01462     // Most of the code is copied directly from the applicable exodus routine,
01463     // and thus this routine may not seem very consistent in usage or variable
01464     // naming, etc.
01465 
01466     // Perform the initializations
01467 
01468     int element_block_index;
01469 
01470     // Inquire on defined string dimension and general dimension for qa
01471 
01472     int dim_str, dim_four, dim_line, dim_time;
01473     if( nc_def_dim( ncFile, "len_string", ExoIIInterface::MAX_STR_LENGTH, &dim_str ) != NC_NOERR )
01474     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to get string length in file" ); }
01475 
01476     if( nc_def_dim( ncFile, "len_line", ExoIIInterface::MAX_STR_LENGTH, &dim_line ) != NC_NOERR )
01477     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to get line length in file" ); }
01478 
01479     if( nc_def_dim( ncFile, "four", 4, &dim_four ) != NC_NOERR )
01480     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to locate four in file" ); }
01481 
01482     if( nc_def_dim( ncFile, "time_step", 1, &dim_time ) != NC_NOERR )
01483     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to locate time step in file" ); }
01484     // some whole_time dummy :(
01485     int dtime;
01486     if( NC_NOERR != nc_def_var( ncFile, "time_whole", NC_DOUBLE, 1, &dim_time, &dtime ) )
01487     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define time whole array" ); }
01488 
01489     /* Put file into define mode */
01490 
01491     // It is possible that an NT filename using backslashes is in the title string
01492     // this can give fits to unix codes where the backslash is assumed to be an escape
01493     // sequence.  For the exodus file, just flip the backslash to a slash to prevent
01494     // this problem
01495 
01496     // Get a working copy of the title_string;
01497 
01498     char working_title[80];
01499     strncpy( working_title, title_string, 79 );
01500 
01501     int length = strlen( working_title );
01502     for( int pos = 0; pos < length; pos++ )
01503     {
01504         if( working_title[pos] == '\\' ) strncpy( &working_title[pos], "/", 1 );
01505     }
01506 
01507     if( NC_NOERR != nc_put_att_text( ncFile, NC_GLOBAL, "title", length, working_title ) )
01508     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define title attribute" ); }
01509 
01510     // Add other attributes while we're at it
01511     float dum_vers = 6.28F;
01512     if( NC_NOERR != nc_put_att_float( ncFile, NC_GLOBAL, "api_version", NC_FLOAT, 1, &dum_vers ) )
01513     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define api_version attribute" ); }
01514     dum_vers = 6.28F;
01515     if( NC_NOERR != nc_put_att_float( ncFile, NC_GLOBAL, "version", NC_FLOAT, 1, &dum_vers ) )
01516     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define version attribute" ); }
01517     int dum_siz = sizeof( double );
01518     if( NC_NOERR != nc_put_att_int( ncFile, NC_GLOBAL, "floating_point_word_size", NC_INT, 1, &dum_siz ) )
01519     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define floating pt word size attribute" ); }
01520 
01521     // Set up number of dimensions
01522 
01523     int num_el_blk, num_elem, num_nodes, num_dim, num_fa_blk, num_faces;
01524     if( nc_def_dim( ncFile, "num_dim", (size_t)mesh_info.num_dim, &num_dim ) != NC_NOERR )
01525     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of dimensions" ); }
01526 
01527     if( nc_def_dim( ncFile, "num_nodes", mesh_info.num_nodes, &num_nodes ) != NC_NOERR )
01528     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of nodes" ); }
01529 
01530     int num_nod_per_fa;  // it is needed for polyhedron only; need to compute it (connectivity of
01531                          // faces of polyhedra)
01532     if( mesh_info.polyhedronFaces.size() > 0 )
01533         if( nc_def_dim( ncFile, "num_faces", (int)mesh_info.polyhedronFaces.size(), &num_faces ) != NC_NOERR )
01534         { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of nodes" ); }
01535 
01536     if( nc_def_dim( ncFile, "num_elem", mesh_info.num_elements, &num_elem ) != NC_NOERR )
01537     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of elements" ); }
01538 
01539     if( nc_def_dim( ncFile, "num_el_blk", mesh_info.num_elementblocks, &num_el_blk ) != NC_NOERR )
01540     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of element blocks" ); }
01541 
01542     /* ...and some variables */
01543 
01544     /* Element block id status array */
01545     int idstat = -1;
01546     if( NC_NOERR != nc_def_var( ncFile, "eb_status", NC_LONG, 1, &num_el_blk, &idstat ) )
01547     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define element block status array" ); }
01548 
01549     /* Element block id array */
01550 
01551     int idarr = -1;
01552     if( NC_NOERR != nc_def_var( ncFile, "eb_prop1", NC_LONG, 1, &num_el_blk, &idarr ) )
01553     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define element block id array" ); }
01554 
01555     /*   store property name as attribute of property array variable */
01556     if( NC_NOERR != nc_put_att_text( ncFile, idarr, "name", strlen( "ID" ), "ID" ) )
01557     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to store element block property name ID" ); }
01558 
01559     // count how many are polyhedron blocks
01560     int num_fa_blocks = 0, num_polyh_blocks = 0;
01561     for( unsigned int i = 0; i < block_data.size(); i++ )
01562     {
01563         MaterialSetData& block = block_data[i];
01564         if( EXOII_POLYHEDRON == block.element_type )
01565         {
01566             num_fa_blocks++;
01567             num_polyh_blocks++;
01568         }
01569     }
01570     if( 0 == this->repeat_face_blocks && num_fa_blocks > 1 ) num_fa_blocks = 1;
01571     char wname[80];
01572 
01573     if( num_fa_blocks > 0 )
01574     {
01575         /* face block id status array */
01576         if( nc_def_dim( ncFile, "num_fa_blk", num_fa_blocks, &num_fa_blk ) != NC_NOERR )
01577         { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of face blocks" ); }
01578 
01579         int idstatf = -1;
01580         if( NC_NOERR != nc_def_var( ncFile, "fa_status", NC_LONG, 1, &num_fa_blk, &idstatf ) )
01581         { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define face block status array" ); }
01582 
01583         /* Element block id array */
01584 
01585         int idarrf = -1;
01586         if( NC_NOERR != nc_def_var( ncFile, "fa_prop1", NC_LONG, 1, &num_fa_blk, &idarrf ) )
01587         { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define face block id array" ); }
01588 
01589         /*   store property name as attribute of property array variable */
01590         if( NC_NOERR != nc_put_att_text( ncFile, idarrf, "name", strlen( "ID" ), "ID" ) )
01591         { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to store face block property name ID" ); }
01592         // determine the number of num_nod_per_face
01593         /*
01594           num_fa_in_blk1 = 15 ;
01595           num_nod_per_fa1 = 58 ;
01596 
01597           int fbconn1(num_nod_per_fa1) ;
01598                   fbconn1:elem_type = "nsided" ;
01599           int fbepecnt1(num_fa_in_blk1) ;
01600                   fbepecnt1:entity_type1 = "NODE" ;
01601                   fbepecnt1:entity_type2 = "FACE" ;
01602            */
01603 
01604         int num_nodes_per_face = 0;
01605 
01606         int dims[1];  // maybe 1 is enough here
01607         for( Range::iterator eit = mesh_info.polyhedronFaces.begin(); eit != mesh_info.polyhedronFaces.end(); eit++ )
01608         {
01609             EntityHandle polyg       = *eit;
01610             int nnodes               = 0;
01611             const EntityHandle* conn = NULL;
01612             ErrorCode rval           = mdbImpl->get_connectivity( polyg, conn, nnodes );MB_CHK_ERR( rval );
01613             num_nodes_per_face += nnodes;
01614         }
01615 
01616         // duplicate if needed; default is not duplicate
01617         for( int j = 1; j <= num_fa_blocks; j++ )
01618         {
01619             INS_ID( wname, "num_nod_per_fa%d", j );
01620             if( nc_def_dim( ncFile, wname, (size_t)num_nodes_per_face, &num_nod_per_fa ) != NC_NOERR )
01621             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of nodes for face block " ); }
01622             dims[0] = num_nod_per_fa;
01623             INS_ID( wname, "fbconn%d", j );  // first one, or more
01624             int fbconn;
01625             if( NC_NOERR != nc_def_var( ncFile, wname, NC_LONG, 1, dims, &fbconn ) )
01626             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create connectivity array for face block " << 1 ); }
01627             std::string element_type_string( "nsided" );
01628             if( NC_NOERR != nc_put_att_text( ncFile, fbconn, "elem_type", element_type_string.length(),
01629                                              element_type_string.c_str() ) )
01630             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to store element type nsided " ); }
01631 
01632             INS_ID( wname, "num_fa_in_blk%d", j );  // first one, or more
01633             int num_fa_in_blk;
01634             if( nc_def_dim( ncFile, wname, (size_t)mesh_info.polyhedronFaces.size(), &num_fa_in_blk ) != NC_NOERR )
01635             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of nodes for face block " ); }
01636 
01637             // fbepecnt
01638             INS_ID( wname, "fbepecnt%d", j );  // first one, or more
01639             int fbepecnt;
01640             dims[0] = num_fa_in_blk;
01641             if( NC_NOERR != nc_def_var( ncFile, wname, NC_LONG, 1, dims, &fbepecnt ) )
01642             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create fbepecnt array for block " << 1 ); }
01643             std::string enttype1( "NODE" );
01644             if( NC_NOERR != nc_put_att_text( ncFile, fbepecnt, "entity_type1", enttype1.length(), enttype1.c_str() ) )
01645             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to store entity type 1  " ); }
01646             std::string enttype2( "FACE" );
01647             if( NC_NOERR != nc_put_att_text( ncFile, fbepecnt, "entity_type2", enttype2.length(), enttype2.c_str() ) )
01648             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to store entity type 2  " ); }
01649         }
01650     }
01651 
01652     // Define element blocks
01653 
01654     for( unsigned int i = 0; i < block_data.size(); i++ )
01655     {
01656         MaterialSetData& block = block_data[i];
01657 
01658         element_block_index = i + 1;
01659         int num_el_in_blk = -1, num_att_in_blk = -1;
01660         int blk_attrib, connect;
01661 
01662         /* Define number of elements in this block */
01663 
01664         INS_ID( wname, "num_el_in_blk%d", element_block_index );
01665         if( nc_def_dim( ncFile, wname, (size_t)block.number_elements, &num_el_in_blk ) != NC_NOERR )
01666         { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of elements/block for block " << i + 1 ); }
01667 
01668         /* Define number of nodes per element for this block */
01669         INS_ID( wname, "num_nod_per_el%d", element_block_index );
01670         int num_nod_per_el = -1;
01671         if( EXOII_POLYHEDRON != block.element_type )
01672             if( nc_def_dim( ncFile, wname, (size_t)block.number_nodes_per_element, &num_nod_per_el ) != NC_NOERR )
01673             {
01674                 MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of nodes/element for block " << block.id );
01675             }
01676 
01677         /* Define element attribute array for this block */
01678         int dims[3];
01679         if( block.number_attributes > 0 )
01680         {
01681             INS_ID( wname, "num_att_in_blk%d", element_block_index );
01682             if( nc_def_dim( ncFile, wname, (size_t)block.number_attributes, &num_att_in_blk ) != NC_NOERR )
01683             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of attributes in block " << block.id ); }
01684 
01685             INS_ID( wname, "attrib%d", element_block_index );
01686             dims[0] = num_el_in_blk;
01687             dims[1] = num_att_in_blk;
01688             if( NC_NOERR != nc_def_var( ncFile, wname, NC_DOUBLE, 2, dims, &blk_attrib ) )
01689             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define attributes for element block " << block.id ); }
01690         }
01691 
01692         /* Define element connectivity array for this block */
01693 
01694         if( EXOII_POLYGON != block.element_type && EXOII_POLYHEDRON != block.element_type )
01695         {
01696             INS_ID( wname, "connect%d", element_block_index );
01697             dims[0] = num_el_in_blk;
01698             dims[1] = num_nod_per_el;
01699             if( NC_NOERR != nc_def_var( ncFile, wname, NC_LONG, 2, dims, &connect ) )
01700             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create connectivity array for block " << i + 1 ); }
01701 
01702             /* Store element type as attribute of connectivity variable */
01703 
01704             std::string element_type_string( ExoIIUtil::ElementTypeNames[block.element_type] );
01705             if( NC_NOERR != nc_put_att_text( ncFile, connect, "elem_type", element_type_string.length(),
01706                                              element_type_string.c_str() ) )
01707             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to store element type name " << (int)block.element_type ); }
01708         }
01709         else if( EXOII_POLYGON == block.element_type )
01710         {
01711             INS_ID( wname, "connect%d", element_block_index );
01712             // need to define num_nod_per_el as total number of nodes
01713             // ebepecnt1 as number of nodes per polygon
01714             /*
01715              * int connect1(num_nod_per_el1) ;
01716                   connect1:elem_type = "nsided" ;
01717                int ebepecnt1(num_el_in_blk1) ;
01718                   ebepecnt1:entity_type1 = "NODE" ;
01719                   ebepecnt1:entity_type2 = "ELEM" ;*/
01720             dims[0] = num_nod_per_el;
01721             if( NC_NOERR != nc_def_var( ncFile, wname, NC_LONG, 1, dims, &connect ) )
01722             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create connectivity array for block " << i + 1 ); }
01723             std::string element_type_string( "nsided" );
01724             if( NC_NOERR != nc_put_att_text( ncFile, connect, "elem_type", element_type_string.length(),
01725                                              element_type_string.c_str() ) )
01726             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to store element type name " << (int)block.element_type ); }
01727             INS_ID( wname, "ebepecnt%d", element_block_index );
01728             int ebepecnt;
01729             dims[0] = num_el_in_blk;
01730             if( NC_NOERR != nc_def_var( ncFile, wname, NC_LONG, 1, dims, &ebepecnt ) )
01731             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create ebepecnt array for block " << i + 1 ); }
01732             std::string etype1( "NODE" );
01733             if( NC_NOERR != nc_put_att_text( ncFile, ebepecnt, "entity_type1", etype1.length(), etype1.c_str() ) )
01734             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to store entity type1 " << (int)block.element_type ); }
01735             std::string etype2( "ELEM" );
01736             if( NC_NOERR != nc_put_att_text( ncFile, ebepecnt, "entity_type2", etype2.length(), etype2.c_str() ) )
01737             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to store entity type2 " << (int)block.element_type ); }
01738         }
01739         else if( EXOII_POLYHEDRON == block.element_type )
01740         {
01741             // INS_ID(wname, "connect%d", element_block_index);
01742             /*
01743             testn  face   example: 3 polyh, 15 total faces, 2 shared; 15+2 = 17
01744             num_elem = 3 ;
01745             num_face = 15 ; // not needed?
01746             num_el_blk = 1 ;
01747 
01748             num_el_in_blk1 = 3 ;
01749             num_fac_per_el1 = 17 ;
01750 
01751              * num faces will be total face conn
01752              * num_faces_in_block will be number of faces (non repeated)
01753              * num_nodes_per_face will have total face connectivity
01754              */
01755             int num_faces2 = 0;
01756 
01757             for( Range::iterator eit = block.elements.begin(); eit != block.elements.end(); eit++ )
01758             {
01759                 EntityHandle polyh       = *eit;
01760                 int nfaces               = 0;
01761                 const EntityHandle* conn = NULL;
01762                 ErrorCode rval           = mdbImpl->get_connectivity( polyh, conn, nfaces );MB_CHK_ERR( rval );
01763                 num_faces2 += nfaces;
01764             }
01765 
01766             int num_fac_per_el;
01767 
01768             INS_ID( wname, "num_fac_per_el%d", element_block_index );
01769             if( nc_def_dim( ncFile, wname, (size_t)num_faces2, &num_fac_per_el ) != NC_NOERR )
01770             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of faces per block " << block.id ); }
01771 
01772             /*
01773             int facconn1(num_fac_per_el1) ;
01774                     facconn1:elem_type = "NFACED" ;
01775             int ebepecnt1(num_el_in_blk1) ;
01776                     ebepecnt1:entity_type1 = "FACE" ;
01777                     ebepecnt1:entity_type2 = "ELEM" ;
01778              */
01779 
01780             // facconn
01781             INS_ID( wname, "facconn%d", element_block_index );
01782             int facconn;
01783             dims[0] = num_fac_per_el;
01784             if( NC_NOERR != nc_def_var( ncFile, wname, NC_LONG, 1, dims, &facconn ) )
01785             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create facconn array for block " << i + 1 ); }
01786             std::string etype( "NFACED" );
01787             if( NC_NOERR != nc_put_att_text( ncFile, facconn, "elem_type", etype.length(), etype.c_str() ) )
01788             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to store elem type " << (int)block.element_type ); }
01789 
01790             // ebepecnt
01791             INS_ID( wname, "ebepecnt%d", element_block_index );
01792             int ebepecnt;
01793             dims[0] = num_el_in_blk;
01794             if( NC_NOERR != nc_def_var( ncFile, wname, NC_LONG, 1, dims, &ebepecnt ) )
01795             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create ebepecnt array for block " << i + 1 ); }
01796             std::string etype1( "FACE" );
01797             if( NC_NOERR != nc_put_att_text( ncFile, ebepecnt, "entity_type1", etype1.length(), etype1.c_str() ) )
01798             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to store entity type1 " << (int)block.element_type ); }
01799             std::string etype2( "ELEM" );
01800             if( NC_NOERR != nc_put_att_text( ncFile, ebepecnt, "entity_type2", etype2.length(), etype2.c_str() ) )
01801             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to store entity type2 " << (int)block.element_type ); }
01802 
01803             block.number_nodes_per_element = num_faces2;  // connectivity for all polyhedra in block
01804         }
01805     }
01806 
01807     /* Node set id array: */
01808 
01809     int non_empty_nss = 0;
01810     // Need to go through nodesets to compute # nodesets, some might be empty
01811     std::vector< DirichletSetData >::iterator ns_it;
01812     for( ns_it = nodeset_data.begin(); ns_it != nodeset_data.end(); ++ns_it )
01813     {
01814         if( 0 != ( *ns_it ).number_nodes ) non_empty_nss++;
01815     }
01816 
01817     int num_ns    = -1;
01818     int ns_idstat = -1, ns_idarr = -1;
01819     if( non_empty_nss > 0 )
01820     {
01821         if( nc_def_dim( ncFile, "num_node_sets", ( size_t )( non_empty_nss ), &num_ns ) != NC_NOERR )
01822         { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of node sets" ); }
01823 
01824         /* Node set id status array: */
01825 
01826         if( NC_NOERR != nc_def_var( ncFile, "ns_status", NC_LONG, 1, &num_ns, &ns_idstat ) )
01827         { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create node sets status array" ); }
01828 
01829         /* Node set id array: */
01830         if( NC_NOERR != nc_def_var( ncFile, "ns_prop1", NC_LONG, 1, &num_ns, &ns_idarr ) )
01831         { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create node sets property array" ); }
01832 
01833         /* Store property name as attribute of property array variable */
01834         if( NC_NOERR != nc_put_att_text( ncFile, NC_GLOBAL, "name", strlen( "ID" ), "ID" ) )
01835         { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to store node set property name ID" ); }
01836 
01837         // Now, define the arrays needed for each node set
01838 
01839         int index = 0;
01840 
01841         for( unsigned i = 0; i < nodeset_data.size(); i++ )
01842         {
01843             DirichletSetData node_set = nodeset_data[i];
01844 
01845             if( 0 == node_set.number_nodes )
01846             {
01847                 MB_SET_ERR_CONT( "WriteNCDF: empty nodeset " << node_set.id );
01848                 continue;
01849             }
01850             index++;
01851 
01852             int num_nod_ns = -1;
01853             INS_ID( wname, "num_nod_ns%d", index );
01854             if( nc_def_dim( ncFile, wname, (size_t)node_set.number_nodes, &num_nod_ns ) != NC_NOERR )
01855             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of nodes for set " << node_set.id ); }
01856 
01857             /* Create variable array in which to store the node set node list */
01858             int node_ns = -1;
01859             INS_ID( wname, "node_ns%d", index );
01860             if( NC_NOERR != nc_def_var( ncFile, wname, NC_LONG, 1, &num_nod_ns, &node_ns ) )
01861             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create node set " << node_set.id << " node list" ); }
01862 
01863             // Create distribution factor array
01864             int fact_ns = -1;
01865             INS_ID( wname, "dist_fact_ns%d", index );
01866             if( NC_NOERR != nc_def_var( ncFile, wname, NC_DOUBLE, 1, &num_nod_ns, &fact_ns ) )
01867             {
01868                 MB_SET_ERR( MB_FAILURE,
01869                             "WriteNCDF: failed to create node set " << node_set.id << " distribution factor list" );
01870             }
01871         }
01872     }
01873 
01874     /* Side set id array: */
01875 
01876     long non_empty_ss = 0;
01877     // Need to go through nodesets to compute # nodesets, some might be empty
01878     std::vector< NeumannSetData >::iterator ss_it;
01879     for( ss_it = sideset_data.begin(); ss_it != sideset_data.end(); ++ss_it )
01880     {
01881         if( 0 != ( *ss_it ).number_elements ) non_empty_ss++;
01882     }
01883 
01884     if( non_empty_ss > 0 )
01885     {
01886         int num_ss = -1;
01887         if( nc_def_dim( ncFile, "num_side_sets", non_empty_ss, &num_ss ) != NC_NOERR )
01888         { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of side sets" ); }
01889 
01890         /* Side set id status array: */
01891         int ss_idstat = -1, ss_idarr = -1;
01892         if( NC_NOERR != nc_def_var( ncFile, "ss_status", NC_LONG, 1, &num_ss, &ss_idstat ) )
01893         { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define side set status" ); }
01894 
01895         /* Side set id array: */
01896         if( NC_NOERR != nc_def_var( ncFile, "ss_prop1", NC_LONG, 1, &num_ss, &ss_idarr ) )
01897         { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define side set property" ); }
01898 
01899         /* Store property name as attribute of property array variable */
01900         if( NC_NOERR != nc_put_att_text( ncFile, ss_idarr, "name", strlen( "ID" ), "ID" ) )
01901         { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to store side set property name ID" ); }
01902 
01903         // Now, define the arrays needed for each side set
01904 
01905         int index = 0;
01906         for( unsigned int i = 0; i < sideset_data.size(); i++ )
01907         {
01908             NeumannSetData side_set = sideset_data[i];
01909 
01910             // Don't define an empty set
01911             if( 0 == side_set.number_elements ) continue;
01912 
01913             index++;
01914 
01915             int num_side_ss = -1;
01916             int elem_ss = -1, side_ss = -1;
01917             INS_ID( wname, "num_side_ss%d", index );
01918             if( nc_def_dim( ncFile, wname, (size_t)side_set.number_elements, &num_side_ss ) != NC_NOERR )
01919             { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of sides in side set " << side_set.id ); }
01920 
01921             INS_ID( wname, "elem_ss%d", index );
01922             if( NC_NOERR != nc_def_var( ncFile, wname, NC_LONG, 1, &num_side_ss, &elem_ss ) )
01923             {
01924                 MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create element list for side set "
01925                                             << side_set.id ); /* Exit define mode and return */
01926             }
01927             INS_ID( wname, "side_ss%d", index );
01928             if( NC_NOERR != nc_def_var( ncFile, wname, NC_LONG, 1, &num_side_ss, &side_ss ) )
01929             {
01930                 MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create side list for side set "
01931                                             << side_set.id ); /* Exit define mode and return */
01932             }
01933 
01934             // sideset distribution factors
01935             int num_df_ss = -1;
01936             INS_ID( wname, "num_df_ss%d", index );
01937             if( nc_def_dim( ncFile, wname, (size_t)side_set.ss_dist_factors.size(), &num_df_ss ) != NC_NOERR )
01938             {
01939                 MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define number of dist factors in side set "
01940                                             << side_set.id ); /* Exit define mode and return */
01941             }
01942 
01943             /* Create variable array in which to store the side set distribution factors */
01944 
01945             int fact_ss = -1;
01946             INS_ID( wname, "dist_fact_ss%d", index );
01947             if( NC_NOERR != nc_def_var( ncFile, wname, NC_LONG, 1, &num_df_ss, &fact_ss ) )
01948             {
01949                 MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create dist factors list for side set "
01950                                             << side_set.id ); /* Exit define mode and return */
01951             }
01952         }
01953     }
01954 
01955     /* Node coordinate arrays: */
01956 
01957     int coord, name_coord, dims[3];
01958     dims[0] = num_dim;
01959     dims[1] = num_nodes;
01960     if( NC_NOERR != nc_def_var( ncFile, "coord", NC_DOUBLE, 2, dims, &coord ) )
01961     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define node coordinate array" ); }
01962 
01963     /* Coordinate names array */
01964 
01965     dims[0] = num_dim;
01966     dims[1] = dim_str;
01967     if( NC_NOERR != nc_def_var( ncFile, "coor_names", NC_CHAR, 2, dims, &name_coord ) )
01968     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define coordinate name array" ); }
01969 
01970     // Define genesis maps if required
01971 
01972     if( write_maps )
01973     {
01974         // Element map
01975         int elem_map = -1, elem_map2 = -1, node_map = -1;
01976         if( NC_NOERR != nc_def_var( ncFile, "elem_map", NC_LONG, 1, &num_elem, &elem_map ) )
01977         {
01978             MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create element map array" ); /* Exit define mode and return */
01979         }
01980 
01981         // Create the element number map
01982         if( NC_NOERR != nc_def_var( ncFile, "elem_num_map", NC_LONG, 1, &num_elem, &elem_map2 ) )
01983         {
01984             MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create element numbering map" ); /* Exit define mode
01985                                                                                               and return */
01986         }
01987 
01988         // Create node number map
01989         if( NC_NOERR != nc_def_var( ncFile, "node_num_map", NC_LONG, 1, &num_nodes, &node_map ) )
01990         {
01991             MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to create node numbering map array" ); /* Exit define mode and
01992                                                                                                  return */
01993         }
01994     }
01995 
01996     // Define qa records to be used
01997 
01998     int num_qa_rec = mesh_info.qaRecords.size() / 4;
01999     int num_qa     = -1;
02000 
02001     if( nc_def_dim( ncFile, "num_qa_rec", (long)num_qa_rec, &num_qa ) != NC_NOERR )
02002     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define qa record array size" ); }
02003 
02004     // Define qa array
02005     int qa_title;
02006     dims[0] = num_qa;
02007     dims[1] = dim_four;
02008     dims[2] = dim_str;
02009     if( NC_NOERR != nc_def_var( ncFile, "qa_records", NC_CHAR, 3, dims, &qa_title ) )
02010     { MB_SET_ERR( MB_FAILURE, "WriteNCDF: failed to define qa record array" ); }
02011 
02012     // Take it out of define mode
02013     if( NC_NOERR != nc_enddef( ncFile ) ) { MB_SET_ERR( MB_FAILURE, "WriteNCDF: Trouble leaving define mode" ); }
02014 
02015     return MB_SUCCESS;
02016 }
02017 
02018 ErrorCode WriteNCDF::open_file( const char* filename )
02019 {
02020     // Not a valid filename
02021     if( strlen( (const char*)filename ) == 0 ) { MB_SET_ERR( MB_FAILURE, "Output Exodus filename not specified" ); }
02022 
02023     int fail = nc_create( filename, NC_CLOBBER, &ncFile );
02024 
02025     // File couldn't be opened
02026     if( NC_NOERR != fail ) { MB_SET_ERR( MB_FAILURE, "Cannot open " << filename ); }
02027 
02028     return MB_SUCCESS;
02029 }
02030 
02031 ErrorCode WriteNCDF::get_sideset_elems( EntityHandle sideset, int current_sense, Range& forward_elems,
02032                                         Range& reverse_elems )
02033 {
02034     Range ss_elems, ss_meshsets;
02035 
02036     // Get the sense tag; don't need to check return, might be an error if the tag
02037     // hasn't been created yet
02038     Tag sense_tag = 0;
02039     mdbImpl->tag_get_handle( "SENSE", 1, MB_TYPE_INTEGER, sense_tag );
02040 
02041     // Get the entities in this set
02042     ErrorCode result = mdbImpl->get_entities_by_handle( sideset, ss_elems, true );
02043     if( MB_FAILURE == result ) return result;
02044 
02045     // Now remove the meshsets into the ss_meshsets; first find the first meshset,
02046     Range::iterator range_iter = ss_elems.begin();
02047     while( TYPE_FROM_HANDLE( *range_iter ) != MBENTITYSET && range_iter != ss_elems.end() )
02048         ++range_iter;
02049 
02050     // Then, if there are some, copy them into ss_meshsets and erase from ss_elems
02051     if( range_iter != ss_elems.end() )
02052     {
02053         std::copy( range_iter, ss_elems.end(), range_inserter( ss_meshsets ) );
02054         ss_elems.erase( range_iter, ss_elems.end() );
02055     }
02056 
02057     // OK, for the elements, check the sense of this set and copy into the right range
02058     // (if the sense is 0, copy into both ranges)
02059 
02060     // Need to step forward on list until we reach the right dimension
02061     Range::iterator dum_it = ss_elems.end();
02062     --dum_it;
02063     int target_dim = CN::Dimension( TYPE_FROM_HANDLE( *dum_it ) );
02064     dum_it         = ss_elems.begin();
02065     while( target_dim != CN::Dimension( TYPE_FROM_HANDLE( *dum_it ) ) && dum_it != ss_elems.end() )
02066         ++dum_it;
02067 
02068     if( current_sense == 1 || current_sense == 0 ) std::copy( dum_it, ss_elems.end(), range_inserter( forward_elems ) );
02069     if( current_sense == -1 || current_sense == 0 )
02070         std::copy( dum_it, ss_elems.end(), range_inserter( reverse_elems ) );
02071 
02072     // Now loop over the contained meshsets, getting the sense of those and calling this
02073     // function recursively
02074     for( range_iter = ss_meshsets.begin(); range_iter != ss_meshsets.end(); ++range_iter )
02075     {
02076         // First get the sense; if it's not there, by convention it's forward
02077         int this_sense;
02078         if( 0 == sense_tag || MB_FAILURE == mdbImpl->tag_get_data( sense_tag, &( *range_iter ), 1, &this_sense ) )
02079             this_sense = 1;
02080 
02081         // Now get all the entities on this meshset, with the proper (possibly reversed) sense
02082         get_sideset_elems( *range_iter, this_sense * current_sense, forward_elems, reverse_elems );
02083     }
02084 
02085     return result;
02086 }
02087 
02088 }  // namespace moab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines