20 #pragma warning( disable : 4786 )
46 #ifndef MOAB_HAVE_NETCDF
47 #error Attempt to compile WriteNCDF with NetCDF support disabled
50 #define CHAR_STR_LEN 128
57 #define INS_ID( stringvar, prefix, id ) snprintf( stringvar, CHAR_STR_LEN, prefix, id )
59 #define GET_DIM( ncdim, name, val ) \
61 int gdfail = nc_inq_dimid( ncFile, name, &( ncdim ) ); \
62 if( NC_NOERR == gdfail ) \
65 gdfail = nc_inq_dimlen( ncFile, ncdim, &tmp_val ); \
66 if( NC_NOERR != gdfail ) \
68 MB_SET_ERR( MB_FAILURE, "WriteNCDF:: couldn't get dimension length" ); \
77 #define GET_DIMB( ncdim, name, varname, id, val ) \
78 INS_ID( name, CHAR_STR_LEN, varname, id ); \
79 GET_DIM( ncdim, name, val );
81 #define GET_VAR( name, id, dims ) \
84 int gvfail = nc_inq_varid( ncFile, name, &( id ) ); \
85 if( NC_NOERR == gvfail ) \
88 gvfail = nc_inq_varndims( ncFile, id, &ndims ); \
89 if( NC_NOERR == gvfail ) \
91 ( dims ).resize( ndims ); \
92 gvfail = nc_inq_vardimid( ncFile, id, &( dims )[0] ); \
103 : mdbImpl( impl ),
ncFile( 0 ), mCurrentMeshHandle( 0 ), mGeomDimensionTag( 0 ), repeat_face_blocks( 0 )
105 assert( impl != NULL );
123 int dum_val_array[] = { -1, -1, -1, -1 };
146 std::vector< MaterialSetData >::iterator iter;
148 for( iter = block_info.begin(); iter != block_info.end(); ++iter )
150 iter->elements.clear();
156 struct tm* local_time;
157 time_t calendar_time;
159 calendar_time = time( NULL );
160 local_time = localtime( &calendar_time );
162 assert( NULL != time_string && NULL != date_string );
164 strftime( time_string,
TIME_STR_LEN,
"%H:%M:%S", local_time );
165 strftime( date_string,
TIME_STR_LEN,
"%m/%d/%Y", local_time );
168 time_string[10] =
'\0';
169 date_string[10] =
'\0';
173 const bool overwrite,
177 const std::vector< std::string >& qa_records,
188 std::vector< EntityHandle > blocks, nodesets, sidesets, entities;
197 std::copy( this_range.
begin(), this_range.
end(), std::back_inserter( blocks ) );
200 std::copy( this_range.
begin(), this_range.
end(), std::back_inserter( nodesets ) );
203 std::copy( this_range.
begin(), this_range.
end(), std::back_inserter( sidesets ) );
206 if( blocks.empty() && nodesets.empty() && sidesets.empty() )
209 for(
int d = user_dimension; d > 0 && this_range.
empty(); --d )
218 blocks.push_back( block_handle );
224 for(
const EntityHandle* iter = ent_handles; iter < ent_handles + num_sets; ++iter )
227 blocks.push_back( *iter );
229 nodesets.push_back( *iter );
231 sidesets.push_back( *iter );
241 std::vector< MaterialSetData > block_info;
242 std::vector< NeumannSetData > sideset_info;
243 std::vector< DirichletSetData > nodeset_info;
245 mesh_info.
num_dim = user_dimension;
247 if( qa_records.empty() )
252 char string1[80], string2[80];
254 mesh_info.
qaRecords.push_back( string2 );
255 mesh_info.
qaRecords.push_back( string1 );
260 assert( qa_records.size() % 4 == 0 );
262 std::copy( qa_records.begin(), qa_records.end(), std::back_inserter( mesh_info.
qaRecords ) );
266 if(
gather_mesh_information( mesh_info, block_info, sideset_info, nodeset_info, blocks, sidesets, nodesets ) !=
274 int fail = nc_create( exodus_file_name, overwrite ? NC_CLOBBER : NC_NOCLOBBER, &
ncFile );
275 if( NC_NOERR !=
fail )
290 size_t start = 0, count = 1;
292 std::vector< int > dims;
293 GET_VAR(
"time_whole", nc_var, dims );
294 fail = nc_put_vara_double(
ncFile, nc_var, &start, &count, &timev );
295 if( NC_NOERR !=
fail )
297 MB_SET_ERR( MB_FAILURE,
"Failed writing dist factor array" );
358 if( NC_NOERR !=
fail )
360 MB_SET_ERR( MB_FAILURE,
"Trouble closing file" );
367 std::vector< MaterialSetData >& block_info,
368 std::vector< NeumannSetData >& sideset_info,
369 std::vector< DirichletSetData >& nodeset_info,
370 std::vector< EntityHandle >& blocks,
371 std::vector< EntityHandle >& sidesets,
372 std::vector< EntityHandle >& nodesets )
375 std::vector< EntityHandle >::iterator vector_iter, end_vector_iter;
384 vector_iter = blocks.begin();
385 end_vector_iter = blocks.end();
387 std::vector< EntityHandle > parent_meshsets;
395 int highest_dimension_of_element_blocks = 0;
397 for( vector_iter = blocks.begin(); vector_iter != blocks.end(); ++vector_iter )
410 if( dummy_range.
empty() )
continue;
415 MB_SET_ERR( MB_FAILURE,
"Couldn't get block id from a tag for an element block" );
438 MB_SET_ERR( MB_FAILURE,
"Entities in block " <<
id <<
" not of common type" );
442 if( entity_type ==
MBQUAD || entity_type ==
MBTRI )
444 else if( entity_type ==
MBEDGE )
449 if( dimension > highest_dimension_of_element_blocks ) highest_dimension_of_element_blocks = dimension;
451 std::vector< EntityHandle > tmp_conn;
458 MB_SET_ERR( MB_FAILURE,
"Element type in block " <<
id <<
" didn't get set correctly" );
495 if( !sidesets.empty() )
500 unsigned char bit = 0x1;
506 block_info.push_back( block_data );
508 const void* data = NULL;
513 const char* qa_rec =
static_cast< const char*
>( data );
516 for(
int i = 0; i < size; i++ )
518 if( qa_rec[i] ==
'\0' )
520 std::string qa_string( &qa_rec[start], i - start );
521 mesh_info.
qaRecords.push_back( qa_string );
528 if( count > 0 ) assert( count % 4 == 0 );
534 for( std::vector< MaterialSetData >::iterator blit = block_info.begin(); blit != block_info.end(); blit++ )
547 if( highest_dimension_of_element_blocks < 2 )
550 mesh_info.
num_dim = highest_dimension_of_element_blocks;
555 end_range_iter = mesh_info.
nodes.
end();
561 vector_iter = nodesets.begin();
562 end_vector_iter = nodesets.end();
564 for( ; vector_iter != end_vector_iter; ++vector_iter )
573 MB_SET_ERR( MB_FAILURE,
"Couldn't get id tag for nodeset " <<
id );
576 nodeset_data.
id = id;
578 std::vector< EntityHandle > node_vector;
582 MB_SET_ERR( MB_FAILURE,
"Couldn't get nodes in nodeset " <<
id );
586 const double* dist_factor_vector;
587 int dist_factor_size;
590 int has_dist_factors = 0;
593 has_dist_factors = 1;
594 dist_factor_size /=
sizeof( double );
595 dist_factor_vector =
reinterpret_cast< const double*
>( ptr );
596 std::vector< EntityHandle >::iterator iter, end_iter;
597 iter = node_vector.begin();
598 end_iter = node_vector.end();
601 unsigned char node_marked = 0;
603 for( ; iter != end_iter; ++iter )
609 if( 0x1 == node_marked )
611 nodeset_data.
nodes.push_back( *iter );
612 if( 0 != has_dist_factors )
621 nodeset_info.push_back( nodeset_data );
625 vector_iter = sidesets.begin();
626 end_vector_iter = sidesets.end();
628 for( ; vector_iter != end_vector_iter; ++vector_iter )
635 sideset_data.
id = id;
640 Range forward_elems, reverse_elems;
641 if(
get_sideset_elems( *vector_iter, 0, forward_elems, reverse_elems ) == MB_FAILURE )
return MB_FAILURE;
645 result =
get_valid_sides( reverse_elems, mesh_info, -1, sideset_data );
649 sideset_info.push_back( sideset_data );
663 const double* dist_factor_vector = 0;
664 int dist_factor_size = 0;
667 const double* dist_fac_iter = 0;
669 bool has_dist_factors =
false;
674 has_dist_factors =
true;
675 dist_factor_vector =
reinterpret_cast< const double*
>( ptr );
676 dist_fac_iter = dist_factor_vector;
677 dist_factor_size /=
sizeof( double );
680 unsigned char element_marked = 0;
688 if( 0x1 == element_marked )
690 sideset_data.
elements.push_back( *iter );
693 sideset_data.
side_numbers.push_back( ( sense == 1 ? 1 : 2 ) );
697 std::vector< EntityHandle > parents;
706 fprintf(stderr,
"[Warning]: Couldn't get adjacencies for sideset.\n");
710 if( !parents.empty() )
713 for(
unsigned int k = 0; k < parents.size(); k++ )
718 int side_no, this_sense, this_offset;
719 if( 0x1 == element_marked &&
721 this_sense == sense )
723 sideset_data.
elements.push_back( parents[k] );
734 fprintf(stderr,
"[Warning]: No parent element exists for element in sideset %i\n", sideset_data.
id);
739 if( sideset_data.
elements.size() != 0 )
745 if( has_dist_factors )
747 std::copy( dist_fac_iter, dist_fac_iter + num_nodes,
749 dist_fac_iter += num_nodes;
753 for(
int j = 0; j < num_nodes; j++ )
766 for( std::vector< std::string >::iterator string_it = qa_record_list.begin(); string_it != qa_record_list.end(); )
768 for(
int j = 0; j < 4; j++ )
780 std::vector< int > dims;
782 GET_VAR(
"qa_records", temp_var, dims );
785 MB_SET_ERR( MB_FAILURE,
"WriteNCDF:: Problem getting qa record variable" );
787 size_t count[3], start[3];
790 start[0] = record_number;
791 start[1] = record_position;
796 count[2] = (long)strlen(
string ) + 1;
797 int fail = nc_put_vara_text(
ncFile, temp_var, start, count,
string );
798 if( NC_NOERR !=
fail )
800 MB_SET_ERR( MB_FAILURE,
"Failed to position qa string variable" );
810 std::vector< int > dims;
811 GET_VAR(
"coor_names", nc_var, dims );
814 MB_SET_ERR( MB_FAILURE,
"Trouble getting coordinate name variable" );
819 strcpy( dum_str,
"x" );
820 int fail = nc_put_vara_text(
ncFile, nc_var, start, count, dum_str );
821 if( NC_NOERR !=
fail )
823 MB_SET_ERR( MB_FAILURE,
"Trouble adding x coordinate name; netcdf message: " << nc_strerror(
fail ) );
827 strcpy( dum_str,
"y" );
828 fail = nc_put_vara_text(
ncFile, nc_var, start, count, dum_str );
829 if( NC_NOERR !=
fail )
831 MB_SET_ERR( MB_FAILURE,
"Trouble adding y coordinate name; netcdf message: " << nc_strerror(
fail ) );
835 strcpy( dum_str,
"z" );
836 fail = nc_put_vara_text(
ncFile, nc_var, start, count, dum_str );
837 if( NC_NOERR !=
fail )
839 MB_SET_ERR( MB_FAILURE,
"Trouble adding z coordinate name; netcdf message: " << nc_strerror(
fail ) );
846 bool transform_needed =
true;
849 int num_coords_to_fill = transform_needed ? 3 : dimension;
851 std::vector< double* > coord_arrays( 3 );
852 coord_arrays[0] =
new double[num_nodes];
853 coord_arrays[1] =
new double[num_nodes];
854 coord_arrays[2] = NULL;
856 if( num_coords_to_fill == 3 ) coord_arrays[2] =
new double[num_nodes];
861 delete[] coord_arrays[0];
862 delete[] coord_arrays[1];
863 if( coord_arrays[2] )
delete[] coord_arrays[2];
867 if( transform_needed )
869 double trans_matrix[16];
874 for(
int i = 0; i < num_nodes; i++ )
879 vec2[0] = coord_arrays[0][i];
880 vec2[1] = coord_arrays[1][i];
881 vec2[2] = coord_arrays[2][i];
883 for(
int row = 0; row < 3; row++ )
886 for(
int col = 0; col < 3; col++ )
887 vec1[row] += ( trans_matrix[( row * 4 ) + col] * vec2[col] );
890 coord_arrays[0][i] = vec1[0];
891 coord_arrays[1][i] = vec1[1];
892 coord_arrays[2][i] = vec1[2];
898 GET_VAR(
"coord", nc_var, dims );
901 MB_SET_ERR( MB_FAILURE,
"Trouble getting coordinate variable" );
904 count[1] = num_nodes;
905 fail = nc_put_vara_double(
ncFile, nc_var, start, count, &( coord_arrays[0][0] ) );
906 if( NC_NOERR !=
fail )
908 MB_SET_ERR( MB_FAILURE,
"Trouble writing x coordinate" );
912 fail = nc_put_vara_double(
ncFile, nc_var, start, count, &( coord_arrays[1][0] ) );
913 if( NC_NOERR !=
fail )
915 MB_SET_ERR( MB_FAILURE,
"Trouble writing y coordinate" );
919 fail = nc_put_vara_double(
ncFile, nc_var, start, count, &( coord_arrays[2][0] ) );
920 if( NC_NOERR !=
fail )
922 MB_SET_ERR( MB_FAILURE,
"Trouble writing z coordinate" );
925 delete[] coord_arrays[0];
926 delete[] coord_arrays[1];
927 if( coord_arrays[2] )
delete[] coord_arrays[2];
949 std::vector< int > dims;
952 int num_faces_in_block = (int)pfaces.
size();
955 INS_ID( wname,
"fbconn%u", bl + 1 );
956 GET_VAR( wname, nc_var, dims );
958 INS_ID( wname,
"num_nod_per_fa%u", bl + 1 );
959 int ncdim, num_nod_per_face;
960 GET_DIM( ncdim, wname, num_nod_per_face );
961 int* connectivity =
new int[num_nod_per_face];
962 int ixcon = 0, j = 0;
963 std::vector< int > fbepe( num_faces_in_block );
970 for(
int k = 0; k < nnodes; k++ )
971 connectivity[ixcon++] = conn[k];
974 size_t start[1] = { 0 }, count[1] = { 0 };
976 int fail = nc_put_vara_int(
ncFile, nc_var, start, count, connectivity );
977 if( NC_NOERR !=
fail )
979 delete[] connectivity;
980 MB_SET_ERR( MB_FAILURE,
"Couldn't write fbconn variable" );
983 INS_ID( wname,
"fbepecnt%u", bl + 1 );
984 GET_VAR( wname, nc_var, dims );
985 count[0] = num_faces_in_block;
987 fail = nc_put_vara_int(
ncFile, nc_var, start, count, &fbepe[0] );
988 if( NC_NOERR !=
fail )
990 delete[] connectivity;
991 MB_SET_ERR( MB_FAILURE,
"Couldn't write fbepecnt variable" );
1003 MB_SET_ERR( MB_FAILURE,
"Problem writing face block status" );
1006 delete[] connectivity;
1013 std::vector< MaterialSetData >& block_info,
1014 std::vector< NeumannSetData >& sideset_info,
1015 std::vector< DirichletSetData >& nodeset_info,
1016 const char* filename )
1023 std::string title_string =
"MOAB";
1024 title_string.append(
"(" );
1025 title_string.append( filename );
1026 title_string.append(
"): " );
1027 title_string.append( date );
1028 title_string.append(
": " );
1029 title_string.append(
"time " );
1036 int result =
initialize_exodus_file( mesh_info, block_info, sideset_info, nodeset_info, title_string.c_str() );
1038 if( result == MB_FAILURE )
return MB_FAILURE;
1046 int block_index = 0;
1049 for( i = 0; i < block_data.size(); i++ )
1070 MB_SET_ERR( MB_FAILURE,
"No elements in block " <<
id );
1075 MB_SET_ERR( MB_FAILURE,
"Problem writing element block status" );
1081 unsigned int num_nodes = num_nodes_per_elem * num_elem;
1084 num_nodes = num_nodes_per_elem;
1086 int* connectivity =
new int[num_nodes];
1091 exodus_id, connectivity );
1094 delete[] connectivity;
1095 MB_SET_ERR( result,
"Couldn't get element array to write from" );
1101 const int* reorder = 0;
1109 std::vector< int > dims;
1112 exodus_id += num_elem;
1113 INS_ID( wname,
"connect%u", i + 1 );
1115 GET_VAR( wname, nc_var, dims );
1118 delete[] connectivity;
1119 MB_SET_ERR( MB_FAILURE,
"Couldn't get connectivity variable" );
1125 size_t start[1] = { 0 }, count[1] = { num_nodes_per_elem };
1126 int fail = nc_put_vara_int(
ncFile, nc_var, start, count, connectivity );
1127 if( NC_NOERR !=
fail )
1129 delete[] connectivity;
1130 MB_SET_ERR( MB_FAILURE,
"Couldn't write connectivity variable" );
1133 INS_ID( wname,
"ebepecnt%u", i + 1 );
1134 GET_VAR( wname, nc_var, dims );
1145 connectivity[j] = nnodes;
1147 fail = nc_put_vara_int(
ncFile, nc_var, start, count, connectivity );
1148 if( NC_NOERR !=
fail )
1150 delete[] connectivity;
1151 MB_SET_ERR( MB_FAILURE,
"Couldn't write ebepecnt variable" );
1156 size_t start[2] = { 0, 0 }, count[2] = { num_elem, num_nodes_per_elem };
1157 int fail = nc_put_vara_int(
ncFile, nc_var, start, count, connectivity );
1158 if( NC_NOERR !=
fail )
1160 delete[] connectivity;
1161 MB_SET_ERR( MB_FAILURE,
"Couldn't write connectivity variable" );
1188 INS_ID( wname,
"facconn%u", i + 1 );
1189 GET_VAR( wname, nc_var, dims );
1192 int ixcon = 0, j = 0;
1193 size_t start[1] = { 0 }, count[1] = { 0 };
1201 for(
int k = 0; k < nfaces; k++ )
1204 if(
index == -1 )
MB_SET_ERR( MB_FAILURE,
"Couldn't find face in polyhedron" );
1205 connectivity[ixcon++] =
index + 1;
1207 ebepe[j++] = nfaces;
1211 int fail = nc_put_vara_int(
ncFile, nc_var, start, count, connectivity );
1212 if( NC_NOERR !=
fail )
1214 delete[] connectivity;
1215 MB_SET_ERR( MB_FAILURE,
"Couldn't write fbconn variable" );
1218 INS_ID( wname,
"ebepecnt%u", i + 1 );
1219 GET_VAR( wname, nc_var, dims );
1222 fail = nc_put_vara_int(
ncFile, nc_var, start, count, &ebepe[0] );
1223 if( NC_NOERR !=
fail )
1225 delete[] connectivity;
1226 MB_SET_ERR( MB_FAILURE,
"Couldn't write fbepecnt variable" );
1230 delete[] connectivity;
1241 int* map =
new int[num_nodes];
1246 range_iter = nodes.
begin();
1247 end_iter = nodes.
end();
1251 for( ; range_iter != end_iter; ++range_iter )
1261 if( map )
delete[] map;
1265 MB_SET_ERR( MB_FAILURE,
"Failed writing global node order map" );
1274 int* map =
new int[num_elements];
1280 for(
int i = 0; i < num_elements; i++ )
1287 if( map )
delete[] map;
1291 MB_SET_ERR( MB_FAILURE,
"Failed writing global element order map" );
1302 int* map =
new int[num_elements];
1306 for(
int i = 0; i < num_elements; i++ )
1315 if( map )
delete[] map;
1319 MB_SET_ERR( MB_FAILURE,
"Failed writing element map" );
1326 int* variable_array,
1336 std::vector< int > dims;
1337 GET_VAR( variable_name, nc_var, dims );
1340 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to locate variable " << variable_name <<
" in file" );
1345 size_t start[1], count[1];
1346 start[0] = start_position;
1347 count[0] = number_values;
1349 int fail = NC_NOERR;
1350 if(
sizeof(
int ) ==
sizeof(
long ) )
1352 fail = nc_put_vara_int(
ncFile, nc_var, start, count, variable_array );
1356 long* lptr =
new long[number_values];
1357 for(
int jj = 0; jj < number_values; jj++ )
1358 lptr[jj] = variable_array[jj];
1359 fail = nc_put_vara_long(
ncFile, nc_var, start, count, lptr );
1363 if( NC_NOERR !=
fail )
1365 MB_SET_ERR( MB_FAILURE,
"Failed to store variable " << variable_name );
1377 for( std::vector< DirichletSetData >::iterator ns_it = nodesets.begin(); ns_it != nodesets.end(); ++ns_it )
1380 int number_nodes = ( *ns_it ).number_nodes;
1381 if( 0 == number_nodes )
continue;
1390 int* exodus_id_array =
new int[number_nodes];
1391 double* dist_factor_array =
new double[number_nodes];
1393 std::vector< EntityHandle >::iterator begin_iter, end_iter;
1394 std::vector< double >::iterator other_iter;
1395 begin_iter = ( *ns_it ).nodes.begin();
1396 end_iter = ( *ns_it ).nodes.end();
1397 other_iter = ( *ns_it ).node_dist_factors.begin();
1403 for( ; begin_iter != end_iter; ++begin_iter )
1408 exodus_id_array[j] = exodus_id;
1409 dist_factor_array[j] = *( other_iter );
1424 if( !number_nodes ) status = 0;
1432 std::vector< int > dims;
1433 INS_ID( wname,
"node_ns%d", ns_index + 1 );
1434 GET_VAR( wname, nc_var, dims );
1437 MB_SET_ERR( MB_FAILURE,
"Failed to get node_ns variable" );
1440 size_t start = 0, count = number_nodes;
1441 int fail = nc_put_vara_int(
ncFile, nc_var, &start, &count, exodus_id_array );
1442 if( NC_NOERR !=
fail )
1444 MB_SET_ERR( MB_FAILURE,
"Failed writing exodus id array" );
1448 INS_ID( wname,
"dist_fact_ns%d", ns_index + 1 );
1450 GET_VAR( wname, nc_var, dims );
1453 MB_SET_ERR( MB_FAILURE,
"Failed to get dist_fact variable" );
1455 fail = nc_put_vara_double(
ncFile, nc_var, &start, &count, dist_factor_array );
1456 if( NC_NOERR !=
fail )
1458 MB_SET_ERR( MB_FAILURE,
"Failed writing dist factor array" );
1461 delete[] dist_factor_array;
1462 delete[] exodus_id_array;
1468 for( i = 0; i < sidesets.size(); i++ )
1473 int side_set_id = sideset_data.
id;
1477 if( 0 == number_elements )
continue;
1480 int* output_element_ids =
new int[number_elements];
1481 int* output_element_side_numbers =
new int[number_elements];
1483 std::vector< EntityHandle >::iterator begin_iter, end_iter;
1484 begin_iter = sideset_data.
elements.begin();
1485 end_iter = sideset_data.
elements.end();
1486 std::vector< int >::iterator side_iter = sideset_data.
side_numbers.begin();
1493 for( ; begin_iter != end_iter; ++begin_iter, ++side_iter )
1496 MB_CHK_SET_ERR( result,
"Problem getting exodus id for sideset element "
1499 output_element_ids[j] = exodus_id;
1500 output_element_side_numbers[j++] = *side_iter;
1503 if( 0 != number_elements )
1520 if( 0 == number_elements ) status = 0;
1534 std::vector< int > dims;
1535 INS_ID( wname,
"elem_ss%d", ss_index );
1536 GET_VAR( wname, nc_var, dims );
1539 MB_SET_ERR( MB_FAILURE,
"Failed to get elem_ss variable" );
1541 size_t start = 0, count = number_elements;
1542 int fail = nc_put_vara_int(
ncFile, nc_var, &start, &count, output_element_ids );
1543 if( NC_NOERR !=
fail )
1545 MB_SET_ERR( MB_FAILURE,
"Failed writing sideset element array" );
1548 INS_ID( wname,
"side_ss%d", ss_index );
1550 GET_VAR( wname, nc_var, dims );
1553 MB_SET_ERR( MB_FAILURE,
"Failed to get side_ss variable" );
1555 fail = nc_put_vara_int(
ncFile, nc_var, &start, &count, output_element_side_numbers );
1556 if( NC_NOERR !=
fail )
1558 MB_SET_ERR( MB_FAILURE,
"Failed writing sideset side array" );
1561 INS_ID( wname,
"dist_fact_ss%d", ss_index );
1563 GET_VAR( wname, nc_var, dims );
1566 MB_SET_ERR( MB_FAILURE,
"Failed to get sideset dist factors variable" );
1570 if( NC_NOERR !=
fail )
1572 MB_SET_ERR( MB_FAILURE,
"Failed writing sideset dist factors array" );
1576 delete[] output_element_ids;
1577 delete[] output_element_side_numbers;
1584 std::vector< MaterialSetData >& block_data,
1585 std::vector< NeumannSetData >& sideset_data,
1586 std::vector< DirichletSetData >& nodeset_data,
1587 const char* title_string,
1609 int element_block_index;
1613 int dim_str, dim_four, dim_line, dim_time;
1616 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to get string length in file" );
1621 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to get line length in file" );
1624 if( nc_def_dim(
ncFile,
"four", 4, &dim_four ) != NC_NOERR )
1626 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to locate four in file" );
1629 if( nc_def_dim(
ncFile,
"time_step", 1, &dim_time ) != NC_NOERR )
1631 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to locate time step in file" );
1635 if( NC_NOERR != nc_def_var(
ncFile,
"time_whole", NC_DOUBLE, 1, &dim_time, &dtime ) )
1637 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define time whole array" );
1649 char working_title[80];
1650 strncpy( working_title, title_string, 79 );
1652 int length = strlen( working_title );
1653 for(
int pos = 0; pos <
length; pos++ )
1655 if( working_title[pos] ==
'\\' ) working_title[pos] =
'/';
1658 if( NC_NOERR != nc_put_att_text(
ncFile, NC_GLOBAL,
"title",
length, working_title ) )
1660 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define title attribute" );
1664 float dum_vers = 6.28F;
1665 if( NC_NOERR != nc_put_att_float(
ncFile, NC_GLOBAL,
"api_version", NC_FLOAT, 1, &dum_vers ) )
1667 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define api_version attribute" );
1670 if( NC_NOERR != nc_put_att_float(
ncFile, NC_GLOBAL,
"version", NC_FLOAT, 1, &dum_vers ) )
1672 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define version attribute" );
1674 int dum_siz =
sizeof( double );
1675 if( NC_NOERR != nc_put_att_int(
ncFile, NC_GLOBAL,
"floating_point_word_size", NC_INT, 1, &dum_siz ) )
1677 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define floating pt word size attribute" );
1682 int num_el_blk, num_elem, num_nodes, num_dim, num_fa_blk, num_faces;
1683 if( nc_def_dim(
ncFile,
"num_dim", (
size_t)mesh_info.
num_dim, &num_dim ) != NC_NOERR )
1685 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of dimensions" );
1688 if( nc_def_dim(
ncFile,
"num_nodes", mesh_info.
num_nodes, &num_nodes ) != NC_NOERR )
1690 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of nodes" );
1698 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of nodes" );
1701 if( nc_def_dim(
ncFile,
"num_elem", mesh_info.
num_elements, &num_elem ) != NC_NOERR )
1703 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of elements" );
1708 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of element blocks" );
1715 if( NC_NOERR != nc_def_var(
ncFile,
"eb_status", NC_LONG, 1, &num_el_blk, &idstat ) )
1717 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define element block status array" );
1723 if( NC_NOERR != nc_def_var(
ncFile,
"eb_prop1", NC_LONG, 1, &num_el_blk, &idarr ) )
1725 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define element block id array" );
1729 if( NC_NOERR != nc_put_att_text(
ncFile, idarr,
"name", strlen(
"ID" ),
"ID" ) )
1731 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to store element block property name ID" );
1735 int num_fa_blocks = 0;
1736 for(
unsigned int i = 0; i < block_data.size(); i++ )
1748 if( num_fa_blocks > 0 )
1751 if( nc_def_dim(
ncFile,
"num_fa_blk", num_fa_blocks, &num_fa_blk ) != NC_NOERR )
1753 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of face blocks" );
1757 if( NC_NOERR != nc_def_var(
ncFile,
"fa_status", NC_LONG, 1, &num_fa_blk, &idstatf ) )
1759 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define face block status array" );
1765 if( NC_NOERR != nc_def_var(
ncFile,
"fa_prop1", NC_LONG, 1, &num_fa_blk, &idarrf ) )
1767 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define face block id array" );
1771 if( NC_NOERR != nc_put_att_text(
ncFile, idarrf,
"name", strlen(
"ID" ),
"ID" ) )
1773 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to store face block property name ID" );
1787 int num_nodes_per_face = 0;
1796 num_nodes_per_face += nnodes;
1800 for(
int j = 1; j <= num_fa_blocks; j++ )
1802 INS_ID( wname,
"num_nod_per_fa%d", j );
1803 if( nc_def_dim(
ncFile, wname, (
size_t)num_nodes_per_face, &num_nod_per_fa ) != NC_NOERR )
1805 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of nodes for face block " );
1807 dims[0] = num_nod_per_fa;
1808 INS_ID( wname,
"fbconn%d", j );
1810 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, dims, &fbconn ) )
1812 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create connectivity array for face block " << 1 );
1814 std::string element_type_string(
"nsided" );
1815 if( NC_NOERR != nc_put_att_text(
ncFile, fbconn,
"elem_type", element_type_string.length(),
1816 element_type_string.c_str() ) )
1818 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to store element type nsided " );
1821 INS_ID( wname,
"num_fa_in_blk%d", j );
1825 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of nodes for face block " );
1829 INS_ID( wname,
"fbepecnt%d", j );
1831 dims[0] = num_fa_in_blk;
1832 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, dims, &fbepecnt ) )
1834 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create fbepecnt array for block " << 1 );
1836 std::string enttype1(
"NODE" );
1837 if( NC_NOERR != nc_put_att_text(
ncFile, fbepecnt,
"entity_type1", enttype1.length(), enttype1.c_str() ) )
1839 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to store entity type 1 " );
1841 std::string enttype2(
"FACE" );
1842 if( NC_NOERR != nc_put_att_text(
ncFile, fbepecnt,
"entity_type2", enttype2.length(), enttype2.c_str() ) )
1844 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to store entity type 2 " );
1851 for(
unsigned int i = 0; i < block_data.size(); i++ )
1855 element_block_index = i + 1;
1856 int num_el_in_blk = -1, num_att_in_blk = -1;
1857 int blk_attrib, connect;
1861 INS_ID( wname,
"num_el_in_blk%d", element_block_index );
1864 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of elements/block for block " << i + 1 );
1868 INS_ID( wname,
"num_nod_per_el%d", element_block_index );
1869 int num_nod_per_el = -1;
1873 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of nodes/element for block " << block.
id );
1880 INS_ID( wname,
"num_att_in_blk%d", element_block_index );
1883 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of attributes in block " << block.
id );
1886 INS_ID( wname,
"attrib%d", element_block_index );
1887 dims[0] = num_el_in_blk;
1888 dims[1] = num_att_in_blk;
1889 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_DOUBLE, 2, dims, &blk_attrib ) )
1891 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define attributes for element block " << block.
id );
1899 INS_ID( wname,
"connect%d", element_block_index );
1900 dims[0] = num_el_in_blk;
1901 dims[1] = num_nod_per_el;
1902 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 2, dims, &connect ) )
1904 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create connectivity array for block " << i + 1 );
1910 if( NC_NOERR != nc_put_att_text(
ncFile, connect,
"elem_type", element_type_string.length(),
1911 element_type_string.c_str() ) )
1918 INS_ID( wname,
"connect%d", element_block_index );
1927 dims[0] = num_nod_per_el;
1928 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, dims, &connect ) )
1930 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create connectivity array for block " << i + 1 );
1932 std::string element_type_string(
"nsided" );
1933 if( NC_NOERR != nc_put_att_text(
ncFile, connect,
"elem_type", element_type_string.length(),
1934 element_type_string.c_str() ) )
1938 INS_ID( wname,
"ebepecnt%d", element_block_index );
1940 dims[0] = num_el_in_blk;
1941 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, dims, &ebepecnt ) )
1943 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create ebepecnt array for block " << i + 1 );
1945 std::string etype1(
"NODE" );
1946 if( NC_NOERR != nc_put_att_text(
ncFile, ebepecnt,
"entity_type1", etype1.length(), etype1.c_str() ) )
1950 std::string etype2(
"ELEM" );
1951 if( NC_NOERR != nc_put_att_text(
ncFile, ebepecnt,
"entity_type2", etype2.length(), etype2.c_str() ) )
1980 num_faces2 += nfaces;
1985 INS_ID( wname,
"num_fac_per_el%d", element_block_index );
1986 if( nc_def_dim(
ncFile, wname, (
size_t)num_faces2, &num_fac_per_el ) != NC_NOERR )
1988 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of faces per block " << block.
id );
2000 INS_ID( wname,
"facconn%d", element_block_index );
2002 dims[0] = num_fac_per_el;
2003 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, dims, &facconn ) )
2005 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create facconn array for block " << i + 1 );
2007 std::string etype(
"NFACED" );
2008 if( NC_NOERR != nc_put_att_text(
ncFile, facconn,
"elem_type", etype.length(), etype.c_str() ) )
2014 INS_ID( wname,
"ebepecnt%d", element_block_index );
2016 dims[0] = num_el_in_blk;
2017 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, dims, &ebepecnt ) )
2019 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create ebepecnt array for block " << i + 1 );
2021 std::string etype1(
"FACE" );
2022 if( NC_NOERR != nc_put_att_text(
ncFile, ebepecnt,
"entity_type1", etype1.length(), etype1.c_str() ) )
2026 std::string etype2(
"ELEM" );
2027 if( NC_NOERR != nc_put_att_text(
ncFile, ebepecnt,
"entity_type2", etype2.length(), etype2.c_str() ) )
2038 int non_empty_nss = 0;
2040 std::vector< DirichletSetData >::iterator ns_it;
2041 for( ns_it = nodeset_data.begin(); ns_it != nodeset_data.end(); ++ns_it )
2043 if( 0 != ( *ns_it ).number_nodes ) non_empty_nss++;
2047 int ns_idstat = -1, ns_idarr = -1;
2048 if( non_empty_nss > 0 )
2050 if( nc_def_dim(
ncFile,
"num_node_sets", (
size_t)( non_empty_nss ), &num_ns ) != NC_NOERR )
2052 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of node sets" );
2057 if( NC_NOERR != nc_def_var(
ncFile,
"ns_status", NC_LONG, 1, &num_ns, &ns_idstat ) )
2059 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create node sets status array" );
2063 if( NC_NOERR != nc_def_var(
ncFile,
"ns_prop1", NC_LONG, 1, &num_ns, &ns_idarr ) )
2065 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create node sets property array" );
2069 if( NC_NOERR != nc_put_att_text(
ncFile, NC_GLOBAL,
"name", strlen(
"ID" ),
"ID" ) )
2071 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to store node set property name ID" );
2078 for(
unsigned i = 0; i < nodeset_data.size(); i++ )
2089 int num_nod_ns = -1;
2091 if( nc_def_dim(
ncFile, wname, (
size_t)node_set.
number_nodes, &num_nod_ns ) != NC_NOERR )
2093 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of nodes for set " << node_set.
id );
2099 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, &num_nod_ns, &node_ns ) )
2101 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create node set " << node_set.
id <<
" node list" );
2107 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_DOUBLE, 1, &num_nod_ns, &fact_ns ) )
2110 "WriteNCDF: failed to create node set " << node_set.
id <<
" distribution factor list" );
2117 long non_empty_ss = 0;
2119 std::vector< NeumannSetData >::iterator ss_it;
2120 for( ss_it = sideset_data.begin(); ss_it != sideset_data.end(); ++ss_it )
2122 if( 0 != ( *ss_it ).number_elements ) non_empty_ss++;
2125 if( non_empty_ss > 0 )
2128 if( nc_def_dim(
ncFile,
"num_side_sets", non_empty_ss, &num_ss ) != NC_NOERR )
2130 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of side sets" );
2134 int ss_idstat = -1, ss_idarr = -1;
2135 if( NC_NOERR != nc_def_var(
ncFile,
"ss_status", NC_LONG, 1, &num_ss, &ss_idstat ) )
2137 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define side set status" );
2141 if( NC_NOERR != nc_def_var(
ncFile,
"ss_prop1", NC_LONG, 1, &num_ss, &ss_idarr ) )
2143 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define side set property" );
2147 if( NC_NOERR != nc_put_att_text(
ncFile, ss_idarr,
"name", strlen(
"ID" ),
"ID" ) )
2149 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to store side set property name ID" );
2155 for(
unsigned int i = 0; i < sideset_data.size(); i++ )
2164 int num_side_ss = -1;
2165 int elem_ss = -1, side_ss = -1;
2169 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of sides in side set " << side_set.
id );
2173 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, &num_side_ss, &elem_ss ) )
2175 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create element list for side set "
2179 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, &num_side_ss, &side_ss ) )
2181 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create side list for side set "
2190 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of dist factors in side set "
2198 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, &num_df_ss, &fact_ss ) )
2200 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create dist factors list for side set "
2208 int coord, name_coord, dims[3];
2210 dims[1] = num_nodes;
2211 if( NC_NOERR != nc_def_var(
ncFile,
"coord", NC_DOUBLE, 2, dims, &coord ) )
2213 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define node coordinate array" );
2220 if( NC_NOERR != nc_def_var(
ncFile,
"coor_names", NC_CHAR, 2, dims, &name_coord ) )
2222 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define coordinate name array" );
2230 int elem_map = -1, elem_map2 = -1, node_map = -1;
2231 if( NC_NOERR != nc_def_var(
ncFile,
"elem_map", NC_LONG, 1, &num_elem, &elem_map ) )
2233 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create element map array" );
2237 if( NC_NOERR != nc_def_var(
ncFile,
"elem_num_map", NC_LONG, 1, &num_elem, &elem_map2 ) )
2239 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create element numbering map" );
2244 if( NC_NOERR != nc_def_var(
ncFile,
"node_num_map", NC_LONG, 1, &num_nodes, &node_map ) )
2246 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create node numbering map array" );
2253 int num_qa_rec = mesh_info.
qaRecords.size() / 4;
2256 if( nc_def_dim(
ncFile,
"num_qa_rec", (
long)num_qa_rec, &num_qa ) != NC_NOERR )
2258 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define qa record array size" );
2266 if( NC_NOERR != nc_def_var(
ncFile,
"qa_records", NC_CHAR, 3, dims, &qa_title ) )
2268 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define qa record array" );
2272 if( NC_NOERR != nc_enddef(
ncFile ) )
2274 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: Trouble leaving define mode" );
2283 if( strlen( (
const char*)filename ) == 0 )
2285 MB_SET_ERR( MB_FAILURE,
"Output Exodus filename not specified" );
2288 int fail = nc_create( filename, NC_CLOBBER, &
ncFile );
2291 if( NC_NOERR !=
fail )
2293 MB_SET_ERR( MB_FAILURE,
"Cannot open " << filename );
2301 Range& forward_elems,
2302 Range& reverse_elems )
2304 Range ss_elems, ss_meshsets;
2313 if( MB_FAILURE == result )
return result;
2321 if( range_iter != ss_elems.
end() )
2324 ss_elems.
erase( range_iter, ss_elems.
end() );
2334 dum_it = ss_elems.
begin();
2338 if( current_sense == 1 || current_sense == 0 ) std::copy( dum_it, ss_elems.
end(),
range_inserter( forward_elems ) );
2339 if( current_sense == -1 || current_sense == 0 )
2344 for( range_iter = ss_meshsets.
begin(); range_iter != ss_meshsets.
end(); ++range_iter )
2348 if( 0 == sense_tag || MB_FAILURE ==
mdbImpl->
tag_get_data( sense_tag, &( *range_iter ), 1, &this_sense ) )
2352 get_sideset_elems( *range_iter, this_sense * current_sense, forward_elems, reverse_elems );