~ubuntu-branches/ubuntu/vivid/dune-grid/vivid

« back to all changes in this revision

Viewing changes to dune/grid/io/file/dgfparser/dgfug.hh

  • Committer: Package Import Robot
  • Author(s): Ansgar Burchardt
  • Date: 2012-04-06 11:31:20 UTC
  • Revision ID: package-import@ubuntu.com-20120406113120-x0z4e0qqgfhmaj2a
Tags: upstream-2.2~svn7982
ImportĀ upstreamĀ versionĀ 2.2~svn7982

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef DUNE_GRID_IO_FILE_DGFPARSER_DGFUG_HH
 
2
#define DUNE_GRID_IO_FILE_DGFPARSER_DGFUG_HH
 
3
 
 
4
//- C++ includes
 
5
#include <fstream>
 
6
#include <istream>
 
7
#include <string>
 
8
#include <vector>
 
9
 
 
10
//- dune-common includes
 
11
#include <dune/common/exceptions.hh>
 
12
#include <dune/common/fvector.hh>
 
13
#include <dune/common/mpihelper.hh>
 
14
 
 
15
//- dune-grid includes
 
16
#include <dune/grid/common/intersection.hh>
 
17
#include <dune/grid/uggrid.hh>
 
18
 
 
19
//- local includes
 
20
#include "dgfparser.hh"
 
21
#include "blocks/gridparameter.hh"
 
22
 
 
23
 
 
24
namespace Dune
 
