openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
SF_abstract_matrix.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #ifndef _SF_ABSTRACT_MATRIX_H
5 #define _SF_ABSTRACT_MATRIX_H
6 
7 #include <mpi.h>
8 
9 #include <sstream>
10 
11 #include "SF_abstract_vector.h"
12 #include "SF_container.h"
13 #include "SF_globals.h"
14 #include "SF_parallel_layout.h"
15 
16 namespace SF {
17 
18 
30 template <class T, class S>
32 {
33  protected:
35  abstract_matrix() : mesh(NULL),
36  NRows(0),
37  NCols(0),
38  row_dpn(0),
39  col_dpn(0),
40  lsize(0),
41  start(0),
42  stop(0)
43  {
44  }
45 
48 
49  T NRows;
50  T NCols;
51  int row_dpn;
52  int col_dpn;
53 
54  T lsize;
55  T start;
56  T stop;
57 
58  public:
59  mutable std::set<T> boundary_idxs;
60 
62  virtual ~abstract_matrix() = default;
63 
74  virtual inline void init(T iNRows, T iNCols, T ilrows, T ilcols,
75  T loc_offset, T mxent)
76  {
77  this->NRows = iNRows;
78  this->NCols = iNCols;
79  this->lsize = ilrows;
80  this->start = loc_offset;
81  this->stop = this->start + this->lsize;
82  }
83 
93  inline void init(const meshdata<mesh_int_t, mesh_real_t> & imesh,
94  const T irow_dpn,
95  const T icol_dpn,
96  const T max_edges)
97  {
98  mesh = &imesh;
99  this->row_dpn = irow_dpn;
100  this->col_dpn = icol_dpn;
101 
102  int rank;
103  MPI_Comm_rank(mesh->comm, &rank);
104 
105  T M = mesh->pl.num_global_idx() * this->row_dpn;
106  T N = mesh->pl.num_global_idx() * this->col_dpn;
107  T m = mesh->pl.num_algebraic_idx() * this->row_dpn;
108  T n = mesh->pl.num_algebraic_idx() * this->col_dpn;
109 
110  T nmax = max_edges;
111  if(max_edges < 0) nmax = max_nodal_edgecount(*mesh);
112 
113  T loc_offset = mesh->pl.algebraic_layout()[rank]*this->row_dpn;
114 
115  init(M, N, m, n, loc_offset, nmax*this->col_dpn);
116  zero();
117  }
118 
125  {
126  return this->mesh;
127  }
128 
134  inline int dpn_row() const { return this->row_dpn; }
135 
141  inline int dpn_col() const { return this->col_dpn; }
142 
146  virtual void zero() = 0;
147 
156  {
157  return false;
158  }
159 
164  {
165  }
166 
175  virtual bool duplicate_pattern_of(const abstract_matrix<T,S>& other)
176  {
177  (void)other;
178  return false;
179  }
180 
187  virtual void mult(const abstract_vector<T,S> & x, abstract_vector<T,S> & b) const = 0;
188 
196  virtual void mult_LR(const abstract_vector<T, S>& L, const abstract_vector<T, S>& R) = 0;
197 
203  virtual void diag_add(const abstract_vector<T, S>& diag) = 0;
204 
210  virtual void get_diagonal(abstract_vector<T, S>& vec) const = 0;
211 
215  virtual void finish_assembly() = 0;
216 
222  virtual void scale(S s) = 0;
223 
231  virtual void add_scaled_matrix(const abstract_matrix<T, S>& A, const S s, const bool same_nnz) = 0;
232 
238  virtual void duplicate(const abstract_matrix<T,S>& M) = 0;
239 
250  virtual void set_values(const vector<T>& row_idx, const vector<T>& col_idx, const vector<S>& vals, bool add) = 0;
251 
262  virtual void set_values(const vector<T> & row_idx, const vector<T> & col_idx,
263  const S* vals, bool add) = 0;
264 
273  virtual void set_value(T row_idx, T col_idx, S val, bool add) = 0;
274 
285  inline void get_values(const vector<T> & row_idx, const vector<T> & col_idx,
286  vector<S> & values) const {
287  assert(row_idx.size() == col_idx.size() && col_idx.size() == values.size());
288 
289  for (int i = 0; i < row_idx.size(); i++)
290  values[i] = get_value(row_idx[i], col_idx[i]);
291  }
292 
301  virtual S get_value(T row_idx, T col_idx) const = 0;
302 
308  virtual void write(const char* filename) const = 0;
309 
310 
321  inline bool equals(const abstract_matrix<T,S> & rhs) const
322  {
323  if (NRows != rhs.NRows or NCols != rhs.NCols)
324  return false;
325 
326  bool equal = true;
327  for (size_t row = 0; row < NRows; row++)
328  for (size_t col = 0; col < NCols; col++)
329  if (fabs(get_value(row, col) - rhs.get_value(row, col)) > 0.0001)
330  equal = false;
331 
332  return equal;
333  }
334 
340  inline std::string to_string() const
341  {
342  std::ostringstream stream;
343  stream << "matrix (" << this->NRows << " x " << this->NCols << ") [\n";
344 
345  for (auto row = 0; row < this->NRows; row++) {
346  stream << " [";
347  for (auto col = 0; col < this->NCols; col++) {
348  if (col > 0) stream << ", ";
349  stream << get_value(row, col);
350  }
351  stream << "]\n";
352  }
353  stream << "]";
354 
355  return stream.str();
356  }
357 };
358 
359 } // namespace SF
360 
361 
362 
363 #endif // _SF_ABSTRACT_MATRIX_H
Basic containers.
Classes and algorithms related to the layout of distributed meshes.
virtual void set_values(const vector< T > &row_idx, const vector< T > &col_idx, const S *vals, bool add)=0
std::string to_string() const
const meshdata< mesh_int_t, mesh_real_t > * mesh_ptr() const
virtual void mult(const abstract_vector< T, S > &x, abstract_vector< T, S > &b) const =0
virtual void scale(S s)=0
void get_values(const vector< T > &row_idx, const vector< T > &col_idx, vector< S > &values) const
virtual void finish_assembly()=0
T NRows
global number of rows
virtual void zero()=0
std::set< T > boundary_idxs
virtual void get_diagonal(abstract_vector< T, S > &vec) const =0
T start
start row index of local matrix portion
T NCols
global number of cols
virtual void finalize_exact_preallocation()
virtual bool begin_exact_preallocation()
virtual void mult_LR(const abstract_vector< T, S > &L, const abstract_vector< T, S > &R)=0
virtual bool duplicate_pattern_of(const abstract_matrix< T, S > &other)
T stop
stop row index of local matrix portion
virtual void init(T iNRows, T iNCols, T ilrows, T ilcols, T loc_offset, T mxent)
virtual void set_values(const vector< T > &row_idx, const vector< T > &col_idx, const vector< S > &vals, bool add)=0
void init(const meshdata< mesh_int_t, mesh_real_t > &imesh, const T irow_dpn, const T icol_dpn, const T max_edges)
virtual void duplicate(const abstract_matrix< T, S > &M)=0
virtual void set_value(T row_idx, T col_idx, S val, bool add)=0
virtual void add_scaled_matrix(const abstract_matrix< T, S > &A, const S s, const bool same_nnz)=0
virtual void write(const char *filename) const =0
const meshdata< mesh_int_t, mesh_real_t > * mesh
pointer to the associated mesh
virtual ~abstract_matrix()=default
virtual void diag_add(const abstract_vector< T, S > &diag)=0
T lsize
size of local matrix (#locally stored rows)
bool equals(const abstract_matrix< T, S > &rhs) const
virtual S get_value(T row_idx, T col_idx) const =0
overlapping_layout< T > pl
nodal parallel layout
Definition: SF_container.h:414
MPI_Comm comm
the parallel mesh is defined on a MPI world
Definition: SF_container.h:389
A vector storing arbitrary data.
Definition: SF_vector.h:28
size_t size() const
The current size of the vector.
Definition: SF_vector.h:89
Definition: dense_mat.hpp:19
int max_nodal_edgecount(const meshdata< T, S > &mesh)
Compute the maximum number of node-to-node edges for a mesh.
Definition: SF_container.h:593