17 #pragma warning( disable : 4786 )
48 #define INS_ID( stringvar, prefix, id, length ) snprintf( stringvar, length, prefix, id )
50 #define GET_DIM( ncdim, name, val ) \
52 int gdfail = nc_inq_dimid( ncFile, name, &( ncdim ) ); \
53 if( NC_NOERR == gdfail ) \
56 gdfail = nc_inq_dimlen( ncFile, ncdim, &tmp_val ); \
57 if( NC_NOERR != gdfail ) \
59 MB_SET_ERR( MB_FAILURE, "ReadNCDF:: Couldn't get dimension length" ); \
68 #define GET_DIMB( ncdim, name, varname, id, val ) \
69 INS_ID( name, varname, id, max_str_length + 1 ); \
70 GET_DIM( ncdim, name, val );
72 #define GET_VAR( name, id, dims ) \
75 int gvfail = nc_inq_varid( ncFile, name, &( id ) ); \
76 if( NC_NOERR == gvfail ) \
79 gvfail = nc_inq_varndims( ncFile, id, &ndims ); \
80 if( NC_NOERR == gvfail ) \
82 ( dims ).resize( ndims ); \
83 gvfail = nc_inq_vardimid( ncFile, id, &( dims )[0] ); \
84 if( NC_NOERR != gvfail ) \
86 MB_SET_ERR( MB_FAILURE, "ReadNCDF:: Couldn't get variable dimension IDs" ); \
92 #define GET_1D_INT_VAR( name, id, vals ) \
94 GET_VAR( name, id, vals ); \
98 int ivfail = nc_inq_dimlen( ncFile, ( vals )[0], &ntmp ); \
99 if( NC_NOERR != ivfail ) \
101 MB_SET_ERR( MB_FAILURE, "ReadNCDF:: Couldn't get dimension length" ); \
103 ( vals ).resize( ntmp ); \
105 ivfail = nc_get_vara_int( ncFile, id, &ntmp1, &ntmp, &( vals )[0] ); \
106 if( NC_NOERR != ivfail ) \
108 MB_SET_ERR( MB_FAILURE, "ReadNCDF:: Problem getting variable " << ( name ) ); \
113 #define GET_1D_DBL_VAR( name, id, vals ) \
115 std::vector< int > dum_dims; \
116 GET_VAR( name, id, dum_dims ); \
120 int dvfail = nc_inq_dimlen( ncFile, dum_dims[0], &ntmp ); \
121 if( NC_NOERR != dvfail ) \
123 MB_SET_ERR( MB_FAILURE, "ReadNCDF:: Couldn't get dimension length" ); \
125 ( vals ).resize( ntmp ); \
127 dvfail = nc_get_vara_double( ncFile, id, &ntmp1, &ntmp, &( vals )[0] ); \
128 if( NC_NOERR != dvfail ) \
130 MB_SET_ERR( MB_FAILURE, "ReadNCDF:: Problem getting variable " << ( name ) ); \
142 assert( impl != NULL );
158 const int negone = -1;
168 const int mids[] = { -1, -1, -1, -1 };
215 const char* tag_name,
217 std::vector< int >& id_array,
227 if( NC_NOWRITE !=
fail )
234 if( MB_FAILURE == rval )
return rval;
238 const char* blocks =
"eb_prop1";
239 const char* nodesets =
"ns_prop1";
240 const char* sidesets =
"ss_prop1";
269 MB_SET_ERR( MB_FAILURE,
"Problem getting prop variable" );
275 if( NC_NOERR !=
fail )
277 MB_SET_ERR( MB_FAILURE,
"Trouble closing file" );
288 const Tag* file_id_tag )
294 const int* blocks_to_load = 0;
317 return update( exodus_file_name, opts, num_blocks, blocks_to_load, *file_set );
324 fail = nc_open( exodus_file_name, 0, &
ncFile );
325 if( NC_NOERR !=
fail )
332 if( MB_FAILURE == status )
return status;
335 if( MB_FAILURE == status )
return status;
339 if( MB_FAILURE == status )
return status;
346 if( MB_FAILURE == status )
return status;
350 if( MB_FAILURE == status )
return status;
356 if( MB_FAILURE == status )
return status;
359 if( MB_FAILURE == status )
return status;
363 if( MB_FAILURE == status )
return status;
367 if( MB_FAILURE == status )
return status;
371 if( MB_FAILURE == status )
return status;
377 if( MB_FAILURE == status )
return status;
384 if( NC_NOERR !=
fail )
386 MB_SET_ERR( MB_FAILURE,
"Trouble closing file" );
403 if( NC_NOERR !=
fail || ndims > NC_MAX_DIMS )
405 MB_SET_ERR( MB_FAILURE,
"ReadNCDF: File contains " << ndims <<
" dims but NetCDF library supports only "
406 << (
int)NC_MAX_DIMS );
410 if( nvars > NC_MAX_VARS )
412 MB_SET_ERR( MB_FAILURE,
"ReadNCDF: File contains " << nvars <<
" vars but NetCDF library supports only "
413 << (
int)NC_MAX_VARS );
421 fail = nc_inq_att(
ncFile, NC_GLOBAL,
"floating_point_word_size", &att_type, &att_len );
422 if( NC_NOERR !=
fail )
424 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting floating_point_word_size attribute" );
426 if( att_type != NC_INT || att_len != 1 )
428 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Word size didn't have type int or size 1" );
431 if( NC_NOERR !=
fail )
433 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Trouble getting word size" );
437 fail = nc_inq_att(
ncFile, NC_GLOBAL,
"version", &att_type, &att_len );
438 if( NC_NOERR !=
fail )
440 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting version attribute" );
442 if( att_type != NC_FLOAT || att_len != 1 )
444 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Version didn't have type float or size 1" );
462 fail = nc_inq_att(
ncFile, NC_GLOBAL,
"title", &att_type, &att_len );
463 if( NC_NOERR !=
fail )
465 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting title attribute" );
467 if( att_type != NC_CHAR )
469 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: title didn't have type char" );
471 char* title =
new char[att_len + 1];
472 fail = nc_get_att_text(
ncFile, NC_GLOBAL,
"title", title );
473 if( NC_NOERR !=
fail )
476 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: trouble getting title" );
493 std::vector< double* > arrays;
501 nc_inq_varid(
ncFile,
"coord", &coord );
504 size_t start[2] = { 0, 0 }, count[2] = { 1,
static_cast< size_t >(
numberNodes_loading ) };
510 fail = nc_get_vara_double(
ncFile, coord, start, count, arrays[d] );
511 if( NC_NOERR !=
fail )
513 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting " << (
char)(
'x' + d ) <<
" coord array" );
520 char varname[] =
"coord ";
523 varname[5] =
'x' + (char)d;
524 fail = nc_inq_varid(
ncFile, varname, &coord );
525 if( NC_NOERR !=
fail )
527 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting " << (
char)(
'x' + d ) <<
" coord variable" );
530 fail = nc_get_vara_double(
ncFile, coord, start, &count[1], arrays[d] );
531 if( NC_NOERR !=
fail )
533 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting " << (
char)(
'x' + d ) <<
" coord array" );
561 int nc_block_ids = -1;
563 if( -1 == nc_block_ids )
565 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting eb_prop1 variable" );
571 if( NULL == blocks_to_load || 0 == num_blocks )
573 blocks_to_load = &block_ids[0];
578 std::vector< int >::iterator iter, end_iter;
579 iter = block_ids.begin();
580 end_iter = block_ids.end();
585 char* temp_string = &temp_string_storage[0];
586 int block_seq_id = 1;
588 for( ; iter != end_iter; ++iter, block_seq_id++ )
592 GET_DIMB( temp_dim, temp_string,
"num_el_in_blk%d", block_seq_id, num_elements );
608 if( std::find( new_blocks.begin(), new_blocks.end(), *iter ) != new_blocks.end() )
614 exodus_id += num_elements;
624 int nc_fblock_ids = -1;
626 if( -1 == nc_fblock_ids )
628 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting fa_prop1 variable" );
633 char* temp_string = &temp_string_storage[0];
641 GET_DIMB( temp_dim, temp_string,
"num_fa_in_blk%d", i, num_elements );
656 exodus_id += num_elements;
664 char* temp_string = &temp_string_storage[0];
666 size_t start[1] = { 0 }, count[1];
667 std::vector< ReadFaceBlockData >::iterator this_it;
670 int fblock_seq_id = 1;
671 std::vector< int > dims;
677 GET_DIMB( temp_dim, temp_string,
"num_fa_in_blk%d", fblock_seq_id, num_fa_in_blk );
679 GET_DIMB( temp_dim, temp_string,
"num_nod_per_fa%d", fblock_seq_id, num_nod_per_fa );
682 GET_VAR( temp_string, nc_var, dims );
683 std::vector< int > fbconn;
684 fbconn.resize( num_nod_per_fa );
685 count[0] = num_nod_per_fa;
687 fail = nc_get_vara_int(
ncFile, nc_var, start, count, &fbconn[0] );
688 if( NC_NOERR !=
fail )
690 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting face polyhedra connectivity " );
692 std::vector< int > fbepecnt;
693 fbepecnt.resize( num_fa_in_blk );
695 GET_VAR( temp_string, nc_var, dims );
696 count[0] = num_fa_in_blk;
697 fail = nc_get_vara_int(
ncFile, nc_var, start, count, &fbepecnt[0] );
698 if( NC_NOERR !=
fail )
700 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting face polyhedra connectivity " );
703 std::vector< EntityHandle > polyConn;
705 for(
int i = 0; i < num_fa_in_blk; i++ )
707 polyConn.resize( fbepecnt[i] );
708 for(
int j = 0; j < fbepecnt[i]; j++ )
740 std::vector< ReadBlockData >::iterator this_it;
745 char* temp_string = &temp_string_storage[0];
748 int block_seq_id = 1;
749 std::vector< int > dims;
750 size_t start[2] = { 0, 0 }, count[2];
752 for( ; this_it !=
blocksLoading.end(); ++this_it, block_seq_id++ )
755 if( !( *this_it ).reading_in )
continue;
758 int block_id = ( *this_it ).blockId;
763 GET_VAR( temp_string, nc_var, dims );
764 if( -1 == nc_var || 0 == nc_var )
768 GET_VAR( temp_string, nc_var, dims );
770 if( -1 == nc_var || 0 == nc_var )
772 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting connec or faccon variable" );
777 int fail = nc_inq_att(
ncFile, nc_var,
"elem_type", &att_type, &att_len );
778 if( NC_NOERR !=
fail )
780 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting elem type attribute" );
782 std::vector< char > dum_str( att_len + 1 );
783 fail = nc_get_att_text(
ncFile, nc_var,
"elem_type", &dum_str[0] );
784 if( NC_NOERR !=
fail )
786 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting elem type" );
789 ( *this_it ).elemType = elem_type;
792 int number_nodes = ( *this_it ).numElements * verts_per_element;
798 int num_nodes_poly_conn;
799 GET_DIMB( temp_dim, temp_string,
"num_nod_per_el%d", block_seq_id, num_nodes_poly_conn );
801 std::vector< int > connec;
802 connec.resize( num_nodes_poly_conn );
803 count[0] = num_nodes_poly_conn;
805 fail = nc_get_vara_int(
ncFile, nc_var, start, count, &connec[0] );
806 if( NC_NOERR !=
fail )
808 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting polygon connectivity " );
811 std::vector< int > ebec;
812 ebec.resize( this_it->numElements );
815 GET_VAR( temp_string, nc_var, dims );
816 count[0] = this_it->numElements;
817 fail = nc_get_vara_int(
ncFile, nc_var, start, count, &ebec[0] );
818 if( NC_NOERR !=
fail )
820 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting polygon nodes per elem " );
827 std::vector< EntityHandle > polyConn;
829 for(
int i = 0; i < this_it->numElements; i++ )
831 polyConn.resize( ebec[i] );
832 for(
int j = 0; j < ebec[i]; j++ )
847 this_it->polys.push_back( newp );
858 GET_DIMB( temp_dim, temp_string,
"num_fac_per_el%d", block_seq_id, num_fac_per_el );
860 std::vector< int > facconn;
861 facconn.resize( num_fac_per_el );
862 count[0] = num_fac_per_el;
864 fail = nc_get_vara_int(
ncFile, nc_var, start, count, &facconn[0] );
865 if( NC_NOERR !=
fail )
867 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting polygon connectivity " );
870 std::vector< int > ebepecnt;
871 ebepecnt.resize( this_it->numElements );
874 GET_VAR( temp_string, nc_var, dims );
875 count[0] = this_it->numElements;
876 fail = nc_get_vara_int(
ncFile, nc_var, start, count, &ebepecnt[0] );
877 if( NC_NOERR !=
fail )
879 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting polygon nodes per elem " );
886 std::vector< EntityHandle > polyConn;
888 for(
int i = 0; i < this_it->numElements; i++ )
890 polyConn.resize( ebepecnt[i] );
891 for(
int j = 0; j < ebepecnt[i]; j++ )
893 polyConn[j] =
polyfaces[facconn[ix++] - 1];
906 this_it->polys.push_back( newp );
917 this_it->startMBId, conn );
921 start_range = ( *this_it ).startMBId;
922 end_range = start_range + ( *this_it ).numElements - 1;
924 Range new_range( start_range, end_range );
944 int* tmp_ptr =
reinterpret_cast< int*
>( conn );
948 count[0] = this_it->numElements;
949 count[1] = verts_per_element;
950 fail = nc_get_vara_int(
ncFile, nc_var, start, count, tmp_ptr );
951 if( NC_NOERR !=
fail )
953 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting connectivity" );
958 for(
int i = number_nodes - 1; i >= 0; --i )
962 MB_SET_ERR( MB_FAILURE,
"Invalid node ID in block connectivity" );
977 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: error getting element connectivity for block " << block_id );
987 range.
insert( this_it->startMBId, this_it->startMBId + this_it->numElements - 1 );
1005 std::vector< ReadBlockData >::iterator iter;
1009 if( iter->reading_in )
1013 if( iter->startMBId != 0 )
1015 Range range( iter->startMBId, iter->startMBId + iter->numElements - 1 );
1018 id_pos += iter->numElements;
1025 for( std::vector< EntityHandle >::iterator eit = iter->polys.begin(); eit != iter->polys.end();
1062 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting ns_prop1 variable" );
1066 std::vector< int > node_handles;
1070 char* temp_string = &temp_string_storage[0];
1075 int number_nodes_in_set;
1076 int number_dist_factors_in_set;
1078 GET_DIMB( temp_dim, temp_string,
"num_nod_ns%d", i + 1, number_nodes_in_set );
1079 GET_DIMB( temp_dim, temp_string,
"num_df_ns%d", i + 1, number_dist_factors_in_set );
1083 std::vector< double > temp_dist_factor_vector( number_nodes_in_set );
1084 if( number_dist_factors_in_set != 0 )
1087 GET_1D_DBL_VAR( temp_string, temp_dim, temp_dist_factor_vector );
1088 if( -1 == temp_dim )
1090 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting dist fact variable" );
1095 if( node_handles.size() < (
unsigned int)number_nodes_in_set )
1097 node_handles.reserve( number_nodes_in_set );
1098 node_handles.resize( number_nodes_in_set );
1104 if( -1 == temp_var )
1106 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting nodeset node variable" );
1110 Range child_meshsets;
1116 iter = child_meshsets.
begin();
1117 end_iter = child_meshsets.
end();
1120 for( ; iter != end_iter; ++iter )
1125 if( id_array[i] == nodeset_id )
1133 std::vector< EntityHandle > nodes_of_nodeset;
1140 std::vector< EntityHandle > nodes;
1141 std::vector< double > dist_factor_vector;
1142 for( j = 0; j < number_nodes_in_set; j++ )
1150 std::find( nodes_of_nodeset.begin(), nodes_of_nodeset.end(), node_id ) == nodes_of_nodeset.end() )
1152 nodes.push_back( node_id );
1154 if( number_dist_factors_in_set != 0 ) dist_factor_vector.push_back( temp_dist_factor_vector[j] );
1160 if( nodes.empty() )
continue;
1163 if( ns_handle == 0 )
1171 int nodeset_id = id_array[i];
1175 if( !dist_factor_vector.empty() )
1177 int size = dist_factor_vector.size();
1178 const void* data = &dist_factor_vector[0];
1183 else if( !dist_factor_vector.empty() )
1186 const void* ptr = 0;
1190 const double* data =
reinterpret_cast< const double*
>( ptr );
1191 dist_factor_vector.reserve( dist_factor_vector.size() +
size );
1192 std::copy( data, data +
size, std::back_inserter( dist_factor_vector ) );
1193 size = dist_factor_vector.size();
1194 ptr = &dist_factor_vector[0];
1218 if( -1 == temp_var )
1220 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting ss_prop1 variable" );
1224 int number_sides_in_set;
1225 int number_dist_factors_in_set;
1228 Range child_meshsets;
1237 char* temp_string = &temp_string_storage[0];
1242 GET_DIMB( temp_dim, temp_string,
"num_side_ss%d", i + 1, number_sides_in_set );
1243 GET_DIMB( temp_dim, temp_string,
"num_df_ss%d", i + 1, number_dist_factors_in_set );
1246 std::vector< int > side_list( number_sides_in_set );
1247 std::vector< int > element_list( number_sides_in_set );
1251 if( -1 == temp_var )
1253 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting sideset side variable" );
1259 if( -1 == temp_var )
1261 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting sideset elem variable" );
1264 std::vector< double > temp_dist_factor_vector;
1265 std::vector< EntityHandle > entities_to_add, reverse_entities;
1267 if(
create_ss_elements( &element_list[0], &side_list[0], number_sides_in_set, number_dist_factors_in_set,
1268 entities_to_add, reverse_entities, temp_dist_factor_vector, i + 1 ) !=
MB_SUCCESS )
1272 if( !entities_to_add.empty() || !reverse_entities.empty() )
1274 iter = child_meshsets.
begin();
1275 end_iter = child_meshsets.
end();
1278 for( ; iter != end_iter; ++iter )
1283 if( id_array[i] == sideset_id )
1292 if( ss_handle == 0 )
1297 if( ss_handle == 0 )
return MB_FAILURE;
1299 int sideset_id = id_array[i];
1304 if( !reverse_entities.empty() )
1315 result =
mdbImpl->
add_entities( reverse_set, &reverse_entities[0], reverse_entities.size() );
1330 if(
mdbImpl->
add_entities( ss_handle, ( entities_to_add.empty() ) ? NULL : &entities_to_add[0],
1335 if( number_dist_factors_in_set )
1338 const void* ptr = 0;
1342 const double* data =
reinterpret_cast< const double*
>( ptr );
1343 std::copy( data, data +
size, std::back_inserter( temp_dist_factor_vector ) );
1346 ptr = &temp_dist_factor_vector[0];
1347 size = temp_dist_factor_vector.size();
1360 int num_dist_factors,
1361 std::vector< EntityHandle >& entities_to_add,
1362 std::vector< EntityHandle >& reverse_entities,
1363 std::vector< double >& dist_factor_vector,
1371 std::vector< double > temp_dist_factor_vector( num_dist_factors );
1373 char* temp_string = &temp_string_storage[0];
1375 if( num_dist_factors )
1379 GET_1D_DBL_VAR( temp_string, temp_var, temp_dist_factor_vector );
1380 if( -1 == temp_var )
1382 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting dist fact variable" );
1387 int num_side_nodes, num_elem_nodes;
1389 std::vector< EntityHandle > connectivity;
1390 int side_node_idx[32];
1394 for(
int i = 0; i < num_sides; i++ )
1407 const int side_num = side_list[i] - 1;
1414 if( num_side_nodes <= 0 )
return MB_FAILURE;
1416 connectivity.resize( num_side_nodes );
1417 for(
int k = 0; k < num_side_nodes; ++k )
1418 connectivity[k] = nodes[side_node_idx[k]];
1422 entities_to_add.push_back( ent_handle );
1424 reverse_entities.push_back( ent_handle );
1427 if( num_dist_factors )
1429 for(
int k = 0; k < 4; k++ )
1430 dist_factor_vector.push_back( temp_dist_factor_vector[df_index++] );
1435 else if( type ==
MBTET )
1441 if( num_side_nodes <= 0 )
return MB_FAILURE;
1443 connectivity.resize( num_side_nodes );
1444 for(
int k = 0; k < num_side_nodes; ++k )
1445 connectivity[k] = nodes[side_node_idx[k]];
1449 entities_to_add.push_back( ent_handle );
1451 reverse_entities.push_back( ent_handle );
1454 if( num_dist_factors )
1456 for(
int k = 0; k < 3; k++ )
1457 dist_factor_vector.push_back( temp_dist_factor_vector[df_index++] );
1465 if( side_list[i] == 1 )
1468 entities_to_add.push_back( ent_handle );
1470 reverse_entities.push_back( ent_handle );
1472 if( num_dist_factors )
1474 for(
int k = 0; k < 4; k++ )
1475 dist_factor_vector.push_back( temp_dist_factor_vector[df_index++] );
1480 else if( side_list[i] == 2 )
1482 reverse_entities.push_back( ent_handle );
1484 if( num_dist_factors )
1486 for(
int k = 0; k < 4; k++ )
1487 dist_factor_vector.push_back( temp_dist_factor_vector[df_index++] );
1499 if( num_side_nodes <= 0 )
return MB_FAILURE;
1501 connectivity.resize( num_side_nodes );
1502 for(
int k = 0; k < num_side_nodes; ++k )
1503 connectivity[k] = nodes[side_node_idx[k]];
1508 entities_to_add.push_back( ent_handle );
1510 reverse_entities.push_back( ent_handle );
1512 if( num_dist_factors )
1514 for(
int k = 0; k < 2; k++ )
1515 dist_factor_vector.push_back( temp_dist_factor_vector[df_index++] );
1520 else if( type ==
MBQUAD )
1526 if( num_side_nodes <= 0 )
return MB_FAILURE;
1528 connectivity.resize( num_side_nodes );
1529 for(
int k = 0; k < num_side_nodes; ++k )
1530 connectivity[k] = nodes[side_node_idx[k]];
1534 entities_to_add.push_back( ent_handle );
1536 reverse_entities.push_back( ent_handle );
1539 if( num_dist_factors )
1541 for(
int k = 0; k < 2; k++ )
1542 dist_factor_vector.push_back( temp_dist_factor_vector[df_index++] );
1545 else if( type ==
MBTRI )
1547 int side_offset = 0;
1551 entities_to_add.push_back( ent_handle );
1553 reverse_entities.push_back( ent_handle );
1554 if( num_dist_factors )
1556 for(
int k = 0; k < 3; k++ )
1557 dist_factor_vector.push_back( temp_dist_factor_vector[df_index++] );
1564 if( side_list[i] > 2 ) side_offset = 2;
1572 if( num_side_nodes <= 0 )
return MB_FAILURE;
1574 connectivity.resize( num_side_nodes );
1575 for(
int k = 0; k < num_side_nodes; ++k )
1576 connectivity[k] = nodes[side_node_idx[k]];
1581 entities_to_add.push_back( ent_handle );
1583 reverse_entities.push_back( ent_handle );
1585 if( num_dist_factors )
1587 for(
int k = 0; k < 2; k++ )
1588 dist_factor_vector.push_back( temp_dist_factor_vector[df_index++] );
1609 std::vector< EntityHandle > adj_ent;
1614 bool match_found =
false;
1615 std::vector< EntityHandle > match_conn;
1618 for(
unsigned int i = 0; i < adj_ent.size() && match_found ==
false; i++ )
1625 if( match_conn.size() != connectivity.size() )
continue;
1628 std::vector< EntityHandle >::iterator iter;
1629 std::vector< EntityHandle >::iterator end_corner = match_conn.begin() + nb_corner_nodes;
1630 iter = std::find( match_conn.begin(), end_corner, connectivity[0] );
1631 if( iter == end_corner )
continue;
1635 std::rotate( match_conn.begin(), iter, end_corner );
1637 bool they_match =
true;
1638 for(
int j = 1; j < nb_corner_nodes; j++ )
1640 if( connectivity[j] != match_conn[j] )
1653 int k = nb_corner_nodes - 1;
1654 for(
int j = 1; j < nb_corner_nodes; )
1656 if( connectivity[j] != match_conn[k] )
1665 if( they_match ) sense = -1;
1667 match_found = they_match;
1668 if( match_found ) handle = adj_ent[i];
1683 std::vector< ReadBlockData >::iterator iter, end_iter;
1688 for( ; iter != end_iter; ++iter )
1690 if( exodus_id >= ( *iter ).startExoId && exodus_id < ( *iter ).startExoId + ( *iter ).numElements )
1692 elem_type = ( *iter ).elemType;
1695 if( !( *iter ).reading_in )
1706 if( side_id == 1 || side_id == 2 )
1727 std::vector< std::string > qa_records;
1730 std::vector< char > tag_data;
1731 for( std::vector< std::string >::iterator i = qa_records.begin(); i != qa_records.end(); ++i )
1733 std::copy( i->begin(), i->end(), std::back_inserter( tag_data ) );
1734 tag_data.push_back(
'\0' );
1738 if( !tag_data.empty() )
1740 const void* ptr = &tag_data[0];
1741 int size = tag_data.size();
1755 int number_records = 0;
1758 GET_DIM( temp_dim,
"num_qa_rec", number_records );
1761 for(
int i = 0; i < number_records; i++ )
1763 for(
int j = 0; j < 4; j++ )
1768 qa_record_list.push_back( &data[0] );
1777 std::vector< int > dims;
1779 GET_VAR(
"qa_records", temp_var, dims );
1780 if( -1 == temp_var )
1782 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting qa record variable" );
1785 size_t count[3], start[3];
1786 start[0] = record_number;
1787 start[1] = record_position;
1792 int fail = nc_get_vara_text(
ncFile, temp_var, start, count, temp_string );
1793 if( NC_NOERR !=
fail )
1795 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem setting current record number variable position" );
1804 const int num_blocks,
1805 const int* blocks_to_load,
1841 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem reading file options" );
1843 std::vector< std::string > tokens;
1848 if( tokens.size() > 1 && !tokens[1].empty() )
1850 const char* time_s = tokens[1].c_str();
1852 long int pval = strtol( time_s, &endptr, 0 );
1853 std::string end = endptr;
1865 if( tokens.size() < 3 || ( tokens[2] !=
"set" && tokens[2] !=
"add" ) )
1869 op = tokens[2].c_str();
1873 int fail = nc_open( exodus_file_name, 0, &
ncFile );
1884 GET_DIM( ncdim,
"time_step", max_time_steps );
1887 std::cout <<
"ReadNCDF: could not get number of time steps" << std::endl;
1890 std::cout <<
" Maximum time step=" << max_time_steps << std::endl;
1891 if( max_time_steps < time_step )
1893 std::cout <<
"ReadNCDF: time step is greater than max_time_steps" << std::endl;
1898 std::vector< double > times( max_time_steps );
1903 std::cout <<
"ReadNCDF: unable to get time variable" << std::endl;
1907 std::cout <<
" Step " << time_step <<
" is at " << times[time_step - 1] <<
" seconds" << std::endl;
1917 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting node number map data" );
1921 std::vector< std::vector< double > > deformed_arrays( 3 );
1922 std::vector< std::vector< double > > orig_coords( 3 );
1929 size_t start[2] = {
static_cast< size_t >( time_step - 1 ), 0 },
1931 std::vector< int > dims;
1932 int coordx = -1, coordy = -1, coordz = -1;
1933 GET_VAR(
"vals_nod_var1", coordx, dims );
1934 GET_VAR(
"vals_nod_var2", coordy, dims );
1938 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting coords variable" );
1941 fail = nc_get_vara_double(
ncFile, coordx, start, count, &deformed_arrays[0][0] );
1942 if( NC_NOERR !=
fail )
1944 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting x deformation array" );
1947 fail = nc_get_vara_double(
ncFile, coordy, start, count, &deformed_arrays[1][0] );
1948 if( NC_NOERR !=
fail )
1950 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting y deformation array" );
1954 fail = nc_get_vara_double(
ncFile, coordz, start, count, &deformed_arrays[2][0] );
1955 if( NC_NOERR !=
fail )
1957 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting z deformation array" );
1961 int coord1 = -1, coord2 = -1, coord3 = -1;
1965 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting x coord array" );
1970 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting y coord array" );
1977 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting z coord array" );
1984 if( strcmp( op,
"set" ) != 0 && strcmp( op,
" set" ) != 0 )
return MB_NOT_IMPLEMENTED;
1987 const bool match_node_ids =
true;
1992 std::cout <<
" cub_file_set contains " << cub_verts.
size() <<
" nodes." << std::endl;
1996 double max_magnitude = 0;
1997 double average_magnitude = 0;
2000 std::map< int, EntityHandle > cub_verts_id_map;
2006 std::map< int, EntityHandle > matched_cub_vert_id_map;
2009 if( match_node_ids )
2011 std::vector< int > cub_ids( cub_verts.
size() );
2013 for(
unsigned i = 0; i != cub_verts.
size(); ++i )
2015 cub_verts_id_map.insert( std::pair< int, EntityHandle >( cub_ids[i], cub_verts[i] ) );
2022 FileOptions tree_opts(
"MAX_PER_LEAF=1;SPLITS_PER_DIR=1;CANDIDATE_PLANE_SET=0" );
2031 int exo_id = ptr[i];
2032 CartVect exo_coords( orig_coords[0][i], orig_coords[1][i], orig_coords[2][i] );
2034 bool found_match =
false;
2037 if( match_node_ids )
2039 std::map< int, EntityHandle >::iterator i_iter;
2040 i_iter = cub_verts_id_map.find( exo_id );
2041 if( i_iter != cub_verts_id_map.end() )
2044 cub_vert = i_iter->second;
2054 const double MAX_NODE_DIST = 1e-1;
2056 std::vector< EntityHandle > leaves;
2057 double min_dist = MAX_NODE_DIST;
2059 for( std::vector< EntityHandle >::const_iterator j = leaves.begin(); j != leaves.end(); ++j )
2061 std::vector< EntityHandle > leaf_verts;
2063 for( std::vector< EntityHandle >::const_iterator k = leaf_verts.begin(); k != leaf_verts.end(); ++k )
2065 CartVect orig_cub_coords, difference;
2067 difference = orig_cub_coords - exo_coords;
2068 double dist = difference.
length();
2069 if( dist < min_dist )
2076 if( 0 != cub_vert ) found_match =
true;
2083 matched_cub_vert_id_map.insert( std::pair< int, EntityHandle >( exo_id, cub_vert ) );
2084 updated_exo_coords[0] = orig_coords[0][i] + deformed_arrays[0][i];
2085 updated_exo_coords[1] = orig_coords[1][i] + deformed_arrays[1][i];
2092 sqrt( deformed_arrays[0][i] * deformed_arrays[0][i] + deformed_arrays[1][i] * deformed_arrays[1][i] +
2093 deformed_arrays[2][i] * deformed_arrays[2][i] );
2094 if( magnitude > max_magnitude ) max_magnitude = magnitude;
2095 average_magnitude += magnitude;
2100 std::cout <<
"cannot match exo node " << exo_id <<
" " << exo_coords << std::endl;
2106 if( matched_cub_vert_id_map.size() < cub_verts.
size() )
2108 Range unmatched_cub_verts = cub_verts;
2109 for( std::map< int, EntityHandle >::const_iterator i = matched_cub_vert_id_map.begin();
2110 i != matched_cub_vert_id_map.end(); ++i )
2112 unmatched_cub_verts.
erase( i->second );
2122 std::cout <<
"cannot match cub node " << cub_id <<
" " << cub_coords << std::endl;
2125 std::cout <<
" " << unmatched_cub_verts.
size() <<
" nodes from the cub file could not be matched."
2130 std::cout <<
" " << found <<
" nodes from the exodus file were matched in the cub_file_set ";
2131 if( match_node_ids )
2133 std::cout <<
"by id." << std::endl;
2137 std::cout <<
"by proximity." << std::endl;
2143 std::cout <<
"Error: " << lost <<
" nodes from the exodus file could not be matched." << std::endl;
2146 std::cout <<
" maximum node displacement magnitude: " << max_magnitude <<
" cm" << std::endl;
2147 std::cout <<
" average node displacement magnitude: " << average_magnitude / found <<
" cm" << std::endl;
2155 GET_DIM( ncdim,
"num_elem_var", n_elem_var );
2159 int cstatus = nc_inq_varid(
ncFile,
"name_elem_var", &varid );
2161 std::vector< char* > names( n_elem_var );
2162 for(
int i = 0; i < n_elem_var; ++i )
2164 if( cstatus != NC_NOERR || varid == -1 )
2166 std::cout <<
"ReadNCDF: name_elem_var does not exist" << std::endl;
2169 int status = nc_get_var_text(
ncFile, varid, &names_memory[0] );
2170 if( NC_NOERR != status )
2172 MB_SET_ERR( MB_FAILURE,
"ReadNCDF: Problem getting element variable names" );
2178 bool found_death_index =
false;
2179 for(
int i = 0; i < n_elem_var; ++i )
2181 std::string temp( names[i] );
2182 std::string::size_type pos0 = temp.find(
"death_status" );
2183 std::string::size_type pos1 = temp.find(
"Death_Status" );
2184 std::string::size_type pos2 = temp.find(
"DEATH_STATUS" );
2185 if( std::string::npos != pos0 || std::string::npos != pos1 || std::string::npos != pos2 )
2187 found_death_index =
true;
2188 death_index = i + 1;
2192 if( !found_death_index )
2194 MB_SET_ERR( MB_FAILURE,
"ReadNCDF: Problem getting index of death_status variable" );
2203 if( MB_FAILURE == rval )
2205 MB_SET_ERR( MB_FAILURE,
"ReadNCDF: Problem reading block headers" );
2212 const bool match_elems_by_connectivity =
true;
2217 if( !match_elems_by_connectivity )
2222 MB_SET_ERR( MB_FAILURE,
"ReadNCDF: Problem getting element number map data" );
2227 int first_elem_id_in_block = 0;
2228 int block_count = 1;
2229 int total_elems = 0;
2230 int total_dead_elems = 0;
2234 std::string temp_string(
"connect" );
2235 std::stringstream temp_ss;
2236 temp_ss << block_count;
2237 temp_string += temp_ss.str();
2238 temp_string +=
"\0";
2239 std::vector< int > ldims;
2240 GET_VAR( temp_string.c_str(), nc_var, ldims );
2243 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting connect variable" );
2248 fail = nc_inq_att(
ncFile, nc_var,
"elem_type", &att_type, &att_len );
2249 if( NC_NOERR !=
fail )
2251 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting elem type attribute" );
2253 std::vector< char > dum_str( att_len + 1 );
2254 fail = nc_get_att_text(
ncFile, nc_var,
"elem_type", &dum_str[0] );
2255 if( NC_NOERR !=
fail )
2257 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting elem type" );
2260 ( *i ).elemType = elem_type;
2268 int* exo_conn =
new int[i->numElements * nodes_per_element];
2270 size_t lstart[2] = { 0, 0 }, lcount[2];
2271 lcount[0] = i->numElements;
2272 lcount[1] = nodes_per_element;
2273 fail = nc_get_vara_int(
ncFile, nc_var, lstart, lcount, exo_conn );
2274 if( NC_NOERR !=
fail )
2277 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting connectivity" );
2281 std::vector< double > death_status( i->numElements );
2282 std::string array_name(
"vals_elem_var" );
2284 temp_ss << death_index;
2285 array_name += temp_ss.str();
2288 temp_ss << block_count;
2289 array_name += temp_ss.str();
2291 GET_VAR( array_name.c_str(), nc_var, ldims );
2294 MB_SET_ERR( MB_FAILURE,
"ReadNCDF:: Problem getting death status variable" );
2296 lstart[0] = time_step - 1;
2299 lcount[1] = i->numElements;
2300 status = nc_get_vara_double(
ncFile, nc_var, lstart, lcount, &death_status[0] );
2301 if( NC_NOERR != status )
2304 MB_SET_ERR( MB_FAILURE,
"ReadNCDF: Problem setting time step for death_status" );
2310 int dead_elem_counter = 0, missing_elem_counter = 0;
2311 for(
int j = 0; j < i->numElements; ++j )
2313 if( 1 != death_status[j] )
2315 Range cub_elem, cub_nodes;
2316 if( match_elems_by_connectivity )
2319 std::vector< int > elem_conn( nodes_per_element );
2320 for(
unsigned int k = 0; k < nodes_per_element; ++k )
2323 elem_conn[k] = exo_conn[j * nodes_per_element + k];
2327 std::vector< int > elem_conn_node_ids( nodes_per_element );
2328 for(
unsigned int k = 0; k < nodes_per_element; ++k )
2330 elem_conn_node_ids[k] = ptr[elem_conn[k] - 1];
2335 for(
unsigned int k = 0; k < nodes_per_element; ++k )
2337 std::map< int, EntityHandle >::iterator k_iter;
2338 k_iter = matched_cub_vert_id_map.find( elem_conn_node_ids[k] );
2340 if( k_iter == matched_cub_vert_id_map.end() )
2342 std::cout <<
"ReadNCDF: Found no cub node with id=" << elem_conn_node_ids[k]
2343 <<
", but expected to find only 1." << std::endl;
2346 cub_nodes.
insert( k_iter->second );
2349 if( nodes_per_element != cub_nodes.
size() )
2351 std::cout <<
"ReadNCDF: nodes_per_elemenet != cub_nodes.size()" << std::endl;
2361 std::cout <<
"ReadNCDF: could not get dead cub element" << std::endl;
2372 int elem_id = elem_ids[first_elem_id_in_block + j];
2373 void*
id[] = { &elem_id };
2384 if( 1 == cub_elem.
size() )
2403 std::cout <<
"ReadNCDF: Should have found 1 element with type=" << mb_type
2404 <<
" in cub_file_set, but instead found " << cub_elem.
size() << std::endl;
2406 ++missing_elem_counter;
2410 ++dead_elem_counter;
2415 temp_ss << i->blockId;
2416 total_dead_elems += dead_elem_counter;
2417 total_elems += i->numElements;
2418 std::cout <<
" Block " << temp_ss.str() <<
" has " << dead_elem_counter <<
"/" << i->numElements
2419 <<
" dead elements." << std::endl;
2420 if( 0 != missing_elem_counter )
2422 std::cout <<
" " << missing_elem_counter
2423 <<
" dead elements in this block were not found in the cub_file_set." << std::endl;
2427 first_elem_id_in_block += i->numElements;
2432 std::cout <<
" Total: " << total_dead_elems <<
"/" << total_elems <<
" dead elements." << std::endl;
2436 if( NC_NOERR !=
fail )
2438 MB_SET_ERR( MB_FAILURE,
"Trouble closing file" );
2445 void ReadNCDF::tokenize(
const std::string& str, std::vector< std::string >& tokens,
const char* delimiters )
2447 std::string::size_type last = str.find_first_not_of( delimiters, 0 );
2448 std::string::size_type pos = str.find_first_of( delimiters, last );
2449 while( std::string::npos != pos && std::string::npos != last )
2451 tokens.push_back( str.substr( last, pos - last ) );
2452 last = str.find_first_not_of( delimiters, pos );
2453 pos = str.find_first_of( delimiters, last );
2454 if( std::string::npos == pos ) pos = str.size();