25
{
 
26
 
 
27
  namespace dgf
 
28
  {
 
29
 
 
30
    // UGGridParameterBlock
 
31
    // --------------------
 
32
 
 
33
    struct UGGridParameterBlock
 
34
    : public GridParameterBlock
 
35
    {
 
36
      /** \brief constructor taking istream */
 
37
      explicit UGGridParameterBlock ( std::istream &input );
 
38
 
 
39
      /** \brief returns true if no closure should be used for UGGrid */
 
40
      bool noClosure () const { return noClosure_; }
 
41
      /** \brief returns true if no copies are made for UGGrid elements */
 
42
      bool noCopy () const { return noCopy_; }
 
43
      /** \brief returns heap size used on construction of the grid */
 
44
      size_t heapSize () const { return heapSize_; }
 
45
 
 
46
    protected:
 
47
      bool noClosure_;  // no closure for UGGrid 
 
48
      bool noCopy_;     // no copies  for UGGrid
 
49
      size_t heapSize_; // heap size  for UGGrid 
 
50
    };
 
51
 
 
52
  } // namespace dgf
 
53
 
 
54
 
 
55
 
 
56
#if ENABLE_UG 
 
57
  template< int dim >
 
58
  struct DGFGridInfo< UGGrid< dim > >
 
59
  {
 
60
    static int refineStepsForHalf ()
 
61
    {
 
62
      return 1;
 
63
    }
 
64
 
 
65
    static double refineWeight ()
 
66
    {
 
67
      return -1.;
 
68
    }
 
69
  };
 
70
 
 
71
 
 
72
 
 
73
  // DGFGridFactory< UGGrid< dim > >
 
74
  // -------------------------------
 
75
 
 
76
  template< int dim >
 
77
  struct DGFGridFactory< UGGrid< dim > >
 
78
  {
 
79
    /** \brief grid type */
 
80
    typedef UGGrid< dim > Grid;
 
81
    /** \brief grid dimension */
 
82
    static const int dimension = dim;
 
83
    /** \brief MPI communicator type */
 
84
    typedef MPIHelper::MPICommunicator MPICommunicatorType;
 
85
 
 
86
    /** \brief constructor taking istream */
 
87
    explicit DGFGridFactory ( std::istream &input,
 
88
                              MPICommunicatorType comm = MPIHelper::getCommunicator() )
 
89
    : grid_( 0 ),
 
90
      factory_(),
 
91
      dgf_( rank( comm ), size( comm ) )
 
92
    { 
 
93
      generate( input );
 
94
    }
 
95
 
 
96
    /** \brief constructor taking filename */
 
97
    explicit DGFGridFactory ( const std::string &filename,
 
98
                              MPICommunicatorType comm = MPIHelper::getCommunicator() )
 
99
    : grid_( 0 ),
 
100
      factory_(),
 
101
      dgf_( rank( comm ), size( comm ) )
 
102
    { 
 
103
      std::ifstream input( filename.c_str() );
 
104
      if ( !input )
 
105
        DUNE_THROW( DGFException, "Error: Macrofile " << filename << " not found" );
 
106
      generate( input );
 
107
    }
 
108
 
 
109
    /** \brief return grid */
 
110
    Grid *grid ()
 
111
    {
 
112
      return grid_;
 
113
    }
 
114
 
 
115
    /** \brief please doc me */
 
116
    template< class GG, template< class > class II >
 
117
    bool wasInserted ( const Dune::Intersection< GG, II > &intersection ) const
 
118
    {
 
119
      return factory_.wasInserted( intersection );
 
120
    }
 
121
 
 
122
    /** \brief will return boundary segment index */
 
123
    template < class GG, template< class > class II >
 
124
    int boundaryId ( const Dune::Intersection< GG, II > &intersection ) const
 
125
    {
 
126
      return intersection.boundarySegmentIndex();
 
127
    }
 
128
 
 
129
    /** \brief return number of parameters */
 
130
    template< int codim >
 
131
    int numParameters () const
 
132
    {
 
133
      if( codim == 0 )
 
134
        return dgf_.nofelparams;
 
135
      else if( codim == dimension )
 
136
        return dgf_.nofvtxparams;
 
137
      else
 
138
        return 0;
 
139
    }
 
140
 
 
141
    /** \brief return number of parameters */
 
142
    template< class Entity >
 
143
    int numParameters ( const Entity & ) const
 
144
    {
 
145
      return numParameters< Entity::codimension >();
 
146
    }
 
147
 
 
148
    /** \brief return parameter for codim 0 entity */
 
149
    std::vector< double > &parameter ( const typename Grid::template Codim< 0 >::Entity &element )
 
150
    {
 
151
      if( numParameters< 0 >() <= 0 )
 
152
      {
 
153
        DUNE_THROW( InvalidStateException,
 
154
                    "Calling DGFGridFactory::parameter is only allowed if there are parameters." );
 
155
      }
 
156
      return dgf_.elParams[ factory_.insertionIndex( element ) ];
 
157
    }
 
158
 
 
159
    /** \brief return parameter for vertex */
 
160
    std::vector< double > &parameter ( const typename Grid::template Codim< dimension >::Entity &vertex )
 
161
    {
 
162
      if( numParameters< dimension >() <= 0 )
 
163
      {
 
164
        DUNE_THROW( InvalidStateException,
 
165
                    "Calling DGFGridFactory::parameter is only allowed if there are parameters." );
 
166
      }
 
167
      return dgf_.vtxParams[ factory_.insertionIndex( vertex ) ];
 
168
    }
 
169
 
 
170
    /** \brief UGGrid does not support boundary parameters */
 
171
    bool haveBoundaryParameters () const
 
172
    {
 
173
      return dgf_.haveBndParameters;
 
174
    }
 
175
 
 
176
    /** \brief return invalid value */
 
177
    template < class GG, template< class > class II >
 
178
    const DGFBoundaryParameter::type &boundaryParameter ( const Dune::Intersection< GG, II > &intersection ) const
 
179
    {
 
180
      typedef Dune::Intersection< GG, II > Intersection;
 
181
      typename Intersection::EntityPointer inside = intersection.inside();
 
182
      const typename Intersection::Entity &entity = *inside;
 
183
      const int face = intersection.indexInInside();
 
184
 
 
185
      const GenericReferenceElement< double, dimension > &refElem 
 
186
        = GenericReferenceElements< double, dimension >::general( entity.type() );
 
187
      int corners = refElem.size( face, 1, dimension );
 
188
      std::vector< unsigned int > bound( corners );
 
189
      for( int i = 0; i < corners; ++i )
 
190
      {
 
191
        const int k = refElem.subEntity( face, 1, i, dimension );
 
192
        bound[ i ] = factory_.insertionIndex( *entity.template subEntity< dimension >( k ) );
 
193
      }
 
194
 
 
195
      DuneGridFormatParser::facemap_t::key_type key( bound, false );
 
196
      const DuneGridFormatParser::facemap_t::const_iterator pos = dgf_.facemap.find( key );
 
197
      if( pos != dgf_.facemap.end() )
 
198
        return dgf_.facemap.find( key )->second.second;
 
199
      else
 
200
        return DGFBoundaryParameter::defaultValue();
 
201
    }
 
202
 
 
203
  private:
 
204
    // create grid
 
205
    void generate ( std::istream &input );
 
206
 
 
207
    // return rank
 
208
    static int rank( MPICommunicatorType MPICOMM )
 
209
    {
 
210
      int rank = 0;
 
211
#if HAVE_MPI
 
212
      MPI_Comm_rank( MPICOMM, &rank );
 
213
#endif
 
214
      return rank;
 
215
    }
 
216
 
 
217
    // return size
 
218
    static int size( MPICommunicatorType MPICOMM )
 
219
    {
 
220
      int size = 1;
 
221
#if HAVE_MPI
 
222
      MPI_Comm_size( MPICOMM, &size );
 
223
#endif
 
224
      return size;
 
225
    }
 
226
 
 
227
    Grid *grid_;
 
228
    GridFactory< UGGrid< dim > > factory_;
 
229
    DuneGridFormatParser dgf_;
 
230
  };
 
231
#endif // #if ENABLE_UG 
 
232
 
 
233
} // namespace Dune
 
234
 
 
235
#endif // #ifndef DUNE_GRID_IO_FILE_DGFPARSER_DGFUG_HH