dune-grid  2.9.0
albertagrid/dgfparser.hh
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 #ifndef DUNE_ALBERTA_DGFPARSER_HH
6 #define DUNE_ALBERTA_DGFPARSER_HH
7 
8 #include <vector>
9 
10 #include <dune/grid/albertagrid.hh>
12 
15 
18 
19 #if HAVE_ALBERTA
20 
21 namespace Dune
22 {
23 
24  // External Forward Declarations
25  // -----------------------------
26 
27  template< class GridImp, class IntersectionImp >
28  class Intersection;
29 
30 
31 
32  // DGFGridFactory for AlbertaGrid
33  // ------------------------------
34 
35  template< int dim, int dimworld >
36  struct DGFGridFactory< AlbertaGrid< dim, dimworld > >
37  {
39  const static int dimension = Grid::dimension;
40  typedef MPIHelper::MPICommunicator MPICommunicatorType;
41  typedef typename Grid::template Codim<0>::Entity Element;
42  typedef typename Grid::template Codim<dimension>::Entity Vertex;
44 
45  explicit DGFGridFactory ( std::istream &input,
46  MPICommunicatorType comm = MPIHelper::getCommunicator() );
47  explicit DGFGridFactory ( const std::string &filename,
48  MPICommunicatorType comm = MPIHelper::getCommunicator() );
49 
50  Grid *grid () const
51  {
52  return grid_;
53  }
54 
55  template< class Intersection >
56  bool wasInserted ( const Intersection &intersection ) const
57  {
58  return factory_.wasInserted( intersection );
59  }
60 
61  template< class Intersection >
62  int boundaryId ( const Intersection &intersection ) const
63  {
64  return intersection.impl().boundaryId();
65  }
66 
67  // return true if boundary parameters found
68  bool haveBoundaryParameters () const
69  {
70  return dgf_.haveBndParameters;
71  }
72 
73  template < class GG, class II >
75  boundaryParameter ( const Intersection< GG, II > & intersection ) const
76  {
78  typename Intersection::Entity entity = intersection.inside();
79  const int face = intersection.indexInInside();
80 
81  auto refElem = referenceElement< double, dimension >( entity.type() );
82  int corners = refElem.size( face, 1, dimension );
83  std :: vector< unsigned int > bound( corners );
84  for( int i=0; i < corners; ++i )
85  {
86  const int k = refElem.subEntity( face, 1, i, dimension );
87  bound[ i ] = factory_.insertionIndex( entity.template subEntity< dimension >( k ) );
88  }
89 
90  DuneGridFormatParser::facemap_t::key_type key( bound, false );
91  const DuneGridFormatParser::facemap_t::const_iterator pos = dgf_.facemap.find( key );
92  if( pos != dgf_.facemap.end() )
93  return dgf_.facemap.find( key )->second.second;
94  else
96  }
97 
98  template< int codim >
99  int numParameters () const
100  {
101  if( codim == 0 )
102  return dgf_.nofelparams;
103  else if( codim == dimension )
104  return dgf_.nofvtxparams;
105  else
106  return 0;
107  }
108 
109  std::vector< double > &parameter ( const Element &element )
110  {
111  if( numParameters< 0 >() <= 0 )
112  {
113  DUNE_THROW( InvalidStateException,
114  "Calling DGFGridFactory::parameter is only allowed if there are parameters." );
115  }
116  return dgf_.elParams[ factory_.insertionIndex( element ) ];
117  }
118 
119  std::vector< double > &parameter ( const Vertex &vertex )
120  {
121  if( numParameters< dimension >() <= 0 )
122  {
123  DUNE_THROW( InvalidStateException,
124  "Calling DGFGridFactory::parameter is only allowed if there are parameters." );
125  }
126  return dgf_.vtxParams[ factory_.insertionIndex( vertex ) ];
127  }
128 
129  private:
130  bool generate( std::istream &input );
131 
132  Grid *grid_;
133  GridFactory factory_;
135  };
136 
137 
138 
139  // DGFGridInfo for AlbertaGrid
140  // ---------------------------
141 
142  template< int dim, int dimworld >
143  struct DGFGridInfo< AlbertaGrid< dim, dimworld > >
144  {
145  static int refineStepsForHalf ()
146  {
147  return dim;
148  }
149 
150  static double refineWeight ()
151  {
152  return 0.5;
153  }
154  };
155 
156 
157 
158  // Implementation of DGFGridFactory for AlbertaGrid
159  // ------------------------------------------------
160 
161  template< int dim, int dimworld >
162  inline DGFGridFactory< AlbertaGrid< dim, dimworld > >
163  ::DGFGridFactory ( std::istream &input, MPICommunicatorType /* comm */ )
164  : dgf_( 0, 1 )
165  {
166  input.clear();
167  input.seekg( 0 );
168  if( !input )
169  DUNE_THROW(DGFException, "Error resetting input stream." );
170  generate( input );
171  }
172 
173 
174  template< int dim, int dimworld >
176  ::DGFGridFactory ( const std::string &filename, MPICommunicatorType /* comm */ )
177  : dgf_( 0, 1 )
178  {
179  std::ifstream input( filename.c_str() );
180  if( !input )
181  DUNE_THROW( DGFException, "Macrofile " << filename << " not found." );
182  if( !generate( input ) )
183  grid_ = new AlbertaGrid< dim, dimworld >( filename.c_str() );
184  input.close();
185  }
186 
187 }
188 
189 #endif // #if HAVE_ALBERTA
190 
191 #endif // #ifndef DUNE_ALBERTA_DGFPARSER_HH
Include standard header files.
Definition: agrid.hh:60
@ vertex
Definition: common.hh:133
Definition: dgfgridfactory.hh:38
DGFGridFactory(const std::string &filename, MPICommunicatorType comm=MPIHelper::getCommunicator())
Definition: dgfgridfactory.hh:50
static const int dimension
Definition: dgfgridfactory.hh:40
[ provides Dune::Grid ]
Definition: agrid.hh:109
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition: common/intersection.hh:164
int indexInInside() const
Local index of codim 1 entity in the inside() entity where intersection is contained in.
Definition: common/intersection.hh:346
Entity inside() const
return Entity on the inside of this intersection. That is the Entity where we started this.
Definition: common/intersection.hh:250
GridImp::template Codim< 0 >::Entity Entity
Type of entity that this Intersection belongs to.
Definition: common/intersection.hh:192
Implementation & impl()
access to the underlying implementation
Definition: common/intersection.hh:178
Grid * grid() const
Definition: albertagrid/dgfparser.hh:50
MPIHelper::MPICommunicator MPICommunicatorType
Definition: albertagrid/dgfparser.hh:40
std::vector< double > & parameter(const Vertex &vertex)
Definition: albertagrid/dgfparser.hh:119
bool haveBoundaryParameters() const
Definition: albertagrid/dgfparser.hh:68
Grid::template Codim< 0 >::Entity Element
Definition: albertagrid/dgfparser.hh:41
AlbertaGrid< dim, dimworld > Grid
Definition: albertagrid/dgfparser.hh:38
std::vector< double > & parameter(const Element &element)
Definition: albertagrid/dgfparser.hh:109
const DGFBoundaryParameter::type & boundaryParameter(const Intersection< GG, II > &intersection) const
Definition: albertagrid/dgfparser.hh:75
int numParameters() const
Definition: albertagrid/dgfparser.hh:99
Dune::GridFactory< Grid > GridFactory
Definition: albertagrid/dgfparser.hh:43
bool wasInserted(const Intersection &intersection) const
Definition: albertagrid/dgfparser.hh:56
int boundaryId(const Intersection &intersection) const
Definition: albertagrid/dgfparser.hh:62
Grid::template Codim< dimension >::Entity Vertex
Definition: albertagrid/dgfparser.hh:42
static int refineStepsForHalf()
Definition: albertagrid/dgfparser.hh:145
static double refineWeight()
Definition: albertagrid/dgfparser.hh:150
Grid abstract base class.
Definition: common/grid.hh:375
constexpr static int dimension
The dimension of the grid.
Definition: common/grid.hh:387
exception class for IO errors in the DGF parser
Definition: dgfexception.hh:16
Some simple static information for a given GridType.
Definition: io/file/dgfparser/dgfparser.hh:56
static const type & defaultValue()
default constructor
Definition: parser.hh:28
std::string type
type of additional boundary parameters
Definition: parser.hh:25
The DuneGridFormatParser class: reads a DGF file and stores build information in vector structures us...
Definition: parser.hh:47
specialization of the generic GridFactory for AlbertaGrid