20 #pragma warning( disable : 4786 )
45 #ifndef MOAB_HAVE_NETCDF
46 #error Attempt to compile WriteSLAC with NetCDF disabled.
52 #define GET_VAR( name, id, dims ) \
55 int gvfail = nc_inq_varid( ncFile, name, &( id ) ); \
56 if( NC_NOERR == gvfail ) \
59 gvfail = nc_inq_varndims( ncFile, id, &ndims ); \
60 if( NC_NOERR == gvfail ) \
62 ( dims ).resize( ndims ); \
63 gvfail = nc_inq_vardimid( ncFile, id, &( dims )[0] ); \
75 assert( impl != NULL );
107 std::vector< WriteSLAC::MaterialSetData >::iterator iter;
109 for( iter = matset_info.begin(); iter != matset_info.end(); ++iter )
110 delete( *iter ).elements;
114 const bool overwrite,
118 const std::vector< std::string >& ,
126 if( NULL == strstr( file_name,
".ncdf" ) )
return MB_FAILURE;
128 std::vector< EntityHandle > matsets, dirsets, neusets,
entities;
139 std::copy( this_range.
begin(), this_range.
end(), std::back_inserter( matsets ) );
142 std::copy( this_range.
begin(), this_range.
end(), std::back_inserter( dirsets ) );
145 std::copy( this_range.
begin(), this_range.
end(), std::back_inserter( neusets ) );
150 for(
const EntityHandle* iter = ent_handles; iter < ent_handles + num_sets; ++iter )
153 matsets.push_back( *iter );
155 dirsets.push_back( *iter );
157 neusets.push_back( *iter );
164 std::vector< WriteSLAC::MaterialSetData > matset_info;
165 std::vector< WriteSLAC::DirichletSetData > dirset_info;
166 std::vector< WriteSLAC::NeumannSetData > neuset_info;
179 int fail = nc_create( file_name, overwrite ? NC_CLOBBER : NC_NOCLOBBER, &
ncFile );
180 if( NC_NOERR !=
fail )
205 if( NC_NOERR !=
fail )
return MB_FAILURE;
211 std::vector< WriteSLAC::MaterialSetData >& matset_info,
212 std::vector< WriteSLAC::NeumannSetData >& neuset_info,
213 std::vector< WriteSLAC::DirichletSetData >& dirset_info,
214 std::vector< EntityHandle >& matsets,
215 std::vector< EntityHandle >& neusets,
216 std::vector< EntityHandle >& dirsets )
218 std::vector< EntityHandle >::iterator vector_iter, end_vector_iter;
226 vector_iter = matsets.begin();
227 end_vector_iter = matsets.end();
231 std::vector< EntityHandle > parent_meshsets;
237 int highest_dimension_of_element_matsets = 0;
239 for( vector_iter = matsets.begin(); vector_iter != matsets.end(); ++vector_iter )
255 entity_iter = dummy_range.
end();
258 entity_iter = dummy_range.
begin();
262 if( entity_iter != dummy_range.
end() )
271 MB_SET_ERR( MB_FAILURE,
"Couldn't get matset id from a tag for an element matset" );
285 --end_elem_range_iter;
288 MB_SET_ERR( MB_FAILURE,
"Entities in matset " <<
id <<
" not of common type" );
292 if( entity_type ==
MBQUAD || entity_type ==
MBTRI )
294 else if( entity_type ==
MBEDGE )
299 if( dimension > highest_dimension_of_element_matsets ) highest_dimension_of_element_matsets = dimension;
304 std::vector< EntityHandle > tmp_conn;
311 MB_SET_ERR( MB_FAILURE,
"Element type in matset " <<
id <<
" didn't get set correctly" );
325 if( !neusets.empty() )
330 unsigned char bit = 0x1;
335 matset_info.push_back( matset_data );
342 if( highest_dimension_of_element_matsets < 2 )
345 mesh_info.
num_dim = highest_dimension_of_element_matsets;
350 end_range_iter = mesh_info.
nodes.
end();
356 vector_iter = dirsets.begin();
357 end_vector_iter = dirsets.end();
359 for( ; vector_iter != end_vector_iter; ++vector_iter )
368 MB_SET_ERR( MB_FAILURE,
"Couldn't get id tag for dirset " <<
id );
373 std::vector< EntityHandle > node_vector;
377 MB_SET_ERR( MB_FAILURE,
"Couldn't get nodes in dirset " <<
id );
380 std::vector< EntityHandle >::iterator iter, end_iter;
381 iter = node_vector.begin();
382 end_iter = node_vector.end();
384 unsigned char node_marked = 0;
386 for( ; iter != end_iter; ++iter )
391 if( 0x1 == node_marked ) dirset_data.
nodes.push_back( *iter );
395 dirset_info.push_back( dirset_data );
399 vector_iter = neusets.begin();
400 end_vector_iter = neusets.end();
402 for( ; vector_iter != end_vector_iter; ++vector_iter )
414 Range forward_elems, reverse_elems;
415 if(
get_neuset_elems( *vector_iter, 0, forward_elems, reverse_elems ) == MB_FAILURE )
return MB_FAILURE;
421 neuset_info.push_back( neuset_data );
432 unsigned char element_marked = 0;
439 if( 0x1 == element_marked )
441 neuset_data.
elements.push_back( *iter );
444 neuset_data.
side_numbers.push_back( ( sense == 1 ? 1 : 2 ) );
448 std::vector< EntityHandle > parents;
454 MB_SET_ERR( MB_FAILURE,
"Couldn't get adjacencies for neuset" );
457 if( !parents.empty() )
460 for(
unsigned int k = 0; k < parents.size(); k++ )
464 int side_no, this_sense, this_offset;
465 if( 0x1 == element_marked &&
467 this_sense == sense )
469 neuset_data.
elements.push_back( parents[k] );
477 MB_SET_ERR( MB_FAILURE,
"No parent element exists for element in neuset " << neuset_data.
id );
491 bool transform_needed =
true;
494 int num_coords_to_fill = transform_needed ? 3 : dimension;
496 std::vector< double* > coord_arrays( 3 );
497 coord_arrays[0] =
new double[num_nodes];
498 coord_arrays[1] =
new double[num_nodes];
499 coord_arrays[2] = NULL;
501 if( num_coords_to_fill == 3 ) coord_arrays[2] =
new double[num_nodes];
506 delete[] coord_arrays[0];
507 delete[] coord_arrays[1];
508 if( coord_arrays[2] )
delete[] coord_arrays[2];
512 if( transform_needed )
514 double trans_matrix[16];
518 for(
int i = 0; i < num_nodes; i++ )
523 vec2[0] = coord_arrays[0][i];
524 vec2[1] = coord_arrays[1][i];
525 vec2[2] = coord_arrays[2][i];
527 for(
int row = 0; row < 3; row++ )
530 for(
int col = 0; col < 3; col++ )
531 vec1[row] += ( trans_matrix[( row * 4 ) + col] * vec2[col] );
534 coord_arrays[0][i] = vec1[0];
535 coord_arrays[1][i] = vec1[1];
536 coord_arrays[2][i] = vec1[2];
542 std::vector< int > dims;
543 GET_VAR(
"coords", nc_var, dims );
544 if( -1 == nc_var )
return MB_FAILURE;
545 size_t start[2] = { 0, 0 }, count[2] = {
static_cast< size_t >( num_nodes ), 1 };
546 int fail = nc_put_vara_double(
ncFile, nc_var, start, count, coord_arrays[0] );
547 if( NC_NOERR !=
fail )
return MB_FAILURE;
549 fail = nc_put_vara_double(
ncFile, nc_var, start, count, coord_arrays[1] );
550 if( NC_NOERR !=
fail )
return MB_FAILURE;
552 fail = nc_put_vara_double(
ncFile, nc_var, start, count, coord_arrays[2] );
553 if( NC_NOERR !=
fail )
return MB_FAILURE;
555 delete[] coord_arrays[0];
556 delete[] coord_arrays[1];
557 if( coord_arrays[2] )
delete[] coord_arrays[2];
563 std::vector< WriteSLAC::MaterialSetData >& matset_data,
564 std::vector< WriteSLAC::NeumannSetData >& neuset_data )
577 for( i = 0; i < matset_data.size(); i++ )
600 std::vector< EntityHandle >::iterator vit;
601 for( i = 0; i < neuset_data.size(); i++ )
622 std::vector< WriteSLAC::MaterialSetData >& matset_data,
623 std::vector< WriteSLAC::NeumannSetData >& neuset_data )
626 std::vector< int > connect;
633 std::vector< int > dims;
636 GET_VAR(
"hexahedron_interior", hex_conn, dims );
637 if( -1 == hex_conn )
return MB_FAILURE;
639 connect.reserve( 13 );
644 size_t start[2] = { 0, 0 }, count[2] = { 1, 1 };
646 for( i = 0; i < matset_data.size(); i++ )
648 matset = matset_data[i];
668 start[0] = elem_num++;
672 fail = nc_put_vara_int(
ncFile, hex_conn, start, count, &connect[0] );
673 if( NC_NOERR !=
fail )
return MB_FAILURE;
680 GET_VAR(
"tetrahedron_interior", tet_conn, dims );
681 if( -1 == tet_conn )
return MB_FAILURE;
686 for( i = 0; i < matset_data.size(); i++ )
688 matset = matset_data[i];
708 start[0] = elem_num++;
710 fail = nc_put_vara_int(
ncFile, tet_conn, start, count, &connect[0] );
712 if( NC_NOERR !=
fail )
return MB_FAILURE;
720 GET_VAR(
"hexahedron_exterior", hex_conn, dims );
721 if( -1 == hex_conn )
return MB_FAILURE;
723 connect.reserve( 15 );
742 for( i = 9; i < 15; i++ )
746 for( i = 0; i < neuset_data.size(); i++ )
748 std::vector< EntityHandle >::iterator vit =
749 std::find( neuset_data[i].elements.begin(), neuset_data[i].elements.end(), *rit );
750 while( vit != neuset_data[i].elements.end() )
753 int side_no = neuset_data[i].side_numbers[vit - neuset_data[i].elements.begin()];
754 connect[9 + side_no] = neuset_data[i].id;
756 vit = std::find( vit, neuset_data[i].elements.end(), *rit );
761 start[0] = elem_num++;
763 fail = nc_put_vara_int(
ncFile, hex_conn, start, count, &connect[0] );
765 if( NC_NOERR !=
fail )
return MB_FAILURE;
773 GET_VAR(
"tetrahedron_exterior", tet_conn, dims );
774 if( -1 == tet_conn )
return MB_FAILURE;
776 connect.reserve( 9 );
795 for( i = 5; i < 9; i++ )
799 for( i = 0; i < neuset_data.size(); i++ )
801 std::vector< EntityHandle >::iterator vit =
802 std::find( neuset_data[i].elements.begin(), neuset_data[i].elements.end(), *rit );
803 while( vit != neuset_data[i].elements.end() )
806 int side_no = neuset_data[i].side_numbers[vit - neuset_data[i].elements.begin()];
807 connect[5 + side_no] = neuset_data[i].id;
809 vit = std::find( vit, neuset_data[i].elements.end(), *rit );
814 start[0] = elem_num++;
816 fail = nc_put_vara_int(
ncFile, tet_conn, start, count, &connect[0] );
818 if( NC_NOERR !=
fail )
return MB_FAILURE;
829 int coord_size = -1, ncoords = -1;
831 int hexinterior = -1, hexinteriorsize, hexexterior = -1, hexexteriorsize = -1;
832 int tetinterior = -1, tetinteriorsize, tetexterior = -1, tetexteriorsize = -1;
834 if( nc_def_dim(
ncFile,
"coord_size", (
size_t)mesh_info.
num_dim, &coord_size ) != NC_NOERR )
836 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to define number of dimensions" );
839 if( nc_def_dim(
ncFile,
"ncoords", (
size_t)mesh_info.
num_nodes, &ncoords ) != NC_NOERR )
841 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to define number of nodes" );
845 nc_def_dim(
ncFile,
"hexinterior", (
size_t)mesh_info.
num_int_hexes, &hexinterior ) != NC_NOERR )
847 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to define number of interior hex elements" );
850 if( nc_def_dim(
ncFile,
"hexinteriorsize", (
size_t)9, &hexinteriorsize ) != NC_NOERR )
852 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to define interior hex element size" );
856 nc_def_dim(
ncFile,
"hexexterior", (
size_t)mesh_info.
bdy_hexes.
size(), &hexexterior ) != NC_NOERR )
858 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to define number of exterior hex elements" );
861 if( nc_def_dim(
ncFile,
"hexexteriorsize", (
size_t)15, &hexexteriorsize ) != NC_NOERR )
863 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to define exterior hex element size" );
867 nc_def_dim(
ncFile,
"tetinterior", (
size_t)mesh_info.
num_int_tets, &tetinterior ) != NC_NOERR )
869 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to define number of interior tet elements" );
872 if( nc_def_dim(
ncFile,
"tetinteriorsize", (
size_t)5, &tetinteriorsize ) != NC_NOERR )
874 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to define interior tet element size" );
878 nc_def_dim(
ncFile,
"tetexterior", (
size_t)mesh_info.
bdy_tets.
size(), &tetexterior ) != NC_NOERR )
880 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to define number of exterior tet elements" );
883 if( nc_def_dim(
ncFile,
"tetexteriorsize", (
size_t)9, &tetexteriorsize ) != NC_NOERR )
885 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to define exterior tet element size" );
891 dims[0] = hexinterior;
892 dims[1] = hexinteriorsize;
895 NC_NOERR != nc_def_var(
ncFile,
"hexahedron_interior", NC_LONG, 2, dims, &dum_var ) )
897 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to create connectivity array for interior hexes" );
900 dims[0] = hexexterior;
901 dims[1] = hexexteriorsize;
903 NC_NOERR != nc_def_var(
ncFile,
"hexahedron_exterior", NC_LONG, 2, dims, &dum_var ) )
905 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to create connectivity array for exterior hexes" );
908 dims[0] = tetinterior;
909 dims[1] = tetinteriorsize;
911 NC_NOERR != nc_def_var(
ncFile,
"tetrahedron_exterior", NC_LONG, 2, dims, &dum_var ) )
913 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to create connectivity array for interior tets" );
916 dims[0] = tetexterior;
917 dims[1] = tetexteriorsize;
919 NC_NOERR != nc_def_var(
ncFile,
"tetrahedron_exterior", NC_LONG, 2, dims, &dum_var ) )
921 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to create connectivity array for exterior tets" );
927 dims[1] = coord_size;
928 if( NC_NOERR != nc_def_var(
ncFile,
"coords", NC_DOUBLE, 2, dims, &dum_var ) )
930 MB_SET_ERR( MB_FAILURE,
"WriteSLAC: failed to define node coordinate array" );
939 if( strlen( (
const char*)filename ) == 0 )
941 MB_SET_ERR( MB_FAILURE,
"Output filename not specified" );
944 int fail = nc_create( filename, NC_CLOBBER, &
ncFile );
946 if( NC_NOERR !=
fail )
948 MB_SET_ERR( MB_FAILURE,
"Cannot open " << filename );
956 Range& forward_elems,
957 Range& reverse_elems )
959 Range ss_elems, ss_meshsets;
968 if( MB_FAILURE == result )
return result;
976 if( range_iter != ss_elems.
end() )
979 ss_elems.
erase( range_iter, ss_elems.
end() );
989 dum_it = ss_elems.
begin();
993 if( current_sense == 1 || current_sense == 0 ) std::copy( dum_it, ss_elems.
end(),
range_inserter( forward_elems ) );
994 if( current_sense == -1 || current_sense == 0 )
999 for( range_iter = ss_meshsets.
begin(); range_iter != ss_meshsets.
end(); ++range_iter )
1003 if( 0 == sense_tag || MB_FAILURE ==
mbImpl->
tag_get_data( sense_tag, &( *range_iter ), 1, &this_sense ) )
1007 get_neuset_elems( *range_iter, this_sense * current_sense, forward_elems, reverse_elems );