dune-pdelab  2.5-dev
uncachedmatrixview.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil -*-
2 #ifndef DUNE_PDELAB_BACKEND_COMMON_UNCACHEDMATRIXVIEW_HH
3 #define DUNE_PDELAB_BACKEND_COMMON_UNCACHEDMATRIXVIEW_HH
4 
5 #include <type_traits>
6 
7 namespace Dune {
8  namespace PDELab {
9 
10 
11  template<typename M_, typename RowCache, typename ColCache>
13  {
14 
15  public:
16 
17  typedef typename std::remove_const<M_>::type Container;
18 
19  static_assert(
20  (std::is_same<
21  typename RowCache::LocalFunctionSpace::Traits::GridFunctionSpace,
22  typename Container::TestGridFunctionSpace
23  >::value),
24  "The RowCache passed to LocalView must belong to the underlying GFSV"
25  );
26 
27  static_assert(
28  (std::is_same<
29  typename ColCache::LocalFunctionSpace::Traits::GridFunctionSpace,
30  typename Container::TrialGridFunctionSpace
31  >::value),
32  "The ColCache passed to LocalView must belong to the underlying GFSU"
33  );
34 
35  public:
36 
37  typedef typename Container::field_type E;
38  typedef typename Container::size_type size_type;
39 
40  typedef E ElementType;
41 
42  typedef RowCache RowIndexCache;
43  typedef ColCache ColIndexCache;
44 
45  typedef typename RowCache::LocalFunctionSpace LFSV;
46  typedef typename ColCache::LocalFunctionSpace LFSU;
47 
48  typedef typename LFSV::Traits::DOFIndex RowDOFIndex;
49  typedef typename LFSV::Traits::GridFunctionSpace::Ordering::Traits::ContainerIndex RowContainerIndex;
50 
51  typedef typename LFSU::Traits::DOFIndex ColDOFIndex;
52  typedef typename LFSU::Traits::GridFunctionSpace::Ordering::Traits::ContainerIndex ColContainerIndex;
53 
55  : _container(nullptr)
56  , _row_cache(nullptr)
57  , _col_cache(nullptr)
58  {}
59 
61  : _container(&container)
62  , _row_cache(nullptr)
63  , _col_cache(nullptr)
64  {}
65 
66  const RowIndexCache& rowIndexCache() const
67  {
68  assert(_row_cache);
69  return *_row_cache;
70  }
71 
72  const ColIndexCache& colIndexCache() const
73  {
74  assert(_col_cache);
75  return *_col_cache;
76  }
77 
78  void attach(M_& container)
79  {
81  }
82 
83  void detach()
84  {
85  _container = nullptr;
86  }
87 
88  void bind(const RowCache& row_cache, const ColCache& col_cache)
89  {
90  _row_cache = &row_cache;
91  _col_cache = &col_cache;
92  }
93 
94  void unbind()
95  {}
96 
97  size_type N() const
98  {
99  return rowIndexCache().size();
100  }
101 
102  size_type M() const
103  {
104  return colIndexCache().size();
105  }
106 
107  template<typename LC>
108  void read(LC& local_container) const
109  {
110  for (size_type i = 0; i < N(); ++i)
111  for (size_type j = 0; j < M(); ++j)
112  local_container.getEntry(i,j) = container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j));
113  }
114 
115 
116 
117  const ElementType& operator()(size_type i, size_type j) const
118  {
119  return container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j));
120  }
121 
122  const ElementType& operator()(const RowDOFIndex& i, const ColDOFIndex& j) const
123  {
124  return container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j));
125  }
126 
127  const ElementType& operator()(const RowContainerIndex& i, const ColContainerIndex& j) const
128  {
129  return container()(i,j);
130  }
131 
132  const ElementType& operator()(const RowContainerIndex& i, size_type j) const
133  {
134  return container()(i,colIndexCache().containerIndex(j));
135  }
136 
137  const ElementType& operator()(size_type i, const ColContainerIndex& j) const
138  {
139  return container()(rowIndexCache().containerIndex(i),j);
140  }
141 
142  const Container& container() const
143  {
144  return *_container;
145  }
146 
147  protected:
148 
150  const RowCache* _row_cache;
151  const ColCache* _col_cache;
152 
153  };
154 
155 
156  template<typename M_, typename RowCache, typename ColCache>
158  : public ConstUncachedMatrixView<M_,RowCache,ColCache>
159  {
160 
162 
163  public:
164 
165  typedef M_ Container;
166  typedef typename Container::ElementType ElementType;
167  typedef typename Container::size_type size_type;
168 
169  typedef RowCache RowIndexCache;
170  typedef ColCache ColIndexCache;
171 
172  typedef typename RowCache::LocalFunctionSpace LFSV;
173  typedef typename ColCache::LocalFunctionSpace LFSU;
174 
175  typedef typename LFSV::Traits::DOFIndex RowDOFIndex;
176  typedef typename LFSV::Traits::GridFunctionSpace::Ordering::Traits::ContainerIndex RowContainerIndex;
177 
178  typedef typename LFSU::Traits::DOFIndex ColDOFIndex;
179  typedef typename LFSU::Traits::GridFunctionSpace::Ordering::Traits::ContainerIndex ColContainerIndex;
180 
181  using BaseT::rowIndexCache;
182  using BaseT::colIndexCache;
183  using BaseT::N;
184  using BaseT::M;
185 
186  // Explicitly pull in operator() from the base class to work around a problem
187  // with clang not finding the const overloads of the operator from the base class.
188  using BaseT::operator();
189 
191  {}
192 
194  : BaseT(container)
195  {}
196 
197  void commit()
198  {}
199 
200  template<typename LC>
201  void write(const LC& local_container)
202  {
203  for (size_type i = 0; i < N(); ++i)
204  for (size_type j = 0; j < M(); ++j)
205  container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j)) = local_container.getEntry(i,j);
206  }
207 
208  template<typename LC>
209  void add(const LC& local_container)
210  {
211  for (size_type i = 0; i < N(); ++i)
212  for (size_type j = 0; j < M(); ++j)
213  container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j)) += local_container.getEntry(i,j);
214  }
215 
216 
217 
218  ElementType& operator()(size_type i, size_type j)
219  {
220  return container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j));
221  }
222 
223  ElementType& operator()(const RowDOFIndex& i, const ColDOFIndex& j)
224  {
225  return container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j));
226  }
227 
228  ElementType& operator()(const RowContainerIndex& i, const ColContainerIndex& j)
229  {
230  return container()(i,j);
231  }
232 
233  ElementType& operator()(const RowContainerIndex& i, size_type j)
234  {
235  return container()(i,colIndexCache().containerIndex(j));
236  }
237 
238  ElementType& operator()(size_type i, const ColContainerIndex& j)
239  {
240  return container()(rowIndexCache().containerIndex(i),j);
241  }
242 
243  void add(size_type i, size_type j, const ElementType& v)
244  {
245  container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j)) += v;
246  }
247 
248  void add(const RowDOFIndex& i, const ColDOFIndex& j, const ElementType& v)
249  {
250  container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j)) += v;
251  }
252 
253  void add(const RowContainerIndex& i, const ColContainerIndex& j, const ElementType& v)
254  {
255  container()(i,j) += v;
256  }
257 
258  void add(const RowContainerIndex& i, size_type j, const ElementType& v)
259  {
260  container()(i,colIndexCache().containerIndex(j)) += v;
261  }
262 
263  void add(size_type i, const ColContainerIndex& j, const ElementType& v)
264  {
265  container()(rowIndexCache().containerIndex(i),j) += v;
266  }
267 
268  Container& container()
269  {
270  return *(this->_container);
271  }
272 
273  };
274 
275 
276  } // namespace PDELab
277 } // namespace Dune
278 
279 #endif // DUNE_PDELAB_BACKEND_COMMON_UNCACHEDMATRIXVIEW_HH
LFSU::Traits::DOFIndex ColDOFIndex
Definition: uncachedmatrixview.hh:51
M_ * _container
Definition: uncachedmatrixview.hh:149
ConstUncachedMatrixView(M_ &container)
Definition: uncachedmatrixview.hh:60
ElementType & operator()(const RowDOFIndex &i, const ColDOFIndex &j)
Definition: uncachedmatrixview.hh:223
ColCache::LocalFunctionSpace LFSU
Definition: uncachedmatrixview.hh:46
LFSV::Traits::GridFunctionSpace::Ordering::Traits::ContainerIndex RowContainerIndex
Definition: uncachedmatrixview.hh:176
const RowIndexCache & rowIndexCache() const
Definition: uncachedmatrixview.hh:66
const ElementType & operator()(const RowContainerIndex &i, size_type j) const
Definition: uncachedmatrixview.hh:132
RowCache::LocalFunctionSpace LFSV
Definition: uncachedmatrixview.hh:172
UncachedMatrixView()
Definition: uncachedmatrixview.hh:190
RowCache RowIndexCache
Definition: uncachedmatrixview.hh:42
Container::ElementType ElementType
Definition: uncachedmatrixview.hh:166
const ElementType & operator()(const RowDOFIndex &i, const ColDOFIndex &j) const
Definition: uncachedmatrixview.hh:122
void detach()
Definition: uncachedmatrixview.hh:83
const RowCache * _row_cache
Definition: uncachedmatrixview.hh:150
void attach(M_ &container)
Definition: uncachedmatrixview.hh:78
RowCache::LocalFunctionSpace LFSV
Definition: uncachedmatrixview.hh:45
UncachedMatrixView(Container &container)
Definition: uncachedmatrixview.hh:193
Container & container()
Definition: uncachedmatrixview.hh:268
size_type N() const
Definition: uncachedmatrixview.hh:97
LFSV::Traits::GridFunctionSpace::Ordering::Traits::ContainerIndex RowContainerIndex
Definition: uncachedmatrixview.hh:49
Definition: uncachedmatrixview.hh:12
void add(const LC &local_container)
Definition: uncachedmatrixview.hh:209
void unbind()
Definition: uncachedmatrixview.hh:94
const ElementType & operator()(size_type i, size_type j) const
Definition: uncachedmatrixview.hh:117
const Container & container() const
Definition: uncachedmatrixview.hh:142
For backward compatibility – Do not use this!
Definition: adaptivity.hh:27
LFSV::Traits::DOFIndex RowDOFIndex
Definition: uncachedmatrixview.hh:175
LFSU::Traits::GridFunctionSpace::Ordering::Traits::ContainerIndex ColContainerIndex
Definition: uncachedmatrixview.hh:179
LFSV::Traits::DOFIndex RowDOFIndex
Definition: uncachedmatrixview.hh:48
void add(const RowContainerIndex &i, const ColContainerIndex &j, const ElementType &v)
Definition: uncachedmatrixview.hh:253
E ElementType
Definition: uncachedmatrixview.hh:40
const ColCache * _col_cache
Definition: uncachedmatrixview.hh:151
RowCache RowIndexCache
Definition: uncachedmatrixview.hh:169
const ColIndexCache & colIndexCache() const
Definition: uncachedmatrixview.hh:72
std::remove_const< M_ >::type Container
Definition: uncachedmatrixview.hh:17
static const unsigned int value
Definition: gridfunctionspace/tags.hh:139
ColCache ColIndexCache
Definition: uncachedmatrixview.hh:43
Container::field_type E
Definition: uncachedmatrixview.hh:25
void commit()
Definition: uncachedmatrixview.hh:197
ElementType & operator()(const RowContainerIndex &i, const ColContainerIndex &j)
Definition: uncachedmatrixview.hh:228
ElementType & operator()(size_type i, const ColContainerIndex &j)
Definition: uncachedmatrixview.hh:238
Definition: uncachedmatrixview.hh:157
Container::size_type size_type
Definition: uncachedmatrixview.hh:38
void read(LC &local_container) const
Definition: uncachedmatrixview.hh:108
size_type M() const
Definition: uncachedmatrixview.hh:102
void add(const RowDOFIndex &i, const ColDOFIndex &j, const ElementType &v)
Definition: uncachedmatrixview.hh:248
void add(const RowContainerIndex &i, size_type j, const ElementType &v)
Definition: uncachedmatrixview.hh:258
void add(size_type i, const ColContainerIndex &j, const ElementType &v)
Definition: uncachedmatrixview.hh:263
const ElementType & operator()(const RowContainerIndex &i, const ColContainerIndex &j) const
Definition: uncachedmatrixview.hh:127
void add(size_type i, size_type j, const ElementType &v)
Definition: uncachedmatrixview.hh:243
ColCache::LocalFunctionSpace LFSU
Definition: uncachedmatrixview.hh:173
ElementType & operator()(const RowContainerIndex &i, size_type j)
Definition: uncachedmatrixview.hh:233
LFSU::Traits::GridFunctionSpace::Ordering::Traits::ContainerIndex ColContainerIndex
Definition: uncachedmatrixview.hh:52
Container::size_type size_type
Definition: uncachedmatrixview.hh:167
const ElementType & operator()(size_type i, const ColContainerIndex &j) const
Definition: uncachedmatrixview.hh:137
ColCache ColIndexCache
Definition: uncachedmatrixview.hh:170
void write(const LC &local_container)
Definition: uncachedmatrixview.hh:201
M_ Container
Definition: uncachedmatrixview.hh:165
LFSU::Traits::DOFIndex ColDOFIndex
Definition: uncachedmatrixview.hh:178
void bind(const RowCache &row_cache, const ColCache &col_cache)
Definition: uncachedmatrixview.hh:88
ElementType & operator()(size_type i, size_type j)
Definition: uncachedmatrixview.hh:218
ConstUncachedMatrixView()
Definition: uncachedmatrixview.hh:54