Mesh Oriented datABase
(version 5.4.1)
Array-based unstructured mesh datastructure
|
00001 /* 00002 * NCWriteGCRM.cpp 00003 * 00004 * Created on: April 9, 2014 00005 */ 00006 00007 #include "NCWriteGCRM.hpp" 00008 #include "MBTagConventions.hpp" 00009 00010 namespace moab 00011 { 00012 00013 NCWriteGCRM::~NCWriteGCRM() 00014 { 00015 // TODO Auto-generated destructor stub 00016 } 00017 00018 ErrorCode NCWriteGCRM::collect_mesh_info() 00019 { 00020 Interface*& mbImpl = _writeNC->mbImpl; 00021 std::vector< std::string >& dimNames = _writeNC->dimNames; 00022 std::vector< int >& dimLens = _writeNC->dimLens; 00023 Tag& mGlobalIdTag = _writeNC->mGlobalIdTag; 00024 00025 ErrorCode rval; 00026 00027 // Look for time dimension 00028 std::vector< std::string >::iterator vecIt; 00029 if( ( vecIt = std::find( dimNames.begin(), dimNames.end(), "Time" ) ) != dimNames.end() ) 00030 tDim = vecIt - dimNames.begin(); 00031 else if( ( vecIt = std::find( dimNames.begin(), dimNames.end(), "time" ) ) != dimNames.end() ) 00032 tDim = vecIt - dimNames.begin(); 00033 else 00034 { 00035 MB_SET_ERR( MB_FAILURE, "Couldn't find 'Time' or 'time' dimension" ); 00036 } 00037 nTimeSteps = dimLens[tDim]; 00038 00039 // Get number of levels 00040 if( ( vecIt = std::find( dimNames.begin(), dimNames.end(), "layers" ) ) != dimNames.end() ) 00041 levDim = vecIt - dimNames.begin(); 00042 else 00043 { 00044 MB_SET_ERR( MB_FAILURE, "Couldn't find 'layers' dimension" ); 00045 } 00046 nLevels = dimLens[levDim]; 00047 00048 // Get local vertices 00049 rval = mbImpl->get_entities_by_dimension( _fileSet, 0, localVertsOwned );MB_CHK_SET_ERR( rval, "Trouble getting local vertices in current file set" ); 00050 assert( !localVertsOwned.empty() ); 00051 00052 // Get local edges 00053 rval = mbImpl->get_entities_by_dimension( _fileSet, 1, localEdgesOwned );MB_CHK_SET_ERR( rval, "Trouble getting local edges in current file set" ); 00054 // There are no edges if NO_EDGES read option is set 00055 00056 // Get local cells 00057 rval = mbImpl->get_entities_by_dimension( _fileSet, 2, localCellsOwned );MB_CHK_SET_ERR( rval, "Trouble getting local cells in current file set" ); 00058 assert( !localCellsOwned.empty() ); 00059 00060 #ifdef MOAB_HAVE_MPI 00061 bool& isParallel = _writeNC->isParallel; 00062 if( isParallel ) 00063 { 00064 ParallelComm*& myPcomm = _writeNC->myPcomm; 00065 int rank = myPcomm->proc_config().proc_rank(); 00066 int procs = myPcomm->proc_config().proc_size(); 00067 if( procs > 1 ) 00068 { 00069 #ifndef NDEBUG 00070 unsigned int num_local_verts = localVertsOwned.size(); 00071 #endif 00072 rval = myPcomm->filter_pstatus( localVertsOwned, PSTATUS_NOT_OWNED, PSTATUS_NOT );MB_CHK_SET_ERR( rval, "Trouble getting owned vertices in current file set" ); 00073 00074 // Assume that PARALLEL_RESOLVE_SHARED_ENTS option is set 00075 // Verify that not all local vertices are owned by the last processor 00076 if( procs - 1 == rank ) 00077 assert( "PARALLEL_RESOLVE_SHARED_ENTS option is set" && localVertsOwned.size() < num_local_verts ); 00078 00079 if( !localEdgesOwned.empty() ) 00080 { 00081 rval = myPcomm->filter_pstatus( localEdgesOwned, PSTATUS_NOT_OWNED, PSTATUS_NOT );MB_CHK_SET_ERR( rval, "Trouble getting owned edges in current file set" ); 00082 } 00083 00084 rval = myPcomm->filter_pstatus( localCellsOwned, PSTATUS_NOT_OWNED, PSTATUS_NOT );MB_CHK_SET_ERR( rval, "Trouble getting owned cells in current file set" ); 00085 } 00086 } 00087 #endif 00088 00089 std::vector< int > gids( localVertsOwned.size() ); 00090 rval = mbImpl->tag_get_data( mGlobalIdTag, localVertsOwned, &gids[0] );MB_CHK_SET_ERR( rval, "Trouble getting global IDs on local vertices" ); 00091 00092 // Get localGidVertsOwned 00093 std::copy( gids.rbegin(), gids.rend(), range_inserter( localGidVertsOwned ) ); 00094 00095 if( !localEdgesOwned.empty() ) 00096 { 00097 gids.resize( localEdgesOwned.size() ); 00098 rval = mbImpl->tag_get_data( mGlobalIdTag, localEdgesOwned, &gids[0] );MB_CHK_SET_ERR( rval, "Trouble getting global IDs on local edges" ); 00099 00100 // Get localGidEdgesOwned 00101 std::copy( gids.rbegin(), gids.rend(), range_inserter( localGidEdgesOwned ) ); 00102 } 00103 00104 gids.resize( localCellsOwned.size() ); 00105 rval = mbImpl->tag_get_data( mGlobalIdTag, localCellsOwned, &gids[0] );MB_CHK_SET_ERR( rval, "Trouble getting global IDs on local cells" ); 00106 00107 // Get localGidCellsOwned 00108 std::copy( gids.rbegin(), gids.rend(), range_inserter( localGidCellsOwned ) ); 00109 00110 return MB_SUCCESS; 00111 } 00112 00113 ErrorCode NCWriteGCRM::collect_variable_data( std::vector< std::string >& var_names, std::vector< int >& tstep_nums ) 00114 { 00115 NCWriteHelper::collect_variable_data( var_names, tstep_nums ); 00116 00117 std::vector< std::string >& dimNames = _writeNC->dimNames; 00118 std::vector< int >& dimLens = _writeNC->dimLens; 00119 00120 // Dimension indices for other optional levels 00121 std::vector< unsigned int > opt_lev_dims; 00122 00123 unsigned int lev_idx; 00124 std::vector< std::string >::iterator vecIt; 00125 00126 // Get index of interface levels 00127 if( ( vecIt = std::find( dimNames.begin(), dimNames.end(), "interfaces" ) ) != dimNames.end() ) 00128 { 00129 lev_idx = vecIt - dimNames.begin(); 00130 opt_lev_dims.push_back( lev_idx ); 00131 } 00132 00133 std::map< std::string, WriteNC::VarData >& varInfo = _writeNC->varInfo; 00134 00135 for( size_t i = 0; i < var_names.size(); i++ ) 00136 { 00137 std::string varname = var_names[i]; 00138 std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( varname ); 00139 if( vit == varInfo.end() ) MB_SET_ERR( MB_FAILURE, "Can't find variable " << varname ); 00140 00141 WriteNC::VarData& currentVarData = vit->second; 00142 std::vector< int >& varDims = currentVarData.varDims; 00143 00144 // Skip edge variables, if there are no edges 00145 if( localEdgesOwned.empty() && currentVarData.entLoc == WriteNC::ENTLOCEDGE ) continue; 00146 00147 // If layers dimension is not found, try other optional levels such as interfaces 00148 if( std::find( varDims.begin(), varDims.end(), levDim ) == varDims.end() ) 00149 { 00150 for( unsigned int j = 0; j < opt_lev_dims.size(); j++ ) 00151 { 00152 if( std::find( varDims.begin(), varDims.end(), opt_lev_dims[j] ) != varDims.end() ) 00153 { 00154 currentVarData.numLev = dimLens[opt_lev_dims[j]]; 00155 break; 00156 } 00157 } 00158 } 00159 00160 // Skip set variables, which were already processed in 00161 // NCWriteHelper::collect_variable_data() 00162 if( WriteNC::ENTLOCSET == currentVarData.entLoc ) continue; 00163 00164 // Set up writeStarts and writeCounts (maximum number of dimensions is 3) 00165 currentVarData.writeStarts.resize( 3 ); 00166 currentVarData.writeCounts.resize( 3 ); 00167 unsigned int dim_idx = 0; 00168 00169 // First: time 00170 if( currentVarData.has_tsteps ) 00171 { 00172 // Non-set variables with timesteps 00173 // 3 dimensions like (time, cells, layers) 00174 // 2 dimensions like (time, cells) 00175 assert( 3 == varDims.size() || 2 == varDims.size() ); 00176 00177 // Time should be the first dimension 00178 assert( tDim == varDims[0] ); 00179 00180 currentVarData.writeStarts[dim_idx] = 0; // This value is timestep dependent, will be set later 00181 currentVarData.writeCounts[dim_idx] = 1; 00182 dim_idx++; 00183 } 00184 else 00185 { 00186 // Non-set variables without timesteps 00187 // 2 dimensions like (cells, layers) 00188 // 1 dimension like (cells) 00189 assert( 2 == varDims.size() || 1 == varDims.size() ); 00190 } 00191 00192 // Next: corners / cells / edges 00193 switch( currentVarData.entLoc ) 00194 { 00195 case WriteNC::ENTLOCVERT: 00196 // Vertices 00197 // Start from the first localGidVerts 00198 // Actually, this will be reset later for writing 00199 currentVarData.writeStarts[dim_idx] = localGidVertsOwned[0] - 1; 00200 currentVarData.writeCounts[dim_idx] = localGidVertsOwned.size(); 00201 break; 00202 case WriteNC::ENTLOCFACE: 00203 // Faces 00204 // Start from the first localGidCells 00205 // Actually, this will be reset later for writing 00206 currentVarData.writeStarts[dim_idx] = localGidCellsOwned[0] - 1; 00207 currentVarData.writeCounts[dim_idx] = localGidCellsOwned.size(); 00208 break; 00209 case WriteNC::ENTLOCEDGE: 00210 // Edges 00211 // Start from the first localGidEdges 00212 // Actually, this will be reset later for writing 00213 currentVarData.writeStarts[dim_idx] = localGidEdgesOwned[0] - 1; 00214 currentVarData.writeCounts[dim_idx] = localGidEdgesOwned.size(); 00215 break; 00216 default: 00217 MB_SET_ERR( MB_FAILURE, "Unexpected entity location type for variable " << varname ); 00218 } 00219 dim_idx++; 00220 00221 // Finally: layers or other optional levels, it is possible that there is no 00222 // level dimension (numLev is 0) for this non-set variable 00223 if( currentVarData.numLev > 0 ) 00224 { 00225 // Non-set variables with levels 00226 // 3 dimensions like (time, cells, layers) 00227 // 2 dimensions like (cells, layers) 00228 assert( 3 == varDims.size() || 2 == varDims.size() ); 00229 00230 currentVarData.writeStarts[dim_idx] = 0; 00231 currentVarData.writeCounts[dim_idx] = currentVarData.numLev; 00232 dim_idx++; 00233 } 00234 else 00235 { 00236 // Non-set variables without levels 00237 // 2 dimensions like (time, cells) 00238 // 1 dimension like (cells) 00239 assert( 2 == varDims.size() || 1 == varDims.size() ); 00240 } 00241 00242 // Get variable size 00243 currentVarData.sz = 1; 00244 for( std::size_t idx = 0; idx < dim_idx; idx++ ) 00245 currentVarData.sz *= currentVarData.writeCounts[idx]; 00246 } // for (size_t i = 0; i < var_names.size(); i++) 00247 00248 return MB_SUCCESS; 00249 } 00250 00251 ErrorCode NCWriteGCRM::write_nonset_variables( std::vector< WriteNC::VarData >& vdatas, std::vector< int >& tstep_nums ) 00252 { 00253 Interface*& mbImpl = _writeNC->mbImpl; 00254 00255 int success; 00256 00257 // For each indexed variable tag, write a time step data 00258 for( unsigned int i = 0; i < vdatas.size(); i++ ) 00259 { 00260 WriteNC::VarData& variableData = vdatas[i]; 00261 00262 // Skip edge variables, if there are no edges 00263 if( localEdgesOwned.empty() && variableData.entLoc == WriteNC::ENTLOCEDGE ) continue; 00264 00265 // Get local owned entities of this variable 00266 Range* pLocalEntsOwned = NULL; 00267 Range* pLocalGidEntsOwned = NULL; 00268 switch( variableData.entLoc ) 00269 { 00270 case WriteNC::ENTLOCVERT: 00271 // Vertices 00272 pLocalEntsOwned = &localVertsOwned; 00273 pLocalGidEntsOwned = &localGidVertsOwned; 00274 break; 00275 case WriteNC::ENTLOCEDGE: 00276 // Edges 00277 pLocalEntsOwned = &localEdgesOwned; 00278 pLocalGidEntsOwned = &localGidEdgesOwned; 00279 break; 00280 case WriteNC::ENTLOCFACE: 00281 // Cells 00282 pLocalEntsOwned = &localCellsOwned; 00283 pLocalGidEntsOwned = &localGidCellsOwned; 00284 break; 00285 default: 00286 MB_SET_ERR( MB_FAILURE, "Unexpected entity location type for variable " << variableData.varName ); 00287 } 00288 00289 unsigned int num_timesteps; 00290 unsigned int ents_idx = 0; 00291 if( variableData.has_tsteps ) 00292 { 00293 // Non-set variables with timesteps 00294 // 3 dimensions like (time, cells, layers) 00295 // 2 dimensions like (time, cells) 00296 num_timesteps = tstep_nums.size(); 00297 ents_idx++; 00298 } 00299 else 00300 { 00301 // Non-set variables without timesteps 00302 // 2 dimensions like (cells, layers) 00303 // 1 dimension like (cells) 00304 num_timesteps = 1; 00305 } 00306 00307 unsigned int num_lev; 00308 if( variableData.numLev > 0 ) 00309 { 00310 // Non-set variables with levels 00311 // 3 dimensions like (time, cells, layers) 00312 // 2 dimensions like (cells, layers) 00313 num_lev = variableData.numLev; 00314 } 00315 else 00316 { 00317 // Non-set variables without levels 00318 // 2 dimensions like (time, cells) 00319 // 1 dimension like (cells) 00320 num_lev = 1; 00321 } 00322 00323 for( unsigned int t = 0; t < num_timesteps; t++ ) 00324 { 00325 // We will write one time step, and count will be one; start will be different 00326 // Use tag_get_data instead of tag_iterate to copy tag data, as localEntsOwned 00327 // might not be contiguous. 00328 if( tDim == variableData.varDims[0] ) variableData.writeStarts[0] = t; // This is start for time 00329 std::vector< double > tag_data( pLocalEntsOwned->size() * num_lev ); 00330 ErrorCode rval = mbImpl->tag_get_data( variableData.varTags[t], *pLocalEntsOwned, &tag_data[0] );MB_CHK_SET_ERR( rval, "Trouble getting tag data on owned entities" ); 00331 00332 #ifdef MOAB_HAVE_PNETCDF 00333 size_t nb_writes = pLocalGidEntsOwned->psize(); 00334 std::vector< int > requests( nb_writes ), statuss( nb_writes ); 00335 size_t idxReq = 0; 00336 #endif 00337 00338 // Now write copied tag data 00339 // Use nonblocking put (request aggregation) 00340 switch( variableData.varDataType ) 00341 { 00342 case NC_DOUBLE: { 00343 size_t indexInDoubleArray = 0; 00344 size_t ic = 0; 00345 for( Range::pair_iterator pair_iter = pLocalGidEntsOwned->pair_begin(); 00346 pair_iter != pLocalGidEntsOwned->pair_end(); ++pair_iter, ic++ ) 00347 { 00348 EntityHandle starth = pair_iter->first; 00349 EntityHandle endh = pair_iter->second; 00350 variableData.writeStarts[ents_idx] = (NCDF_SIZE)( starth - 1 ); 00351 variableData.writeCounts[ents_idx] = (NCDF_SIZE)( endh - starth + 1 ); 00352 00353 // Do a partial write, in each subrange 00354 #ifdef MOAB_HAVE_PNETCDF 00355 // Wait outside this loop 00356 success = 00357 NCFUNCREQP( _vara_double )( _fileId, variableData.varId, &( variableData.writeStarts[0] ), 00358 &( variableData.writeCounts[0] ), 00359 &( tag_data[indexInDoubleArray] ), &requests[idxReq++] ); 00360 #else 00361 success = NCFUNCAP( 00362 _vara_double )( _fileId, variableData.varId, &( variableData.writeStarts[0] ), 00363 &( variableData.writeCounts[0] ), &( tag_data[indexInDoubleArray] ) ); 00364 #endif 00365 if( success ) 00366 MB_SET_ERR( MB_FAILURE, 00367 "Failed to write double data in a loop for variable " << variableData.varName ); 00368 // We need to increment the index in double array for the 00369 // next subrange 00370 indexInDoubleArray += ( endh - starth + 1 ) * num_lev; 00371 } 00372 assert( ic == pLocalGidEntsOwned->psize() ); 00373 #ifdef MOAB_HAVE_PNETCDF 00374 success = ncmpi_wait_all( _fileId, requests.size(), &requests[0], &statuss[0] ); 00375 if( success ) MB_SET_ERR( MB_FAILURE, "Failed on wait_all" ); 00376 #endif 00377 break; 00378 } 00379 default: 00380 MB_SET_ERR( MB_NOT_IMPLEMENTED, "Writing non-double data is not implemented yet" ); 00381 } 00382 } 00383 } 00384 00385 return MB_SUCCESS; 00386 } 00387 00388 } /* namespace moab */