PolyDiM
C++ library for POLYtopal DIscretization Methods
Loading...
Searching...
No Matches
PETSc_SparseArray.hpp
Go to the documentation of this file.
1// _LICENSE_HEADER_
2//
3// Copyright (C) 2019 - 2025.
4// Terms register on the GPL-3.0 license.
5//
6// This file can be redistributed and/or modified under the license terms.
7//
8// See top level LICENSE file for more details.
9//
10// This file can be used citing references in CITATION.cff file.
11
12#ifndef __PETSc_SparseArray_HPP
13#define __PETSc_SparseArray_HPP
14
15#include "Gedim_Macro.hpp"
16
17#if ENABLE_PETSC == 1
18
19#include "ISparseArray.hpp"
20
21#include "IOUtilities.hpp"
22
23#include "petscmat.h"
24
25namespace Gedim
26{
28template <typename PETSc_ArrayType = Vec, typename PETSc_SparseArrayType = Mat>
29class PETSc_SparseArray final : public ISparseArray
30{
31 private:
32 PETSc_SparseArrayType _matrix;
33 SparseArrayTypes _matrixType;
34
35 public:
37 {
38 _matrixType = SparseArrayTypes::None;
39 }
41 {
42 }
43
44 PETSc_SparseArray(const PETSc_SparseArrayType &matrix, const SparseArrayTypes &type = SparseArrayTypes::None)
45 {
46 _matrix = matrix;
47 _matrixType = type;
48 }
49
50 operator PETSc_SparseArrayType &()
51 {
52 return _matrix;
53 }
54 operator const PETSc_SparseArrayType &() const
55 {
56 return _matrix;
57 }
58 inline PETSc_SparseArrayType &Cast(ISparseArray &v)
59 {
61 }
62 inline const PETSc_SparseArrayType &Cast(const ISparseArray &v)
63 {
65 }
66 inline const PETSc_SparseArrayType &Cast(const ISparseArray &v) const
67 {
69 }
70
71 void SetSize(const unsigned int &numRows, const unsigned int &numCols, const SparseArrayTypes &type = SparseArrayTypes::None);
72
73 inline SparseArrayTypes Type() const
74 {
75 return _matrixType;
76 }
77
78 void Create();
79 void Destroy();
80 void Flush();
81 void Reset();
82
83 void Triplet(const unsigned int &i, const unsigned int &j, const double &value);
84
85 void Triplets(const std::vector<unsigned int> &i, const std::vector<unsigned int> &j, const std::vector<double> &values);
86
87 std::ostream &Print(std::ostream &output) const;
88
89 inline ISparseArray &operator+=(const ISparseArray &A)
90 {
91 MatAXPY(_matrix, 1.0, Cast(A), DIFFERENT_NONZERO_PATTERN);
92 return *this;
93 }
94 inline ISparseArray &operator-=(const ISparseArray &A)
95 {
96 MatAXPY(_matrix, -1.0, Cast(A), DIFFERENT_NONZERO_PATTERN);
97 return *this;
98 }
99 inline ISparseArray &operator*=(const double &c)
100 {
101 MatScale(_matrix, c);
102 return *this;
103 }
104 inline ISparseArray &operator/=(const double &c)
105 {
106 MatScale(_matrix, 1.0 / c);
107 return *this;
108 }
109
110 unsigned int rows() const;
111 unsigned int cols() const;
112
114 {
115 throw std::runtime_error("unimplemented method");
116 }
117
118 void ToBinaryFile(const std::string &, const bool &) const
119 {
120 throw std::runtime_error("unimplemented method");
121 }
122
123 inline double Norm() const
124 {
125 double norm;
126 MatNorm(_matrix, NORM_FROBENIUS, &norm);
127 return norm;
128 }
129
130 inline double Cond(const ISparseArray::ConditionNumberAlgorithm &algorithm) const
131 {
132 throw std::runtime_error("unimplemented method");
133 }
134
135 unsigned int NonZeros() const;
136};
137} // namespace Gedim
138
139#endif // ENABLE_PETSC
140
141#endif // __PETSc_SparseArray_HPP
std::ostream & Print(std::ostream &output) const
Print the array.
Definition Eigen_Array.hpp:135
double Norm() const
Definition Eigen_Array.hpp:193
Eigen_ArrayType & Cast(IArray &v)
Definition Eigen_Array.hpp:51
void SetSize(const unsigned int &numCols)
Set the size the Array. Resize a Array for Eigen application.
Definition Eigen_Array.hpp:66
IArray & operator+=(const IArray &v)
Definition Eigen_Array.hpp:140
IArray & operator-=(const IArray &v)
Definition Eigen_Array.hpp:145
void Destroy()
Definition Eigen_Array.hpp:63
void ToBinaryFile(const std::string &filePath, const unsigned int &dataSizeToWrite=0, const unsigned int &dataStartingPositionToWrite=0, const bool &append=false) const
Write array to binary file.
Definition Eigen_Array.cpp:111
void Create()
Matrix create call. Last call to complete the matrix structure.
Definition Eigen_Array.hpp:60
IArray & operator*=(const double &c)
Definition Eigen_Array.hpp:150
Eigen_Array()
Definition Eigen_Array.hpp:28
IArray & operator/=(const double &c)
Definition Eigen_Array.hpp:155
Eigen_Array< Eigen_ArrayType, Eigen_SparseArrayType > & operator=(Eigen_ArrayType &&vector)
Definition Eigen_Array.hpp:182
ConditionNumberAlgorithm
Definition ISparseArray.hpp:37
Definition Eigen_Array.cpp:22