/usr/include/dune/grid/io/file/dgfparser/dgfwriter.hh is in libdune-grid-dev 2.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_DGFWRITER_HH
#define DUNE_DGFWRITER_HH
/** \file
* \brief write a GridView to a DGF file
* \author Martin Nolte
*/
#include <fstream>
#include <vector>
#include <dune/grid/common/grid.hh>
#include <dune/geometry/referenceelements.hh>
namespace Dune
{
/** \class DGFWriter
* \ingroup DuneGridFormatParser
* \brief write a GridView to a DGF file
*
* The DGFWriter allows create a DGF file from a given GridView. It allows
* for the easy creation of file format converters.
*
* \tparam GV GridView to write in DGF format
*/
template< class GV >
class DGFWriter
{
typedef DGFWriter< GV > This;
public:
/** \brief type of grid view */
typedef GV GridView;
/** \brief type of underlying hierarchical grid */
typedef typename GridView::Grid Grid;
/** \brief dimension of the grid */
static const int dimGrid = GridView::dimension;
private:
typedef typename GridView::IndexSet IndexSet;
typedef typename GridView::template Codim< 0 >::Iterator ElementIterator;
typedef typename GridView::IntersectionIterator IntersectionIterator;
typedef typename GridView::template Codim< dimGrid >::EntityPointer VertexPointer;
typedef typename ElementIterator :: Entity Element ;
typedef typename Element :: EntityPointer ElementPointer;
typedef typename Element :: EntitySeed ElementSeed ;
typedef typename IndexSet::IndexType Index;
typedef ReferenceElement< typename Grid::ctype, dimGrid > RefElement;
typedef ReferenceElements< typename Grid::ctype, dimGrid > RefElements;
public:
/** \brief constructor
*
* \param[in] gridView grid view to operate on
*/
DGFWriter ( const GridView &gridView )
: gridView_( gridView )
{}
/** \brief write the GridView into a std::ostream
*
* \param gridout std::ostream to write the grid to
* \param newElemOrder vector providing a new ordering for the elements in the given GridView
* \param addParams additional data to write to dgf file, such as projections
* etc. (defaults to an emoty data stream)
*/
void write ( std::ostream &gridout,
const std::vector< Index >& newElemOrder,
const std::stringstream& addParams = std::stringstream() ) const;
/** \brief write the GridView into a std::ostream
*
* \param gridout std::ostream to write the grid to
*/
void write ( std::ostream &gridout ) const;
/** \brief write the GridView into a std::ostream
*
* \param gridout std::ostream to write the grid to
* \param addParams additional data to write to dgf file, such as projections
* etc. (defaults to an emoty data stream)
*/
void write ( std::ostream &gridout,
const std::stringstream& addParams ) const;
/** \brief write the GridView to a file
*
* \param[in] fileName name of the write to write the grid to
*/
void write ( const std::string &fileName ) const;
protected:
GridView gridView_;
protected:
/////////////////////////////////////////////
// helper methods
/////////////////////////////////////////////
// write all elements of type elementType
void writeAllElements( const std::vector<ElementSeed>& elementSeeds,
const IndexSet& indexSet,
const GeometryType& elementType,
const std::vector< Index >& vertexIndex,
std::ostream &gridout ) const
{
if( elementSeeds.size() > 0 )
{
// perform grid traversal based on new element ordering
typedef typename std::vector<ElementSeed> :: const_iterator iterator ;
const iterator end = elementSeeds.end();
for( iterator it = elementSeeds.begin(); it != end ; ++ it )
{
// convert entity seed into entity pointer
const ElementPointer ep = gridView_.grid().entityPointer( *it );
// write element
writeElement( *ep, indexSet, elementType, vertexIndex, gridout );
}
}
else
{
// perform default grid traversal
const ElementIterator end = gridView_.template end< 0 >();
for( ElementIterator it = gridView_.template begin< 0 >(); it != end; ++it )
{
// write element
writeElement( *it, indexSet, elementType, vertexIndex, gridout );
}
}
}
// write one element
void writeElement( const Element& element,
const IndexSet& indexSet,
const GeometryType& elementType,
const std::vector< Index >& vertexIndex,
std::ostream &gridout ) const
{
// if element's type is not the same as the type to write the return
if( element.type() != elementType )
return ;
// get vertex numbers of the element
const size_t vxSize = element.subEntities( Element::dimension );
std::vector<Index> vertices(vxSize);
for( size_t i = 0; i < vxSize; ++i )
vertices[ i ] = vertexIndex[ indexSet.subIndex( element, i, dimGrid ) ];
gridout << vertices[ 0 ];
for( size_t i = 1; i < vxSize; ++i )
gridout << " " << vertices[ i ];
gridout << std::endl;
}
};
template< class GV >
inline void DGFWriter< GV >::
write ( std::ostream &gridout,
const std::vector< Index >& newElemOrder,
const std::stringstream& addParams ) const
{
// set the stream to full double precision
gridout.setf( std::ios_base::scientific, std::ios_base::floatfield );
gridout.precision( 16 );
const IndexSet &indexSet = gridView_.indexSet();
// vector containing entity seed (only needed if new ordering is given)
std::vector< ElementSeed > elementSeeds;
// if ordering was provided
const size_t orderSize = newElemOrder.size() ;
if( orderSize == indexSet.size( 0 ) )
{
const ElementIterator end = gridView_.template end< 0 >();
ElementIterator it = gridView_.template begin< 0 >();
if( it != end )
{
elementSeeds.resize( orderSize, (*it).seed() ) ;
size_t countElements = 0 ;
for( ; it != end; ++it, ++countElements )
{
const Element& element = *it ;
assert( newElemOrder[ indexSet.index( element ) ] < orderSize );
elementSeeds[ newElemOrder[ indexSet.index( element ) ] ] = element.seed();
}
// make sure that the size of the index set is equal
// to the number of counted elements
if( countElements != orderSize )
DUNE_THROW(InvalidStateException,"DGFWriter::write: IndexSet not consecutive");
}
}
// write DGF header
gridout << "DGF" << std::endl;
const Index vxSize = indexSet.size( dimGrid );
std::vector< Index > vertexIndex( vxSize, vxSize );
gridout << "%" << " Elements = " << indexSet.size( 0 ) << " | Vertices = " << vxSize << std::endl;
// write all vertices into the "vertex" block
gridout << std::endl << "VERTEX" << std::endl;
Index vertexCount = 0;
const ElementIterator end = gridView_.template end< 0 >();
for( ElementIterator it = gridView_.template begin< 0 >(); it != end; ++it )
{
const Element& element = *it ;
const int numCorners = element.subEntities( dimGrid );
for( int i=0; i<numCorners; ++i )
{
const Index vxIndex = indexSet.subIndex( element, i, dimGrid );
assert( vxIndex < vxSize );
if( vertexIndex[ vxIndex ] == vxSize )
{
vertexIndex[ vxIndex ] = vertexCount++;
gridout << element.geometry().corner( i ) << std::endl;
}
}
}
gridout << "#" << std::endl;
if( vertexCount != vxSize )
DUNE_THROW( GridError, "Index set reports wrong number of vertices." );
if( dimGrid > 1 )
{
// type of element to write
GeometryType simplex( GeometryType::simplex, dimGrid );
// only write simplex block if grid view contains simplices
if( indexSet.size( simplex ) > 0 )
{
// write all simplices to the "simplex" block
gridout << std::endl << "SIMPLEX" << std::endl;
// write all simplex elements
writeAllElements( elementSeeds, indexSet, simplex, vertexIndex, gridout );
// write end marker for block
gridout << "#" << std::endl;
}
}
{
// cube geometry type
GeometryType cube( GeometryType::cube, dimGrid );
// only write cube block if grid view contains cubes
if( indexSet.size( cube ) > 0 )
{
// write all cubes to the "cube" block
gridout << std::endl << "CUBE" << std::endl;
// write all simplex elements
writeAllElements( elementSeeds, indexSet, cube, vertexIndex, gridout );
// write end marker for block
gridout << "#" << std::endl;
}
}
// write all boundaries to the "boundarysegments" block
#if DUNE_GRID_EXPERIMENTAL_GRID_EXTENSIONS
gridout << std::endl << "BOUNDARYSEGMENTS" << std::endl;
for( ElementIterator it = gridView_.template begin< 0 >(); it != end; ++it )
{
const Element& element = *it ;
if( !it->hasBoundaryIntersections() )
continue;
const RefElement &refElement = RefElements::general( element.type() );
const IntersectionIterator iend = gridView_.iend( element ) ;
for( IntersectionIterator iit = gridView_.ibegin( element ); iit != iend; ++iit )
{
if( !iit->boundary() )
continue;
const int boundaryId = iit->boundaryId();
if( boundaryId <= 0 )
{
std::cerr << "Warning: Ignoring nonpositive boundary id: "
<< boundaryId << "." << std::endl;
continue;
}
const int faceNumber = iit->indexInInside();
const unsigned int faceSize = refElement.size( faceNumber, 1, dimGrid );
std::vector< Index > vertices( faceSize );
for( unsigned int i = 0; i < faceSize; ++i )
{
const int j = refElement.subEntity( faceNumber, 1, i, dimGrid );
vertices[ i ] = vertexIndex[ indexSet.subIndex( element, j, dimGrid ) ];
}
gridout << boundaryId << " " << vertices[ 0 ];
for( unsigned int i = 1; i < faceSize; ++i )
gridout << " " << vertices[ i ];
gridout << std::endl;
}
}
gridout << "#" << std::endl << std::endl;
#endif // #if DUNE_GRID_EXPERIMENTAL_GRID_EXTENSIONS
// add additional parameters given by the user
gridout << addParams.str() << std::endl;
gridout << std::endl << "#" << std::endl;
}
template< class GV >
inline void DGFWriter< GV >::
write ( std::ostream &gridout) const
{
// empty vector means no new ordering
std::vector< Index > noNewOrdering ;
std::stringstream addParams;
write( gridout, noNewOrdering, addParams );
}
template< class GV >
inline void DGFWriter< GV >::
write ( std::ostream &gridout, const std::stringstream& addParams ) const
{
// empty vector means no new ordering
std::vector< Index > noNewOrdering ;
write( gridout, noNewOrdering, addParams );
}
template< class GV >
inline void DGFWriter< GV >::write ( const std::string &fileName ) const
{
std::ofstream gridout( fileName.c_str() );
if( gridout )
write( gridout );
else
std::cerr << "Couldn't open file `"<< fileName << "'!"<< std::endl;
}
}
#endif // #ifndef DUNE_DGFWRITER_HH
|