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 row_degree(const Index row) const
260 {
261 return _container.row_degree(row);
262 }
263
264 template<typename IT2_>
265 Index get_row_col_indices(const Index row, IT2_* const pcol_idx, const IT2_ col_offset) const
266 {
267 return _container.get_row_col_indices(row, pcol_idx, col_offset);
268 }
269
270 template<typename DT2_>
271 Index get_row_values(const Index row, DT2_ * const pvals) const
272 {
273 return _container.get_row_values(row, pvals);
274 }
275
276 template<typename DT2_>
277 Index set_row_values(const Index row, const DT2_ * const pvals)
278 {
279 return _container.set_row_values(row, pvals);
280 }
281
282 ContClass& get_container()
283 {
284 return _container;
285 }
286
287 const ContClass& get_container() const
288 {
289 return _container;
290 }
292
293 VectorTypeL create_vector_l() const
294 {
295 return _container.create_vector_l();
296 }
297
298 VectorTypeR create_vector_r() const
299 {
300 return _container.create_vector_r();
301 }
302
310 {
311 return _container.num_rows();
312 }
313
314 Index num_rows_raw() const
315 {
316 return _container.num_rows_raw();
317 }
318
326 {
327 return _container.num_cols();
328 }
329
330 Index num_cols_raw() const
331 {
332 return _container.num_cols_raw();
333 }
334
342 {
343 return _container.num_nzes();
344 }
345
346 Index num_nzes_raw() const
347 {
348 return _container.num_nzes_raw();
349 }
350
352 static String name()
353 {
354 return String("PowerFullMatrix<") + SubMatrixType::name() + "," + stringify(width_) + "," + stringify(height_) + ">";
355 }
356
357
358 void format()
359 {
360 _container.format();
361 }
362
363 void format(DataType value)
364 {
365 _container.format(value);
366 }
367
369 void extract_diag(VectorTypeL& diag) const
370 {
371 static_assert(width_ == height_, "cannot extract diagonal from rectangular matrix");
372 Intern::ExtDiagPFM<width_>::extract_diag(*this, diag);
373 }
374
375 void apply(VectorTypeL& r, const VectorTypeR& x) const
376 {
377 _container.apply(r, x);
378 }
379
380 void apply(DenseVector<DataType, IndexType>& r, const DenseVector<DataType, IndexType>& x) const
381 {
382 _container.apply(r, x);
383 }
384
385 void apply(VectorTypeL& r, const VectorTypeR& x, const VectorTypeL& y, DataType alpha = DataType(1)) const
386 {
387 _container.apply(r, x, y, alpha);
388 }
389
390 void apply(DenseVector<DataType, IndexType>& r, const DenseVector<DataType, IndexType>& x,
391 const DenseVector<DataType, IndexType>& y, DataType alpha = DataType(1)) const
392 {
393 _container.apply(r, x, y, alpha);
394 }
395
396 void apply_transposed(VectorTypeR& r, const VectorTypeL& x) const
397 {
398 _container.apply_transposed(r, x);
399 }
400
401 void apply_transposed(DenseVector<DataType, IndexType>& r, const DenseVector<DataType, IndexType>& x) const
402 {
403 _container.apply_transposed(r, x);
404 }
405
406 void apply_transposed(VectorTypeR& r, const VectorTypeL& x, const VectorTypeR& y, DataType alpha = DataType(1)) const
407 {
408 _container.apply_transposed(r, x, y, alpha);
409 }
410
411 void apply_transposed(DenseVector<DataType, IndexType>& r, const DenseVector<DataType, IndexType>& x,
412 const DenseVector<DataType, IndexType>& y, DataType alpha = DataType(1)) const
413 {
414 _container.apply_transposed(r, x, y, alpha);
415 }
416
424 {
425 return this->_container.max_rel_diff(x._container);
426 }
427
436 bool same_layout(const PowerFullMatrix& x) const
437 {
438 return this->_container.same_layout(x._container);
439 }
440
441 template <typename SubType2_>
442 void convert(const PowerFullMatrix<SubType2_, width_, height_> & source)
443 {
444 _container.convert(source._container);
445 }
446
447 template <typename SubType2_>
448 void copy(const PowerFullMatrix<SubType2_, width_, height_> & source)
449 {
450 _container.copy(source._container);
451 }
452
453 template <typename SubType2_>
454 void copy_to(PowerFullMatrix<SubType2_, width_, height_> & target) const
455 {
456 _container.copy_to(target._container);
457 }
458
460 std::uint64_t get_checkpoint_size(SerialConfig& config)
461 {
462 return _container.get_checkpoint_size(config);
463 }
464
466 void restore_from_checkpoint_data(std::vector<char> & data)
467 {
468 _container.restore_from_checkpoint_data(data);
469 }
470
472 std::uint64_t set_checkpoint_data(std::vector<char>& data, SerialConfig& config)
473 {
474 return _container.set_checkpoint_data(data, config);
475 }
476 }; // class PowerFullMatrix
477
479 namespace Intern
480 {
481 // JacobiHelper specialization for PowerFullMatrix
482 template<int n_>
483 struct ExtDiagPFM
484 {
485 static_assert(n_ > 1, "invalid block size");
486 template<typename SMT_, int m_>
487 static void extract_diag(
488 const PowerFullMatrix<SMT_, m_, m_>& matrix,
489 PowerVector<typename SMT_::VectorTypeL, m_>& diag)
490 {
491 ExtDiagPFM<n_-1>::extract_diag(matrix, diag);
492 matrix.template at<n_-1,n_-1>().extract_diag(diag.template at<n_-1>());
493 }
494 };
495
496 template<>
497 struct ExtDiagPFM<1>
498 {
499 template<typename SMT_, int m_>
500 static void extract_diag(
501 const PowerFullMatrix<SMT_, m_, m_>& matrix,
502 PowerVector<typename SMT_::VectorTypeL, m_>& diag)
503 {
504 matrix.template at<0,0>().extract_diag(diag.template at<0>());
505 }
506 };
507 } // namespace Intern
509 } // namespace LAFEM
510} // 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.
Index num_cols() const
Returns the total number of columns in this matrix.
Index num_nzes() const
Returns the total number of non-zeros in this matrix.
Index num_rows() const
Returns the total number of rows in this matrix.
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.
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.
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 .
void convert(const PowerColMatrix< SubType2_, blocks_ > &source)
Conversion method.
void format()
Reset all elements of the container to a given value or zero if missing.
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 num_cols() const
Returns the total number of columns 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)
Index num_rows() const
Returns the total number of rows in this matrix.
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 num_nzes() const
Returns the total number of non-zeros 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.
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:50
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.