3#ifndef DUNE_POLYHEDRALGRID_DGFPARSER_HH
4#define DUNE_POLYHEDRALGRID_DGFPARSER_HH
11#include <dune/common/typetraits.hh>
12#include <dune/common/version.hh>
14#include <dune/geometry/referenceelements.hh>
16#include <dune/grid/common/gridfactory.hh>
17#include <dune/grid/io/file/dgfparser/dgfparser.hh>
19#include <dune/grid/io/file/dgfparser/blocks/polyhedron.hh>
23#include <opm/input/eclipse/Parser/ParseContext.hpp>
24#include <opm/input/eclipse/Parser/Parser.hpp>
25#include <opm/input/eclipse/Deck/Deck.hpp>
32 template<
int dim,
int dimworld,
class coord_t >
37 const static int dimension = Grid::dimension;
39 typedef typename Grid::template Codim<0>::Entity
Element;
40 typedef typename Grid::template Codim<dimension>::Entity
Vertex;
49 DUNE_THROW( DGFException,
"Error resetting input stream" );
57 std::ifstream input( filename );
59 DUNE_THROW( DGFException,
"Macrofile '" << filename <<
"' not found" );
61 if( !DuneGridFormatParser::isDuneGridFormat( input ) )
64 const auto deck = parser.parseString( filename );
65 std::vector<double> porv;
67 gridPtr_.reset(
new Grid( deck, porv ) );
81 grid_ = gridPtr_.release();
86 template<
class Intersection >
92 template<
class Intersection >
100 template<
int codim >
107 template<
class Intersection >
108 const typename DGFBoundaryParameter::type &
111 return DGFBoundaryParameter::defaultValue();;
114 template<
class Entity >
117 static std::vector< double > dummy;
122 int readVertices ( std::istream &input, std::vector< std::vector< double > > &vertices )
124 int dimWorld = Grid::dimensionworld ;
125 dgf::VertexBlock vtxBlock( input, dimWorld );
126 if( !vtxBlock.isactive() )
127 DUNE_THROW( DGFException,
"Vertex block not found" );
129 vtxBlock.get( vertices, vtxParams_, numVtxParams_ );
130 return vtxBlock.offset();
133 std::vector< std::vector< int > > readPolygons ( std::istream &input,
int numVtx,
int vtxOfs )
135 dgf::PolygonBlock polygonBlock( input, numVtx, vtxOfs );
136 if( !polygonBlock.isactive() )
137 DUNE_THROW( DGFException,
"Polygon block not found" );
139 std::vector< std::vector< int > > polygons;
140 polygonBlock.get( polygons );
144 std::vector< std::vector< int > > readPolyhedra ( std::istream &input,
int numPolygons )
146 dgf::PolyhedronBlock polyhedronBlock( input, numPolygons );
147 std::vector< std::vector< int > > polyhedra;
148 if( polyhedronBlock.isactive() )
150 polyhedronBlock.get( polyhedra );
155 template<
class Iterator >
156 void copy ( Iterator begin, Iterator end,
double *dest )
158 for( ; begin != end; ++begin )
159 dest = std::ranges::copy(*begin, dest);
162 template<
class Iterator >
163 void copy ( Iterator begin, Iterator end,
int *dest,
int *offset )
166 for( ; begin != end; ++begin )
169 size += begin->size();
170 dest = std::ranges::copy(*begin, dest);
175 void generate ( std::istream &input )
178 dgf::IntervalBlock intervalBlock( input );
179 if( intervalBlock.isactive() )
181 if( intervalBlock.numIntervals() != 1 )
182 DUNE_THROW( DGFException,
"Currently, CpGrid can only handle 1 interval block." );
184 if( intervalBlock.dimw() != dimworld )
185 DUNE_THROW( DGFException,
"CpGrid cannot handle an interval of dimension " << intervalBlock.dimw() <<
"." );
186 const dgf::IntervalBlock::Interval &interval = intervalBlock.get( 0 );
188 std::vector< double > spacing( dimworld );
189 for(
int i=0; i<dimworld; ++i )
190 spacing[ i ] = (interval.p[ 1 ][ i ] - interval.p[ 0 ][ i ]) / interval.n[ i ];
192 gridPtr_.reset(
new Grid( interval.n, spacing ) );
197 typedef std::vector< std::vector< double > > CoordinateVectorType;
198 CoordinateVectorType nodes;
200 typedef std::vector< std::vector< int > > IndexVectorType;
201 IndexVectorType faces;
202 IndexVectorType cells;
204 const int vtxOfs = readVertices( input, nodes );
206 faces = readPolygons ( input, nodes.size(), vtxOfs );
207 cells = readPolyhedra( input, faces.size() );
211 DUNE_THROW( DGFException,
"Polyhedron block not found!" );
214 typedef GridFactory< Grid > GridFactoryType;
215 typedef typename GridFactoryType :: Coordinate Coordinate ;
217 GridFactoryType gridFactory;
219 const int nNodes = nodes.size();
220 Coordinate node( 0 );
221 for(
int i=0; i<nNodes; ++i )
223 for(
int d=0; d<Coordinate::dimension; ++d )
224 node[ d ] = nodes[ i ][ d ];
226 gridFactory.insertVertex( node );
232 type = Dune::GeometryTypes::none(Grid::dimension-1);
233 std::vector< unsigned int > numbers;
235 const int nFaces = faces.size();
236 for(
int i = 0; i < nFaces; ++ i )
239 std::vector<int>& face = faces[ i ];
240 numbers.resize( face.size() );
241 std::ranges::copy(face, numbers.begin());
242 gridFactory.insertElement( type, numbers );
249 type = Dune::GeometryTypes::none(Grid::dimension);
251 const int nCells = cells.size();
252 for(
int i = 0; i < nCells; ++ i )
255 std::vector<int>& cell = cells[ i ];
256 numbers.resize( cell.size() );
257 std::ranges::copy(cell, numbers.begin());
258 gridFactory.insertElement( type, numbers );
262 gridPtr_ = gridFactory.createGrid();
269 nodes.resize( dgf.nofvtx );
271 std::ranges::copy(dgf.vtx, nodes.begin());
273 for(
const auto& node : nodes )
275 for(
size_t i=0; i<node.size(); ++i )
276 std::cout << node[i] <<
" ";
277 std::cout << std::endl;
280 const unsigned int nVx = dgf.elements[ 0 ].size();
282 typedef std::vector< int > face_t;
283 std::map< face_t, int > tmpFaces;
285 const int nFaces = (nVx == dim+1) ? dim+1 : 2*dim;
287 Dune::GeometryType type( (nVx == dim+1) ?
288 Impl :: SimplexTopology< dim > :: type :: id :
289 Impl :: CubeTopology < dim > :: type :: id,
292 const auto& refElem = Dune::referenceElement< typename Grid::ctype, dim >( type );
294 cells.resize( dgf.nofelements );
298 for(
int n = 0; n < dgf.nofelements; ++n )
300 const auto& elem = dgf.elements[ n ];
301 auto& cell = cells[ n ];
302 assert( elem.size() == nVx );
303 cell.resize( nFaces );
304 for(
int f=0; f<nFaces; ++f )
306 const int nFaceVx = refElem.size(f, 1, dim);
307 face.resize( nFaceVx );
308 for(
int j=0; j<nFaceVx; ++j )
310 face[ j ] = elem[ refElem.subEntity(f, 1, j , dim) ];
312 std::ranges::sort(face);
313 auto it = tmpFaces.find( face );
315 if( it == tmpFaces.end() )
318 tmpFaces.insert( std::make_pair( face, myFaceNo ) );
321 myFaceNo = it->second;
323 cell[ f ] = myFaceNo;
337 mutable std::unique_ptr< Grid > gridPtr_;
340 std::vector< std::vector< double > > vtxParams_;
348 template<
int dim,
int dimworld >
identical grid wrapper
Definition: grid.hh:157
The namespace Dune is the main namespace for all Dune code.
Definition: common/CartesianIndexMapper.hpp:10
int numParameters() const
Definition: polyhedralgrid/dgfparser.hh:101
Grid * grid() const
Definition: polyhedralgrid/dgfparser.hh:76
bool haveBoundaryParameters() const
Definition: polyhedralgrid/dgfparser.hh:98
Grid::template Codim< 0 >::Entity Element
Definition: polyhedralgrid/dgfparser.hh:39
PolyhedralGrid< dim, dimworld, coord_t > Grid
Definition: polyhedralgrid/dgfparser.hh:35
int boundaryId(const Intersection &) const
Definition: polyhedralgrid/dgfparser.hh:93
std::vector< double > & parameter(const Entity &)
Definition: polyhedralgrid/dgfparser.hh:115
bool wasInserted(const Intersection &) const
Definition: polyhedralgrid/dgfparser.hh:87
DGFGridFactory(const std::string &filename, MPICommunicator=MPIHelper::getCommunicator())
Definition: polyhedralgrid/dgfparser.hh:53
const DGFBoundaryParameter::type & boundaryParameter(const Intersection &) const
Definition: polyhedralgrid/dgfparser.hh:109
DGFGridFactory(std::istream &input, MPICommunicator=MPIHelper::getCommunicator())
Definition: polyhedralgrid/dgfparser.hh:42
Grid::template Codim< dimension >::Entity Vertex
Definition: polyhedralgrid/dgfparser.hh:40
MPIHelper::MPICommunicator MPICommunicator
Definition: polyhedralgrid/dgfparser.hh:38
static double refineWeight()
Definition: polyhedralgrid/dgfparser.hh:356
static int refineStepsForHalf()
Definition: polyhedralgrid/dgfparser.hh:351