dune-grid 2.12-git
Loading...
Searching...
No Matches
dgfgridfactory.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © 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_DGF_GRIDFACTORY_HH
6#define DUNE_DGF_GRIDFACTORY_HH
7
8#include <iostream>
9#include <string>
10#include <vector>
11#include <map>
12#include <assert.h>
13
17
20
21
22namespace Dune
23{
24
25 // External Forward Declarations
26 // -----------------------------
27
28 template < class GridImp, class IntersectionImp >
29 class Intersection;
30
31
32
33 // DGFGridFactory
34 // --------------
35
36 template < class G >
38 {
39 typedef G Grid;
40 const static int dimension = Grid::dimension;
42 static constexpr bool noFactory = MacroGrid::noDGFFactory<G>;
43
44 private:
45 typedef typename Grid::template Codim< 0 >::Entity Element;
46
47 typedef typename Grid::template Codim< dimension >::Entity Vertex;
48
49 public:
50
51 explicit DGFGridFactory ( const std::string &filename,
53 : macroGrid_( filename.c_str(), comm )
54 {
55 grid_ = macroGrid_.template createGrid< Grid >();
56
57 if( macroGrid_.nofelparams > 0 )
58 {
59 const size_t nofElements = macroGrid_.elements.size();
60 for( size_t i = 0; i < nofElements; ++i )
61 {
63
64 DomainType p(0);
65 const size_t nofCorners = macroGrid_.elements[i].size();
66 for (size_t k=0; k<nofCorners; ++k)
67 for (int j=0; j<DomainType::dimension; ++j)
68 p[j]+=macroGrid_.vtx[macroGrid_.elements[i][k]][j];
69 p/=double(nofCorners);
70
71 elInsertOrder_.insert( std::make_pair( p, i ) );
72 }
73 }
74
75 if( macroGrid_.nofvtxparams > 0 )
76 {
77 const size_t nofVertices = macroGrid_.vtx.size();
78 for( size_t i = 0; i < nofVertices; ++i )
79 {
81
82 DomainType p;
83 for( int k = 0; k < DomainType::dimension; ++k )
84 p[ k ] = macroGrid_.vtx[i][k];
85
86 vtxInsertOrder_.insert( std::make_pair( p, i ) );
87 }
88 }
89 }
90
92 {
93 return grid_;
94 }
95
96 template <class Intersection>
97 bool wasInserted(const Intersection &intersection) const
98 {
99 return intersection.boundary();
100 }
101
102 template <class Intersection>
103 int boundaryId(const Intersection &intersection) const
104 {
105 return (intersection.boundary()) ? int(intersection.indexInInside()+1) : int(0);
106 }
107
108 template< int codim >
109 int numParameters () const
110 {
111 if( codim == 0 )
112 return macroGrid_.nofelparams;
113 else if( codim == dimension )
114 return macroGrid_.nofvtxparams;
115 else
116 return 0;
117 }
118
119 template < class Entity >
120 int numParameters ( const Entity & ) const
121 {
122 return numParameters< Entity::codimension >();
123 }
124
125 std::vector<double>& parameter(const Element &element)
126 {
127 const typename Element::Geometry &geo = element.geometry();
128 DomainType coord( geo.corner( 0 ) );
129 for( int i = 1; i < geo.corners(); ++i )
130 coord += geo.corner( i );
131 coord /= double( geo.corners() );
132
133 InsertOrderIterator it = elInsertOrder_.find( coord );
134 if( it != elInsertOrder_.end() )
135 return macroGrid_.elParams[ it->second ];
136 assert(0);
137 return emptyParam;
138 }
139
140 std::vector<double>& parameter(const Vertex &vertex)
141 {
142 const typename Vertex::Geometry &geo = vertex.geometry();
143 DomainType coord( geo.corner( 0 ) );
144
145 InsertOrderIterator it = vtxInsertOrder_.find( coord );
146 if( it != vtxInsertOrder_.end() )
147 return macroGrid_.vtxParams[ it->second ];
148 return emptyParam;
149 }
150
151 // return true if boundary parameters found
153 {
154 return false;
155 }
156
157 template< class GG, class II >
158 const typename DGFBoundaryParameter::type &
159 boundaryParameter ( const Intersection< GG, II > & /*intersection*/ ) const
160 {
162 }
163
164 private:
166 struct Compare
167 {
168 bool operator() ( const DomainType &a, const DomainType &b ) const
169 {
170 // returns true, if a < b; c[i] < -eps;
171 const DomainType c = a - b;
172 const double eps = 1e-8;
173
174 for( int i = 0; i < DomainType::dimension; ++i )
175 {
176 if( c[ i ] <= -eps )
177 return true;
178 if( c[ i ] >= eps )
179 return false;
180 }
181 return false;
182 }
183 };
184 typedef std::map< DomainType, size_t, Compare > InsertOrderMap;
185 typedef typename InsertOrderMap::const_iterator InsertOrderIterator;
186
187 MacroGrid macroGrid_;
188 Grid *grid_;
189 InsertOrderMap elInsertOrder_;
190 InsertOrderMap vtxInsertOrder_;
191 std::vector<double> emptyParam;
192 };
193
194} // end namespace Dune
195
196#endif
virtual void operator()()=0
Include standard header files.
static constexpr int dimension
MPI_Comm MPICommunicator
static MPICommunicator getCommunicator()
Definition dgfgridfactory.hh:38
const DGFBoundaryParameter::type & boundaryParameter(const Intersection< GG, II > &) const
Definition dgfgridfactory.hh:159
int numParameters() const
Definition dgfgridfactory.hh:109
int boundaryId(const Intersection &intersection) const
Definition dgfgridfactory.hh:103
std::vector< double > & parameter(const Vertex &vertex)
Definition dgfgridfactory.hh:140
static constexpr bool noFactory
Definition dgfgridfactory.hh:42
MPIHelper::MPICommunicator MPICommunicatorType
Definition dgfgridfactory.hh:41
int numParameters(const Entity &) const
Definition dgfgridfactory.hh:120
bool wasInserted(const Intersection &intersection) const
Definition dgfgridfactory.hh:97
DGFGridFactory(const std::string &filename, MPICommunicatorType comm=MPIHelper::getCommunicator())
Definition dgfgridfactory.hh:51
G Grid
Definition dgfgridfactory.hh:39
static const int dimension
Definition dgfgridfactory.hh:40
Grid * grid()
Definition dgfgridfactory.hh:91
std::vector< double > & parameter(const Element &element)
Definition dgfgridfactory.hh:125
bool haveBoundaryParameters() const
Definition dgfgridfactory.hh:152
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition common/intersection.hh:164
bool boundary() const
Return true if intersection is with interior or exterior boundary (see the cases above)
Definition common/intersection.hh:216
int indexInInside() const
Local index of codim 1 entity in the inside() entity where intersection is contained in.
Definition common/intersection.hh:351
Wrapper class for entities.
Definition common/entity.hh:66
static constexpr int dimension
The dimension of the grid.
Definition common/grid.hh:407
static const type & defaultValue()
default constructor
Definition parser.hh:28
int nofvtxparams
Definition parser.hh:163
std::vector< std::vector< double > > vtxParams
Definition parser.hh:165
int nofelparams
Definition parser.hh:163
std::vector< std::vector< double > > elParams
Definition parser.hh:165
std::vector< std::vector< double > > vtx
Definition parser.hh:125
std ::vector< std ::vector< unsigned int > > elements
Definition parser.hh:134
T end(T... args)
T find(T... args)
T insert(T... args)
T make_pair(T... args)
T size(T... args)