DUNE PDELab (git)

aliasedmatrixview.hh
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
7namespace Dune {
8 namespace PDELab {
9
10
11 template<typename M_, typename RowCache, typename ColCache>
12 class ConstAliasedMatrixView
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
54 using value_type = ElementType;
55 using weight_type = ElementType;
56
57 ConstAliasedMatrixView()
58 : _container(nullptr)
59 , _row_cache(nullptr)
60 , _col_cache(nullptr)
61 , _data(nullptr)
62 {}
63
64 ConstAliasedMatrixView(M_& container)
65 : _container(&container)
66 , _row_cache(nullptr)
67 , _col_cache(nullptr)
68 , _data(nullptr)
69 {}
70
71 const RowIndexCache& rowIndexCache() const
72 {
73 assert(_row_cache);
74 return *_row_cache;
75 }
76
77 const ColIndexCache& colIndexCache() const
78 {
79 assert(_col_cache);
80 return *_col_cache;
81 }
82
83 void attach(M_& container)
84 {
85 _container = &container;
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
125 const ElementType& operator()(size_type i, size_type j) const
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
135 const ElementType& operator()(const RowContainerIndex& i, const ColContainerIndex& j) const
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
153 M_* _container;
154 const RowCache* _row_cache;
155 const ColCache* _col_cache;
156 typename std::conditional<
157 std::is_const<M_>::value,
158 const ElementType*,
159 ElementType*
160 >::type _data;
161
162 };
163
164
165 template<typename M_, typename RowCache, typename ColCache>
166 class AliasedMatrixView
167 : public ConstAliasedMatrixView<M_,RowCache,ColCache>
168 {
169
170 typedef ConstAliasedMatrixView<M_,RowCache,ColCache> BaseT;
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
202 AliasedMatrixView()
203 : weight_(1.0)
204 {}
205
206 AliasedMatrixView(Container& container)
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
232 ElementType& operator()(size_type i, size_type j)
233 {
234 return container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j));
235 }
236
237 ElementType& operator()(const RowDOFIndex& i, const ColDOFIndex& j)
238 {
239 return container()(rowIndexCache().containerIndex(i),colIndexCache().containerIndex(j));
240 }
241
242 ElementType& operator()(const RowContainerIndex& i, const ColContainerIndex& j)
243 {
244 return container()(i,j);
245 }
246
247 ElementType& operator()(const RowContainerIndex& i, size_type j)
248 {
249 return container()(i,colIndexCache().containerIndex(j));
250 }
251
252 ElementType& operator()(size_type i, const ColContainerIndex& j)
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)] += weight_ * 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
269 ElementType* data()
270 {
271 return this->_data;
272 }
273
274
275 Container& container()
276 {
277 return *(this->_container);
278 }
279
280 void setWeight(weight_type weight)
281 {
282 weight_ = weight;
283 }
284
285 weight_type weight()
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
constexpr T accumulate(Range &&range, T value, F &&f)
Accumulate values.
Definition: hybridutilities.hh:279
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)