dgfwriter.hh

Go to the documentation of this file.
00001 #ifndef DUNE_DGFWRITER_HH
00002 #define DUNE_DGFWRITER_HH
00003 
00009 #include <fstream>
00010 #include <vector>
00011 
00012 #include <dune/grid/common/grid.hh>
00013 #include <dune/grid/common/genericreferenceelements.hh>
00014 
00015 namespace Dune
00016 {
00017 
00027   template< class GV >
00028   class DGFWriter
00029   {
00030     typedef DGFWriter< GV > This;
00031 
00032   public:
00034     typedef GV GridView;
00036     typedef typename GridView::Grid Grid;
00037 
00039     static const int dimGrid = GridView::dimension;
00040 
00041   private:
00042     typedef typename GridView::IndexSet IndexSet;
00043     typedef typename GridView::template Codim< 0 >::Iterator ElementIterator;
00044     typedef typename GridView::template Codim< dimGrid >::Iterator VertexIterator;
00045     typedef typename GridView::IntersectionIterator IntersectionIterator;
00046 
00047     typedef typename IndexSet::IndexType Index;
00048 
00049     typedef GenericReferenceElement< typename Grid::ctype, dimGrid > RefElement;
00050     typedef GenericReferenceElements< typename Grid::ctype, dimGrid > RefElements;
00051 
00052   public:
00057     DGFWriter ( const GridView &gridView )
00058     : gridView_( gridView )
00059     {}
00060 
00065     void write ( std::ostream &gridout ) const;
00066 
00071     void write ( const std::string &fileName ) const;
00072 
00073   private:
00074     GridView gridView_;
00075   };
00076 
00077 
00078 
00079   template< class GV >
00080   inline void DGFWriter< GV >::write ( std::ostream &gridout ) const
00081   {
00082     // set the stream to full double precision
00083     gridout.setf( std::ios_base::scientific, std::ios_base::floatfield );
00084     gridout.precision( 16 );
00085     
00086     const IndexSet &indexSet = gridView_.indexSet();
00087    
00088     // write DGF header
00089     gridout << "DGF" << std::endl;
00090 
00091     std::vector< Index > vertexIndex( indexSet.size( dimGrid ) );
00092 
00093     // write all vertices into the "vertex" block
00094     gridout << std::endl << "VERTEX" << std::endl;
00095     Index vertexCount = 0;
00096     const VertexIterator vend = gridView_.template end< dimGrid >();
00097     for( VertexIterator vit = gridView_.template begin< dimGrid >(); vit != vend; ++vit )
00098     {
00099       vertexIndex[ indexSet.index( *vit ) ] = vertexCount++;
00100       gridout << vit->geometry().corner( 0 ) << std::endl;
00101     }
00102     gridout << "#" << std::endl;
00103     if( vertexCount != indexSet.size( dimGrid ) )
00104       DUNE_THROW( GridError, "Index set reports wrong number of vertices." );
00105 
00106     // write all simplices to the "simplex" block
00107     gridout << std::endl << "SIMPLEX" << std::endl;
00108     const ElementIterator end = gridView_.template end< 0 >();
00109     for( ElementIterator it = gridView_.template begin< 0 >(); it != end; ++it )
00110     {
00111       if( !it->type().isSimplex() )
00112         continue;
00113 
00114       std::vector< Index > vertices( dimGrid+1 );
00115       for( size_t i = 0; i < vertices.size(); ++i )
00116         vertices[ i ] = vertexIndex[ indexSet.subIndex( *it, i, dimGrid ) ];
00117       
00118       gridout << vertices[ 0 ];
00119       for( size_t i = 1; i < vertices.size(); ++i )
00120         gridout << " " << vertices[ i ];
00121       gridout << std::endl;
00122     }
00123     gridout << "#" << std::endl;
00124 
00125     // write all cubes to the "cube" block
00126     gridout << std::endl << "CUBE" << std::endl;
00127     for( ElementIterator it = gridView_.template begin< 0 >(); it != end; ++it )
00128     {
00129       if( !it->type().isCube() )
00130         continue;
00131 
00132       std::vector< Index > vertices( 1 << dimGrid );
00133       for( size_t i = 0; i < vertices.size(); ++i )
00134         vertices[ i ] = vertexIndex[ indexSet.subIndex( *it, i, dimGrid ) ];
00135       
00136       gridout << vertices[ 0 ];
00137       for( size_t i = 1; i < vertices.size(); ++i )
00138         gridout << " " << vertices[ i ];
00139       gridout << std::endl;
00140     }
00141     gridout << "#" << std::endl;
00142 
00143     // write all boundaries to the "boundarysegments" block
00144     gridout << std::endl << "BOUNDARYSEGMENTS" << std::endl;
00145     for( ElementIterator it = gridView_.template begin< 0 >(); it != end; ++it )
00146     {
00147       if( !it->hasBoundaryIntersections() )
00148         continue;
00149 
00150       const RefElement &refElement = RefElements::general( it->type() );
00151 
00152       const IntersectionIterator iend = it->ileafend();
00153       for( IntersectionIterator iit = it->ileafbegin(); iit != iend; ++iit )
00154       {
00155         if( !iit->boundary() )
00156           continue;
00157 
00158         const int boundaryId = iit->boundaryId();
00159         if( boundaryId <= 0 )
00160         {
00161           std::cerr << "Warning: Ignoring nonpositive boundary id: "
00162                     << boundaryId << "." << std::endl;
00163           continue;
00164         }
00165 
00166         const int faceNumber = iit->indexInInside();
00167         const unsigned int faceSize = refElement.size( faceNumber, 1, dimGrid );
00168         std::vector< Index > vertices( faceSize );
00169         for( unsigned int i = 0; i < faceSize; ++i )
00170         {
00171           const int j = refElement.subEntity( faceNumber, 1, i, dimGrid );
00172           vertices[ i ] = vertexIndex[ indexSet.subIndex( *it, j, dimGrid ) ];
00173         }
00174         gridout << boundaryId << "   " << vertices[ 0 ];
00175         for( unsigned int i = 1; i < faceSize; ++i )
00176           gridout << " " << vertices[ i ];
00177         gridout << std::endl;
00178       }
00179     }
00180     gridout << "#" << std::endl;
00181 
00182     // write the name into the "gridparameter" block
00183     gridout << std::endl << "GRIDPARAMETER" << std::endl;
00184     // gridout << "NAME " << gridView_.grid().name() << std::endl;
00185     gridout << "#" << std::endl;
00186     
00187     gridout << std::endl << "#" << std::endl;
00188   }
00189 
00190 
00191   template< class GV >
00192   inline void DGFWriter< GV >::write ( const std::string &fileName ) const
00193   {
00194     std::ofstream gridout( fileName.c_str() );
00195     write( gridout );
00196   }
00197 
00198 }
00199 
00200 #endif // #ifndef DUNE_DGFWRITER_HH

Generated on Fri Apr 29 2011 with Doxygen (ver 1.7.1) [doxygen-log,error-log].