27 :
public GridFactoryInterface< PolyhedralGrid< dim, dimworld, coord_t > >
32 const static int dimension = Grid::dimension;
33 const static int dimensionworld = Grid::dimensionworld;
36 typedef MPIHelper::MPICommunicator MPICommunicatorType;
37 typedef typename Grid::template Codim<0>::Entity Element;
38 typedef typename Grid::template Codim<dimension>::Entity Vertex;
40 typedef Dune::FieldVector<ctype,dimensionworld> CoordinateType;
41 typedef CoordinateType Coordinate;
43#if DUNE_VERSION_LT(DUNE_GRID, 2, 8)
44 typedef ToUniquePtr<Grid> UniquePtrType;
46 using UniquePtrType = std::unique_ptr<Grid>;
50 explicit GridFactory (
const MPICommunicatorType& = MPIHelper::getCommunicator() )
56 virtual void insertVertex(
const CoordinateType& pos)
58 nodes_.push_back( pos );
71 const std::vector<unsigned int>& items)
76 std::vector< int > numbers( items.size() );
77 std::copy( items.begin(), items.end(), numbers.begin() );
79 if( type.dim() == dimension-1 )
81 faces_.push_back( numbers );
83 else if( type.dim() == dimension )
87 cells_.push_back( numbers );
91 DUNE_THROW(Dune::NotImplemented,
"insertElement not implemented for type " << type );
101 void insertBoundarySegment(
const std::vector<unsigned int>&)
103 DUNE_THROW(NotImplemented,
"yet");
106 UniquePtrType createGrid()
108 std::vector< CoordinateType >& nodes = nodes_;
109 std::vector< std::vector< int > >& faces = faces_;
110 std::vector< std::vector< int > >& cells = cells_;
114 DUNE_THROW( GridError,
"No cells found for PolyhedralGrid" );
117 const auto sumSize = [] ( std::size_t s,
const std::vector< int > &v ) {
return s + v.size(); };
118 const std::size_t numFaceNodes = std::accumulate( faces.begin(), faces.end(), std::size_t( 0 ), sumSize );
119 const std::size_t numCellFaces = std::accumulate( cells.begin(), cells.end(), std::size_t( 0 ), sumSize );
121 typename Grid::UnstructuredGridPtr ug =
122 Grid::allocateGrid( cells.size(), faces.size(), numFaceNodes, numCellFaces, nodes.size() );
127 std::map< std::vector< int >, std::vector< int > > faceMap;
130 const int nFaces = faces.size();
132 std::fill( ug->face_cells, ug->face_cells + 2*nFaces, -1 );
135 std::vector< int > faceVertices;
136 faceVertices.reserve( 30 );
137 for(
int face = 0; face < nFaces; ++face )
140 faceVertices.clear();
141 ug->face_nodepos[ face ] = facepos;
142 const int nVertices = faces[ face ].size();
143 for(
int vx = 0; vx < nVertices; ++vx, ++facepos )
146 ug->face_nodes[ facepos ] = faces[ face ][ vx ];
147 faceVertices.push_back( faces[ face ][ vx ] );
153 std::sort( faceVertices.begin(), faceVertices.end() );
155 faceMap[ faceVertices ].push_back( face );
156 assert( faceMap[ faceVertices ].size() == 1 );
159 ug->face_nodepos[ nFaces ] = facepos ;
164 const int nCells = cells.size();
166 for(
int cell = 0; cell < nCells; ++cell )
169 ug->cell_facepos[ cell ] = cellpos;
170 const int nFaces = cells[ cell ].size();
171 for(
int f = 0; f < nFaces; ++f, ++cellpos )
173 const int face = cells[ cell ][ f ];
175 ug->cell_faces[ cellpos ] = face;
178 if( ug->face_cells[ 2*face ] == -1 )
180 ug->face_cells[ 2*face ] = cell;
185 ug->face_cells[ 2*face+1 ] = cell;
190 ug->cell_facepos[ nCells ] = cellpos ;
195 const int nNodes = nodes.size();
197 for(
int vx = 0 ; vx < nNodes; ++vx )
199 for(
int d=0; d<dim; ++d, ++nodepos )
200 ug->node_coordinates[ nodepos ] = nodes[ vx ][ d ];
213 if( ug->cell_facetag )
215 std::free( ug->cell_facetag );
216 ug->cell_facetag = nullptr ;
217 for(
int i=0; i<3; ++i ) ug->cartdims[ i ] = 0;
221 Grid::computeGeometry( ug );
225 for(
int face = 0 ; face < ug->number_of_faces; ++face )
227 const int a = ug->face_cells[ 2*face ];
228 const int b = ug->face_cells[ 2*face + 1 ];
232 Coordinate centerDiff( 0 );
233 Coordinate normal( 0 );
235 for(
int d=0; d<dim; ++d )
238 centerDiff[ d ] = ug->cell_centroids[ b*dim + d ] - ug->cell_centroids[ a*dim + d ];
239 normal[ d ] = ug->face_normals[ face*dim + d ];
243 if( centerDiff * normal > 0 )
245 ug->face_cells[ 2*face ] = b;
246 ug->face_cells[ 2*face + 1 ] = a;
251 return UniquePtrType(
new Grid( std::move( ug ) ));
255 std::vector< CoordinateType > nodes_;
256 std::vector< std::vector< int > > faces_;
257 std::vector< std::vector< int > > cells_;