Dune Core Modules (2.8.0)

dgfgridfactory.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_DGF_GRIDFACTORY_HH
4#define DUNE_DGF_GRIDFACTORY_HH
5
6#include <iostream>
7#include <string>
8#include <vector>
9#include <map>
10#include <assert.h>
11
13#include <dune/grid/io/file/dgfparser/dgfexception.hh>
14#include <dune/grid/io/file/dgfparser/macrogrid.hh>
15
16#include <dune/grid/io/file/dgfparser/parser.hh>
17#include <dune/grid/common/intersection.hh>
18
19
20namespace Dune
21{
22
23 // External Forward Declarations
24 // -----------------------------
25
26 template < class GridImp, class IntersectionImp >
27 class Intersection;
28
29
30
31 // DGFGridFactory
32 // --------------
33
34 template < class G >
35 struct DGFGridFactory
36 {
37 typedef G Grid;
38 const static int dimension = Grid::dimension;
39 typedef MPIHelper::MPICommunicator MPICommunicatorType;
40
41 private:
42 typedef typename Grid::template Codim< 0 >::Entity Element;
43
44 typedef typename Grid::template Codim< dimension >::Entity Vertex;
45
46 public:
47
48 explicit DGFGridFactory ( const std::string &filename,
49 MPICommunicatorType comm = MPIHelper::getCommunicator() )
50 : macroGrid_( filename.c_str(), comm )
51 {
52 grid_ = macroGrid_.template createGrid< Grid >();
53
54 if( macroGrid_.nofelparams > 0 )
55 {
56 const size_t nofElements = macroGrid_.elements.size();
57 for( size_t i = 0; i < nofElements; ++i )
58 {
59 std::vector< double > coord;
60
61 DomainType p(0);
62 const size_t nofCorners = macroGrid_.elements[i].size();
63 for (size_t k=0; k<nofCorners; ++k)
64 for (int j=0; j<DomainType::dimension; ++j)
65 p[j]+=macroGrid_.vtx[macroGrid_.elements[i][k]][j];
66 p/=double(nofCorners);
67
68 elInsertOrder_.insert( std::make_pair( p, i ) );
69 }
70 }
71
72 if( macroGrid_.nofvtxparams > 0 )
73 {
74 const size_t nofVertices = macroGrid_.vtx.size();
75 for( size_t i = 0; i < nofVertices; ++i )
76 {
77 std::vector< double > coord;
78
79 DomainType p;
80 for( int k = 0; k < DomainType::dimension; ++k )
81 p[ k ] = macroGrid_.vtx[i][k];
82
83 vtxInsertOrder_.insert( std::make_pair( p, i ) );
84 }
85 }
86 }
87
88 Grid *grid()
89 {
90 return grid_;
91 }
92
93 template <class Intersection>
94 bool wasInserted(const Intersection &intersection) const
95 {
96 return intersection.boundary();
97 }
98
99 template <class Intersection>
100 int boundaryId(const Intersection &intersection) const
101 {
102 return (intersection.boundary()) ? int(intersection.indexInInside()+1) : int(0);
103 }
104
105 template< int codim >
106 int numParameters () const
107 {
108 if( codim == 0 )
109 return macroGrid_.nofelparams;
110 else if( codim == dimension )
111 return macroGrid_.nofvtxparams;
112 else
113 return 0;
114 }
115
116 template < class Entity >
117 int numParameters ( const Entity & ) const
118 {
119 return numParameters< Entity::codimension >();
120 }
121
122 std::vector<double>& parameter(const Element &element)
123 {
124 const typename Element::Geometry &geo = element.geometry();
125 DomainType coord( geo.corner( 0 ) );
126 for( int i = 1; i < geo.corners(); ++i )
127 coord += geo.corner( i );
128 coord /= double( geo.corners() );
129
130 InsertOrderIterator it = elInsertOrder_.find( coord );
131 if( it != elInsertOrder_.end() )
132 return macroGrid_.elParams[ it->second ];
133 assert(0);
134 return emptyParam;
135 }
136
137 std::vector<double>& parameter(const Vertex &vertex)
138 {
139 const typename Vertex::Geometry &geo = vertex.geometry();
140 DomainType coord( geo.corner( 0 ) );
141
142 InsertOrderIterator it = vtxInsertOrder_.find( coord );
143 if( it != vtxInsertOrder_.end() )
144 return macroGrid_.vtxParams[ it->second ];
145 return emptyParam;
146 }
147
148 // return true if boundary parameters found
149 bool haveBoundaryParameters () const
150 {
151 return false;
152 }
153
154 template< class GG, class II >
155 const typename DGFBoundaryParameter::type &
156 boundaryParameter ( const Intersection< GG, II > & intersection ) const
157 {
159 }
160
161 private:
162 typedef FieldVector<typename Grid::ctype,Grid::dimensionworld> DomainType;
163 struct Compare
164 {
165 bool operator() ( const DomainType &a, const DomainType &b ) const
166 {
167 // returns true, if a < b; c[i] < -eps;
168 const DomainType c = a - b;
169 const double eps = 1e-8;
170
171 for( int i = 0; i < DomainType::dimension; ++i )
172 {
173 if( c[ i ] <= -eps )
174 return true;
175 if( c[ i ] >= eps )
176 return false;
177 }
178 return false;
179 }
180 };
181 typedef std::map< DomainType, size_t, Compare > InsertOrderMap;
182 typedef typename InsertOrderMap::const_iterator InsertOrderIterator;
183
184 MacroGrid macroGrid_;
185 Grid *grid_;
186 InsertOrderMap elInsertOrder_;
187 InsertOrderMap vtxInsertOrder_;
188 std::vector<double> emptyParam;
189 };
190
191} // end namespace Dune
192
193#endif
@ dimension
The size of this vector.
Definition: fvector.hh:102
@ dimension
The dimension of the grid.
Definition: grid.hh:386
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:188
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:196
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:504
Helpers for dealing with MPI.
Dune namespace.
Definition: alignedallocator.hh:11
static const type & defaultValue()
default constructor
Definition: parser.hh:26
std::string type
type of additional boundary parameters
Definition: parser.hh:23
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 13, 23:29, 2024)