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 Index row_degree(const Index row) const
276 {
277 return _container.row_degree(row);
278 }
279
280 template<typename IT2_>
281 Index get_row_col_indices(const Index row, IT2_* const pcol_idx, const IT2_ col_offset) const
282 {
283 return _container.get_row_col_indices(row, pcol_idx, col_offset);
284 }
285
286 template<typename DT2_>
287 Index get_row_values(const Index row, DT2_ * const pvals) const
288 {
289 return _container.get_row_values(row, pvals);
290 }
291
292 template<typename DT2_>
293 Index set_row_values(const Index row, const DT2_ * const pvals)
294 {
295 return _container.set_row_values(row, pvals);
296 }
297
298 ContClass& get_container()
299 {
300 return _container;
301 }
302
303 const ContClass& get_container() const
304 {
305 return _container;
306 }
308
309 VectorTypeL create_vector_l() const
310 {
311 return _container.create_vector_l();
312 }
313
314 VectorTypeR create_vector_r() const
315 {
316 return _container.create_vector_r();
317 }
318
325 template <Perspective perspective_ = Perspective::native>
326 Index rows() const
327 {
328 return _container.template rows<perspective_>();
329 }
330
337 template <Perspective perspective_ = Perspective::native>
339 {
340 return _container.template columns<perspective_>();
341 }
342
349 template <Perspective perspective_ = Perspective::native>
351 {
352 return _container.template used_elements<perspective_>();
353 }
354
356 static String name()
357 {
358 return String("PowerFullMatrix<") + SubMatrixType::name() + "," + stringify(width_) + "," + stringify(height_) + ">";
359 }
360
361 template <Perspective perspective_ = Perspective::native>
362 Index size() const
363 {
364 return rows<perspective_>() * columns<perspective_>();
365 }
366
367 void format(DataType value = DataType(0))
368 {
369 _container.format(value);
370 }
371
373 void extract_diag(VectorTypeL& diag) const
374 {
375 static_assert(width_ == height_, "cannot extract diagonal from rectangular matrix");
376 Intern::ExtDiagPFM<width_>::extract_diag(*this, diag);
377 }
378
379 void apply(VectorTypeL& r, const VectorTypeR& x) const
380 {
381 _container.apply(r, x);
382 }
383
384 void apply(DenseVector<DataType, IndexType>& r, const DenseVector<DataType, IndexType>& x) const
385 {
386 _container.apply(r, x);
387 }
388
389 void apply(VectorTypeL& r, const VectorTypeR& x, const VectorTypeL& y, DataType alpha = DataType(1)) const
390 {
391 _container.apply(r, x, y, alpha);
392 }
393
394 void apply(DenseVector<DataType, IndexType>& r, const DenseVector<DataType, IndexType>& x,
395 const DenseVector<DataType, IndexType>& y, DataType alpha = DataType(1)) const
396 {
397 _container.apply(r, x, y, alpha);
398 }
399
400 void apply_transposed(VectorTypeR& r, const VectorTypeL& x) const
401 {
402 _container.apply_transposed(r, x);
403 }
404
405 void apply_transposed(DenseVector<DataType, IndexType>& r, const DenseVector<DataType, IndexType>& x) const
406 {
407 _container.apply_transposed(r, x);
408 }
409
410 void apply_transposed(VectorTypeR& r, const VectorTypeL& x, const VectorTypeR& y, DataType alpha = DataType(1)) const
411 {
412 _container.apply_transposed(r, x, y, alpha);
413 }
414
415 void apply_transposed(DenseVector<DataType, IndexType>& r, const DenseVector<DataType, IndexType>& x,
416 const DenseVector<DataType, IndexType>& y, DataType alpha = DataType(1)) const
417 {
418 _container.apply_transposed(r, x, y, alpha);
419 }
420
428 {
429 return this->_container.max_rel_diff(x._container);
430 }
431
440 bool same_layout(const PowerFullMatrix& x) const
441 {
442 return this->_container.same_layout(x._container);
443 }
444
445 template <typename SubType2_>
446 void convert(const PowerFullMatrix<SubType2_, width_, height_> & other)
447 {
448 _container.convert(other._container);
449 }
450
451 template <typename SubType2_>
452 void convert_reverse(PowerFullMatrix<SubType2_, width_, height_> & other) const
453 {
454 _container.convert_reverse(other._container);
455 }
456
458 std::uint64_t get_checkpoint_size(SerialConfig& config)
459 {
460 return _container.get_checkpoint_size(config);
461 }
462
464 void restore_from_checkpoint_data(std::vector<char> & data)
465 {
466 _container.restore_from_checkpoint_data(data);
467 }
468
470 std::uint64_t set_checkpoint_data(std::vector<char>& data, SerialConfig& config)
471 {
472 return _container.set_checkpoint_data(data, config);
473 }
474 }; // class PowerFullMatrix
475
477 namespace Intern
478 {
479 // JacobiHelper specialization for PowerFullMatrix
480 template<int n_>
481 struct ExtDiagPFM
482 {
483 static_assert(n_ > 1, "invalid block size");
484 template<typename SMT_, int m_>
485 static void extract_diag(
486 const PowerFullMatrix<SMT_, m_, m_>& matrix,
487 PowerVector<typename SMT_::VectorTypeL, m_>& diag)
488 {
489 ExtDiagPFM<n_-1>::extract_diag(matrix, diag);
490 matrix.template at<n_-1,n_-1>().extract_diag(diag.template at<n_-1>());
491 }
492 };
493
494 template<>
495 struct ExtDiagPFM<1>
496 {
497 template<typename SMT_, int m_>
498 static void extract_diag(
499 const PowerFullMatrix<SMT_, m_, m_>& matrix,
500 PowerVector<typename SMT_::VectorTypeL, m_>& diag)
501 {
502 matrix.template at<0,0>().extract_diag(diag.template at<0>());
503 }
504 };
505 } // namespace Intern
507 } // namespace LAFEM
508} // 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:47
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:993
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.