3 #ifndef DUNE_POLYHEDRALGRID_GRIDFACTORY_HH
4 #define DUNE_POLYHEDRALGRID_GRIDFACTORY_HH
9 #include <dune/common/typetraits.hh>
10 #include <dune/common/version.hh>
12 #include <dune/grid/common/gridfactory.hh>
13 #include <opm/grid/polyhedralgrid/grid.hh>
22 template<
int dim,
int dimworld,
class coord_t >
24 :
public GridFactoryInterface< PolyhedralGrid< dim, dimworld, coord_t > >
29 const static int dimension = Grid::dimension;
30 const static int dimensionworld = Grid::dimensionworld;
33 typedef MPIHelper::MPICommunicator MPICommunicatorType;
34 typedef typename Grid::template Codim<0>::Entity Element;
35 typedef typename Grid::template Codim<dimension>::Entity Vertex;
37 typedef Dune::FieldVector<ctype,dimensionworld> CoordinateType;
38 typedef CoordinateType Coordinate;
40 #if DUNE_VERSION_GTE(DUNE_GRID, 2, 7)
41 #if DUNE_VERSION_LT(DUNE_GRID, 2, 8)
52 explicit GridFactory (
const MPICommunicatorType& = MPIHelper::getCommunicator() )
58 virtual void insertVertex(
const CoordinateType& pos)
60 nodes_.push_back( pos );
73 const std::vector<unsigned int>& items)
78 std::vector< int > numbers( items.size() );
79 std::copy( items.begin(), items.end(), numbers.begin() );
81 if( type.dim() == dimension-1 )
83 faces_.push_back( numbers );
85 else if( type.dim() == dimension )
89 cells_.push_back( numbers );
93 DUNE_THROW(Dune::NotImplemented,
"insertElement not implemented for type " << type );
103 void insertBoundarySegment(
const std::vector<unsigned int>&)
105 DUNE_THROW(NotImplemented,
"yet");
108 UniquePtrType createGrid()
110 std::vector< CoordinateType >& nodes = nodes_;
111 std::vector< std::vector< int > >& faces = faces_;
112 std::vector< std::vector< int > >& cells = cells_;
116 DUNE_THROW( GridError,
"No cells found for PolyhedralGrid" );
119 const auto sumSize = [] ( std::size_t s,
const std::vector< int > &v ) {
return s + v.size(); };
120 const std::size_t numFaceNodes = std::accumulate( faces.begin(), faces.end(), std::size_t( 0 ), sumSize );
121 const std::size_t numCellFaces = std::accumulate( cells.begin(), cells.end(), std::size_t( 0 ), sumSize );
123 typename Grid::UnstructuredGridPtr ug =
124 Grid::allocateGrid( cells.size(), faces.size(), numFaceNodes, numCellFaces, nodes.size() );
129 std::map< std::vector< int >, std::vector< int > > faceMap;
132 const int nFaces = faces.size();
134 std::fill( ug->face_cells, ug->face_cells + 2*nFaces, -1 );
137 std::vector< int > faceVertices;
138 faceVertices.reserve( 30 );
139 for(
int face = 0; face < nFaces; ++face )
142 faceVertices.clear();
143 ug->face_nodepos[ face ] = facepos;
144 const int nVertices = faces[ face ].size();
145 for(
int vx = 0; vx < nVertices; ++vx, ++facepos )
148 ug->face_nodes[ facepos ] = faces[ face ][ vx ];
149 faceVertices.push_back( faces[ face ][ vx ] );
155 std::sort( faceVertices.begin(), faceVertices.end() );
157 faceMap[ faceVertices ].push_back( face );
158 assert( faceMap[ faceVertices ].size() == 1 );
161 ug->face_nodepos[ nFaces ] = facepos ;
166 const int nCells = cells.size();
168 for(
int cell = 0; cell < nCells; ++cell )
171 ug->cell_facepos[ cell ] = cellpos;
172 const int nFaces = cells[ cell ].size();
173 for(
int f = 0; f < nFaces; ++f, ++cellpos )
175 const int face = cells[ cell ][ f ];
177 ug->cell_faces[ cellpos ] = face;
180 if( ug->face_cells[ 2*face ] == -1 )
182 ug->face_cells[ 2*face ] = cell;
187 ug->face_cells[ 2*face+1 ] = cell;
192 ug->cell_facepos[ nCells ] = cellpos ;
197 const int nNodes = nodes.size();
199 for(
int vx = 0 ; vx < nNodes; ++vx )
201 for(
int d=0; d<dim; ++d, ++nodepos )
202 ug->node_coordinates[ nodepos ] = nodes[ vx ][ d ];
215 if( ug->cell_facetag )
217 std::free( ug->cell_facetag );
218 ug->cell_facetag = nullptr ;
219 for(
int i=0; i<3; ++i ) ug->cartdims[ i ] = 0;
223 Grid::computeGeometry( ug );
227 for(
int face = 0 ; face < ug->number_of_faces; ++face )
229 const int a = ug->face_cells[ 2*face ];
230 const int b = ug->face_cells[ 2*face + 1 ];
234 Coordinate centerDiff( 0 );
235 Coordinate normal( 0 );
237 for(
int d=0; d<dim; ++d )
240 centerDiff[ d ] = ug->cell_centroids[ b*dim + d ] - ug->cell_centroids[ a*dim + d ];
241 normal[ d ] = ug->face_normals[ face*dim + d ];
245 if( centerDiff * normal > 0 )
247 ug->face_cells[ 2*face ] = b;
248 ug->face_cells[ 2*face + 1 ] = a;
253 return UniquePtrType(
new Grid( std::move( ug ) ));
257 std::vector< CoordinateType > nodes_;
258 std::vector< std::vector< int > > faces_;
259 std::vector< std::vector< int > > cells_;
virtual void insertElement(const GeometryType &type, const std::vector< unsigned int > &items)
Insert an element into the coarse grid.
Definition: gridfactory.hh:72
GridFactory(const MPICommunicatorType &=MPIHelper::getCommunicator())
Default constructor.
Definition: gridfactory.hh:52
identical grid wrapper
Definition: grid.hh:163
Traits::ctype ctype
type of vector coordinates (e.g., double)
Definition: grid.hh:312
Copyright 2019 Equinor AS.
Definition: CartesianIndexMapper.hpp:10