dune-pdelab  2.5-dev
aliasedmatrixview.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil -*-
2 #ifndef DUNE_PDELAB_BACKEND_COMMON_ALIASEDMATRIXVIEW_HH
3 #define DUNE_PDELAB_BACKEND_COMMON_ALIASEDMATRIXVIEW_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 
56 
58  : _container(nullptr)
59  , _row_cache(nullptr)
60  , _col_cache(nullptr)
61  , _data(nullptr)
62  {}
63 
66  , _row_cache(nullptr)
67  , _col_cache(nullptr)
68  , _data(nullptr)
69  {}
70 
72  {
73  assert(_row_cache);
74  return *_row_cache;
75  }
76 
78  {
79  assert(_col_cache);
80  return *_col_cache;
81  }
82 
83  void attach(M_& container)
84  {
86  }
87 
88  void detach()
89  {
90  _container = nullptr;
91  }
92 
93  void bind(const RowCache& row_cache, const ColCache& col_cache)
94  {
95  _row_cache = &row_cache;
96  _col_cache = &col_cache;
97  _data = _container->data(row_cache,col_cache);
98  }
99 
100  void unbind()
101  {
102  _row_cache = nullptr;
103  _col_cache = nullptr;
104  _data = nullptr;
105  }
106 
107  size_type N() const
108  {
109  return rowIndexCache().size();
110  }
111 
112  size_type M() const
113  {
114  return colIndexCache().size();
115  }
116 
117  template<typename LC>
118  void read(LC& local_container) const
119  {
120  for (size_type i = 0; i < N(); ++i)
121  for (size_type j = 0; j < M(); ++j)
122  local_container.getEntry(i,j) = container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j));
123  }
124 
126  {
127  return _data[i * colIndexCache().size() + j];
128  }
129 
130  const ElementType& operator()(const RowDOFIndex& i, const ColDOFIndex& j) const
131  {
132  return container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j));
133  }
134 
136  {
137  return container()(i,j);
138  }
139 
140  template<typename LFSV, typename LFSU>
141  const ElementType& operator()(const LFSV& lfsv, size_type i, const LFSU& lfsu, size_type j) const
142  {
143  return _data[lfsv.localIndex(i) * colIndexCache().size() + lfsu.localIndex(j)];
144  }
145 
146  const Container& container() const
147  {
148  return *_container;
149  }
150 
151  protected:
152 
154  const RowCache* _row_cache;
155  const ColCache* _col_cache;
156  typename std::conditional<
158  const ElementType*,
159  ElementType*
160  >::type _data;
161 
162  };
163 
164 
165  template<typename M_, typename RowCache, typename ColCache>
167  : public ConstAliasedMatrixView<M_,RowCache,ColCache>
168  {
169 
171 
172  public:
173 
174  typedef M_ Container;
175  typedef typename Container::ElementType ElementType;
176  typedef typename Container::size_type size_type;
177 
178  typedef RowCache RowIndexCache;
179  typedef ColCache ColIndexCache;
180 
181  typedef typename RowCache::LocalFunctionSpace LFSV;
182  typedef typename ColCache::LocalFunctionSpace LFSU;
183 
184  typedef typename LFSV::Traits::DOFIndex RowDOFIndex;
185  typedef typename LFSV::Traits::GridFunctionSpace::Ordering::Traits::ContainerIndex RowContainerIndex;
186 
187  typedef typename LFSU::Traits::DOFIndex ColDOFIndex;
188  typedef typename LFSU::Traits::GridFunctionSpace::Ordering::Traits::ContainerIndex ColContainerIndex;
189 
190  using BaseT::rowIndexCache;
191  using BaseT::colIndexCache;
192  using BaseT::N;
193  using BaseT::M;
194 
195  using typename BaseT::value_type;
196  using typename BaseT::weight_type;
197 
198  // Explicitly pull in operator() from the base class to work around a problem
199  // with clang not finding the const overloads of the operator from the base class.
200  using BaseT::operator();
201 
203  : weight_(1.0)
204  {}
205 
207  : BaseT(container)
208  , weight_(1.0)
209  {}
210 
211  void commit()
212  {}
213 
214  template<typename LC>
215  void write(const LC& local_container)
216  {
217  for (size_type i = 0; i < N(); ++i)
218  for (size_type j = 0; j < M(); ++j)
219  container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j)) = local_container.getEntry(i,j);
220  }
221 
222  template<typename LC>
223  void add(const LC& local_container)
224  {
225  for (size_type i = 0; i < N(); ++i)
226  for (size_type j = 0; j < M(); ++j)
227  container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j)) += local_container.getEntry(i,j);
228  }
229 
230 
231 
233  {
234  return container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j));
235  }
236 
238  {
239  return container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j));
240  }
241 
243  {
244  return container()(i,j);
245  }
246 
248  {
249  return container()(i,colIndexCache().containerIndex(j));
250  }
251 
253  {
254  return container()(rowIndexCache().containerIndex(i),j);
255  }
256 
257  template<typename LFSV, typename LFSU>
258  void accumulate(const LFSV& lfsv, size_type i, const LFSU& lfsu, size_type j, value_type value)
259  {
260  this->_data[lfsv.localIndex(i) * colIndexCache().size() + lfsu.localIndex(j)] += value;
261  }
262 
263  template<typename LFSV, typename LFSU>
264  void rawAccumulate(const LFSV& lfsv, size_type i, const LFSU& lfsu, size_type j, value_type value)
265  {
266  this->_data[lfsv.localIndex(i) * colIndexCache().size() + lfsu.localIndex(j)] += value;
267  }
268 
270  {
271  return this->_data;
272  }
273 
274 
275  Container& container()
276  {
277  return *(this->_container);
278  }
279 
281  {
282  weight_ = weight;
283  }
284 
286  {
287  return weight_;
288  }
289 
290  private :
291  weight_type weight_;
292  };
293 
294 
295  } // namespace PDELab
296 } // namespace Dune
297 
298 #endif // DUNE_PDELAB_BACKEND_COMMON_ALIASEDMATRIXVIEW_HH
Dune::PDELab::AliasedMatrixView::operator()
ElementType & operator()(size_type i, const ColContainerIndex &j)
Definition: aliasedmatrixview.hh:252
Dune::PDELab::ConstAliasedMatrixView::_container
M_ * _container
Definition: aliasedmatrixview.hh:153
Dune::PDELab::ConstAliasedMatrixView::rowIndexCache
const RowIndexCache & rowIndexCache() const
Definition: aliasedmatrixview.hh:71
Dune::PDELab::AliasedMatrixView::container
Container & container()
Definition: aliasedmatrixview.hh:275
Dune::PDELab::ConstAliasedMatrixView::ColIndexCache
ColCache ColIndexCache
Definition: aliasedmatrixview.hh:43
Dune::PDELab::ConstAliasedMatrixView::unbind
void unbind()
Definition: aliasedmatrixview.hh:100
Dune::PDELab::ConstAliasedMatrixView::ColDOFIndex
LFSU::Traits::DOFIndex ColDOFIndex
Definition: aliasedmatrixview.hh:51
Dune::PDELab::AliasedMatrixView
Definition: aliasedmatrixview.hh:166
Dune::PDELab::ConstAliasedMatrixView::LFSV
RowCache::LocalFunctionSpace LFSV
Definition: aliasedmatrixview.hh:45
Dune::PDELab::ConstAliasedMatrixView::_row_cache
const RowCache * _row_cache
Definition: aliasedmatrixview.hh:154
Dune::PDELab::ConstAliasedMatrixView::operator()
const ElementType & operator()(size_type i, size_type j) const
Definition: aliasedmatrixview.hh:125
Dune::PDELab::ConstAliasedMatrixView::weight_type
ElementType weight_type
Definition: aliasedmatrixview.hh:55
Dune::PDELab::ConstAliasedMatrixView::E
Container::field_type E
Definition: aliasedmatrixview.hh:25
Dune::PDELab::ConstAliasedMatrixView::operator()
const ElementType & operator()(const RowContainerIndex &i, const ColContainerIndex &j) const
Definition: aliasedmatrixview.hh:135
Dune::PDELab::AliasedMatrixView::data
ElementType * data()
Definition: aliasedmatrixview.hh:269
Dune::PDELab::AliasedMatrixView::RowContainerIndex
LFSV::Traits::GridFunctionSpace::Ordering::Traits::ContainerIndex RowContainerIndex
Definition: aliasedmatrixview.hh:185
Dune::PDELab::AliasedMatrixView::size_type
Container::size_type size_type
Definition: aliasedmatrixview.hh:176
Dune::PDELab::AliasedMatrixView::operator()
ElementType & operator()(const RowContainerIndex &i, const ColContainerIndex &j)
Definition: aliasedmatrixview.hh:242
Dune
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
Dune::PDELab::ConstAliasedMatrixView::detach
void detach()
Definition: aliasedmatrixview.hh:88
Dune::PDELab::AliasedMatrixView::RowIndexCache
RowCache RowIndexCache
Definition: aliasedmatrixview.hh:178
Dune::PDELab::ConstAliasedMatrixView::M
size_type M() const
Definition: aliasedmatrixview.hh:112
Dune::PDELab::ConstAliasedMatrixView
Definition: aliasedmatrixview.hh:12
Dune::PDELab::ConstAliasedMatrixView::size_type
Container::size_type size_type
Definition: aliasedmatrixview.hh:38
Dune::PDELab::AliasedMatrixView::weight
weight_type weight()
Definition: aliasedmatrixview.hh:285
Dune::PDELab::AliasedMatrixView::AliasedMatrixView
AliasedMatrixView(Container &container)
Definition: aliasedmatrixview.hh:206
Dune::PDELab::AliasedMatrixView::write
void write(const LC &local_container)
Definition: aliasedmatrixview.hh:215
Dune::PDELab::ConstAliasedMatrixView::_data
std::conditional< std::is_const< M_ >::value, const ElementType *, ElementType * >::type _data
Definition: aliasedmatrixview.hh:160
Dune::PDELab::AliasedMatrixView::RowDOFIndex
LFSV::Traits::DOFIndex RowDOFIndex
Definition: aliasedmatrixview.hh:184
Dune::PDELab::AliasedMatrixView::commit
void commit()
Definition: aliasedmatrixview.hh:211
Dune::PDELab::ConstAliasedMatrixView::read
void read(LC &local_container) const
Definition: aliasedmatrixview.hh:118
Dune::PDELab::ConstAliasedMatrixView::ElementType
E ElementType
Definition: aliasedmatrixview.hh:40
Dune::PDELab::ConstAliasedMatrixView::operator()
const ElementType & operator()(const LFSV &lfsv, size_type i, const LFSU &lfsu, size_type j) const
Definition: aliasedmatrixview.hh:141
Dune::PDELab::ConstAliasedMatrixView::value_type
ElementType value_type
Definition: aliasedmatrixview.hh:54
Dune::PDELab::AliasedMatrixView::LFSV
RowCache::LocalFunctionSpace LFSV
Definition: aliasedmatrixview.hh:181
Dune::PDELab::ConstAliasedMatrixView::colIndexCache
const ColIndexCache & colIndexCache() const
Definition: aliasedmatrixview.hh:77
Dune::PDELab::AliasedMatrixView::setWeight
void setWeight(weight_type weight)
Definition: aliasedmatrixview.hh:280
Dune::PDELab::ConstAliasedMatrixView::RowDOFIndex
LFSV::Traits::DOFIndex RowDOFIndex
Definition: aliasedmatrixview.hh:48
Dune::PDELab::AliasedMatrixView::operator()
ElementType & operator()(const RowContainerIndex &i, size_type j)
Definition: aliasedmatrixview.hh:247
Dune::PDELab::AliasedMatrixView::ElementType
Container::ElementType ElementType
Definition: aliasedmatrixview.hh:175
Dune::PDELab::ConstAliasedMatrixView::ColContainerIndex
LFSU::Traits::GridFunctionSpace::Ordering::Traits::ContainerIndex ColContainerIndex
Definition: aliasedmatrixview.hh:52
Dune::PDELab::AliasedMatrixView::ColContainerIndex
LFSU::Traits::GridFunctionSpace::Ordering::Traits::ContainerIndex ColContainerIndex
Definition: aliasedmatrixview.hh:188
Dune::PDELab::ConstAliasedMatrixView::operator()
const ElementType & operator()(const RowDOFIndex &i, const ColDOFIndex &j) const
Definition: aliasedmatrixview.hh:130
Dune::PDELab::AliasedMatrixView::ColIndexCache
ColCache ColIndexCache
Definition: aliasedmatrixview.hh:179
Dune::PDELab::ConstAliasedMatrixView::ConstAliasedMatrixView
ConstAliasedMatrixView()
Definition: aliasedmatrixview.hh:57
Dune::PDELab::AliasedMatrixView::ColDOFIndex
LFSU::Traits::DOFIndex ColDOFIndex
Definition: aliasedmatrixview.hh:187
Dune::PDELab::ConstAliasedMatrixView::_col_cache
const ColCache * _col_cache
Definition: aliasedmatrixview.hh:155
Dune::PDELab::ConstAliasedMatrixView::LFSU
ColCache::LocalFunctionSpace LFSU
Definition: aliasedmatrixview.hh:46
Dune::PDELab::AliasedMatrixView::rawAccumulate
void rawAccumulate(const LFSV &lfsv, size_type i, const LFSU &lfsu, size_type j, value_type value)
Definition: aliasedmatrixview.hh:264
Dune::PDELab::ConstAliasedMatrixView::Container
std::remove_const< M_ >::type Container
Definition: aliasedmatrixview.hh:17
Dune::PDELab::ConstAliasedMatrixView::RowContainerIndex
LFSV::Traits::GridFunctionSpace::Ordering::Traits::ContainerIndex RowContainerIndex
Definition: aliasedmatrixview.hh:49
Dune::PDELab::AliasedMatrixView::AliasedMatrixView
AliasedMatrixView()
Definition: aliasedmatrixview.hh:202
Dune::PDELab::AliasedMatrixView::operator()
ElementType & operator()(size_type i, size_type j)
Definition: aliasedmatrixview.hh:232
Dune::PDELab::ConstAliasedMatrixView::container
const Container & container() const
Definition: aliasedmatrixview.hh:146
Dune::PDELab::ConstAliasedMatrixView::attach
void attach(M_ &container)
Definition: aliasedmatrixview.hh:83
Dune::PDELab::AliasedMatrixView::LFSU
ColCache::LocalFunctionSpace LFSU
Definition: aliasedmatrixview.hh:182
value
static const unsigned int value
Definition: gridfunctionspace/tags.hh:139
Dune::PDELab::ConstAliasedMatrixView::N
size_type N() const
Definition: aliasedmatrixview.hh:107
Dune::PDELab::ConstAliasedMatrixView::bind
void bind(const RowCache &row_cache, const ColCache &col_cache)
Definition: aliasedmatrixview.hh:93
Dune::PDELab::ConstAliasedMatrixView::ConstAliasedMatrixView
ConstAliasedMatrixView(M_ &container)
Definition: aliasedmatrixview.hh:64
Dune::PDELab::AliasedMatrixView::accumulate
void accumulate(const LFSV &lfsv, size_type i, const LFSU &lfsu, size_type j, value_type value)
Definition: aliasedmatrixview.hh:258
Dune::PDELab::AliasedMatrixView::add
void add(const LC &local_container)
Definition: aliasedmatrixview.hh:223
Dune::PDELab::AliasedMatrixView::operator()
ElementType & operator()(const RowDOFIndex &i, const ColDOFIndex &j)
Definition: aliasedmatrixview.hh:237
Dune::PDELab::AliasedMatrixView::Container
M_ Container
Definition: aliasedmatrixview.hh:174
Dune::PDELab::ConstAliasedMatrixView::RowIndexCache
RowCache RowIndexCache
Definition: aliasedmatrixview.hh:42