/usr/include/dune/grid/alugrid/2d/alu2dgridfactory.cc 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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#include <config.h>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <fstream>
#include <dune/grid/alugrid/2d/alu2dgridfactory.hh>
#include <dune/grid/alugrid/common/declaration.hh>
#include <dune/grid/alugrid/2d/alugrid.hh>
namespace Dune
{
template< class GridImp >
void ALU2dGridFactory< GridImp >::insertVertex ( const VertexType &pos )
{
vertices_.push_back( pos );
}
template< class GridImp >
void ALU2dGridFactory< GridImp >
::insertElement ( const GeometryType &geometry,
const std::vector< unsigned int > &vertices )
{
switch( elementType )
{
case ALU2DSPACE triangle :
if( !geometry.isTriangle() )
DUNE_THROW( GridError, "Only triangles can be inserted into "
"ALUGrid< 2, " << dimensionworld << ", triangle >." );
break;
case ALU2DSPACE quadrilateral :
if( !geometry.isCube() )
DUNE_THROW( GridError, "Only cubes can be inserted into "
"ALUGrid< 2, " << dimensionworld << ", quadrilateral >." );
break;
default :
assert( geometry.isSimplex() || geometry.isCube() );
}
if( (geometry.isSimplex() && (vertices.size() != 3))
|| (geometry.isCube() && (vertices.size() != 4)) )
DUNE_THROW( GridError, "Wrong number of vertices." );
elements_.push_back( vertices );
}
template< class GridImp >
void ALU2dGridFactory< GridImp >
::insertBoundary ( const GeometryType &geometry,
const std::vector< unsigned int > &vertices,
const int id )
{
// lines can be either cube or simplex
assert( geometry.isSimplex() || geometry.isCube() );
if( geometry.dim() != dimension-1 )
{
DUNE_THROW( GridError, "Only 1-dimensional boundaries can be inserted "
"into a 2-dimensional ALUGrid." );
}
if( vertices.size() != numFaceCorners )
DUNE_THROW( GridError, "Wrong number of vertices." );
if( id <= 0 )
DUNE_THROW( GridError, "Boundary ids must be positive." );
std::pair< FaceType, int > boundaryId;
for( unsigned int i = 0; i < numFaceCorners; ++i )
boundaryId.first[ i ] = vertices[ i ];
boundaryId.second = -id; // store negative id
boundaryIds_.push_back( boundaryId );
}
template< class GridImp >
void ALU2dGridFactory< GridImp >
::insertBoundary ( const int element, const int face, const int id )
{
if( (element < 0) || (element >= (int)elements_.size()) )
DUNE_THROW( RangeError, "ALU2dGridFactory::insertBoundary: invalid element index given." );
std::pair< FaceType, int > boundaryId;
generateFace( elements_[ element ], face, boundaryId.first );
boundaryId.second = -id; // store negative id
boundaryIds_.push_back( boundaryId );
}
template< class GridImp >
void ALU2dGridFactory< GridImp >
::insertBoundaryProjection ( const DuneBoundaryProjectionType &bndProjection )
{
if( globalProjection_ )
DUNE_THROW(InvalidStateException,"You can only insert one globalProjection");
globalProjection_ = &bndProjection;
}
template< class GridImp >
void ALU2dGridFactory< GridImp >
::insertBoundaryProjection ( const GeometryType &type,
const std::vector< unsigned int > &vertices,
const DuneBoundaryProjectionType *projection )
{
assert( type.isSimplex() || type.isCube() );
if( (int)type.dim() != dimension-1 )
DUNE_THROW( GridError, "Inserting boundary face of wrong dimension: " << type.dim() );
FaceType faceId;
copyAndSort( vertices, faceId );
if( vertices.size() != numFaceCorners )
DUNE_THROW( GridError, "Wrong number of face vertices passed: " << vertices.size() << "." );
if( boundaryProjections_.find( faceId ) != boundaryProjections_.end() )
DUNE_THROW( GridError, "Only one boundary projection can be attached to a face." );
boundaryProjections_[ faceId ] = projection;
}
template< class GridImp >
void ALU2dGridFactory< GridImp >
::insertBoundarySegment ( const std::vector< unsigned int >& vertices )
{
FaceType faceId;
copyAndSort( vertices, faceId );
if( vertices.size() != numFaceCorners )
DUNE_THROW( GridError, "Wrong number of face vertices passed: " << vertices.size() << "." );
if( boundaryProjections_.find( faceId ) != boundaryProjections_.end() )
DUNE_THROW( GridError, "Only one boundary projection can be attached to a face." );
boundaryProjections_[ faceId ] = 0;
std::pair< FaceType, int > boundaryId;
for( unsigned int i = 0; i < numFaceCorners; ++i )
boundaryId.first[ i ] = vertices[ i ];
boundaryId.second = -1; // some default
boundaryIds_.push_back( boundaryId );
}
template< class GridImp >
void ALU2dGridFactory< GridImp >
::insertBoundarySegment ( const std::vector< unsigned int > &vertices,
const shared_ptr< BoundarySegment< 2, dimensionworld > > &boundarySegment )
{
FaceType faceId;
copyAndSort( vertices, faceId );
if( vertices.size() != numFaceCorners )
DUNE_THROW( GridError, "Wrong number of face vertices passed: " << vertices.size() << "." );
if( boundaryProjections_.find( faceId ) != boundaryProjections_.end() )
DUNE_THROW( GridError, "Only one boundary projection can be attached to a face." );
const size_t numVx = vertices.size();
GeometryType type;
if( numVx == 3 )
type.makeSimplex( dimension-1 );
else
type.makeCube( dimension-1 );
std::vector< VertexType > coords( numVx );
for( size_t i = 0; i < numVx; ++i )
{
// if this assertions is thrown vertices were not inserted at first
assert( vertices_.size() > vertices[ i ] );
// get global coordinate and copy it
const VertexType &x = vertices_[ vertices[ i ] ];
for( int j = 0; j < dimensionworld; ++j )
coords[ i ][ j ] = x[ j ];
}
BoundarySegmentWrapperType* prj
= new BoundarySegmentWrapperType( type, coords, boundarySegment );
boundaryProjections_[ faceId ] = prj;
#ifndef NDEBUG
// consistency check
for( size_t i = 0; i < numVx; ++i )
{
VertexType global = (*prj)( coords [ i ] );
if( (global - coords[ i ]).two_norm() >= epsilon_ )
{
DUNE_THROW(GridError,"BoundarySegment does not map face vertices to face vertices.");
}
}
#endif
std::pair< FaceType, int > boundaryId;
for( unsigned int i = 0; i < numFaceCorners; ++i )
boundaryId.first[ i ] = vertices[ i ];
boundaryId.second = -1; // some default
boundaryIds_.push_back( boundaryId );
}
template< class GridImp >
void ALU2dGridFactory< GridImp >
:: insertFaceTransformation ( const WorldMatrix &matrix, const WorldVector &shift )
{
faceTransformations_.push_back( Transformation( matrix, shift ) );
}
template< class GridImp >
GridImp *ALU2dGridFactory< GridImp >
::createGrid ( const bool addMissingBoundaries, bool temporary, const std::string filename )
{
numFacesInserted_ = boundaryIds_.size();
if( addMissingBoundaries || !faceTransformations_.empty() )
recreateBoundaryIds();
std::ofstream outfile;
std::stringstream temp;
std::ostream* outptr = 0;
if( temporary )
outptr = & temp;
else
{
outfile.open( filename.c_str() , std::ios::out );
outptr = & outfile;
}
std::ostream& out = *outptr ;
out.setf( std::ios_base::scientific, std::ios_base::floatfield );
out.precision( 16 );
out << "!Triangles" << std::endl;
const unsigned int numVertices = vertices_.size();
// now start writing grid
out << numVertices << std :: endl;
typedef typename std :: vector< VertexType > :: iterator VertexIteratorType;
const VertexIteratorType endV = vertices_.end();
for( VertexIteratorType it = vertices_.begin(); it != endV; ++it )
{
const VertexType &vertex = *it;
out << vertex[ 0 ];
for( int i = 1; i < dimensionworld; ++i )
out << " " << vertex[ i ];
out << std :: endl;
}
out << elements_.size() << std :: endl;
typedef typename ElementVector::iterator ElementIteratorType;
const ElementIteratorType endE = elements_.end();
for( ElementIteratorType it = elements_.begin(); it != endE; ++it )
{
out << (*it)[ 0 ];
out << " " << (*it)[ 1 ];
if ( it->size() == 4 )
out << " " << (*it)[ 3 ];
out << " " << (*it)[ 2 ];
out << std :: endl;
}
const size_t boundarySegments = boundaryIds_.size();
out << boundarySegments << std::endl;
typedef typename std::vector< std::pair< FaceType, int > >::iterator BoundaryIdIterator;
BoundaryProjectionVector* bndProjections = 0;
const size_t bndProjectionSize = boundaryProjections_.size();
if( bndProjectionSize > 0 )
{
// the memory is freed by the grid on destruction
bndProjections = new BoundaryProjectionVector( boundarySegments,
(DuneBoundaryProjectionType*)0 );
}
const BoundaryIdIterator endB = boundaryIds_.end();
int segmentIndex = 0;
for( BoundaryIdIterator it = boundaryIds_.begin(); it != endB; ++it, ++segmentIndex )
{
const std::pair< FaceType, int > &boundaryId = *it;
out << boundaryId.second;
for( unsigned int i = 0; i < numFaceCorners; ++i )
out << " " << boundaryId.first[ i ];
if( boundaryId.second == periodicBndId )
{
FaceType key = boundaryId.first;
std::sort( key.begin(), key.end() );
typename PeriodicNeighborMap::const_iterator pos = periodicNeighborMap_.find( key );
if( pos != periodicNeighborMap_.end() )
out << " " << pos->second;
else
DUNE_THROW( InvalidStateException, "Periodic Neighbor not found." );
}
out << std::endl;
if( bndProjectionSize > 0 )
{
// generate boundary segment pointer
FaceType faceId (boundaryId.first);
std::sort( faceId.begin(), faceId.end() );
const DuneBoundaryProjectionType* projection = boundaryProjections_[ faceId ];
// if no projection given we use global projection, otherwise identity
if( ! projection && globalProjection_ )
{
typedef BoundaryProjectionWrapper< dimensionworld > ProjectionWrapperType;
// we need to wrap the global projection because of
// deleting in desctructor of ALUGrid
projection = new ProjectionWrapperType( *globalProjection_ );
}
// copy pointer
(*bndProjections)[ segmentIndex ] = projection;
}
}
outfile.close();
vertices_.clear();
elements_.clear();
boundaryIds_.clear();
boundaryProjections_.clear();
std::istream& inFile = temp;
// if we have a vector reset global projection
// because empty positions are filled with global projection anyway
if( bndProjections ) globalProjection_ = 0;
// ALUGrid is taking ownership of the bndProjections pointer
Grid *grid = createGridObj( temporary, filename, inFile, bndProjections );
// remove pointers
globalProjection_ = 0;
// is removed by the grid
bndProjections = 0;
return grid;
}
template< class GridImp >
inline void ALU2dGridFactory< GridImp >
::generateFace ( const ElementType &element, const int f, FaceType &face )
{
if (element.size() == 3)
switch (f)
{
case 0 :
face[0]=element[0]; face[1]=element[1];
break;
case 1 :
face[0]=element[0]; face[1]=element[2];
break;
case 2 :
face[0]=element[1]; face[1]=element[2];
break;
}
else
switch (f)
{
case 0 :
face[0]=element[0]; face[1]=element[2];
break;
case 1 :
face[0]=element[1]; face[1]=element[3];
break;
case 2 :
face[0]=element[0]; face[1]=element[1];
break;
case 3 :
face[0]=element[2]; face[1]=element[3];
break;
}
}
template< class GridImp >
typename ALU2dGridFactory< GridImp >::FaceMap::const_iterator
ALU2dGridFactory< GridImp >
::findPeriodicNeighbor ( const FaceMap &faceMap, const FaceType &key ) const
{
typedef typename FaceTransformationVector::const_iterator TrafoIterator;
typedef typename FaceMap::const_iterator FaceMapIterator;
const WorldVector v[ 2 ] = { vertices_[ key[ 0 ] ], vertices_[ key[ 1 ] ] };
// Note This should be done using a kd-tree
const TrafoIterator trend = faceTransformations_.end();
for( TrafoIterator trit = faceTransformations_.begin(); trit != trend; ++trit )
{
WorldVector w[ 2 ];
w[ 0 ] = trit->evaluate( v[ 0 ] );
w[ 1 ] = trit->evaluate( v[ 1 ] );
const FaceMapIterator fend = faceMap.end();
for( FaceMapIterator fit = faceMap.begin(); fit != fend; ++fit )
{
const WorldVector vv[ 2 ] = { vertices_[ fit->first[ 0 ] ], vertices_[ fit->first[ 1 ] ] };
if( ((vv[ 0 ] - w[ 0 ]).two_norm() < epsilon_) && ((vv[ 1 ] - w[ 1 ]).two_norm() < epsilon_) )
return fit;
if( ((vv[ 0 ] - w[ 1 ]).two_norm() < epsilon_) && ((vv[ 1 ] - w[ 0 ]).two_norm() < epsilon_) )
return fit;
WorldVector ww[ 2 ];
ww[ 0 ] = trit->evaluate( vv[ 0 ] );
ww[ 1 ] = trit->evaluate( vv[ 1 ] );
if( ((v[ 0 ] - ww[ 0 ]).two_norm() < epsilon_) && ((v[ 1 ] - ww[ 1 ]).two_norm() < epsilon_) )
return fit;
if( ((v[ 0 ] - ww[ 1 ]).two_norm() < epsilon_) && ((v[ 1 ] - ww[ 0 ]).two_norm() < epsilon_) )
return fit;
}
}
return faceMap.end();
}
template< class GridImp >
void ALU2dGridFactory< GridImp >
::reinsertBoundary ( const FaceMap &faceMap, const typename FaceMap::const_iterator &pos, const int id )
{
typedef typename PeriodicNeighborMap::const_iterator PeriodicNbIterator;
typedef typename FaceMap::const_iterator FaceMapIterator;
typedef std::pair< FaceType, int > BoundaryId;
const size_t index = boundaryIds_.size();
insertBoundary( pos->second.first, pos->second.second, -id );
BoundaryId &boundaryId = boundaryIds_[ index ];
const FaceType &key = pos->first;
const PeriodicNbIterator nb = periodicNeighborMap_.find( key );
if( nb != periodicNeighborMap_.end() )
{
boundaryId.second = periodicBndId;
const BoundaryId &nbBoundaryId = boundaryIds_[ nb->second ];
FaceType nbKey = nbBoundaryId.first;
std::sort( nbKey.begin(), nbKey.end() );
periodicNeighborMap_[ nbKey ] = index;
}
else
{
const FaceMapIterator nbPos = findPeriodicNeighbor( faceMap, key );
if( nbPos != faceMap.end() )
{
boundaryId.second = periodicBndId;
const FaceType &nbKey = nbPos->first;
if( periodicNeighborMap_.find( nbKey ) != periodicNeighborMap_.end() )
DUNE_THROW( GridError, "One boundary segment can only identified with one other." );
periodicNeighborMap_[ nbKey ] = index;
}
}
}
template< class GridImp >
void ALU2dGridFactory< GridImp >::recreateBoundaryIds ( const int defaultId )
{
typedef typename FaceMap::iterator FaceIterator;
if( defaultId <= 0 )
DUNE_THROW( GridError, "Boundary ids must be positive." );
FaceMap faceMap;
const unsigned int numElements = elements_.size();
for( unsigned int n = 0; n < numElements; ++n )
{
for( unsigned int face = 0; face < elements_[ n ].size(); ++face )
{
FaceType key;
generateFace( elements_[ n ], face, key );
std::sort( key.begin(), key.end() );
const FaceIterator pos = faceMap.find( key );
if( pos != faceMap.end() )
faceMap.erase( key );
else
faceMap.insert( std::make_pair( key, SubEntity( n, face ) ) );
}
}
// swap current boundary ids with an empty vector
BoundaryIdVector boundaryIds;
boundaryIds_.swap( boundaryIds );
assert( boundaryIds_.size() == 0 );
// add all current boundary ids again (with their reordered keys)
typedef typename BoundaryIdVector::iterator BoundaryIterator;
const BoundaryIterator bndEnd = boundaryIds.end();
for( BoundaryIterator bndIt = boundaryIds.begin(); bndIt != bndEnd; ++bndIt )
{
FaceType key = bndIt->first;
std::sort( key.begin(), key.end() );
const FaceIterator pos = faceMap.find( key );
if( pos == faceMap.end() )
DUNE_THROW( GridError, "Inserted boundary segment is not part of the boundary." );
reinsertBoundary( faceMap, pos, bndIt->second );
faceMap.erase( pos );
}
// add all new boundaries (with defaultId)
const FaceIterator faceEnd = faceMap.end();
for( FaceIterator faceIt = faceMap.begin(); faceIt != faceEnd; ++faceIt )
reinsertBoundary( faceMap, faceIt, defaultId );
}
}
// Template Instantiation
// ----------------------
template class Dune::ALU2dGridFactory< Dune::ALUGrid< 2, 2, Dune::simplex, Dune::conforming, Dune::No_Comm > >;
template class Dune::ALU2dGridFactory< Dune::ALUGrid< 2, 2, Dune::simplex, Dune::nonconforming, Dune::No_Comm > >;
#if HAVE_MPI
template class Dune::ALU2dGridFactory< Dune::ALUGrid< 2, 2, Dune::simplex, Dune::conforming, MPI_Comm > >;
template class Dune::ALU2dGridFactory< Dune::ALUGrid< 2, 2, Dune::simplex, Dune::nonconforming, MPI_Comm > >;
#endif // #if HAVE_MPI
template class Dune::ALU2dGridFactory< Dune::ALUGrid< 2, 2, Dune::cube, Dune::nonconforming, Dune::No_Comm > >;
template class Dune::ALU2dGridFactory< Dune::ALUGrid< 2, 3, Dune::simplex, Dune::conforming, Dune::No_Comm > >;
template class Dune::ALU2dGridFactory< Dune::ALUGrid< 2, 3, Dune::simplex, Dune::nonconforming, Dune::No_Comm > >;
template class Dune::ALU2dGridFactory< Dune::ALUGrid< 2, 3, Dune::cube, Dune::nonconforming, Dune::No_Comm > >;
#if HAVE_MPI
template class Dune::ALU2dGridFactory< Dune::ALUGrid< 2, 2, Dune::cube, Dune::nonconforming, MPI_Comm > >;
template class Dune::ALU2dGridFactory< Dune::ALUGrid< 2, 3, Dune::simplex, Dune::conforming, MPI_Comm > >;
template class Dune::ALU2dGridFactory< Dune::ALUGrid< 2, 3, Dune::simplex, Dune::nonconforming, MPI_Comm > >;
template class Dune::ALU2dGridFactory< Dune::ALUGrid< 2, 3, Dune::cube, Dune::nonconforming, MPI_Comm > >;
#endif // #if HAVE_MPI
|