FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
power_full_matrix.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
9#include <kernel/lafem/power_row_matrix.hpp>
10#include <kernel/lafem/power_col_matrix.hpp>
11#include <kernel/lafem/power_vector.hpp>
12#include <kernel/lafem/sparse_layout.hpp>
13#include <kernel/lafem/container.hpp>
14
15
16#include <fstream>
17
18namespace FEAT
19{
20 namespace LAFEM
21 {
23 namespace Intern
24 {
25 // helper class for extract_diag function of PowerFullMatrix
26 template<int n_>
27 struct ExtDiagPFM;
28 }
30
48 template<
49 typename SubType_,
50 int width_,
51 int height_>
53 {
54 static_assert((width_ > 0) && (height_ > 0), "invalid matrix dimensions");
55
56 public:
60 typedef SubType_ SubMatrixType;
62 typedef typename SubMatrixType::DataType DataType;
64 typedef typename SubMatrixType::IndexType IndexType;
66 static constexpr SparseLayoutId layout_id = SubMatrixType::layout_id;
72 template <typename DT2_ = DataType, typename IT2_ = IndexType>
74 typename SubType_::template ContainerType<DT2_, IT2_>, width_, height_>;
75
77 static constexpr int num_row_blocks = height_;
79 static constexpr int num_col_blocks = width_;
80
81 protected:
82 // the container
83 ContClass _container;
84
85 protected:
87 explicit PowerFullMatrix(ContClass&& cont) :
88 _container(std::move(cont))
89 {
90 }
91
92 public:
95 {
96 }
97
100 _container(layout)
101 {
102 }
103
106 _container(std::move(other._container))
107 {
108 }
109
118 explicit PowerFullMatrix(FileMode mode, const String& filename)
119 {
120 ContClass other(mode, filename);
121 _container = std::move(other);
122 }
123
132 explicit PowerFullMatrix(FileMode mode, std::istream& file, const String& directory = "")
133 {
134 ContClass other(mode, file, directory);
135 _container = std::move(other);
136 }
137
144 void read_from(FileMode mode, const String& filename)
145 {
146 ContClass other(mode, filename);
147 _container = std::move(other);
148 }
149
152 {
153 if(this != &other)
154 {
155 _container = std::move(other._container);
156 }
157 return *this;
158 }
159
164
167 {
168 }
169
176 void write_out(FileMode mode, const String& filename) const
177 {
178 _container.write_out(mode, filename);
179 }
180
185 {
186 return PowerFullMatrix(_container.clone(mode));
187 }
188
190 std::size_t bytes() const
191 {
192 return _container.bytes();
193 }
194
207 template<int i_, int j_>
209 {
210 static_assert((0 <= i_) && (i_ < height_), "invalid sub-matrix index");
211 static_assert((0 <= j_) && (j_ < width_), "invalid sub-matrix index");
212 return _container.template at<i_, 0>().template at<0, j_>();
213 }
214
216 template<int i_, int j_>
217 const SubMatrixType& at() const
218 {
219 static_assert((0 <= i_) && (i_ < height_), "invalid sub-matrix index");
220 static_assert((0 <= j_) && (j_ < width_), "invalid sub-matrix index");
221 return _container.template at<i_, 0>().template at<0, j_>();
222 }
223
233 SubMatrixType& get(int i, int j)
234 {
235 XASSERTM((0 <= i) && (i < height_), "invalid sub-matrix row index");
236 XASSERTM((0 <= j) && (j < width_), "invalid sub-matrix column index");
237 return _container.get(i, 0).get(0, j);
238 }
239
241 const SubMatrixType& get(int i, int j) const
242 {
243 XASSERTM((0 <= i) && (i < height_), "invalid sub-matrix row index");
244 XASSERTM((0 <= j) && (j < width_), "invalid sub-matrix column index");
245 return _container.get(i, 0).get(0, j);
246 }
247
249 int row_blocks() const
250 {
251 return num_row_blocks;
252 }
253
254 int col_blocks() const
255 {
256 return num_col_blocks;
257 }
258
259 Index get_length_of_line(const Index row) const
260 {
261 return _container.get_length_of_line(row);
262 }
263
264 void set_line(const Index row, DataType * const pval_set, IndexType * const pcol_set,
265 const Index col_start, const Index stride = 1) const
266 {
267 _container.set_line(row, pval_set, pcol_set, col_start, stride);
268 }
269
270 void set_line_reverse(const Index row, DataType * const pval_set, const Index stride = 1)
271 {
272 _container.set_line_reverse(row, pval_set, stride);
273 }
274
275 ContClass& get_container()
276 {
277 return _container;
278 }
279
280 const ContClass& get_container() const
281 {
282 return _container;
283 }
285
286 VectorTypeL create_vector_l() const
287 {
288 return _container.create_vector_l();
289 }
290
291 VectorTypeR create_vector_r() const
292 {
293 return _container.create_vector_r();
294 }
295
302 template <Perspective perspective_ = Perspective::native>
303 Index rows() const
304 {
305 return _container.template rows<perspective_>();
306 }
307
314 template <Perspective perspective_ = Perspective::native>
316 {
317 return _container.template columns<perspective_>();
318 }
319
326 template <Perspective perspective_ = Perspective::native>
328 {
329 return _container.template used_elements<perspective_>();
330 }
331
333 static String name()
334 {
335 return String("PowerFullMatrix<") + SubMatrixType::name() + "," + stringify(width_) + "," + stringify(height_) + ">";
336 }
337
338 template <Perspective perspective_ = Perspective::native>
339 Index size() const
340 {
341 return rows<perspective_>() * columns<perspective_>();
342 }
343
344 void format(DataType value = DataType(0))
345 {
346 _container.format(value);
347 }
348
350 void extract_diag(VectorTypeL& diag) const
351 {
352 static_assert(width_ == height_, "cannot extract diagonal from rectangular matrix");
353 Intern::ExtDiagPFM<width_>::extract_diag(*this, diag);
354 }
355
356 void apply(VectorTypeL& r, const VectorTypeR& x) const
357 {
358 _container.apply(r, x);
359 }
360
361 void apply(DenseVector<DataType, IndexType>& r, const DenseVector<DataType, IndexType>& x) const
362 {
363 _container.apply(r, x);
364 }
365
366 void apply(VectorTypeL& r, const VectorTypeR& x, const VectorTypeL& y, DataType alpha = DataType(1)) const
367 {
368 _container.apply(r, x, y, alpha);
369 }
370
371 void apply(DenseVector<DataType, IndexType>& r, const DenseVector<DataType, IndexType>& x,
372 const DenseVector<DataType, IndexType>& y, DataType alpha = DataType(1)) const
373 {
374 _container.apply(r, x, y, alpha);
375 }
376
377 void apply_transposed(VectorTypeR& r, const VectorTypeL& x) const
378 {
379 _container.apply_transposed(r, x);
380 }
381
382 void apply_transposed(DenseVector<DataType, IndexType>& r, const DenseVector<DataType, IndexType>& x) const
383 {
384 _container.apply_transposed(r, x);
385 }
386
387 void apply_transposed(VectorTypeR& r, const VectorTypeL& x, const VectorTypeR& y, DataType alpha = DataType(1)) const
388 {
389 _container.apply_transposed(r, x, y, alpha);
390 }
391
392 void apply_transposed(DenseVector<DataType, IndexType>& r, const DenseVector<DataType, IndexType>& x,
393 const DenseVector<DataType, IndexType>& y, DataType alpha = DataType(1)) const
394 {
395 _container.apply_transposed(r, x, y, alpha);
396 }
397
405 {
406 return this->_container.max_rel_diff(x._container);
407 }
408
417 bool same_layout(const PowerFullMatrix& x) const
418 {
419 return this->_container.same_layout(x._container);
420 }
421
422 template <typename SubType2_>
423 void convert(const PowerFullMatrix<SubType2_, width_, height_> & other)
424 {
425 _container.convert(other._container);
426 }
427
428 template <typename SubType2_>
429 void convert_reverse(PowerFullMatrix<SubType2_, width_, height_> & other) const
430 {
431 _container.convert_reverse(other._container);
432 }
433
435 std::uint64_t get_checkpoint_size(SerialConfig& config)
436 {
437 return _container.get_checkpoint_size(config);
438 }
439
441 void restore_from_checkpoint_data(std::vector<char> & data)
442 {
443 _container.restore_from_checkpoint_data(data);
444 }
445
447 std::uint64_t set_checkpoint_data(std::vector<char>& data, SerialConfig& config)
448 {
449 return _container.set_checkpoint_data(data, config);
450 }
451 }; // class PowerFullMatrix
452
454 namespace Intern
455 {
456 // JacobiHelper specialization for PowerFullMatrix
457 template<int n_>
458 struct ExtDiagPFM
459 {
460 static_assert(n_ > 1, "invalid block size");
461 template<typename SMT_, int m_>
462 static void extract_diag(
463 const PowerFullMatrix<SMT_, m_, m_>& matrix,
464 PowerVector<typename SMT_::VectorTypeL, m_>& diag)
465 {
466 ExtDiagPFM<n_-1>::extract_diag(matrix, diag);
467 matrix.template at<n_-1,n_-1>().extract_diag(diag.template at<n_-1>());
468 }
469 };
470
471 template<>
472 struct ExtDiagPFM<1>
473 {
474 template<typename SMT_, int m_>
475 static void extract_diag(
476 const PowerFullMatrix<SMT_, m_, m_>& matrix,
477 PowerVector<typename SMT_::VectorTypeL, m_>& diag)
478 {
479 matrix.template at<0,0>().extract_diag(diag.template at<0>());
480 }
481 };
482 } // namespace Intern
484 } // namespace LAFEM
485} // namespace FEAT
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Power-Col-Matrix meta class template.
PowerColMatrix clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a deep copy of this matrix.
VectorTypeL create_vector_l() const
Returns a new compatible L-Vector.
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
std::uint64_t get_checkpoint_size(SerialConfig &config)
Calculate size.
void write_out(FileMode mode, String filename) const
Write out matrix to file.
VectorTypeR create_vector_r() const
Returns a new compatible R-Vector.
void convert(const PowerColMatrix< SubType2_, blocks_ > &other)
Conversion method.
SubMatrixType & get(int i, int j)
Returns a sub-matrix block.
std::uint64_t set_checkpoint_data(std::vector< char > &data, SerialConfig &config)
void apply_transposed(VectorTypeR &r, const VectorTypeL &x) const
Applies this matrix onto a vector.
void format(DataType value=DataType(0))
Reset all elements of the container to a given value or zero if missing.
bool same_layout(const PowerColMatrix &x) const
Checks if the structural layout of this matrix matches that of another matrix. This excludes comparis...
void apply(VectorTypeL &r, const VectorTypeR &x) const
Applies this matrix onto a vector.
Index get_length_of_line(const Index row) const
Returns the number of NNZ-elements of the selected row.
std::size_t bytes() const
Returns the total amount of bytes allocated.
DataType max_rel_diff(const PowerColMatrix &x) const
Retrieve the maximum relative difference of this matrix and another one y.max_rel_diff(x) returns .
Power-Full-Matrix meta class template.
PowerFullMatrix(const SparseLayout< IndexType, layout_id > &layout)
sub-matrix layout ctor
PowerFullMatrix & operator=(PowerFullMatrix &&other)
move-assign operator
static constexpr int num_col_blocks
number of column blocks (horizontal size)
SubMatrixType & get(int i, int j)
Returns a sub-matrix block.
Index used_elements() const
Returns the total number of non-zeros in this matrix.
PowerFullMatrix(const PowerFullMatrix &)=delete
deleted copy-ctor
void write_out(FileMode mode, const String &filename) const
Write out matrix to file.
static constexpr SparseLayoutId layout_id
sub-matrix layout type
PowerFullMatrix & operator=(const PowerFullMatrix &)=delete
deleted copy-assign operator
const SubMatrixType & get(int i, int j) const
Returns a sub-matrix block.
PowerFullMatrix(FileMode mode, const String &filename)
Constructor.
std::size_t bytes() const
Returns the total amount of bytes allocated.
std::uint64_t set_checkpoint_data(std::vector< char > &data, SerialConfig &config)
PowerFullMatrix(FileMode mode, std::istream &file, const String &directory="")
Constructor.
static constexpr int num_row_blocks
number of row blocks (vertical size)
PowerFullMatrix clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a deep copy of this matrix.
SubMatrixType::DataType DataType
sub-matrix data type
DataType max_rel_diff(const PowerFullMatrix &x) const
Retrieve the maximum relative difference of this matrix and another one y.max_rel_diff(x) returns .
SubType_ SubMatrixType
sub-matrix type
PowerVector< typename SubMatrixType::VectorTypeL, height_ > VectorTypeL
Compatible L-vector type.
PowerFullMatrix(PowerFullMatrix &&other)
move ctor
SubMatrixType::IndexType IndexType
sub-matrix index type
const SubMatrixType & at() const
Returns a sub-matrix block.
Index columns() const
Returns the total number of columns in this matrix.
SubMatrixType & at()
Returns a sub-matrix block.
static String name()
Returns a descriptive string for this container.
PowerFullMatrix(ContClass &&cont)
base-class emplacement constructor
std::uint64_t get_checkpoint_size(SerialConfig &config)
Calculate size.
virtual ~PowerFullMatrix()
virtual destructor
bool same_layout(const PowerFullMatrix &x) const
Checks if the structural layout of this matrix matches that of another matrix. This excludes comparis...
PowerColMatrix< PowerRowMatrix< SubType_, width_ >, height_ > ContClass
container-class typedef
PowerVector< typename SubMatrixType::VectorTypeR, width_ > VectorTypeR
Compatible R-vector type.
Index rows() const
Returns the total number of rows in this matrix.
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
void extract_diag(VectorTypeL &diag) const
extract main diagonal vector from matrix
void read_from(FileMode mode, const String &filename)
Read in matrix from file.
SubMatrixType & get(int i, int j)
Returns a sub-matrix block.
Power-Vector meta class template.
Config class for serialize parameter.
Definition: container.hpp:47
Layout scheme for sparse matrix containers.
String class implementation.
Definition: string.hpp:46
SparseLayoutId
Definition: base.hpp:63
FEAT namespace.
Definition: adjactor.hpp:12
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:944
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.