dune-fem  2.4.1-rc
restrictprolongfunction.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_RESTRICTPROLONGFUNCTION_HH
2 #define DUNE_FEM_RESTRICTPROLONGFUNCTION_HH
3 
4 #include <dune/common/exceptions.hh>
5 
6 #include <dune/grid/common/grid.hh>
7 
8 namespace Dune
9 {
10 
11  namespace Fem
12  {
13 
21  template< class LRP >
23  {
25  typedef LRP LocalRestrictProlong;
26 
34  template< class CoarseFunction, class FineFunction >
35  void operator() ( const CoarseFunction &coarseFunction,
36  FineFunction &fineFunction ) const
37  {
38  typedef typename CoarseFunction::LocalFunctionType CoarseLocalFunction;
39  typedef typename CoarseFunction::DiscreteFunctionSpaceType CoarseSpace;
40 
41  typedef typename FineFunction::LocalFunctionType FineLocalFunction;
42 
43  const CoarseSpace &coarseSpace = coarseFunction.space();
44  for( const auto& entity : coarseSpace )
45  {
46  CoarseLocalFunction coarseLocalFunction = coarseFunction.localFunction( entity );
47 
48  if( isDefinedOn( fineFunction, entity ) )
49  {
50  FineLocalFunction fineLocalFunction = fineFunction.localFunction( entity );
51  fineLocalFunction.assign( coarseLocalFunction );
52  }
53  else
54  hierarchicProlong( coarseLocalFunction, fineFunction );
55  }
56  }
57 
58  private:
59  template< class CoarseLocalFunction, class FineFunction >
60  void hierarchicProlong ( const CoarseLocalFunction &coarseLocalFunction,
61  FineFunction &fineFunction ) const
62  {
63  typedef typename CoarseLocalFunction::EntityType Entity;
64  typedef typename Entity::HierarchicIterator HierarchicIterator;
65  typedef typename FineFunction::LocalFunctionType FineLocalFunction;
66 
67  const Entity &father = coarseLocalFunction.entity();
68  const int childLevel = father.level()+1;
69 
70  const HierarchicIterator hend = father.hend( childLevel );
71  for( HierarchicIterator hit = father.hbegin( childLevel ); hit != hend; ++hit )
72  {
73  const Entity &son = *hit;
74  if( isDefinedOn( fineFunction, son ) )
75  {
76  FineLocalFunction fineLocalFunction = fineFunction.localFunction( son );
77  localRestrictProlong_.prolongLocal( coarseLocalFunction, fineLocalFunction, son.geometryInFather(), true );
78  }
79  else
80  DUNE_THROW( GridError, "Cannot prolong over more than one level." );
81  }
82  }
83 
84  template< class Function >
85  static bool isDefinedOn ( const Function &function, const typename Function::GridPartType::template Codim< 0 >::EntityType &entity )
86  {
87  typedef typename Function::GridPartType::IndexSetType IndexSet;
88  const IndexSet &indexSet = function.gridPart().indexSet();
89  return indexSet.contains( entity );
90  }
91 
92  private:
93  LocalRestrictProlong localRestrictProlong_;
94  };
95 
96 
97 
105  template< class LRP >
107  {
109  typedef LRP LocalRestrictProlong;
110 
111  public:
119  template< class FineFunction, class CoarseFunction >
120  void operator() ( const FineFunction &fineFunction,
121  CoarseFunction &coarseFunction ) const
122  {
123  typedef typename CoarseFunction::LocalFunctionType CoarseLocalFunction;
124  typedef typename CoarseFunction::DiscreteFunctionSpaceType CoarseSpace;
125 
126  typedef typename FineFunction::LocalFunctionType FineLocalFunction;
127 
128  const CoarseSpace &coarseSpace = coarseFunction.space();
129  for( const auto& entity : coarseSpace )
130  {
131  CoarseLocalFunction coarseLocalFunction = coarseFunction.localFunction( entity );
132 
133  if( isDefinedOn( fineFunction, entity ) )
134  {
135  FineLocalFunction fineLocalFunction = fineFunction.localFunction( entity );
136  coarseLocalFunction.assign( fineLocalFunction );
137  }
138  else
139  hierarchicRestrict( fineFunction, coarseLocalFunction );
140  }
141  }
142 
143  private:
144  template< class FineFunction, class CoarseLocalFunction >
145  void hierarchicRestrict ( const FineFunction &fineFunction,
146  CoarseLocalFunction &coarseLocalFunction ) const
147  {
148  typedef typename CoarseLocalFunction::EntityType Entity;
149  typedef typename Entity::HierarchicIterator HierarchicIterator;
150  typedef typename FineFunction::LocalFunctionType FineLocalFunction;
151 
152  const Entity &father = coarseLocalFunction.entity();
153  const int childLevel = father.level()+1;
154 
155  bool initialize = true;
156  const HierarchicIterator hend = father.hend( childLevel );
157  for( HierarchicIterator hit = father.hbegin( childLevel ); hit != hend; ++hit )
158  {
159  const Entity &son = *hit;
160  if( isDefinedOn( fineFunction, son ) )
161  {
162  FineLocalFunction fineLocalFunction = fineFunction.localFunction( son );
163  localRestrictProlong_.restrictLocal( coarseLocalFunction, fineLocalFunction, son.geometryInFather(), initialize );
164  }
165  else
166  DUNE_THROW( GridError, "Cannot restrict over more than one level." );
167  initialize = false;
168  }
169  }
170 
171  template< class Function >
172  static bool isDefinedOn ( const Function &function, const typename Function::GridPartType::template Codim< 0 >::EntityType &entity )
173  {
174  typedef typename Function::GridPartType::IndexSetType IndexSet;
175  const IndexSet &indexSet = function.gridPart().indexSet();
176  return indexSet.contains( entity );
177  }
178 
179  private:
180  LocalRestrictProlong localRestrictProlong_;
181  };
182 
183  } // namespace Fem
184 
185 } // namespace Dune
186 
187 #endif // #ifndef DUNE_FEM_RESTRICTPROLONGFUNCTION_HH
interface documentation for (grid part) index sets
Definition: common/indexset.hh:25
bool contains(const Entity &entity) const
return true if entity has index
Definition: common/indexset.hh:147
restrict discrete functions between grid levels
Definition: restrictprolongfunction.hh:106
LRP LocalRestrictProlong
type of the local restriction and prolongation operator
Definition: restrictprolongfunction.hh:109
Definition: coordinate.hh:4
void operator()(const CoarseFunction &coarseFunction, FineFunction &fineFunction) const
prolong a discrete function to finer grid level
Definition: restrictprolongfunction.hh:35
prolong discrete functions between grid levels
Definition: restrictprolongfunction.hh:22
LRP LocalRestrictProlong
type of the local restriction and prolongation operator
Definition: restrictprolongfunction.hh:25
Abstract class representing a function.
Definition: function.hh:43