dune-grid  2.2.1
coordcache.hh
Go to the documentation of this file.
1 #ifndef DUNE_ALBERTA_COORDCACHE_HH
2 #define DUNE_ALBERTA_COORDCACHE_HH
3 
7 
8 #if HAVE_ALBERTA
9 
10 namespace Dune
11 {
12 
13  namespace Alberta
14  {
15 
16  // CoordCache
17  // ----------
18 
19  template< int dim >
20  class CoordCache
21  {
24 
25  class LocalCaching;
26  struct Interpolation;
27 
28  public:
29  static const int dimension = dim;
30 
34 
35  GlobalVector &operator() ( const Element *element, int vertex ) const
36  {
37  assert( !(!coords_) );
38  GlobalVector *array = (GlobalVector *)coords_;
39  return array[ dofAccess_( element, vertex ) ];
40  }
41 
42  GlobalVector &operator() ( const ElementInfo &elementInfo, int vertex ) const
43  {
44  return (*this)( elementInfo.el(), vertex );
45  }
46 
47  void create ( const DofNumbering &dofNumbering )
48  {
49  MeshPointer mesh = dofNumbering.mesh();
50  const DofSpace *dofSpace = dofNumbering.dofSpace( dimension );
51 
52  coords_.create( dofSpace, "Coordinate Cache" );
53  LocalCaching localCaching( coords_ );
55  coords_.template setupInterpolation< Interpolation >();
56 
57  dofAccess_ = DofAccess( dofSpace );
58  }
59 
60  void release ()
61  {
62  coords_.release();
63  }
64 
65  private:
66  CoordVectorPointer coords_;
67  DofAccess dofAccess_;
68  };
69 
70 
71 
72  // CoordCache::LocalCaching
73  // ------------------------
74 
75  template< int dim >
76  class CoordCache< dim >::LocalCaching
77  {
78  CoordVectorPointer coords_;
79  DofAccess dofAccess_;
80 
81  public:
82  explicit LocalCaching ( const CoordVectorPointer &coords )
83  : coords_( coords ),
84  dofAccess_( coords.dofSpace() )
85  {}
86 
87  void operator() ( const ElementInfo &elementInfo ) const
88  {
89  GlobalVector *array = (GlobalVector *)coords_;
90  for( int i = 0; i < DofAccess::numSubEntities; ++i )
91  {
92  const GlobalVector &x = elementInfo.coordinate( i );
93  GlobalVector &y = array[ dofAccess_( elementInfo.el(), i ) ];
94  for( int i = 0; i < dimWorld; ++i )
95  y[ i ] = x[ i ];
96  }
97  }
98  };
99 
100 
101 
102  // CoordCache::Interpolation
103  // -------------------------
104 
105  template< int dim >
106  struct CoordCache< dim >::Interpolation
107  {
108  static const int dimension = dim;
109 
111 
112  static void
113  interpolateVector ( const CoordVectorPointer &dofVector, const Patch &patch )
114  {
115  DofAccess dofAccess( dofVector.dofSpace() );
116  GlobalVector *array = (GlobalVector *)dofVector;
117 
118  const Element *element = patch[ 0 ];
119 
120  // new vertex is always the last one
121  assert( element->child[ 0 ] != NULL );
122  GlobalVector &newCoord = array[ dofAccess( element->child[ 0 ], dimension ) ];
123 
124  if( element->new_coord != NULL )
125  {
126  for( int j = 0; j < dimWorld; ++j )
127  newCoord[ j ] = element->new_coord[ j ];
128  }
129  else
130  {
131  // new coordinate is the average of of old ones on the same edge
132  // refinement edge is always between vertices 0 and 1
133  const GlobalVector &coord0 = array[ dofAccess( element, 0 ) ];
134  const GlobalVector &coord1 = array[ dofAccess( element, 1 ) ];
135  for( int j = 0; j < dimWorld; ++j )
136  newCoord[ j ] = 0.5 * (coord0[ j ] + coord1[ j ]);
137  }
138  }
139  };
140 
141  }
142 
143 }
144 
145 #endif // #if HAVE_ALBERTA
146 
147 #endif // #ifndef DUNE_ALBERTA_COORDCACHE_HH