DUNE-FEM (unstable)

fmatrixconverter.hh
1#ifndef DUNE_FEM_FIELDMATRIXCONVERTER_HH
2#define DUNE_FEM_FIELDMATRIXCONVERTER_HH
3
4#include <cassert>
5
7
8namespace Dune
9{
10
11 namespace Fem
12 {
13
14 // Internal Forward Declarations
15 // -----------------------------
16
17 template< class VectorType, class ConvertToType >
18 class FieldMatrixConverter;
19
20 template< class K, int m >
21 class FieldMatrixConverterRow;
22
23 template< class K, int n, int m >
24 class FlatFieldMatrix;
25 }
26
27
28 template< class K, int n, int m >
29 struct DenseMatVecTraits< Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > >
30 {
31 typedef Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > derived_type;
32 typedef DenseMatVecTraits< FieldMatrix< K, n, m > > Traits;
33 typedef typename Traits::container_type container_type;
34 typedef typename Traits::value_type value_type;
35 typedef typename Traits::size_type size_type;
36 typedef typename Traits::row_type row_type;
37
38 typedef Fem::FieldMatrixConverterRow< K, m > row_reference;
39 typedef Fem::FieldMatrixConverterRow< K, m > const_row_reference;
40 };
41
42 template< class K, int m >
43 struct DenseMatVecTraits< Fem::FieldMatrixConverterRow< K, m > >
44 {
45 typedef Fem::FieldMatrixConverterRow< K, m > derived_type;
46 typedef K *container_type;
47 typedef K value_type;
48 typedef size_t size_type;
49 };
50
51 template< class K, int n, int m >
52 struct DenseMatVecTraits< Fem::FlatFieldMatrix< K, n, m > >
53 : public DenseMatVecTraits< Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > >
54 {
55 };
56
57
58
59 namespace Fem
60 {
61
62 // derive from dense vector to inherit functionality
63 template< class K, int m >
64 class FieldMatrixConverterRow
65 : public Dune::DenseVector< FieldMatrixConverterRow< K, m > >
66 {
67 typedef FieldMatrixConverterRow< K, m > This;
69
70 public:
71 typedef typename Base::size_type size_type;
72
73 using Base::operator=;
74 using Base::operator*;
75
76 FieldMatrixConverterRow ( K *ptr )
77 : ptr_( ptr )
78 {
79 assert( ptr_ );
80 }
81
82 FieldMatrixConverterRow ( const This &other )
83 : ptr_( other.ptr_ )
84 {}
85
86 FieldMatrixConverterRow &operator= ( const This &other )
87 {
88 for( size_t i = 0; i < size(); ++i )
89 ptr_[ i ] = other[ i ];
90 return *this;
91 }
92
93 template< class Impl >
94 FieldMatrixConverterRow &operator= ( const DenseVector< Impl > &other )
95 {
96 assert( other.size() == size() );
97 for( size_t i = 0; i < size(); ++i )
98 ptr_[ i ] = other[ i ];
99 return *this;
100 }
101
102 template< class V >
103 K operator* ( const DenseVector< V > &x ) const
104 {
105 assert( size() == x.size() );
106 K result( 0 );
107 for( size_type i = 0; i < size(); ++i )
108 result += ptr_[ i ] * x[ i ];
109 return result;
110 }
111
112 // make this thing a vector
113 size_t size () const { return m; }
114 K &operator[] ( size_t i ) { assert( i < size() ); return ptr_[ i ]; }
115 const K &operator[] ( size_t i ) const { assert( i < size() ); return ptr_[ i ]; }
116
117 private:
118 K *ptr_;
119 };
120
121
122
124 template< typename K, int n, int m >
125 class FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > >
127 {
129
130 public:
133
135 //typedef FieldMatrixConverterRow< K , m> RowType;
136
137 typedef typename Base::row_type row_type;
138 typedef typename Base::row_reference row_reference;
139 typedef typename Base::const_row_reference const_row_reference;
140
142 typedef K field_type;
143
145 typedef K block_type;
146
148 typedef std::size_t size_type;
149
150 enum {
152 rows = n,
154 cols = m,
156 dimension = n * m
157 };
158
159 FieldMatrixConverter ( InteralVectorType &v )
160 : vec_( &v )
161#ifndef NDEBUG
162 , mutableVec_( true )
163#endif
164 {}
165
166 FieldMatrixConverter ( const InteralVectorType &v )
167 : vec_( const_cast< InteralVectorType * >( &v ) )
168#ifndef NDEBUG
169 , mutableVec_( false )
170#endif
171 {}
172
173 FieldMatrixConverter ( const FieldMatrixConverter &other )
174 : vec_( other.vec_ )
175#ifndef NDEBUG
176 , mutableVec_( other.mutableVec_ )
177#endif
178 {}
179
180 FieldMatrixConverter &operator= ( const FieldMatrixConverter &other )
181 {
182 assert( mutableVec_ );
183 vec_ = other.vec_;
184 #ifndef NDEBUG
185 mutableVec_ = other.mutableVec_;
186 #endif
187 return *this;
188 }
189
190 FieldMatrixConverter &operator= ( const FieldMatrix< K, n, m > &matrix )
191 {
192 assert( mutableVec_ );
193 for( int i = 0; i<rows; ++i )
194 {
195 for( int j = 0, ij = i * cols; j<cols; ++j, ++ij )
196 {
197 (*vec_)[ ij ] = matrix[ i ][ j ];
198 }
199 }
200
201 return *this;
202 }
203
204 FieldMatrixConverter &operator+= ( const FieldMatrix< K, n, m > &matrix )
205 {
206 assert( mutableVec_ );
207 for( int i = 0; i<rows; ++i )
208 {
209 for( int j = 0, ij = i * cols; j<cols; ++j, ++ij )
210 {
211 (*vec_)[ ij ] += matrix[ i ][ j ];
212 }
213 }
214 return *this;
215 }
216
218 friend std::ostream &operator<< ( std::ostream &s,
219 const FieldMatrixConverter &a )
220 {
221 for( size_type i = 0; i < n; ++i )
222 {
223 for( size_type j = 0; j < m; ++j )
224 s << a[ i ][ j ] << " ";
225 s << std::endl;
226 }
227 return s;
228 }
229
230 // make this thing a matrix
231 size_type mat_rows () const { return rows; }
232 size_type mat_cols () const { return cols; }
233
234 row_reference mat_access ( size_type i )
235 {
236 assert( i < rows );
237 return row_reference( (&(*vec_)[ i * cols ]) );
238 }
239
240 const_row_reference mat_access ( size_type i ) const
241 {
242 assert( i < rows );
243 return const_row_reference( (&(*vec_)[ i * cols ]) );
244 }
245
246 protected:
247 InteralVectorType *vec_;
248 #ifndef NDEBUG
249 bool mutableVec_;
250 #endif
251 };
252
253 template< typename K, int n, int m >
254 class FlatFieldMatrix
255 : public Dune::DenseMatrix< FlatFieldMatrix< K, n, m > >
256 {
258
259 public:
261 typedef FieldVector< K, n * m > InteralVectorType;
262
264 typedef typename Base::row_type row_type;
265 typedef typename Base::row_reference row_reference;
266 typedef typename Base::const_row_reference const_row_reference;
267
269 typedef K field_type;
270
272 typedef K block_type;
273
275 typedef std::size_t size_type;
276
277 enum {
279 rows = n,
281 cols = m,
283 dimension = n * m
284 };
285
286 public:
287 FlatFieldMatrix() {}
288 FlatFieldMatrix( const K& val )
289 {
290 // all entries == val
291 data_ = val;
292 }
293
294 FlatFieldMatrix( const FlatFieldMatrix& other )
295 {
296 data_ = other.data_;
297 }
298
299 FlatFieldMatrix( const FieldMatrix< K, n, m >& other )
300 {
301 assign( other );
302 }
303
304 FlatFieldMatrix &operator= ( const FlatFieldMatrix &other )
305 {
306 data_ = other.data_;
307 return *this;
308 }
309
310 FlatFieldMatrix &operator= ( const K& val )
311 {
312 data_ = val;
313 return *this;
314 }
315
316 FlatFieldMatrix &operator= ( const FieldMatrix< K, n, m > &other )
317 {
318 assign( other );
319 return *this;
320 }
321
322 FlatFieldMatrix &operator+= ( const FieldMatrix< K, n, m > &other )
323 {
324 for( int i = 0; i<rows; ++i )
325 {
326 for( int j = 0, ij = i * cols; j<cols; ++j, ++ij )
327 data_[ ij ] += other[ i ][ j ];
328 }
329 return *this;
330 }
331
332 void assign( const FieldMatrix< K, n, m > &other )
333 {
334 for( int i = 0; i<rows; ++i )
335 {
336 for( int j = 0, ij = i * cols; j<cols; ++j, ++ij )
337 data_[ ij ] = other[ i ][ j ];
338 }
339 }
340
341 K* data() { return &data_[ 0 ]; }
342 const K* data() const { return &data_[ 0 ]; }
343
344 // make this thing a matrix
345 size_type mat_rows () const { return rows; }
346 size_type mat_cols () const { return cols; }
347
348 row_reference mat_access ( size_type i )
349 {
350 assert( i < rows );
351 return row_reference( &(data_[ i * cols ]) );
352 }
353
354 const_row_reference mat_access ( size_type i ) const
355 {
356 assert( i < rows );
357 return const_row_reference( &(data_[ i * cols ]) );
358 }
359
361 friend std::ostream &operator<< ( std::ostream &s,
362 const FlatFieldMatrix &a )
363 {
364 for( size_type i = 0; i < n; ++i )
365 {
366 for( size_type j = 0; j < m; ++j )
367 s << a[ i ][ j ] << " ";
368 s << std::endl;
369 }
370 return s;
371 }
372
373 protected:
374 InteralVectorType data_;
375 };
376
377
378 } // namespace Fem
379
380
381 namespace detail {
382
383 template < class K, int n, int m, class Converter>
384 inline void toMatrix( FieldMatrix< K, n, m>& A, const Converter& B )
385 {
386 for( int i = 0; i < n; ++i )
387 for( int j = 0; j < m; ++j )
388 A[ i ][ j ] = B[ i ][ j ];
389 }
390 }
391
392 // method that implements the operator= of a FieldMatrix taking a FieldMatrixConverter
393 template< class K, int n, int m >
394 struct DenseMatrixAssigner< FieldMatrix< K, n, m >,
395 Fem::FieldMatrixConverter< FieldVector< K, n*m >, FieldMatrix< K, n, m > > >
396 {
397 static void apply ( FieldMatrix< K, n, m > &A,
398 const Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > &B )
399 {
400 detail::toMatrix( A, B );
401 }
402 };
403
404 template< class K, int n, int m >
405 struct DenseMatrixAssigner< DenseMatrix< FieldMatrix< K, n, m > >,
406 Fem::FieldMatrixConverter< FieldVector< K, n*m >, FieldMatrix< K, n, m > > >
407 {
408 static void apply ( DenseMatrix< FieldMatrix< K, n, m > > &A,
409 const Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > &B )
410 {
411 detail::toMatrix( A, B );
412 }
413 };
414
415 // method that implements the operator= of a FieldMatrix taking a FlatFieldMatrix
416 template< class K, int n, int m >
417 struct DenseMatrixAssigner< FieldMatrix< K, n, m >, Fem::FlatFieldMatrix< K, n, m > >
418 {
419 static void apply ( FieldMatrix< K, n, m > &A,
420 const Fem::FlatFieldMatrix< K, n, m > &B )
421 {
422 detail::toMatrix( A, B );
423 }
424 };
425
426 template< class K, int n, int m >
427 struct DenseMatrixAssigner< DenseMatrix< FieldMatrix< K, n, m > >, Fem::FlatFieldMatrix< K, n, m > >
428 {
429 static void apply ( DenseMatrix< FieldMatrix< K, n, m > > &A,
430 const Fem::FlatFieldMatrix< K, n, m > &B )
431 {
432 detail::toMatrix( A, B );
433 }
434 };
435
436} // namespace Dune
437
438#endif // #ifndef DUNE_FEM_FIELDMATRIXCONVERTER_HH
A dense n x m matrix.
Definition: densematrix.hh:140
constexpr size_type cols() const
number of columns
Definition: densematrix.hh:715
Traits::row_type row_type
The type used to represent a row (must fulfill the Dune::DenseVector interface)
Definition: densematrix.hh:169
Traits::const_row_reference const_row_reference
The type used to represent a reference to a constant row (usually const row_type &)
Definition: densematrix.hh:175
Traits::row_reference row_reference
The type used to represent a reference to a row (usually row_type &)
Definition: densematrix.hh:172
Interface for a class of dense vectors over a given field.
Definition: densevector.hh:229
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densevector.hh:259
K block_type
export the type representing the components
Definition: fmatrixconverter.hh:145
std::size_t size_type
The type used for the index access and size operations.
Definition: fmatrixconverter.hh:148
K field_type
export the type representing the field
Definition: fmatrixconverter.hh:142
FieldVector< K, n *m > InteralVectorType
internal storage of matrix
Definition: fmatrixconverter.hh:132
Base::row_type row_type
type of class return upon operator [] which behaves like a reference
Definition: fmatrixconverter.hh:137
vector space out of a tensor product of fields.
Definition: fvector.hh:95
Implements a matrix constructed from a given type representing a field and compile-time given number ...
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
void assign(T &dst, const T &src, bool mask)
masked Simd assignment (scalar version)
Definition: simd.hh:447
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 27, 22:29, 2024)