20 #pragma warning( disable : 4786 )
46 #ifndef MOAB_HAVE_NETCDF
47 #error Attempt to compile WriteNCDF with NetCDF support disabled
55 #define INS_ID( stringvar, prefix, id ) sprintf( stringvar, prefix, id )
57 #define GET_DIM( ncdim, name, val ) \
59 int gdfail = nc_inq_dimid( ncFile, name, &( ncdim ) ); \
60 if( NC_NOERR == gdfail ) \
63 gdfail = nc_inq_dimlen( ncFile, ncdim, &tmp_val ); \
64 if( NC_NOERR != gdfail ) \
66 MB_SET_ERR( MB_FAILURE, "WriteNCDF:: couldn't get dimension length" ); \
75 #define GET_DIMB( ncdim, name, varname, id, val ) \
76 INS_ID( name, varname, id ); \
77 GET_DIM( ncdim, name, val );
79 #define GET_VAR( name, id, dims ) \
82 int gvfail = nc_inq_varid( ncFile, name, &( id ) ); \
83 if( NC_NOERR == gvfail ) \
86 gvfail = nc_inq_varndims( ncFile, id, &ndims ); \
87 if( NC_NOERR == gvfail ) \
89 ( dims ).resize( ndims ); \
90 gvfail = nc_inq_vardimid( ncFile, id, &( dims )[0] ); \
101 : mdbImpl( impl ),
ncFile( 0 ), mCurrentMeshHandle( 0 ), mGeomDimensionTag( 0 ), repeat_face_blocks( 0 )
103 assert( impl != NULL );
121 int dum_val_array[] = { -1, -1, -1, -1 };
144 std::vector< MaterialSetData >::iterator iter;
146 for( iter = block_info.begin(); iter != block_info.end(); ++iter )
148 iter->elements.clear();
154 struct tm* local_time;
155 time_t calendar_time;
157 calendar_time = time( NULL );
158 local_time = localtime( &calendar_time );
160 assert( NULL != time_string && NULL != date_string );
162 strftime( time_string,
TIME_STR_LEN,
"%H:%M:%S", local_time );
163 strftime( date_string,
TIME_STR_LEN,
"%m/%d/%Y", local_time );
166 time_string[10] =
'\0';
167 date_string[10] =
'\0';
171 const bool overwrite,
175 const std::vector< std::string >& qa_records,
186 std::vector< EntityHandle > blocks, nodesets, sidesets,
entities;
195 std::copy( this_range.
begin(), this_range.
end(), std::back_inserter( blocks ) );
198 std::copy( this_range.
begin(), this_range.
end(), std::back_inserter( nodesets ) );
201 std::copy( this_range.
begin(), this_range.
end(), std::back_inserter( sidesets ) );
204 if( blocks.empty() && nodesets.empty() && sidesets.empty() )
207 for(
int d = user_dimension; d > 0 && this_range.
empty(); --d )
216 blocks.push_back( block_handle );
222 for(
const EntityHandle* iter = ent_handles; iter < ent_handles + num_sets; ++iter )
225 blocks.push_back( *iter );
227 nodesets.push_back( *iter );
229 sidesets.push_back( *iter );
239 std::vector< MaterialSetData > block_info;
240 std::vector< NeumannSetData > sideset_info;
241 std::vector< DirichletSetData > nodeset_info;
243 mesh_info.
num_dim = user_dimension;
245 if( qa_records.empty() )
250 char string1[80], string2[80];
252 mesh_info.
qaRecords.push_back( string2 );
253 mesh_info.
qaRecords.push_back( string1 );
258 assert( qa_records.size() % 4 == 0 );
260 std::copy( qa_records.begin(), qa_records.end(), std::back_inserter( mesh_info.
qaRecords ) );
264 if(
gather_mesh_information( mesh_info, block_info, sideset_info, nodeset_info, blocks, sidesets, nodesets ) !=
272 int fail = nc_create( exodus_file_name, overwrite ? NC_CLOBBER : NC_NOCLOBBER, &
ncFile );
273 if( NC_NOERR !=
fail )
288 size_t start = 0, count = 1;
290 std::vector< int > dims;
291 GET_VAR(
"time_whole", nc_var, dims );
292 fail = nc_put_vara_double(
ncFile, nc_var, &start, &count, &timev );
293 if( NC_NOERR !=
fail )
295 MB_SET_ERR( MB_FAILURE,
"Failed writing dist factor array" );
356 if( NC_NOERR !=
fail )
358 MB_SET_ERR( MB_FAILURE,
"Trouble closing file" );
365 std::vector< MaterialSetData >& block_info,
366 std::vector< NeumannSetData >& sideset_info,
367 std::vector< DirichletSetData >& nodeset_info,
368 std::vector< EntityHandle >& blocks,
369 std::vector< EntityHandle >& sidesets,
370 std::vector< EntityHandle >& nodesets )
373 std::vector< EntityHandle >::iterator vector_iter, end_vector_iter;
382 vector_iter = blocks.begin();
383 end_vector_iter = blocks.end();
385 std::vector< EntityHandle > parent_meshsets;
393 int highest_dimension_of_element_blocks = 0;
395 for( vector_iter = blocks.begin(); vector_iter != blocks.end(); ++vector_iter )
408 if( dummy_range.
empty() )
continue;
413 MB_SET_ERR( MB_FAILURE,
"Couldn't get block id from a tag for an element block" );
436 MB_SET_ERR( MB_FAILURE,
"Entities in block " <<
id <<
" not of common type" );
440 if( entity_type ==
MBQUAD || entity_type ==
MBTRI )
442 else if( entity_type ==
MBEDGE )
447 if( dimension > highest_dimension_of_element_blocks ) highest_dimension_of_element_blocks = dimension;
449 std::vector< EntityHandle > tmp_conn;
456 MB_SET_ERR( MB_FAILURE,
"Element type in block " <<
id <<
" didn't get set correctly" );
493 if( !sidesets.empty() )
498 unsigned char bit = 0x1;
504 block_info.push_back( block_data );
506 const void* data = NULL;
511 const char* qa_rec =
static_cast< const char*
>( data );
514 for(
int i = 0; i <
size; i++ )
516 if( qa_rec[i] ==
'\0' )
518 std::string qa_string( &qa_rec[start], i - start );
519 mesh_info.
qaRecords.push_back( qa_string );
526 if( count > 0 ) assert( count % 4 == 0 );
532 for( std::vector< MaterialSetData >::iterator blit = block_info.begin(); blit != block_info.end(); blit++ )
545 if( highest_dimension_of_element_blocks < 2 )
548 mesh_info.
num_dim = highest_dimension_of_element_blocks;
553 end_range_iter = mesh_info.
nodes.
end();
559 vector_iter = nodesets.begin();
560 end_vector_iter = nodesets.end();
562 for( ; vector_iter != end_vector_iter; ++vector_iter )
571 MB_SET_ERR( MB_FAILURE,
"Couldn't get id tag for nodeset " <<
id );
574 nodeset_data.
id = id;
576 std::vector< EntityHandle > node_vector;
580 MB_SET_ERR( MB_FAILURE,
"Couldn't get nodes in nodeset " <<
id );
584 const double* dist_factor_vector;
585 int dist_factor_size;
588 int has_dist_factors = 0;
591 has_dist_factors = 1;
592 dist_factor_size /=
sizeof( double );
593 dist_factor_vector =
reinterpret_cast< const double*
>( ptr );
594 std::vector< EntityHandle >::iterator iter, end_iter;
595 iter = node_vector.begin();
596 end_iter = node_vector.end();
599 unsigned char node_marked = 0;
601 for( ; iter != end_iter; ++iter )
606 if( 0x1 == node_marked )
608 nodeset_data.
nodes.push_back( *iter );
609 if( 0 != has_dist_factors )
618 nodeset_info.push_back( nodeset_data );
622 vector_iter = sidesets.begin();
623 end_vector_iter = sidesets.end();
625 for( ; vector_iter != end_vector_iter; ++vector_iter )
632 sideset_data.
id = id;
637 Range forward_elems, reverse_elems;
638 if(
get_sideset_elems( *vector_iter, 0, forward_elems, reverse_elems ) == MB_FAILURE )
return MB_FAILURE;
644 sideset_info.push_back( sideset_data );
658 const double* dist_factor_vector = 0;
659 int dist_factor_size = 0;
662 const double* dist_fac_iter = 0;
664 bool has_dist_factors =
false;
669 has_dist_factors =
true;
670 dist_factor_vector =
reinterpret_cast< const double*
>( ptr );
671 dist_fac_iter = dist_factor_vector;
672 dist_factor_size /=
sizeof( double );
675 unsigned char element_marked = 0;
682 if( 0x1 == element_marked )
684 sideset_data.
elements.push_back( *iter );
687 sideset_data.
side_numbers.push_back( ( sense == 1 ? 1 : 2 ) );
691 std::vector< EntityHandle > parents;
700 fprintf(stderr,
"[Warning]: Couldn't get adjacencies for sideset.\n");
704 if( !parents.empty() )
707 for(
unsigned int k = 0; k < parents.size(); k++ )
711 int side_no, this_sense, this_offset;
712 if( 0x1 == element_marked &&
714 this_sense == sense )
716 sideset_data.
elements.push_back( parents[k] );
727 fprintf(stderr,
"[Warning]: No parent element exists for element in sideset %i\n", sideset_data.
id);
732 if( sideset_data.
elements.size() != 0 )
738 if( has_dist_factors )
740 std::copy( dist_fac_iter, dist_fac_iter + num_nodes,
742 dist_fac_iter += num_nodes;
746 for(
int j = 0; j < num_nodes; j++ )
759 for( std::vector< std::string >::iterator string_it = qa_record_list.begin(); string_it != qa_record_list.end(); )
761 for(
int j = 0; j < 4; j++ )
773 std::vector< int > dims;
775 GET_VAR(
"qa_records", temp_var, dims );
778 MB_SET_ERR( MB_FAILURE,
"WriteNCDF:: Problem getting qa record variable" );
780 size_t count[3], start[3];
783 start[0] = record_number;
784 start[1] = record_position;
789 count[2] = (long)strlen(
string ) + 1;
790 int fail = nc_put_vara_text(
ncFile, temp_var, start, count,
string );
791 if( NC_NOERR !=
fail )
793 MB_SET_ERR( MB_FAILURE,
"Failed to position qa string variable" );
803 std::vector< int > dims;
804 GET_VAR(
"coor_names", nc_var, dims );
807 MB_SET_ERR( MB_FAILURE,
"Trouble getting coordinate name variable" );
812 strcpy( dum_str,
"x" );
813 int fail = nc_put_vara_text(
ncFile, nc_var, start, count, dum_str );
814 if( NC_NOERR !=
fail )
816 MB_SET_ERR( MB_FAILURE,
"Trouble adding x coordinate name; netcdf message: " << nc_strerror(
fail ) );
820 strcpy( dum_str,
"y" );
821 fail = nc_put_vara_text(
ncFile, nc_var, start, count, dum_str );
822 if( NC_NOERR !=
fail )
824 MB_SET_ERR( MB_FAILURE,
"Trouble adding y coordinate name; netcdf message: " << nc_strerror(
fail ) );
828 strcpy( dum_str,
"z" );
829 fail = nc_put_vara_text(
ncFile, nc_var, start, count, dum_str );
830 if( NC_NOERR !=
fail )
832 MB_SET_ERR( MB_FAILURE,
"Trouble adding z coordinate name; netcdf message: " << nc_strerror(
fail ) );
839 bool transform_needed =
true;
842 int num_coords_to_fill = transform_needed ? 3 : dimension;
844 std::vector< double* > coord_arrays( 3 );
845 coord_arrays[0] =
new double[num_nodes];
846 coord_arrays[1] =
new double[num_nodes];
847 coord_arrays[2] = NULL;
849 if( num_coords_to_fill == 3 ) coord_arrays[2] =
new double[num_nodes];
854 delete[] coord_arrays[0];
855 delete[] coord_arrays[1];
856 if( coord_arrays[2] )
delete[] coord_arrays[2];
860 if( transform_needed )
862 double trans_matrix[16];
866 for(
int i = 0; i < num_nodes; i++ )
871 vec2[0] = coord_arrays[0][i];
872 vec2[1] = coord_arrays[1][i];
873 vec2[2] = coord_arrays[2][i];
875 for(
int row = 0; row < 3; row++ )
878 for(
int col = 0; col < 3; col++ )
879 vec1[row] += ( trans_matrix[( row * 4 ) + col] * vec2[col] );
882 coord_arrays[0][i] = vec1[0];
883 coord_arrays[1][i] = vec1[1];
884 coord_arrays[2][i] = vec1[2];
890 GET_VAR(
"coord", nc_var, dims );
893 MB_SET_ERR( MB_FAILURE,
"Trouble getting coordinate variable" );
896 count[1] = num_nodes;
897 fail = nc_put_vara_double(
ncFile, nc_var, start, count, &( coord_arrays[0][0] ) );
898 if( NC_NOERR !=
fail )
900 MB_SET_ERR( MB_FAILURE,
"Trouble writing x coordinate" );
904 fail = nc_put_vara_double(
ncFile, nc_var, start, count, &( coord_arrays[1][0] ) );
905 if( NC_NOERR !=
fail )
907 MB_SET_ERR( MB_FAILURE,
"Trouble writing y coordinate" );
911 fail = nc_put_vara_double(
ncFile, nc_var, start, count, &( coord_arrays[2][0] ) );
912 if( NC_NOERR !=
fail )
914 MB_SET_ERR( MB_FAILURE,
"Trouble writing z coordinate" );
917 delete[] coord_arrays[0];
918 delete[] coord_arrays[1];
919 if( coord_arrays[2] )
delete[] coord_arrays[2];
941 std::vector< int > dims;
944 int num_faces_in_block = (int)pfaces.
size();
947 INS_ID( wname,
"fbconn%u", bl + 1 );
948 GET_VAR( wname, nc_var, dims );
950 INS_ID( wname,
"num_nod_per_fa%u", bl + 1 );
951 int ncdim, num_nod_per_face;
952 GET_DIM( ncdim, wname, num_nod_per_face );
953 int* connectivity =
new int[num_nod_per_face];
954 int ixcon = 0, j = 0;
955 std::vector< int > fbepe( num_faces_in_block );
962 for(
int k = 0; k < nnodes; k++ )
963 connectivity[ixcon++] = conn[k];
966 size_t start[1] = { 0 }, count[1] = { 0 };
968 int fail = nc_put_vara_int(
ncFile, nc_var, start, count, connectivity );
969 if( NC_NOERR !=
fail )
971 delete[] connectivity;
972 MB_SET_ERR( MB_FAILURE,
"Couldn't write fbconn variable" );
975 INS_ID( wname,
"fbepecnt%u", bl + 1 );
976 GET_VAR( wname, nc_var, dims );
977 count[0] = num_faces_in_block;
979 fail = nc_put_vara_int(
ncFile, nc_var, start, count, &fbepe[0] );
980 if( NC_NOERR !=
fail )
982 delete[] connectivity;
983 MB_SET_ERR( MB_FAILURE,
"Couldn't write fbepecnt variable" );
995 MB_SET_ERR( MB_FAILURE,
"Problem writing face block status" );
998 delete[] connectivity;
1005 std::vector< MaterialSetData >& block_info,
1006 std::vector< NeumannSetData >& sideset_info,
1007 std::vector< DirichletSetData >& nodeset_info,
1008 const char* filename )
1015 std::string title_string =
"MOAB";
1016 title_string.append(
"(" );
1017 title_string.append( filename );
1018 title_string.append(
"): " );
1019 title_string.append( date );
1020 title_string.append(
": " );
1021 title_string.append(
"time " );
1028 int result =
initialize_exodus_file( mesh_info, block_info, sideset_info, nodeset_info, title_string.c_str() );
1030 if( result == MB_FAILURE )
return MB_FAILURE;
1038 int block_index = 0;
1041 for( i = 0; i < block_data.size(); i++ )
1062 MB_SET_ERR( MB_FAILURE,
"No elements in block " <<
id );
1067 MB_SET_ERR( MB_FAILURE,
"Problem writing element block status" );
1073 unsigned int num_nodes = num_nodes_per_elem * num_elem;
1076 num_nodes = num_nodes_per_elem;
1078 int* connectivity =
new int[num_nodes];
1083 exodus_id, connectivity );
1086 delete[] connectivity;
1087 MB_SET_ERR( result,
"Couldn't get element array to write from" );
1093 const int* reorder = 0;
1101 std::vector< int > dims;
1104 exodus_id += num_elem;
1105 INS_ID( wname,
"connect%u", i + 1 );
1107 GET_VAR( wname, nc_var, dims );
1110 delete[] connectivity;
1111 MB_SET_ERR( MB_FAILURE,
"Couldn't get connectivity variable" );
1117 size_t start[1] = { 0 }, count[1] = { num_nodes_per_elem };
1118 int fail = nc_put_vara_int(
ncFile, nc_var, start, count, connectivity );
1119 if( NC_NOERR !=
fail )
1121 delete[] connectivity;
1122 MB_SET_ERR( MB_FAILURE,
"Couldn't write connectivity variable" );
1125 INS_ID( wname,
"ebepecnt%u", i + 1 );
1126 GET_VAR( wname, nc_var, dims );
1137 connectivity[j] = nnodes;
1139 fail = nc_put_vara_int(
ncFile, nc_var, start, count, connectivity );
1140 if( NC_NOERR !=
fail )
1142 delete[] connectivity;
1143 MB_SET_ERR( MB_FAILURE,
"Couldn't write ebepecnt variable" );
1148 size_t start[2] = { 0, 0 }, count[2] = { num_elem, num_nodes_per_elem };
1149 int fail = nc_put_vara_int(
ncFile, nc_var, start, count, connectivity );
1150 if( NC_NOERR !=
fail )
1152 delete[] connectivity;
1153 MB_SET_ERR( MB_FAILURE,
"Couldn't write connectivity variable" );
1180 INS_ID( wname,
"facconn%u", i + 1 );
1181 GET_VAR( wname, nc_var, dims );
1184 int ixcon = 0, j = 0;
1185 size_t start[1] = { 0 }, count[1] = { 0 };
1193 for(
int k = 0; k < nfaces; k++ )
1195 int index = block_faces.
index( conn[k] );
1196 if( index == -1 )
MB_SET_ERR( MB_FAILURE,
"Couldn't find face in polyhedron" );
1197 connectivity[ixcon++] = index + 1;
1199 ebepe[j++] = nfaces;
1203 int fail = nc_put_vara_int(
ncFile, nc_var, start, count, connectivity );
1204 if( NC_NOERR !=
fail )
1206 delete[] connectivity;
1207 MB_SET_ERR( MB_FAILURE,
"Couldn't write fbconn variable" );
1210 INS_ID( wname,
"ebepecnt%u", i + 1 );
1211 GET_VAR( wname, nc_var, dims );
1214 fail = nc_put_vara_int(
ncFile, nc_var, start, count, &ebepe[0] );
1215 if( NC_NOERR !=
fail )
1217 delete[] connectivity;
1218 MB_SET_ERR( MB_FAILURE,
"Couldn't write fbepecnt variable" );
1222 delete[] connectivity;
1233 int* map =
new int[num_nodes];
1238 range_iter = nodes.
begin();
1239 end_iter = nodes.
end();
1243 for( ; range_iter != end_iter; ++range_iter )
1253 if( map )
delete[] map;
1257 MB_SET_ERR( MB_FAILURE,
"Failed writing global node order map" );
1266 int* map =
new int[num_elements];
1272 for(
int i = 0; i < num_elements; i++ )
1279 if( map )
delete[] map;
1283 MB_SET_ERR( MB_FAILURE,
"Failed writing global element order map" );
1294 int* map =
new int[num_elements];
1298 for(
int i = 0; i < num_elements; i++ )
1307 if( map )
delete[] map;
1311 MB_SET_ERR( MB_FAILURE,
"Failed writing element map" );
1318 int* variable_array,
1328 std::vector< int > dims;
1329 GET_VAR( variable_name, nc_var, dims );
1332 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to locate variable " << variable_name <<
" in file" );
1337 size_t start[1], count[1];
1338 start[0] = start_position;
1339 count[0] = number_values;
1341 int fail = NC_NOERR;
1342 if(
sizeof(
int ) ==
sizeof(
long ) )
1344 fail = nc_put_vara_int(
ncFile, nc_var, start, count, variable_array );
1348 long* lptr =
new long[number_values];
1349 for(
int jj = 0; jj < number_values; jj++ )
1350 lptr[jj] = variable_array[jj];
1351 fail = nc_put_vara_long(
ncFile, nc_var, start, count, lptr );
1355 if( NC_NOERR !=
fail )
1357 MB_SET_ERR( MB_FAILURE,
"Failed to store variable " << variable_name );
1369 for( std::vector< DirichletSetData >::iterator ns_it = nodesets.begin(); ns_it != nodesets.end(); ++ns_it )
1372 int number_nodes = ( *ns_it ).number_nodes;
1373 if( 0 == number_nodes )
continue;
1382 int* exodus_id_array =
new int[number_nodes];
1383 double* dist_factor_array =
new double[number_nodes];
1385 std::vector< EntityHandle >::iterator begin_iter, end_iter;
1386 std::vector< double >::iterator other_iter;
1387 begin_iter = ( *ns_it ).nodes.begin();
1388 end_iter = ( *ns_it ).nodes.end();
1389 other_iter = ( *ns_it ).node_dist_factors.begin();
1395 for( ; begin_iter != end_iter; ++begin_iter )
1399 exodus_id_array[j] = exodus_id;
1400 dist_factor_array[j] = *( other_iter );
1414 if( !number_nodes ) status = 0;
1421 std::vector< int > dims;
1422 INS_ID( wname,
"node_ns%d", ns_index + 1 );
1423 GET_VAR( wname, nc_var, dims );
1426 MB_SET_ERR( MB_FAILURE,
"Failed to get node_ns variable" );
1429 size_t start = 0, count = number_nodes;
1430 int fail = nc_put_vara_int(
ncFile, nc_var, &start, &count, exodus_id_array );
1431 if( NC_NOERR !=
fail )
1433 MB_SET_ERR( MB_FAILURE,
"Failed writing exodus id array" );
1437 INS_ID( wname,
"dist_fact_ns%d", ns_index + 1 );
1439 GET_VAR( wname, nc_var, dims );
1442 MB_SET_ERR( MB_FAILURE,
"Failed to get dist_fact variable" );
1444 fail = nc_put_vara_double(
ncFile, nc_var, &start, &count, dist_factor_array );
1445 if( NC_NOERR !=
fail )
1447 MB_SET_ERR( MB_FAILURE,
"Failed writing dist factor array" );
1450 delete[] dist_factor_array;
1451 delete[] exodus_id_array;
1457 for( i = 0; i < sidesets.size(); i++ )
1462 int side_set_id = sideset_data.
id;
1466 if( 0 == number_elements )
continue;
1469 int* output_element_ids =
new int[number_elements];
1470 int* output_element_side_numbers =
new int[number_elements];
1472 std::vector< EntityHandle >::iterator begin_iter, end_iter;
1473 begin_iter = sideset_data.
elements.begin();
1474 end_iter = sideset_data.
elements.end();
1475 std::vector< int >::iterator side_iter = sideset_data.
side_numbers.begin();
1482 for( ; begin_iter != end_iter; ++begin_iter, ++side_iter )
1487 output_element_ids[j] = exodus_id;
1488 output_element_side_numbers[j++] = *side_iter;
1491 if( 0 != number_elements )
1507 if( 0 == number_elements ) status = 0;
1520 std::vector< int > dims;
1521 INS_ID( wname,
"elem_ss%d", ss_index );
1522 GET_VAR( wname, nc_var, dims );
1525 MB_SET_ERR( MB_FAILURE,
"Failed to get elem_ss variable" );
1527 size_t start = 0, count = number_elements;
1528 int fail = nc_put_vara_int(
ncFile, nc_var, &start, &count, output_element_ids );
1529 if( NC_NOERR !=
fail )
1531 MB_SET_ERR( MB_FAILURE,
"Failed writing sideset element array" );
1534 INS_ID( wname,
"side_ss%d", ss_index );
1536 GET_VAR( wname, nc_var, dims );
1539 MB_SET_ERR( MB_FAILURE,
"Failed to get side_ss variable" );
1541 fail = nc_put_vara_int(
ncFile, nc_var, &start, &count, output_element_side_numbers );
1542 if( NC_NOERR !=
fail )
1544 MB_SET_ERR( MB_FAILURE,
"Failed writing sideset side array" );
1547 INS_ID( wname,
"dist_fact_ss%d", ss_index );
1549 GET_VAR( wname, nc_var, dims );
1552 MB_SET_ERR( MB_FAILURE,
"Failed to get sideset dist factors variable" );
1556 if( NC_NOERR !=
fail )
1558 MB_SET_ERR( MB_FAILURE,
"Failed writing sideset dist factors array" );
1562 delete[] output_element_ids;
1563 delete[] output_element_side_numbers;
1570 std::vector< MaterialSetData >& block_data,
1571 std::vector< NeumannSetData >& sideset_data,
1572 std::vector< DirichletSetData >& nodeset_data,
1573 const char* title_string,
1595 int element_block_index;
1599 int dim_str, dim_four, dim_line, dim_time;
1602 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to get string length in file" );
1607 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to get line length in file" );
1610 if( nc_def_dim(
ncFile,
"four", 4, &dim_four ) != NC_NOERR )
1612 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to locate four in file" );
1615 if( nc_def_dim(
ncFile,
"time_step", 1, &dim_time ) != NC_NOERR )
1617 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to locate time step in file" );
1621 if( NC_NOERR != nc_def_var(
ncFile,
"time_whole", NC_DOUBLE, 1, &dim_time, &dtime ) )
1623 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define time whole array" );
1635 char working_title[80];
1636 strncpy( working_title, title_string, 79 );
1638 int length = strlen( working_title );
1639 for(
int pos = 0; pos <
length; pos++ )
1641 if( working_title[pos] ==
'\\' ) working_title[pos] =
'/';
1644 if( NC_NOERR != nc_put_att_text(
ncFile, NC_GLOBAL,
"title",
length, working_title ) )
1646 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define title attribute" );
1650 float dum_vers = 6.28F;
1651 if( NC_NOERR != nc_put_att_float(
ncFile, NC_GLOBAL,
"api_version", NC_FLOAT, 1, &dum_vers ) )
1653 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define api_version attribute" );
1656 if( NC_NOERR != nc_put_att_float(
ncFile, NC_GLOBAL,
"version", NC_FLOAT, 1, &dum_vers ) )
1658 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define version attribute" );
1660 int dum_siz =
sizeof( double );
1661 if( NC_NOERR != nc_put_att_int(
ncFile, NC_GLOBAL,
"floating_point_word_size", NC_INT, 1, &dum_siz ) )
1663 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define floating pt word size attribute" );
1668 int num_el_blk, num_elem, num_nodes, num_dim, num_fa_blk, num_faces;
1669 if( nc_def_dim(
ncFile,
"num_dim", (
size_t)mesh_info.
num_dim, &num_dim ) != NC_NOERR )
1671 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of dimensions" );
1674 if( nc_def_dim(
ncFile,
"num_nodes", mesh_info.
num_nodes, &num_nodes ) != NC_NOERR )
1676 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of nodes" );
1684 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of nodes" );
1687 if( nc_def_dim(
ncFile,
"num_elem", mesh_info.
num_elements, &num_elem ) != NC_NOERR )
1689 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of elements" );
1694 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of element blocks" );
1701 if( NC_NOERR != nc_def_var(
ncFile,
"eb_status", NC_LONG, 1, &num_el_blk, &idstat ) )
1703 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define element block status array" );
1709 if( NC_NOERR != nc_def_var(
ncFile,
"eb_prop1", NC_LONG, 1, &num_el_blk, &idarr ) )
1711 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define element block id array" );
1715 if( NC_NOERR != nc_put_att_text(
ncFile, idarr,
"name", strlen(
"ID" ),
"ID" ) )
1717 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to store element block property name ID" );
1721 int num_fa_blocks = 0, num_polyh_blocks = 0;
1722 for(
unsigned int i = 0; i < block_data.size(); i++ )
1734 if( num_fa_blocks > 0 )
1737 if( nc_def_dim(
ncFile,
"num_fa_blk", num_fa_blocks, &num_fa_blk ) != NC_NOERR )
1739 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of face blocks" );
1743 if( NC_NOERR != nc_def_var(
ncFile,
"fa_status", NC_LONG, 1, &num_fa_blk, &idstatf ) )
1745 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define face block status array" );
1751 if( NC_NOERR != nc_def_var(
ncFile,
"fa_prop1", NC_LONG, 1, &num_fa_blk, &idarrf ) )
1753 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define face block id array" );
1757 if( NC_NOERR != nc_put_att_text(
ncFile, idarrf,
"name", strlen(
"ID" ),
"ID" ) )
1759 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to store face block property name ID" );
1773 int num_nodes_per_face = 0;
1782 num_nodes_per_face += nnodes;
1786 for(
int j = 1; j <= num_fa_blocks; j++ )
1788 INS_ID( wname,
"num_nod_per_fa%d", j );
1789 if( nc_def_dim(
ncFile, wname, (
size_t)num_nodes_per_face, &num_nod_per_fa ) != NC_NOERR )
1791 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of nodes for face block " );
1793 dims[0] = num_nod_per_fa;
1794 INS_ID( wname,
"fbconn%d", j );
1796 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, dims, &fbconn ) )
1798 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create connectivity array for face block " << 1 );
1800 std::string element_type_string(
"nsided" );
1801 if( NC_NOERR != nc_put_att_text(
ncFile, fbconn,
"elem_type", element_type_string.length(),
1802 element_type_string.c_str() ) )
1804 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to store element type nsided " );
1807 INS_ID( wname,
"num_fa_in_blk%d", j );
1811 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of nodes for face block " );
1815 INS_ID( wname,
"fbepecnt%d", j );
1817 dims[0] = num_fa_in_blk;
1818 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, dims, &fbepecnt ) )
1820 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create fbepecnt array for block " << 1 );
1822 std::string enttype1(
"NODE" );
1823 if( NC_NOERR != nc_put_att_text(
ncFile, fbepecnt,
"entity_type1", enttype1.length(), enttype1.c_str() ) )
1825 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to store entity type 1 " );
1827 std::string enttype2(
"FACE" );
1828 if( NC_NOERR != nc_put_att_text(
ncFile, fbepecnt,
"entity_type2", enttype2.length(), enttype2.c_str() ) )
1830 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to store entity type 2 " );
1837 for(
unsigned int i = 0; i < block_data.size(); i++ )
1841 element_block_index = i + 1;
1842 int num_el_in_blk = -1, num_att_in_blk = -1;
1843 int blk_attrib, connect;
1847 INS_ID( wname,
"num_el_in_blk%d", element_block_index );
1850 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of elements/block for block " << i + 1 );
1854 INS_ID( wname,
"num_nod_per_el%d", element_block_index );
1855 int num_nod_per_el = -1;
1859 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of nodes/element for block " << block.
id );
1866 INS_ID( wname,
"num_att_in_blk%d", element_block_index );
1869 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of attributes in block " << block.
id );
1872 INS_ID( wname,
"attrib%d", element_block_index );
1873 dims[0] = num_el_in_blk;
1874 dims[1] = num_att_in_blk;
1875 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_DOUBLE, 2, dims, &blk_attrib ) )
1877 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define attributes for element block " << block.
id );
1885 INS_ID( wname,
"connect%d", element_block_index );
1886 dims[0] = num_el_in_blk;
1887 dims[1] = num_nod_per_el;
1888 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 2, dims, &connect ) )
1890 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create connectivity array for block " << i + 1 );
1896 if( NC_NOERR != nc_put_att_text(
ncFile, connect,
"elem_type", element_type_string.length(),
1897 element_type_string.c_str() ) )
1904 INS_ID( wname,
"connect%d", element_block_index );
1913 dims[0] = num_nod_per_el;
1914 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, dims, &connect ) )
1916 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create connectivity array for block " << i + 1 );
1918 std::string element_type_string(
"nsided" );
1919 if( NC_NOERR != nc_put_att_text(
ncFile, connect,
"elem_type", element_type_string.length(),
1920 element_type_string.c_str() ) )
1924 INS_ID( wname,
"ebepecnt%d", element_block_index );
1926 dims[0] = num_el_in_blk;
1927 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, dims, &ebepecnt ) )
1929 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create ebepecnt array for block " << i + 1 );
1931 std::string etype1(
"NODE" );
1932 if( NC_NOERR != nc_put_att_text(
ncFile, ebepecnt,
"entity_type1", etype1.length(), etype1.c_str() ) )
1936 std::string etype2(
"ELEM" );
1937 if( NC_NOERR != nc_put_att_text(
ncFile, ebepecnt,
"entity_type2", etype2.length(), etype2.c_str() ) )
1966 num_faces2 += nfaces;
1971 INS_ID( wname,
"num_fac_per_el%d", element_block_index );
1972 if( nc_def_dim(
ncFile, wname, (
size_t)num_faces2, &num_fac_per_el ) != NC_NOERR )
1974 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of faces per block " << block.
id );
1986 INS_ID( wname,
"facconn%d", element_block_index );
1988 dims[0] = num_fac_per_el;
1989 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, dims, &facconn ) )
1991 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create facconn array for block " << i + 1 );
1993 std::string etype(
"NFACED" );
1994 if( NC_NOERR != nc_put_att_text(
ncFile, facconn,
"elem_type", etype.length(), etype.c_str() ) )
2000 INS_ID( wname,
"ebepecnt%d", element_block_index );
2002 dims[0] = num_el_in_blk;
2003 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, dims, &ebepecnt ) )
2005 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create ebepecnt array for block " << i + 1 );
2007 std::string etype1(
"FACE" );
2008 if( NC_NOERR != nc_put_att_text(
ncFile, ebepecnt,
"entity_type1", etype1.length(), etype1.c_str() ) )
2012 std::string etype2(
"ELEM" );
2013 if( NC_NOERR != nc_put_att_text(
ncFile, ebepecnt,
"entity_type2", etype2.length(), etype2.c_str() ) )
2024 int non_empty_nss = 0;
2026 std::vector< DirichletSetData >::iterator ns_it;
2027 for( ns_it = nodeset_data.begin(); ns_it != nodeset_data.end(); ++ns_it )
2029 if( 0 != ( *ns_it ).number_nodes ) non_empty_nss++;
2033 int ns_idstat = -1, ns_idarr = -1;
2034 if( non_empty_nss > 0 )
2036 if( nc_def_dim(
ncFile,
"num_node_sets", (
size_t)( non_empty_nss ), &num_ns ) != NC_NOERR )
2038 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of node sets" );
2043 if( NC_NOERR != nc_def_var(
ncFile,
"ns_status", NC_LONG, 1, &num_ns, &ns_idstat ) )
2045 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create node sets status array" );
2049 if( NC_NOERR != nc_def_var(
ncFile,
"ns_prop1", NC_LONG, 1, &num_ns, &ns_idarr ) )
2051 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create node sets property array" );
2055 if( NC_NOERR != nc_put_att_text(
ncFile, NC_GLOBAL,
"name", strlen(
"ID" ),
"ID" ) )
2057 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to store node set property name ID" );
2064 for(
unsigned i = 0; i < nodeset_data.size(); i++ )
2075 int num_nod_ns = -1;
2076 INS_ID( wname,
"num_nod_ns%d", index );
2077 if( nc_def_dim(
ncFile, wname, (
size_t)node_set.
number_nodes, &num_nod_ns ) != NC_NOERR )
2079 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of nodes for set " << node_set.
id );
2084 INS_ID( wname,
"node_ns%d", index );
2085 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, &num_nod_ns, &node_ns ) )
2087 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create node set " << node_set.
id <<
" node list" );
2092 INS_ID( wname,
"dist_fact_ns%d", index );
2093 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_DOUBLE, 1, &num_nod_ns, &fact_ns ) )
2096 "WriteNCDF: failed to create node set " << node_set.
id <<
" distribution factor list" );
2103 long non_empty_ss = 0;
2105 std::vector< NeumannSetData >::iterator ss_it;
2106 for( ss_it = sideset_data.begin(); ss_it != sideset_data.end(); ++ss_it )
2108 if( 0 != ( *ss_it ).number_elements ) non_empty_ss++;
2111 if( non_empty_ss > 0 )
2114 if( nc_def_dim(
ncFile,
"num_side_sets", non_empty_ss, &num_ss ) != NC_NOERR )
2116 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of side sets" );
2120 int ss_idstat = -1, ss_idarr = -1;
2121 if( NC_NOERR != nc_def_var(
ncFile,
"ss_status", NC_LONG, 1, &num_ss, &ss_idstat ) )
2123 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define side set status" );
2127 if( NC_NOERR != nc_def_var(
ncFile,
"ss_prop1", NC_LONG, 1, &num_ss, &ss_idarr ) )
2129 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define side set property" );
2133 if( NC_NOERR != nc_put_att_text(
ncFile, ss_idarr,
"name", strlen(
"ID" ),
"ID" ) )
2135 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to store side set property name ID" );
2141 for(
unsigned int i = 0; i < sideset_data.size(); i++ )
2150 int num_side_ss = -1;
2151 int elem_ss = -1, side_ss = -1;
2152 INS_ID( wname,
"num_side_ss%d", index );
2155 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of sides in side set " << side_set.
id );
2158 INS_ID( wname,
"elem_ss%d", index );
2159 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, &num_side_ss, &elem_ss ) )
2161 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create element list for side set "
2164 INS_ID( wname,
"side_ss%d", index );
2165 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, &num_side_ss, &side_ss ) )
2167 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create side list for side set "
2173 INS_ID( wname,
"num_df_ss%d", index );
2176 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define number of dist factors in side set "
2183 INS_ID( wname,
"dist_fact_ss%d", index );
2184 if( NC_NOERR != nc_def_var(
ncFile, wname, NC_LONG, 1, &num_df_ss, &fact_ss ) )
2186 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create dist factors list for side set "
2194 int coord, name_coord, dims[3];
2196 dims[1] = num_nodes;
2197 if( NC_NOERR != nc_def_var(
ncFile,
"coord", NC_DOUBLE, 2, dims, &coord ) )
2199 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define node coordinate array" );
2206 if( NC_NOERR != nc_def_var(
ncFile,
"coor_names", NC_CHAR, 2, dims, &name_coord ) )
2208 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define coordinate name array" );
2216 int elem_map = -1, elem_map2 = -1, node_map = -1;
2217 if( NC_NOERR != nc_def_var(
ncFile,
"elem_map", NC_LONG, 1, &num_elem, &elem_map ) )
2219 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create element map array" );
2223 if( NC_NOERR != nc_def_var(
ncFile,
"elem_num_map", NC_LONG, 1, &num_elem, &elem_map2 ) )
2225 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create element numbering map" );
2230 if( NC_NOERR != nc_def_var(
ncFile,
"node_num_map", NC_LONG, 1, &num_nodes, &node_map ) )
2232 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to create node numbering map array" );
2239 int num_qa_rec = mesh_info.
qaRecords.size() / 4;
2242 if( nc_def_dim(
ncFile,
"num_qa_rec", (
long)num_qa_rec, &num_qa ) != NC_NOERR )
2244 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define qa record array size" );
2252 if( NC_NOERR != nc_def_var(
ncFile,
"qa_records", NC_CHAR, 3, dims, &qa_title ) )
2254 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: failed to define qa record array" );
2258 if( NC_NOERR != nc_enddef(
ncFile ) )
2260 MB_SET_ERR( MB_FAILURE,
"WriteNCDF: Trouble leaving define mode" );
2269 if( strlen( (
const char*)filename ) == 0 )
2271 MB_SET_ERR( MB_FAILURE,
"Output Exodus filename not specified" );
2274 int fail = nc_create( filename, NC_CLOBBER, &
ncFile );
2277 if( NC_NOERR !=
fail )
2279 MB_SET_ERR( MB_FAILURE,
"Cannot open " << filename );
2287 Range& forward_elems,
2288 Range& reverse_elems )
2290 Range ss_elems, ss_meshsets;
2299 if( MB_FAILURE == result )
return result;
2307 if( range_iter != ss_elems.
end() )
2310 ss_elems.
erase( range_iter, ss_elems.
end() );
2320 dum_it = ss_elems.
begin();
2324 if( current_sense == 1 || current_sense == 0 ) std::copy( dum_it, ss_elems.
end(),
range_inserter( forward_elems ) );
2325 if( current_sense == -1 || current_sense == 0 )
2330 for( range_iter = ss_meshsets.
begin(); range_iter != ss_meshsets.
end(); ++range_iter )
2334 if( 0 == sense_tag || MB_FAILURE ==
mdbImpl->
tag_get_data( sense_tag, &( *range_iter ), 1, &this_sense ) )
2338 get_sideset_elems( *range_iter, this_sense * current_sense, forward_elems, reverse_elems );