MOAB: Mesh Oriented datABase  (version 5.2.1)
WriteCCMIO.cpp
Go to the documentation of this file.
00001 /*
00002  * CCMIO file structure
00003  *
00004  * Root
00005  *   State(kCCMIOState)
00006  *     Processor*
00007  *       VerticesID
00008  *       TopologyID
00009  *       InitialID
00010  *       SolutionID
00011  *   Vertices*
00012  *     ->WriteVerticesx, WriteMap
00013  *   Topology*
00014  *     Boundary faces*(kCCMIOBoundaryFaces)
00015  *        ->WriteFaces, WriteFaceCells, WriteMap
00016  *     Internal faces(kCCMIOInternalFaces)
00017  *     Cells (kCCMIOCells)
00018  *        ->WriteCells (mapID), WriteMap, WriteCells
00019  *   Solution
00020  *     Phase
00021  *       Field
00022  *         FieldData
00023  *   Problem(kCCMIOProblemDescription)
00024  *     CellType* (kCCMIOCellType)
00025  *       Index (GetEntityIndex), MaterialId(WriteOpti), MaterialType(WriteOptstr),
00026  *         PorosityId(WriteOpti), SpinId(WriteOpti), GroupId(WriteOpti)
00027  *
00028  * MaterialType (CCMIOWriteOptstr in readexample)
00029  * constants (see readexample)
00030  * lagrangian data (CCMIOWriteLagrangianData)
00031  * vertices label (CCMIOEntityDescription)
00032  * restart info: char solver[], iterations, time, char timeUnits[], angle
00033  *      (CCMIOWriteRestartInfo, kCCMIORestartData), reference data?
00034  * phase:
00035  *   field: char name[], dims, CCMIODataType datatype, char units[]
00036  *       dims = kCCMIOScalar (CCMIOWriteFieldDataf),
00037  *              kCCMIOVector (CCMIOWriteMultiDimensionalFieldData),
00038  *              kCCMIOTensor
00039  * MonitoringSets: num, name (CellSet, VertexSet, BoundarySet, BlockSet, SplineSet, CoupleSet)
00040  *      CCMIOGetProstarSet, CCMIOWriteOpt1i,
00041  */
00042 
00043 #ifdef WIN32
00044 #ifdef _DEBUG
00045 // turn off warnings that say they debugging identifier has been truncated
00046 // this warning comes up when using some STL containers
00047 #pragma warning( disable : 4786 )
00048 #endif
00049 #endif
00050 
00051 #include "WriteCCMIO.hpp"
00052 #include "ccmio.h"
00053 #include "ccmioutility.h"
00054 #include "ccmiocore.h"
00055 #include <utility>
00056 #include <algorithm>
00057 #include <time.h>
00058 #include <string>
00059 #include <vector>
00060 #include <stdio.h>
00061 #include <iostream>
00062 #include <algorithm>
00063 #include <sstream>
00064 
00065 #include "moab/Interface.hpp"
00066 #include "moab/Range.hpp"
00067 #include "moab/CN.hpp"
00068 #include "moab/Skinner.hpp"
00069 #include "assert.h"
00070 #include "Internals.hpp"
00071 #include "ExoIIUtil.hpp"
00072 #include "MBTagConventions.hpp"
00073 #ifdef MOAB_HAVE_MPI
00074 #include "MBParallelConventions.h"
00075 #endif
00076 #include "moab/WriteUtilIface.hpp"
00077 
00078 namespace moab
00079 {
00080 
00081 static char const kStateName[] = "default";
00082 
00083 /*
00084   static const int ccm_types[] = {
00085     1,   // MBVERTEX
00086     2,   // MBEDGE
00087     -1,  // MBTRI
00088     -1,  // MBQUAD
00089     -1,  // MBPOLYGON
00090     13,  // MBTET
00091     14,  // MBPYRAMID
00092     12,  // MBPRISM
00093     -1,  // MBKNIFE
00094     11,  // MBHEX
00095     255  // MBPOLYHEDRON
00096   };
00097 */
00098 
00099 #define INS_ID( stringvar, prefix, id ) sprintf( stringvar, prefix, id )
00100 
00101 #define CHK_SET_CCMERR( ccm_err_code, ccm_err_msg )                              \
00102     {                                                                            \
00103         if( kCCMIONoErr != ccm_err_code ) MB_SET_ERR( MB_FAILURE, ccm_err_msg ); \
00104     }
00105 
00106 WriterIface* WriteCCMIO::factory( Interface* iface )
00107 {
00108     return new WriteCCMIO( iface );
00109 }
00110 
00111 WriteCCMIO::WriteCCMIO( Interface* impl )
00112     : mbImpl( impl ), mCurrentMeshHandle( 0 ), mPartitionSetTag( 0 ), mNameTag( 0 ), mMaterialIdTag( 0 ),
00113       mMaterialTypeTag( 0 ), mRadiationTag( 0 ), mPorosityIdTag( 0 ), mSpinIdTag( 0 ), mGroupIdTag( 0 ),
00114       mColorIdxTag( 0 ), mProcessorIdTag( 0 ), mLightMaterialTag( 0 ), mFreeSurfaceMaterialTag( 0 ), mThicknessTag( 0 ),
00115       mProstarRegionNumberTag( 0 ), mBoundaryTypeTag( 0 ), mCreatingProgramTag( 0 ), mDimension( 0 ),
00116       mWholeMesh( false )
00117 {
00118     assert( impl != NULL );
00119 
00120     impl->query_interface( mWriteIface );
00121 
00122     // Initialize in case tag_get_handle fails below
00123     //! Get and cache predefined tag handles
00124     int negone = -1;
00125     impl->tag_get_handle( MATERIAL_SET_TAG_NAME, 1, MB_TYPE_INTEGER, mMaterialSetTag, MB_TAG_SPARSE | MB_TAG_CREAT,
00126                           &negone );
00127 
00128     impl->tag_get_handle( DIRICHLET_SET_TAG_NAME, 1, MB_TYPE_INTEGER, mDirichletSetTag, MB_TAG_SPARSE | MB_TAG_CREAT,
00129                           &negone );
00130 
00131     impl->tag_get_handle( NEUMANN_SET_TAG_NAME, 1, MB_TYPE_INTEGER, mNeumannSetTag, MB_TAG_SPARSE | MB_TAG_CREAT,
00132                           &negone );
00133 
00134     mGlobalIdTag = impl->globalId_tag();
00135 
00136 #ifdef MOAB_HAVE_MPI
00137     impl->tag_get_handle( PARALLEL_PARTITION_TAG_NAME, 1, MB_TYPE_INTEGER, mPartitionSetTag, MB_TAG_SPARSE );
00138     // No need to check result, if it's not there, we don't create one
00139 #endif
00140 
00141     int dum_val_array[] = { -1, -1, -1, -1 };
00142     impl->tag_get_handle( HAS_MID_NODES_TAG_NAME, 4, MB_TYPE_INTEGER, mHasMidNodesTag, MB_TAG_SPARSE | MB_TAG_CREAT,
00143                           dum_val_array );
00144 
00145     impl->tag_get_handle( "__WriteCCMIO element mark", 1, MB_TYPE_BIT, mEntityMark, MB_TAG_CREAT );
00146 
00147     // Don't need to check return of following, since it doesn't matter if there isn't one
00148     mbImpl->tag_get_handle( NAME_TAG_NAME, NAME_TAG_SIZE, MB_TYPE_OPAQUE, mNameTag );
00149 }
00150 
00151 WriteCCMIO::~WriteCCMIO()
00152 {
00153     mbImpl->release_interface( mWriteIface );
00154     mbImpl->tag_delete( mEntityMark );
00155 }
00156 
00157 ErrorCode WriteCCMIO::write_file( const char* file_name, const bool overwrite, const FileOptions&,
00158                                   const EntityHandle* ent_handles, const int num_sets,
00159                                   const std::vector< std::string >& /* qa_list */, const Tag* /* tag_list */,
00160                                   int /* num_tags */, int /* export_dimension */ )
00161 {
00162     assert( 0 != mMaterialSetTag && 0 != mNeumannSetTag && 0 != mDirichletSetTag );
00163 
00164     ErrorCode result;
00165 
00166     // Check overwrite flag and file existence
00167     if( !overwrite )
00168     {
00169         FILE* file = fopen( file_name, "r" );
00170         if( file )
00171         {
00172             fclose( file );
00173             MB_SET_ERR( MB_FILE_WRITE_ERROR, "File exists but overwrite set to false" );
00174         }
00175     }
00176 
00177     mDimension = 3;
00178 
00179     std::vector< EntityHandle > matsets, dirsets, neusets, partsets;
00180 
00181     // Separate into material, dirichlet, neumann, partition sets
00182     result = get_sets( ent_handles, num_sets, matsets, dirsets, neusets, partsets );MB_CHK_SET_ERR( result, "Failed to get material/etc. sets" );
00183 
00184     // If entity handles were input but didn't contain matsets, return error
00185     if( ent_handles && matsets.empty() )
00186     { MB_SET_ERR( MB_FILE_WRITE_ERROR, "Sets input to write but no material sets found" ); }
00187 
00188     // Otherwise, if no matsets, use root set
00189     if( matsets.empty() ) matsets.push_back( 0 );
00190 
00191     std::vector< MaterialSetData > matset_info;
00192     Range all_verts;
00193     result = gather_matset_info( matsets, matset_info, all_verts );MB_CHK_SET_ERR( result, "gathering matset info failed" );
00194 
00195     // Assign vertex gids
00196     result = mWriteIface->assign_ids( all_verts, mGlobalIdTag, 1 );MB_CHK_SET_ERR( result, "Failed to assign vertex global ids" );
00197 
00198     // Some CCMIO descriptors
00199     CCMIOID rootID, topologyID, stateID, problemID, verticesID, processorID;
00200 
00201     // Try to open the file and establish state
00202     result = open_file( file_name, overwrite, rootID );MB_CHK_SET_ERR( result, "Couldn't open file or create state" );
00203 
00204     result = create_ccmio_structure( rootID, stateID, processorID );MB_CHK_SET_ERR( result, "Problem creating CCMIO file structure" );
00205 
00206     result = write_nodes( rootID, all_verts, mDimension, verticesID );MB_CHK_SET_ERR( result, "write_nodes failed" );
00207 
00208     std::vector< NeumannSetData > neuset_info;
00209     result = gather_neuset_info( neusets, neuset_info );MB_CHK_SET_ERR( result, "Failed to get neumann set info" );
00210 
00211     result = write_cells_and_faces( rootID, matset_info, neuset_info, all_verts, topologyID );MB_CHK_SET_ERR( result, "write_cells_and_faces failed" );
00212 
00213     result = write_problem_description( rootID, stateID, problemID, processorID, matset_info, neuset_info );MB_CHK_SET_ERR( result, "write_problem_description failed" );
00214 
00215     result = write_solution_data();MB_CHK_SET_ERR( result, "Trouble writing solution data" );
00216 
00217     result = write_processor( processorID, verticesID, topologyID );MB_CHK_SET_ERR( result, "Trouble writing processor" );
00218 
00219     result = close_and_compress( file_name, rootID );MB_CHK_SET_ERR( result, "Close or compress failed" );
00220 
00221     return MB_SUCCESS;
00222 }
00223 
00224 ErrorCode WriteCCMIO::write_solution_data()
00225 {
00226     // For now, no solution (tag) data
00227     return MB_SUCCESS;
00228 }
00229 
00230 ErrorCode WriteCCMIO::write_processor( CCMIOID processorID, CCMIOID verticesID, CCMIOID topologyID )
00231 {
00232     CCMIOError error = kCCMIONoErr;
00233 
00234     // Now we have the mesh (vertices and topology) and the post data written.
00235     // Since we now have their IDs, we can write out the processor information.
00236     CCMIOWriteProcessor( &error, processorID, NULL, &verticesID, NULL, &topologyID, NULL, NULL, NULL, NULL );
00237     CHK_SET_CCMERR( error, "Problem writing CCMIO processor" );
00238 
00239     return MB_SUCCESS;
00240 }
00241 
00242 ErrorCode WriteCCMIO::create_ccmio_structure( CCMIOID rootID, CCMIOID& stateID, CCMIOID& processorID )
00243 {
00244     // Create problem state and other CCMIO nodes under it
00245     CCMIOError error = kCCMIONoErr;
00246 
00247     // Create a new state (or re-use an existing one).
00248     if( CCMIOGetState( NULL, rootID, kStateName, NULL, &stateID ) != kCCMIONoErr )
00249     {
00250         CCMIONewState( &error, rootID, kStateName, NULL, NULL, &stateID );
00251         CHK_SET_CCMERR( error, "Trouble creating state" );
00252     }
00253 
00254     // Create or get an old processor for this state
00255     CCMIOSize_t i = CCMIOSIZEC( 0 );
00256     if( CCMIONextEntity( NULL, stateID, kCCMIOProcessor, &i, &processorID ) != kCCMIONoErr )
00257     {
00258         CCMIONewEntity( &error, stateID, kCCMIOProcessor, NULL, &processorID );
00259         CHK_SET_CCMERR( error, "Trouble creating processor node" );
00260     }
00261     // Get rid of any data that may be in this processor (if the state was
00262     // not new).
00263     else
00264     {
00265         CCMIOClearProcessor( &error, stateID, processorID, TRUE, TRUE, TRUE, TRUE, TRUE );
00266         CHK_SET_CCMERR( error, "Trouble clearing processor data" );
00267     }
00268 
00269     /*
00270      //  for (; i < CCMIOSIZEC(partsets.size()); i++) {
00271      CCMIOSize_t id = CCMIOSIZEC(0);
00272      if (CCMIONextEntity(NULL, stateID, kCCMIOProcessor, &id, &processorID) != kCCMIONoErr)
00273      CCMIONewEntity(&error, stateID, kCCMIOProcessor, NULL, &processorID);
00274      CHKCCMERR(error, "Trouble creating processor node.");
00275      */
00276     return MB_SUCCESS;
00277 }
00278 
00279 ErrorCode WriteCCMIO::close_and_compress( const char*, CCMIOID rootID )
00280 {
00281     CCMIOError error = kCCMIONoErr;
00282     CCMIOCloseFile( &error, rootID );
00283     CHK_SET_CCMERR( error, "File close failed" );
00284 
00285     // The CCMIO library uses ADF to store the actual data.  Unfortunately,
00286     // ADF leaks disk space;  deleting a node does not recover all the disk
00287     // space.  Now that everything is successfully written it might be useful
00288     // to call CCMIOCompress() here to ensure that the file is as small as
00289     // possible.  Please see the Core API documentation for caveats on its
00290     // usage.
00291     // CCMIOCompress(&error, const_cast<char*>(filename));CHK_SET_CCMERR(error, "Error compressing
00292     // file");
00293 
00294     return MB_SUCCESS;
00295 }
00296 
00297 ErrorCode WriteCCMIO::open_file( const char* filename, bool, CCMIOID& rootID )
00298 {
00299     CCMIOError error = kCCMIONoErr;
00300     CCMIOOpenFile( &error, filename, kCCMIOWrite, &rootID );
00301     CHK_SET_CCMERR( error, "Cannot open file" );
00302 
00303     return MB_SUCCESS;
00304 }
00305 
00306 ErrorCode WriteCCMIO::get_sets( const EntityHandle* ent_handles, int num_sets, std::vector< EntityHandle >& matsets,
00307                                 std::vector< EntityHandle >& dirsets, std::vector< EntityHandle >& neusets,
00308                                 std::vector< EntityHandle >& partsets )
00309 {
00310     if( num_sets == 0 )
00311     {
00312         // Default to all defined sets
00313         Range this_range;
00314         mbImpl->get_entities_by_type_and_tag( 0, MBENTITYSET, &mMaterialSetTag, NULL, 1, this_range );
00315         std::copy( this_range.begin(), this_range.end(), std::back_inserter( matsets ) );
00316         this_range.clear();
00317         mbImpl->get_entities_by_type_and_tag( 0, MBENTITYSET, &mDirichletSetTag, NULL, 1, this_range );
00318         std::copy( this_range.begin(), this_range.end(), std::back_inserter( dirsets ) );
00319         this_range.clear();
00320         mbImpl->get_entities_by_type_and_tag( 0, MBENTITYSET, &mNeumannSetTag, NULL, 1, this_range );
00321         std::copy( this_range.begin(), this_range.end(), std::back_inserter( neusets ) );
00322         if( mPartitionSetTag )
00323         {
00324             this_range.clear();
00325             mbImpl->get_entities_by_type_and_tag( 0, MBENTITYSET, &mPartitionSetTag, NULL, 1, this_range );
00326             std::copy( this_range.begin(), this_range.end(), std::back_inserter( partsets ) );
00327         }
00328     }
00329     else
00330     {
00331         int dummy;
00332         for( const EntityHandle* iter = ent_handles; iter < ent_handles + num_sets; ++iter )
00333         {
00334             if( MB_SUCCESS == mbImpl->tag_get_data( mMaterialSetTag, &( *iter ), 1, &dummy ) )
00335                 matsets.push_back( *iter );
00336             else if( MB_SUCCESS == mbImpl->tag_get_data( mDirichletSetTag, &( *iter ), 1, &dummy ) )
00337                 dirsets.push_back( *iter );
00338             else if( MB_SUCCESS == mbImpl->tag_get_data( mNeumannSetTag, &( *iter ), 1, &dummy ) )
00339                 neusets.push_back( *iter );
00340             else if( mPartitionSetTag && MB_SUCCESS == mbImpl->tag_get_data( mPartitionSetTag, &( *iter ), 1, &dummy ) )
00341                 partsets.push_back( *iter );
00342         }
00343     }
00344 
00345     return MB_SUCCESS;
00346 }
00347 
00348 ErrorCode WriteCCMIO::write_problem_description( CCMIOID rootID, CCMIOID stateID, CCMIOID& problemID,
00349                                                  CCMIOID processorID,
00350                                                  std::vector< WriteCCMIO::MaterialSetData >& matset_data,
00351                                                  std::vector< WriteCCMIO::NeumannSetData >& neuset_data )
00352 {
00353     // Write out a dummy problem description.  If we happen to know that
00354     // there already is a problem description previously recorded that
00355     // is valid we could skip this step.
00356     CCMIOID id;
00357     CCMIOError error = kCCMIONoErr;
00358     ErrorCode rval;
00359     const EntityHandle mesh = 0;
00360 
00361     bool root_tagged = false, other_set_tagged = false;
00362     Tag simname;
00363     Range dum_sets;
00364     rval = mbImpl->tag_get_handle( "Title", 0, MB_TYPE_OPAQUE, simname, MB_TAG_ANY );
00365     if( MB_SUCCESS == rval )
00366     {
00367         int tag_size;
00368         rval = mbImpl->tag_get_bytes( simname, tag_size );
00369         if( MB_SUCCESS == rval )
00370         {
00371             std::vector< char > title_tag( tag_size + 1 );
00372             rval = mbImpl->get_entities_by_type_and_tag( 0, MBENTITYSET, &simname, NULL, 1, dum_sets );
00373             if( MB_SUCCESS == rval && !dum_sets.empty() )
00374             {
00375                 rval = mbImpl->tag_get_data( simname, &( *dum_sets.begin() ), 1, &title_tag[0] );MB_CHK_SET_ERR( rval, "Problem getting simulation name tag" );
00376                 other_set_tagged = true;
00377             }
00378             else if( MB_SUCCESS == rval )
00379             {
00380                 // Check to see if interface was tagged
00381                 rval = mbImpl->tag_get_data( simname, &mesh, 1, &title_tag[0] );
00382                 if( MB_SUCCESS == rval )
00383                     root_tagged = true;
00384                 else
00385                     rval = MB_SUCCESS;
00386             }
00387             *title_tag.rbegin() = '\0';
00388             if( root_tagged || other_set_tagged )
00389             {
00390                 CCMIONode rootNode;
00391                 if( kCCMIONoErr == CCMIOGetEntityNode( &error, rootID, &rootNode ) )
00392                 {
00393                     CCMIOSetTitle( &error, rootNode, &title_tag[0] );
00394                     CHK_SET_CCMERR( error, "Trouble setting title" );
00395                 }
00396             }
00397         }
00398     }
00399 
00400     rval = mbImpl->tag_get_handle( "CreatingProgram", 0, MB_TYPE_OPAQUE, mCreatingProgramTag, MB_TAG_ANY );
00401     if( MB_SUCCESS == rval )
00402     {
00403         int tag_size;
00404         rval = mbImpl->tag_get_bytes( mCreatingProgramTag, tag_size );
00405         if( MB_SUCCESS == rval )
00406         {
00407             std::vector< char > cp_tag( tag_size + 1 );
00408             rval = mbImpl->get_entities_by_type_and_tag( 0, MBENTITYSET, &mCreatingProgramTag, NULL, 1, dum_sets );
00409             if( MB_SUCCESS == rval && !dum_sets.empty() )
00410             {
00411                 rval = mbImpl->tag_get_data( mCreatingProgramTag, &( *dum_sets.begin() ), 1, &cp_tag[0] );MB_CHK_SET_ERR( rval, "Problem getting creating program tag" );
00412                 other_set_tagged = true;
00413             }
00414             else if( MB_SUCCESS == rval )
00415             {
00416                 // Check to see if interface was tagged
00417                 rval = mbImpl->tag_get_data( mCreatingProgramTag, &mesh, 1, &cp_tag[0] );
00418                 if( MB_SUCCESS == rval )
00419                     root_tagged = true;
00420                 else
00421                     rval = MB_SUCCESS;
00422             }
00423             *cp_tag.rbegin() = '\0';
00424             if( root_tagged || other_set_tagged )
00425             {
00426                 CCMIONode rootNode;
00427                 if( kCCMIONoErr == CCMIOGetEntityNode( &error, rootID, &rootNode ) )
00428                 {
00429                     CCMIOWriteOptstr( &error, processorID, "CreatingProgram", &cp_tag[0] );
00430                     CHK_SET_CCMERR( error, "Trouble setting creating program" );
00431                 }
00432             }
00433         }
00434     }
00435 
00436     CCMIONewEntity( &error, rootID, kCCMIOProblemDescription, NULL, &problemID );
00437     CHK_SET_CCMERR( error, "Trouble creating problem node" );
00438 
00439     // Write material types and other info
00440     for( unsigned int i = 0; i < matset_data.size(); i++ )
00441     {
00442         if( !matset_data[i].setName.empty() )
00443         {
00444             CCMIONewIndexedEntity( &error, problemID, kCCMIOCellType, matset_data[i].matsetId,
00445                                    matset_data[i].setName.c_str(), &id );
00446             CHK_SET_CCMERR( error, "Failure creating celltype node" );
00447 
00448             CCMIOWriteOptstr( &error, id, "MaterialType", matset_data[i].setName.c_str() );
00449             CHK_SET_CCMERR( error, "Error assigning material name" );
00450         }
00451         else
00452         {
00453             char dum_name[NAME_TAG_SIZE];
00454             std::ostringstream os;
00455             std::string mat_name = "Material", temp_str;
00456             os << mat_name << ( i + 1 );
00457             temp_str = os.str();
00458             strcpy( dum_name, temp_str.c_str() );
00459             CCMIONewIndexedEntity( &error, problemID, kCCMIOCellType, matset_data[i].matsetId, dum_name, &id );
00460             CHK_SET_CCMERR( error, "Failure creating celltype node" );
00461 
00462             CCMIOWriteOptstr( &error, id, "MaterialType", dum_name );
00463             CHK_SET_CCMERR( error, "Error assigning material name" );
00464 
00465             os.str( "" );
00466         }
00467         rval = write_int_option( "MaterialId", matset_data[i].setHandle, mMaterialIdTag, id );MB_CHK_SET_ERR( rval, "Trouble writing MaterialId option" );
00468 
00469         rval = write_int_option( "Radiation", matset_data[i].setHandle, mRadiationTag, id );MB_CHK_SET_ERR( rval, "Trouble writing Radiation option" );
00470 
00471         rval = write_int_option( "PorosityId", matset_data[i].setHandle, mPorosityIdTag, id );MB_CHK_SET_ERR( rval, "Trouble writing PorosityId option" );
00472 
00473         rval = write_int_option( "SpinId", matset_data[i].setHandle, mSpinIdTag, id );MB_CHK_SET_ERR( rval, "Trouble writing SpinId option" );
00474 
00475         rval = write_int_option( "GroupId", matset_data[i].setHandle, mGroupIdTag, id );MB_CHK_SET_ERR( rval, "Trouble writing GroupId option" );
00476 
00477         rval = write_int_option( "ColorIdx", matset_data[i].setHandle, mColorIdxTag, id );MB_CHK_SET_ERR( rval, "Trouble writing ColorIdx option" );
00478 
00479         rval = write_int_option( "ProcessorId", matset_data[i].setHandle, mProcessorIdTag, id );MB_CHK_SET_ERR( rval, "Trouble writing ProcessorId option" );
00480 
00481         rval = write_int_option( "LightMaterial", matset_data[i].setHandle, mLightMaterialTag, id );MB_CHK_SET_ERR( rval, "Trouble writing LightMaterial option." );
00482 
00483         rval = write_int_option( "FreeSurfaceMaterial", matset_data[i].setHandle, mFreeSurfaceMaterialTag, id );MB_CHK_SET_ERR( rval, "Trouble writing FreeSurfaceMaterial option" );
00484 
00485         rval = write_dbl_option( "Thickness", matset_data[i].setHandle, mThicknessTag, id );MB_CHK_SET_ERR( rval, "Trouble writing Thickness option" );
00486 
00487         rval = write_str_option( "MaterialType", matset_data[i].setHandle, mMaterialTypeTag, id );MB_CHK_SET_ERR( rval, "Trouble writing MaterialType option" );
00488     }
00489 
00490     // Write neumann set info
00491     for( unsigned int i = 0; i < neuset_data.size(); i++ )
00492     {
00493         // Use the label to encode the id
00494         std::ostringstream dum_id;
00495         dum_id << neuset_data[i].neusetId;
00496         CCMIONewIndexedEntity( &error, problemID, kCCMIOBoundaryRegion, neuset_data[i].neusetId, dum_id.str().c_str(),
00497                                &id );
00498         CHK_SET_CCMERR( error, "Failure creating BoundaryRegion node" );
00499 
00500         rval = write_str_option( "BoundaryName", neuset_data[i].setHandle, mNameTag, id );MB_CHK_SET_ERR( rval, "Trouble writing boundary type number" );
00501 
00502         rval = write_str_option( "BoundaryType", neuset_data[i].setHandle, mBoundaryTypeTag, id );MB_CHK_SET_ERR( rval, "Trouble writing boundary type number" );
00503 
00504         rval = write_int_option( "ProstarRegionNumber", neuset_data[i].setHandle, mProstarRegionNumberTag, id );MB_CHK_SET_ERR( rval, "Trouble writing prostar region number" );
00505     }
00506 
00507     CCMIOWriteState( &error, stateID, problemID, "Example state" );
00508     CHK_SET_CCMERR( error, "Failure writing problem state" );
00509 
00510     // Get cell types; reuse cell ids array
00511     //  for (i = 0, rit = all_elems.begin(); i < num_elems; i++, ++rit) {
00512     //    egids[i] = ccm_types[mbImpl->type_from_handle(*rit)];
00513     //    assert(-1 != egids[i]);
00514     //  }
00515 
00516     return MB_SUCCESS;
00517 }
00518 
00519 ErrorCode WriteCCMIO::write_int_option( const char* opt_name, EntityHandle seth, Tag& tag, CCMIOID& node )
00520 {
00521     ErrorCode rval;
00522 
00523     if( !tag )
00524     {
00525         rval = mbImpl->tag_get_handle( opt_name, 1, MB_TYPE_INTEGER, tag );
00526         // Return success since that just means we don't have to write this option
00527         if( MB_SUCCESS != rval ) return MB_SUCCESS;
00528     }
00529 
00530     int dum_val;
00531     rval = mbImpl->tag_get_data( tag, &seth, 1, &dum_val );
00532     // Return success since that just means we don't have to write this option
00533     if( MB_SUCCESS != rval ) return MB_SUCCESS;
00534 
00535     CCMIOError error = kCCMIONoErr;
00536     CCMIOWriteOpti( &error, node, opt_name, dum_val );
00537     CHK_SET_CCMERR( error, "Trouble writing int option" );
00538 
00539     return MB_SUCCESS;
00540 }
00541 
00542 ErrorCode WriteCCMIO::write_dbl_option( const char* opt_name, EntityHandle seth, Tag& tag, CCMIOID& node )
00543 {
00544     ErrorCode rval;
00545 
00546     if( !tag )
00547     {
00548         rval = mbImpl->tag_get_handle( opt_name, 1, MB_TYPE_DOUBLE, tag );
00549         // Return success since that just means we don't have to write this option
00550         if( MB_SUCCESS != rval ) return MB_SUCCESS;
00551     }
00552 
00553     double dum_val;
00554     rval = mbImpl->tag_get_data( tag, &seth, 1, &dum_val );
00555     // Return success since that just means we don't have to write this option
00556     if( MB_SUCCESS != rval ) return MB_SUCCESS;
00557 
00558     CCMIOError error = kCCMIONoErr;
00559     CCMIOWriteOptf( &error, node, opt_name, dum_val );
00560     CHK_SET_CCMERR( error, "Trouble writing int option" );
00561 
00562     return MB_SUCCESS;
00563 }
00564 
00565 ErrorCode WriteCCMIO::write_str_option( const char* opt_name, EntityHandle seth, Tag& tag, CCMIOID& node,
00566                                         const char* other_name )
00567 {
00568     int tag_size;
00569     ErrorCode rval;
00570 
00571     if( !tag )
00572     {
00573         rval = mbImpl->tag_get_handle( opt_name, 0, MB_TYPE_OPAQUE, tag, MB_TAG_ANY );
00574         // Return success since that just means we don't have to write this option
00575         if( MB_SUCCESS != rval ) return MB_SUCCESS;
00576     }
00577 
00578     rval = mbImpl->tag_get_bytes( tag, tag_size );
00579     if( MB_SUCCESS != rval ) return MB_SUCCESS;
00580     std::vector< char > opt_val( tag_size + 1 );
00581 
00582     rval = mbImpl->tag_get_data( tag, &seth, 1, &opt_val[0] );
00583     if( MB_SUCCESS != rval ) return MB_SUCCESS;
00584 
00585     // Null-terminate if necessary
00586     if( std::find( opt_val.begin(), opt_val.end(), '\0' ) == opt_val.end() ) *opt_val.rbegin() = '\0';
00587 
00588     CCMIOError error = kCCMIONoErr;
00589     if( other_name )
00590     {
00591         CCMIOWriteOptstr( &error, node, other_name, &opt_val[0] );
00592         CHK_SET_CCMERR( error, "Failure writing an option string MaterialType" );
00593     }
00594     else
00595     {
00596         CCMIOWriteOptstr( &error, node, opt_name, &opt_val[0] );
00597         CHK_SET_CCMERR( error, "Failure writing an option string MaterialType" );
00598     }
00599 
00600     return MB_SUCCESS;
00601 }
00602 
00603 ErrorCode WriteCCMIO::gather_matset_info( std::vector< EntityHandle >& matsets,
00604                                           std::vector< MaterialSetData >& matset_data, Range& all_verts )
00605 {
00606     ErrorCode result;
00607     matset_data.resize( matsets.size() );
00608     if( 1 == matsets.size() && 0 == matsets[0] )
00609     {
00610         // Whole mesh
00611         mWholeMesh = true;
00612 
00613         result = mbImpl->get_entities_by_dimension( 0, mDimension, matset_data[0].elems );MB_CHK_SET_ERR( result, "Trouble getting all elements in mesh" );
00614         result = mWriteIface->gather_nodes_from_elements( matset_data[0].elems, mEntityMark, all_verts );MB_CHK_SET_ERR( result, "Trouble gathering nodes from elements" );
00615 
00616         return result;
00617     }
00618 
00619     std::vector< unsigned char > marks;
00620     for( unsigned int i = 0; i < matsets.size(); i++ )
00621     {
00622         EntityHandle this_set = matset_data[i].setHandle = matsets[i];
00623 
00624         // Get all Entity Handles in the set
00625         result = mbImpl->get_entities_by_dimension( this_set, mDimension, matset_data[i].elems, true );MB_CHK_SET_ERR( result, "Trouble getting m-dimensional ents" );
00626 
00627         // Get all connected vertices
00628         result = mWriteIface->gather_nodes_from_elements( matset_data[i].elems, mEntityMark, all_verts );MB_CHK_SET_ERR( result, "Trouble getting vertices for a matset" );
00629 
00630         // Check for consistent entity type
00631         EntityType start_type = mbImpl->type_from_handle( *matset_data[i].elems.begin() );
00632         if( start_type == mbImpl->type_from_handle( *matset_data[i].elems.rbegin() ) )
00633             matset_data[i].entityType = start_type;
00634 
00635         // Mark elements in this matset
00636         marks.resize( matset_data[i].elems.size(), 0x1 );
00637         result = mbImpl->tag_set_data( mEntityMark, matset_data[i].elems, &marks[0] );MB_CHK_SET_ERR( result, "Couln't mark entities being output" );
00638 
00639         // Get id for this matset
00640         result = mbImpl->tag_get_data( mMaterialSetTag, &this_set, 1, &matset_data[i].matsetId );MB_CHK_SET_ERR( result, "Couln't get global id for material set" );
00641 
00642         // Get name for this matset
00643         if( mNameTag )
00644         {
00645             char dum_name[NAME_TAG_SIZE];
00646             result = mbImpl->tag_get_data( mNameTag, &this_set, 1, dum_name );
00647             if( MB_SUCCESS == result ) matset_data[i].setName = dum_name;
00648 
00649             // Reset success, so later checks don't fail
00650             result = MB_SUCCESS;
00651         }
00652     }
00653 
00654     if( all_verts.empty() ) { MB_SET_ERR( MB_FILE_WRITE_ERROR, "No vertices from elements" ); }
00655 
00656     return MB_SUCCESS;
00657 }
00658 
00659 ErrorCode WriteCCMIO::gather_neuset_info( std::vector< EntityHandle >& neusets,
00660                                           std::vector< NeumannSetData >& neuset_info )
00661 {
00662     ErrorCode result;
00663 
00664     neuset_info.resize( neusets.size() );
00665     for( unsigned int i = 0; i < neusets.size(); i++ )
00666     {
00667         EntityHandle this_set = neuset_info[i].setHandle = neusets[i];
00668 
00669         // Get all Entity Handles of one less dimension than that being output
00670         result = mbImpl->get_entities_by_dimension( this_set, mDimension - 1, neuset_info[i].elems, true );MB_CHK_SET_ERR( result, "Trouble getting (m-1)-dimensional ents for neuset" );
00671 
00672         result = mbImpl->tag_get_data( mGlobalIdTag, &this_set, 1, &neuset_info[i].neusetId );
00673         if( MB_TAG_NOT_FOUND == result )
00674         {
00675             result = mbImpl->tag_get_data( mNeumannSetTag, &this_set, 1, &neuset_info[i].neusetId );
00676             if( MB_SUCCESS != result )
00677                 // Need some id; use the loop iteration number
00678                 neuset_info[i].neusetId = i;
00679         }
00680 
00681         // Get name for this neuset
00682         if( mNameTag )
00683         {
00684             char dum_name[NAME_TAG_SIZE];
00685             result = mbImpl->tag_get_data( mNameTag, &this_set, 1, dum_name );
00686             if( MB_SUCCESS == result ) neuset_info[i].setName = dum_name;
00687 
00688             // Reset success, so later checks don't fail
00689             result = MB_SUCCESS;
00690         }
00691     }
00692 
00693     return MB_SUCCESS;
00694 }
00695 
00696 ErrorCode WriteCCMIO::get_gids( const Range& ents, int*& gids, int& minid, int& maxid )
00697 {
00698     int num_ents     = ents.size();
00699     gids             = new int[num_ents];
00700     ErrorCode result = mbImpl->tag_get_data( mGlobalIdTag, ents, &gids[0] );MB_CHK_SET_ERR( result, "Couldn't get global id data" );
00701     minid = *std::min_element( gids, gids + num_ents );
00702     maxid = *std::max_element( gids, gids + num_ents );
00703     if( 0 == minid )
00704     {
00705         // gids need to be assigned
00706         for( int i = 1; i <= num_ents; i++ )
00707             gids[i] = i;
00708         result = mbImpl->tag_set_data( mGlobalIdTag, ents, &gids[0] );MB_CHK_SET_ERR( result, "Couldn't set global id data" );
00709         maxid = num_ents;
00710     }
00711 
00712     return MB_SUCCESS;
00713 }
00714 
00715 ErrorCode WriteCCMIO::write_nodes( CCMIOID rootID, const Range& verts, const int dimension, CCMIOID& verticesID )
00716 {
00717     // Get/write map (global ids) first (gids already assigned)
00718     unsigned int num_verts = verts.size();
00719     std::vector< int > vgids( num_verts );
00720     ErrorCode result = mbImpl->tag_get_data( mGlobalIdTag, verts, &vgids[0] );MB_CHK_SET_ERR( result, "Failed to get global ids for vertices" );
00721 
00722     // Create the map node for vertex ids, and write them to that node
00723     CCMIOID mapID;
00724     CCMIOError error = kCCMIONoErr;
00725     CCMIONewEntity( &error, rootID, kCCMIOMap, "Vertex map", &mapID );
00726     CHK_SET_CCMERR( error, "Failure creating Vertex map node" );
00727 
00728     int maxid = *std::max_element( vgids.begin(), vgids.end() );
00729 
00730     CCMIOWriteMap( &error, mapID, CCMIOSIZEC( num_verts ), CCMIOSIZEC( maxid ), &vgids[0], CCMIOINDEXC( kCCMIOStart ),
00731                    CCMIOINDEXC( kCCMIOEnd ) );
00732     CHK_SET_CCMERR( error, "Problem writing node map" );
00733 
00734     // Create the vertex coordinate node, and write it
00735     CCMIONewEntity( &error, rootID, kCCMIOVertices, "Vertices", &verticesID );
00736     CHK_SET_CCMERR( error, "Trouble creating vertices node" );
00737 
00738     // Get the vertex locations
00739     double* coords = new double[3 * num_verts];
00740     std::vector< double* > coord_arrays( 3 );
00741     // Cppcheck warning (false positive): variable coord_arrays is assigned a value that is never
00742     // used
00743     coord_arrays[0] = coords;
00744     coord_arrays[1] = coords + num_verts;
00745     coord_arrays[2] = ( dimension == 3 ? coords + 2 * num_verts : NULL );
00746     result          = mWriteIface->get_node_coords( -1, verts.begin(), verts.end(), 3 * num_verts, coords );
00747     if( result != MB_SUCCESS )
00748     {
00749         delete[] coords;
00750         return result;
00751     }
00752 
00753     // Transform coordinates, if necessary
00754     result = transform_coords( dimension, num_verts, coords );
00755     if( result != MB_SUCCESS )
00756     {
00757         delete[] coords;
00758         MB_SET_ERR( result, "Trouble transforming vertex coordinates" );
00759     }
00760 
00761     // Write the vertices
00762     CCMIOWriteVerticesd( &error, verticesID, CCMIOSIZEC( dimension ), 1.0, mapID, coords, CCMIOINDEXC( kCCMIOStart ),
00763                          CCMIOINDEXC( kCCMIOEnd ) );
00764     CHK_SET_CCMERR( error, "CCMIOWriteVertices failed" );
00765 
00766     // Clean up
00767     delete[] coords;
00768 
00769     return MB_SUCCESS;
00770 }
00771 
00772 ErrorCode WriteCCMIO::transform_coords( const int dimension, const int num_nodes, double* coords )
00773 {
00774     Tag trans_tag;
00775     ErrorCode result = mbImpl->tag_get_handle( MESH_TRANSFORM_TAG_NAME, 16, MB_TYPE_DOUBLE, trans_tag );
00776     if( result == MB_TAG_NOT_FOUND )
00777         return MB_SUCCESS;
00778     else if( MB_SUCCESS != result )
00779         return result;
00780     double trans_matrix[16];
00781     const EntityHandle mesh = 0;
00782     result                  = mbImpl->tag_get_data( trans_tag, &mesh, 1, trans_matrix );MB_CHK_SET_ERR( result, "Couldn't get transform data" );
00783 
00784     double* tmp_coords = coords;
00785     for( int i = 0; i < num_nodes; i++, tmp_coords += 1 )
00786     {
00787         double vec1[3] = { 0.0, 0.0, 0.0 };
00788         for( int row = 0; row < 3; row++ )
00789         {
00790             vec1[row] += ( trans_matrix[( row * 4 ) + 0] * coords[0] );
00791             vec1[row] += ( trans_matrix[( row * 4 ) + 1] * coords[num_nodes] );
00792             if( 3 == dimension ) vec1[row] += ( trans_matrix[( row * 4 ) + 2] * coords[2 * num_nodes] );
00793         }
00794 
00795         coords[0]             = vec1[0];
00796         coords[num_nodes]     = vec1[1];
00797         coords[2 * num_nodes] = vec1[2];
00798     }
00799 
00800     return MB_SUCCESS;
00801 }
00802 
00803 ErrorCode WriteCCMIO::write_cells_and_faces( CCMIOID rootID, std::vector< MaterialSetData >& matset_data,
00804                                              std::vector< NeumannSetData >& neuset_data, Range& /* verts */,
00805                                              CCMIOID& topologyID )
00806 {
00807     std::vector< int > connect;
00808     ErrorCode result;
00809     CCMIOID cellMapID, cells;
00810     CCMIOError error = kCCMIONoErr;
00811 
00812     // Don't usually have anywhere near 31 nodes per element
00813     connect.reserve( 31 );
00814     Range::const_iterator rit;
00815 
00816     // Create the topology node, and the cell and cell map nodes
00817     CCMIONewEntity( &error, rootID, kCCMIOTopology, "Topology", &topologyID );
00818     CHK_SET_CCMERR( error, "Trouble creating topology node" );
00819 
00820     CCMIONewEntity( &error, rootID, kCCMIOMap, "Cell map", &cellMapID );
00821     CHK_SET_CCMERR( error, "Failure creating Cell Map node" );
00822 
00823     CCMIONewEntity( &error, topologyID, kCCMIOCells, "Cells", &cells );
00824     CHK_SET_CCMERR( error, "Trouble creating Cell node under Topology node" );
00825 
00826     //================================================
00827     // Loop over material sets, doing each one at a time
00828     //================================================
00829     Range all_elems;
00830     unsigned int i, num_elems = 0;
00831     int max_id = 1;
00832     std::vector< int > egids;
00833     int tot_elems = 0;
00834 
00835     for( unsigned int m = 0; m < matset_data.size(); m++ )
00836         tot_elems += matset_data[m].elems.size();
00837 
00838     for( unsigned int m = 0; m < matset_data.size(); m++ )
00839     {
00840         unsigned int this_num = matset_data[m].elems.size();
00841 
00842         //================================================
00843         // Save all elements being output
00844         //================================================
00845         all_elems.merge( matset_data[m].elems );
00846 
00847         //================================================
00848         // Assign global ids for elements being written
00849         //================================================
00850         egids.resize( matset_data[m].elems.size() );
00851         for( i = 0; i < this_num; i++ )
00852             egids[i] = max_id++;
00853         result = mbImpl->tag_set_data( mGlobalIdTag, matset_data[m].elems, &egids[0] );MB_CHK_SET_ERR( result, "Failed to assign global ids for all elements being written" );
00854 
00855         //================================================
00856         // Write cell ids and material types for this matset; reuse egids for cell mat type
00857         //================================================
00858         CCMIOWriteMap( &error, cellMapID, CCMIOSIZEC( tot_elems ), CCMIOSIZEC( tot_elems ), &egids[0],
00859                        CCMIOINDEXC( 0 == m ? kCCMIOStart : num_elems ),
00860                        CCMIOINDEXC( matset_data.size() == m ? kCCMIOEnd : num_elems + this_num ) );
00861         CHK_SET_CCMERR( error, "Trouble writing cell map" );
00862 
00863         if( -1 == matset_data[m].matsetId )
00864         {
00865             for( i = 0; i < this_num; i++ )
00866                 egids[i] = m;
00867         }
00868         else
00869         {
00870             for( i = 0; i < this_num; i++ )
00871                 egids[i] = matset_data[m].matsetId;
00872         }
00873 
00874         CCMIOWriteCells( &error, cells, cellMapID, &egids[0], CCMIOINDEXC( 0 == m ? kCCMIOStart : num_elems ),
00875                          CCMIOINDEXC( matset_data.size() == m ? kCCMIOEnd : num_elems + this_num ) );
00876         CHK_SET_CCMERR( error, "Trouble writing Cell node" );
00877 
00878         //================================================
00879         // Write cell entity types
00880         //================================================
00881         const EntityHandle* conn;
00882         int num_conn;
00883         int has_mid_nodes[4];
00884         std::vector< EntityHandle > storage;
00885         for( i = 0, rit = matset_data[m].elems.begin(); i < this_num; i++, ++rit )
00886         {
00887             result = mbImpl->get_connectivity( *rit, conn, num_conn, false, &storage );MB_CHK_SET_ERR( result, "Trouble getting connectivity for entity type check" );
00888             CN::HasMidNodes( mbImpl->type_from_handle( *rit ), num_conn, has_mid_nodes );
00889             egids[i] = moab_to_ccmio_type( mbImpl->type_from_handle( *rit ), has_mid_nodes );
00890         }
00891 
00892         CCMIOWriteOpt1i( &error, cells, "CellTopologyType", CCMIOSIZEC( tot_elems ), &egids[0],
00893                          CCMIOINDEXC( 0 == m ? kCCMIOStart : num_elems ),
00894                          CCMIOINDEXC( matset_data.size() == m ? kCCMIOEnd : num_elems + this_num ) );
00895         CHK_SET_CCMERR( error, "Failed to write cell topo types" );
00896 
00897         num_elems += this_num;
00898     }
00899 
00900     //================================================
00901     // Get skin and neumann set faces
00902     //================================================
00903     Range neuset_facets, skin_facets;
00904     Skinner skinner( mbImpl );
00905     result = skinner.find_skin( 0, all_elems, mDimension - 1, skin_facets );MB_CHK_SET_ERR( result, "Failed to get skin facets" );
00906 
00907     // Remove neumann set facets from skin facets, we have to output these
00908     // separately
00909     for( i = 0; i < neuset_data.size(); i++ )
00910         neuset_facets.merge( neuset_data[i].elems );
00911 
00912     skin_facets -= neuset_facets;
00913     // Make neuset_facets the union, and get ids for them
00914     neuset_facets.merge( skin_facets );
00915     result = mWriteIface->assign_ids( neuset_facets, mGlobalIdTag, 1 );
00916 
00917     int fmaxid = neuset_facets.size();
00918 
00919     //================================================
00920     // Write external faces
00921     //================================================
00922     for( i = 0; i < neuset_data.size(); i++ )
00923     {
00924         Range::reverse_iterator rrit;
00925         unsigned char cmarks[2];
00926         Range ext_faces;
00927         std::vector< EntityHandle > mcells;
00928         // Removing the faces connected to two regions
00929         for( rrit = neuset_data[i].elems.rbegin(); rrit != neuset_data[i].elems.rend(); ++rrit )
00930         {
00931             mcells.clear();
00932             result = mbImpl->get_adjacencies( &( *rrit ), 1, mDimension, false, mcells );MB_CHK_SET_ERR( result, "Trouble getting bounding cells" );
00933 
00934             result = mbImpl->tag_get_data( mEntityMark, &mcells[0], mcells.size(), cmarks );MB_CHK_SET_ERR( result, "Trouble getting mark tags on cells bounding facets" );
00935 
00936             if( mcells.size() == 2 && ( mWholeMesh || ( cmarks[0] && cmarks[1] ) ) ) {}
00937             else
00938             {
00939                 // External face
00940                 ext_faces.insert( *rrit );
00941             }
00942         }
00943         if( ext_faces.size() != 0 && neuset_data[i].neusetId != 0 )
00944         {
00945             result = write_external_faces( rootID, topologyID, neuset_data[i].neusetId, ext_faces );MB_CHK_SET_ERR( result, "Trouble writing Neumann set facets" );
00946         }
00947         ext_faces.clear();
00948     }
00949 
00950     if( !skin_facets.empty() )
00951     {
00952         result = write_external_faces( rootID, topologyID, 0, skin_facets );MB_CHK_SET_ERR( result, "Trouble writing skin facets" );
00953     }
00954 
00955     //================================================
00956     // Now internal faces; loop over elements, do each face on the element
00957     //================================================
00958     // Mark tag, for face marking on each non-polyhedral element
00959 
00960     if( num_elems > 1 )
00961     {  // No internal faces for just one element
00962         Tag fmark_tag;
00963         unsigned char mval = 0x0, omval;
00964         result = mbImpl->tag_get_handle( "__fmark", 1, MB_TYPE_OPAQUE, fmark_tag, MB_TAG_DENSE | MB_TAG_CREAT, &mval );MB_CHK_SET_ERR( result, "Couldn't create mark tag" );
00965 
00966         std::vector< EntityHandle > tmp_face_cells, storage;
00967         std::vector< int > iface_connect, iface_cells;
00968         EntityHandle tmp_connect[CN::MAX_NODES_PER_ELEMENT];  // tmp connect vector
00969         const EntityHandle *connectc, *oconnectc;
00970         int num_connectc;  // Cell connectivity
00971         const EntityHandle* connectf;
00972         int num_connectf;  // Face connectivity
00973 
00974         for( i = 0, rit = all_elems.begin(); i < num_elems; i++, ++rit )
00975         {
00976             EntityType etype = TYPE_FROM_HANDLE( *rit );
00977 
00978             //-----------------------
00979             // If not polyh, get mark
00980             //-----------------------
00981             if( MBPOLYHEDRON != etype && MBPOLYGON != etype )
00982             {
00983                 result = mbImpl->tag_get_data( fmark_tag, &( *rit ), 1, &mval );MB_CHK_SET_ERR( result, "Couldn't get mark data" );
00984             }
00985 
00986             //-----------------------
00987             // Get cell connectivity, and whether it's a polyhedron
00988             //-----------------------
00989             result = mbImpl->get_connectivity( *rit, connectc, num_connectc, false, &storage );MB_CHK_SET_ERR( result, "Couldn't get entity connectivity" );
00990 
00991             // If polyh, write faces directly
00992             bool is_polyh = ( MBPOLYHEDRON == etype );
00993 
00994             int num_facets = ( is_polyh ? num_connectc : CN::NumSubEntities( etype, mDimension - 1 ) );
00995 
00996             //----------------------------------------------------------
00997             // Loop over each facet of element, outputting it if not marked
00998             //----------------------------------------------------------
00999             for( int f = 0; f < num_facets; f++ )
01000             {
01001                 //.............................................
01002                 // If this face marked, skip
01003                 //.............................................
01004                 if( !is_polyh && ( ( mval >> f ) & 0x1 ) ) continue;
01005 
01006                 //.................
01007                 // Get face connect and adj cells
01008                 //.................
01009                 if( !is_polyh )
01010                 {
01011                     // (from CN)
01012                     CN::SubEntityConn( connectc, etype, mDimension - 1, f, tmp_connect, num_connectf );
01013                     connectf = tmp_connect;
01014                 }
01015                 else
01016                 {
01017                     // Directly
01018                     result = mbImpl->get_connectivity( connectc[f], connectf, num_connectf, false );MB_CHK_SET_ERR( result, "Couldn't get polyhedron connectivity" );
01019                 }
01020 
01021                 //............................
01022                 // Get adj cells from face connect (same for poly's and not, since both usually
01023                 // go through vertices anyway)
01024                 //............................
01025                 tmp_face_cells.clear();
01026                 result = mbImpl->get_adjacencies( connectf, num_connectf, mDimension, false, tmp_face_cells );MB_CHK_SET_ERR( result, "Error getting adj hexes" );
01027 
01028                 //...............................
01029                 // If this face only bounds one cell, skip, since we exported external faces
01030                 // before this loop
01031                 //...............................
01032                 if( tmp_face_cells.size() != 2 ) continue;
01033 
01034                 //.................
01035                 // Switch cells so that *rit is always 1st (face connectivity is always written such
01036                 // that that one is with forward sense)
01037                 //.................
01038                 int side_num = 0, sense = 0, offset = 0;
01039                 if( !is_polyh && tmp_face_cells[0] != *rit )
01040                 {
01041                     EntityHandle tmph = tmp_face_cells[0];
01042                     tmp_face_cells[0] = tmp_face_cells[1];
01043                     tmp_face_cells[1] = tmph;
01044                 }
01045 
01046                 //.................
01047                 // Save ids of cells
01048                 //.................
01049                 assert( tmp_face_cells[0] != tmp_face_cells[1] );
01050                 iface_cells.resize( iface_cells.size() + 2 );
01051                 result = mbImpl->tag_get_data( mGlobalIdTag, &tmp_face_cells[0], tmp_face_cells.size(),
01052                                                &iface_cells[iface_cells.size() - 2] );MB_CHK_SET_ERR( result, "Trouble getting global ids for bounded cells" );
01053                 iface_connect.push_back( num_connectf );
01054 
01055                 //.................
01056                 // Save indices of face vertices
01057                 //.................
01058                 unsigned int tmp_size = iface_connect.size();
01059                 iface_connect.resize( tmp_size + num_connectf );
01060                 result = mbImpl->tag_get_data( mGlobalIdTag, connectf, num_connectf, &iface_connect[tmp_size] );MB_CHK_SET_ERR( result, "Trouble getting global id for internal face" );
01061 
01062                 //.................
01063                 // Mark other cell with the right side #
01064                 //.................
01065                 if( !is_polyh )
01066                 {
01067                     // Mark other cell for this face, if there is another cell
01068 
01069                     result = mbImpl->get_connectivity( tmp_face_cells[1], oconnectc, num_connectc, false, &storage );MB_CHK_SET_ERR( result, "Couldn't get other entity connectivity" );
01070 
01071                     // Get side number in other cell
01072                     CN::SideNumber( TYPE_FROM_HANDLE( tmp_face_cells[1] ), oconnectc, connectf, num_connectf,
01073                                     mDimension - 1, side_num, sense, offset );
01074                     // Set mark for that face on the other cell
01075                     result = mbImpl->tag_get_data( fmark_tag, &tmp_face_cells[1], 1, &omval );MB_CHK_SET_ERR( result, "Couldn't get mark data for other cell" );
01076                 }
01077 
01078                 omval |= ( 0x1 << (unsigned int)side_num );
01079                 result = mbImpl->tag_set_data( fmark_tag, &tmp_face_cells[1], 1, &omval );MB_CHK_SET_ERR( result, "Couldn't set mark data for other cell" );
01080             }  // Loop over faces in elem
01081         }      // Loop over elems
01082 
01083         //================================================
01084         // Write internal faces
01085         //================================================
01086 
01087         CCMIOID mapID;
01088         CCMIONewEntity( &error, rootID, kCCMIOMap, NULL, &mapID );
01089         CHK_SET_CCMERR( error, "Trouble creating Internal Face map node" );
01090 
01091         unsigned int num_ifaces = iface_cells.size() / 2;
01092 
01093         // Set gids for internal faces; reuse egids
01094         egids.resize( num_ifaces );
01095         for( i = 1; i <= num_ifaces; i++ )
01096             egids[i - 1] = fmaxid + i;
01097         CCMIOWriteMap( &error, mapID, CCMIOSIZEC( num_ifaces ), CCMIOSIZEC( fmaxid + num_ifaces ), &egids[0],
01098                        CCMIOINDEXC( kCCMIOStart ), CCMIOINDEXC( kCCMIOEnd ) );
01099         CHK_SET_CCMERR( error, "Trouble writing Internal Face map node" );
01100 
01101         CCMIOID id;
01102         CCMIONewEntity( &error, topologyID, kCCMIOInternalFaces, "Internal faces", &id );
01103         CHK_SET_CCMERR( error, "Failed to create Internal face node under Topology node" );
01104         CCMIOWriteFaces( &error, id, kCCMIOInternalFaces, mapID, CCMIOSIZEC( iface_connect.size() ), &iface_connect[0],
01105                          CCMIOINDEXC( kCCMIOStart ), CCMIOINDEXC( kCCMIOEnd ) );
01106         CHK_SET_CCMERR( error, "Failure writing Internal face connectivity" );
01107         CCMIOWriteFaceCells( &error, id, kCCMIOInternalFaces, mapID, &iface_cells[0], CCMIOINDEXC( kCCMIOStart ),
01108                              CCMIOINDEXC( kCCMIOEnd ) );
01109         CHK_SET_CCMERR( error, "Failure writing Internal face cells" );
01110     }
01111 
01112     return MB_SUCCESS;
01113 }
01114 
01115 int WriteCCMIO::moab_to_ccmio_type( EntityType etype, int has_mid_nodes[] )
01116 {
01117     int ctype = -1;
01118     if( has_mid_nodes[0] || has_mid_nodes[2] || has_mid_nodes[3] ) return ctype;
01119 
01120     switch( etype )
01121     {
01122         case MBVERTEX:
01123             ctype = 1;
01124             break;
01125         case MBEDGE:
01126             if( !has_mid_nodes[1] )
01127                 ctype = 2;
01128             else
01129                 ctype = 28;
01130             break;
01131         case MBQUAD:
01132             if( has_mid_nodes[1] )
01133                 ctype = 4;
01134             else
01135                 ctype = 3;
01136             break;
01137         case MBTET:
01138             if( has_mid_nodes[1] )
01139                 ctype = 23;
01140             else
01141                 ctype = 13;
01142             break;
01143         case MBPRISM:
01144             if( has_mid_nodes[1] )
01145                 ctype = 22;
01146             else
01147                 ctype = 12;
01148             break;
01149         case MBPYRAMID:
01150             if( has_mid_nodes[1] )
01151                 ctype = 24;
01152             else
01153                 ctype = 14;
01154             break;
01155         case MBHEX:
01156             if( has_mid_nodes[1] )
01157                 ctype = 21;
01158             else
01159                 ctype = 11;
01160             break;
01161         case MBPOLYHEDRON:
01162             ctype = 255;
01163             break;
01164         default:
01165             break;
01166     }
01167 
01168     return ctype;
01169 }
01170 
01171 ErrorCode WriteCCMIO::write_external_faces( CCMIOID rootID, CCMIOID topologyID, int set_num, Range& facets )
01172 {
01173     CCMIOError error = kCCMIONoErr;
01174     CCMIOID mapID, id;
01175 
01176     // Get gids for these faces
01177     int *gids        = NULL, minid, maxid;
01178     ErrorCode result = get_gids( facets, gids, minid, maxid );MB_CHK_SET_ERR( result, "Trouble getting global ids for facets" );
01179 
01180     // Write the face id map
01181     CCMIONewEntity( &error, rootID, kCCMIOMap, NULL, &mapID );
01182     CHK_SET_CCMERR( error, "Problem creating face id map" );
01183 
01184     CCMIOWriteMap( &error, mapID, CCMIOSIZEC( facets.size() ), CCMIOSIZEC( maxid ), gids, CCMIOINDEXC( kCCMIOStart ),
01185                    CCMIOINDEXC( kCCMIOEnd ) );
01186     CHK_SET_CCMERR( error, "Problem writing face id map" );
01187 
01188     // Get the connectivity of the faces; set size by how many verts in last facet
01189     const EntityHandle* connect;
01190     int num_connect;
01191     result = mbImpl->get_connectivity( *facets.rbegin(), connect, num_connect );MB_CHK_SET_ERR( result, "Failed to get connectivity of last facet" );
01192     std::vector< int > fconnect( facets.size() * ( num_connect + 1 ) );
01193 
01194     result = mWriteIface->get_element_connect( facets.begin(), facets.end(), num_connect, mGlobalIdTag, fconnect.size(),
01195                                                &fconnect[0], true );MB_CHK_SET_ERR( result, "Failed to get facet connectivity" );
01196 
01197     // Get and write a new external face entity
01198     CCMIONewIndexedEntity( &error, topologyID, kCCMIOBoundaryFaces, set_num, "Boundary faces", &id );
01199     CHK_SET_CCMERR( error, "Problem creating boundary face entity" );
01200 
01201     CCMIOWriteFaces( &error, id, kCCMIOBoundaryFaces, mapID, CCMIOSIZEC( fconnect.size() ), &fconnect[0],
01202                      CCMIOINDEXC( kCCMIOStart ), CCMIOINDEXC( kCCMIOEnd ) );
01203     CHK_SET_CCMERR( error, "Problem writing boundary faces" );
01204 
01205     // Get info on bounding cells; reuse fconnect
01206     std::vector< EntityHandle > cells;
01207     unsigned char cmarks[2];
01208     int i, j = 0;
01209     Range dead_facets;
01210     Range::iterator rit;
01211 
01212     // About error checking in this loop: if any facets have no bounding cells,
01213     // this is an error, since global ids for facets are computed outside this loop
01214     for( rit = facets.begin(), i = 0; rit != facets.end(); ++rit, i++ )
01215     {
01216         cells.clear();
01217 
01218         // Get cell then gid of cell
01219         result = mbImpl->get_adjacencies( &( *rit ), 1, mDimension, false, cells );MB_CHK_SET_ERR( result, "Trouble getting bounding cells" );
01220         if( cells.empty() ) { MB_SET_ERR( MB_FILE_WRITE_ERROR, "External facet with no output bounding cell" ); }
01221 
01222         // Check we don't bound more than one cell being output
01223         result = mbImpl->tag_get_data( mEntityMark, &cells[0], cells.size(), cmarks );MB_CHK_SET_ERR( result, "Trouble getting mark tags on cells bounding facets" );
01224         if( cells.size() == 2 && ( mWholeMesh || ( cmarks[0] && cmarks[1] ) ) )
01225         { MB_SET_ERR( MB_FILE_WRITE_ERROR, "External facet with two output bounding cells" ); }
01226         else if( 1 == cells.size() && !mWholeMesh && !cmarks[0] )
01227         {
01228             MB_SET_ERR( MB_FILE_WRITE_ERROR, "External facet with no output bounding cells" );
01229         }
01230 
01231         // Make sure 1st cell is the one being output
01232         if( 2 == cells.size() && !( cmarks[0] | 0x0 ) && ( cmarks[1] & 0x1 ) ) cells[0] = cells[1];
01233 
01234         // Get gid for bounded cell
01235         result = mbImpl->tag_get_data( mGlobalIdTag, &cells[0], 1, &fconnect[j] );MB_CHK_SET_ERR( result, "Couldn't get global id tag for bounded cell" );
01236 
01237         j++;
01238     }
01239 
01240     // Write the bounding cell data
01241     CCMIOWriteFaceCells( &error, id, kCCMIOBoundaryFaces, mapID, &fconnect[0], CCMIOINDEXC( kCCMIOStart ),
01242                          CCMIOINDEXC( kCCMIOEnd ) );
01243     CHK_SET_CCMERR( error, "Problem writing boundary cell data" );
01244 
01245     return MB_SUCCESS;
01246 }
01247 
01248 ErrorCode WriteCCMIO::get_neuset_elems( EntityHandle neuset, int current_sense, Range& forward_elems,
01249                                         Range& reverse_elems )
01250 {
01251     Range neuset_elems, neuset_meshsets;
01252 
01253     // Get the sense tag; don't need to check return, might be an error if the tag
01254     // hasn't been created yet
01255     Tag sense_tag = 0;
01256     mbImpl->tag_get_handle( "SENSE", 1, MB_TYPE_INTEGER, sense_tag );
01257 
01258     // Get the entities in this set, non-recursive
01259     ErrorCode result = mbImpl->get_entities_by_handle( neuset, neuset_elems );
01260     if( MB_FAILURE == result ) return result;
01261 
01262     // Now remove the meshsets into the neuset_meshsets; first find the first meshset,
01263     Range::iterator range_iter = neuset_elems.begin();
01264     while( TYPE_FROM_HANDLE( *range_iter ) != MBENTITYSET && range_iter != neuset_elems.end() )
01265         ++range_iter;
01266 
01267     // Then, if there are some, copy them into neuset_meshsets and erase from neuset_elems
01268     if( range_iter != neuset_elems.end() )
01269     {
01270         std::copy( range_iter, neuset_elems.end(), range_inserter( neuset_meshsets ) );
01271         neuset_elems.erase( range_iter, neuset_elems.end() );
01272     }
01273 
01274     // OK, for the elements, check the sense of this set and copy into the right range
01275     // (if the sense is 0, copy into both ranges)
01276 
01277     // Need to step forward on list until we reach the right dimension
01278     Range::iterator dum_it = neuset_elems.end();
01279     --dum_it;
01280     int target_dim = CN::Dimension( TYPE_FROM_HANDLE( *dum_it ) );
01281     dum_it         = neuset_elems.begin();
01282     while( target_dim != CN::Dimension( TYPE_FROM_HANDLE( *dum_it ) ) && dum_it != neuset_elems.end() )
01283         ++dum_it;
01284 
01285     if( current_sense == 1 || current_sense == 0 )
01286         std::copy( dum_it, neuset_elems.end(), range_inserter( forward_elems ) );
01287     if( current_sense == -1 || current_sense == 0 )
01288         std::copy( dum_it, neuset_elems.end(), range_inserter( reverse_elems ) );
01289 
01290     // Now loop over the contained meshsets, getting the sense of those and calling this
01291     // function recursively
01292     for( range_iter = neuset_meshsets.begin(); range_iter != neuset_meshsets.end(); ++range_iter )
01293     {
01294         // First get the sense; if it's not there, by convention it's forward
01295         int this_sense;
01296         if( 0 == sense_tag || MB_FAILURE == mbImpl->tag_get_data( sense_tag, &( *range_iter ), 1, &this_sense ) )
01297             this_sense = 1;
01298 
01299         // Now get all the entities on this meshset, with the proper (possibly reversed) sense
01300         get_neuset_elems( *range_iter, this_sense * current_sense, forward_elems, reverse_elems );
01301     }
01302 
01303     return result;
01304 }
01305 }  // namespace moab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines