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