Salome HOME
Added a function getArray to retrieve the list of matrix coeffcients
[tools/solverlab.git] / CDMATH / linearsolver / inc / SparseMatrixPetsc.hxx
1 /*
2  * SparseMatrixPetsc.hxx
3  *
4  *  Created on: 03/11/2017. 2017
5  *      Author: mndjinga
6  */
7
8 #ifndef SOURCE_DIRECTORY__BASE_INC_SparseMatrixPetsc_HXX_
9 #define SOURCE_DIRECTORY__BASE_INC_SparseMatrixPetsc_HXX_
10
11 #include <iostream>
12 #include <vector>
13 #include <cstring>
14
15 #include "MEDCouplingUMesh.hxx"
16 #include "Vector.hxx"
17 #include <petsc.h>
18
19 #include <slepceps.h>
20 #include <slepcsvd.h>
21
22 class SparseMatrixPetsc: public GenericMatrix {
23
24 public:
25         SparseMatrixPetsc();
26         virtual ~SparseMatrixPetsc();
27
28         /**
29          * constructor for a block sparse matrix
30          * @param numberOfRows : The number of rows
31          * @param numberOfColumns : The number of columns
32          */
33         SparseMatrixPetsc( int numberOfRows, int numberOfColumns ) ;
34
35         /**
36          * constructor for a block sparse matrix with number of non zero coefficients given
37          * @param numberOfRows : The number of rows
38          * @param numberOfColumns : The number of columns
39          * @param nnz : The maximum number of nonzeros coefficients per line (or an upper bound)
40          */
41         SparseMatrixPetsc( int numberOfRows, int numberOfColumns, int nnz ) ;
42
43         /**
44          * constructor for a sparse matrix with block structure
45          * @param blockSize : The block size
46          * @param numberOfRows : The number of rows
47          * @param numberOfColumns : The number of columns
48          * @param nnz : The maximum number of nonzeros coefficients per line (or an upper bound)
49      * @comment blockSize should always divide numberOfRows and numberOfColumns
50          */
51     SparseMatrixPetsc( int blockSize, int numberOfRows, int numberOfColumns, int nnz );
52
53         /**
54          * constructor by copy
55          * @param SparseMatrixPetsc : The SparseMatrixPetsc object to be copied
56          */
57         SparseMatrixPetsc ( const SparseMatrixPetsc& sparseMatrixPetsc ) ;
58
59         /**
60          * constructor with data
61          * @param Petsc matris : mat
62          */
63         SparseMatrixPetsc(Mat mat);
64
65         SparseMatrixPetsc transpose() const ;
66
67         double operator()( int i, int j ) const ;
68
69         void setValue( int i, int j, double value ) ;
70         void addValue( int i, int j, double value ) ;
71
72         void setValue( int i, int j, Matrix M ) ;
73         void addValue( int i, int j, Matrix M ) ;
74
75         void setValuesBlocked( int i, int j, Matrix M ) ;
76         void addValuesBlocked( int i, int j, Matrix M ) ;
77
78         SparseMatrixPetsc& operator+= (const SparseMatrixPetsc& SparseMatrixPetsc) ;
79
80         SparseMatrixPetsc& operator-= (const SparseMatrixPetsc& SparseMatrixPetsc) ;
81
82         SparseMatrixPetsc& operator*= (double value) ;
83
84         SparseMatrixPetsc& operator/= (double value) ;
85
86         SparseMatrixPetsc& operator*= (const SparseMatrixPetsc& matrix) ;
87
88         Vector operator* (const Vector& vector) const ;
89
90         const SparseMatrixPetsc& operator= ( const SparseMatrixPetsc& SparseMatrixPetsc ) ;
91
92         friend SparseMatrixPetsc operator+ (const SparseMatrixPetsc& SparseMatrixPetsc1, const SparseMatrixPetsc& SparseMatrixPetsc2);
93
94         friend SparseMatrixPetsc operator- (const SparseMatrixPetsc& SparseMatrixPetsc1, const SparseMatrixPetsc& SparseMatrixPetsc2);
95
96         friend SparseMatrixPetsc operator* (double value , const SparseMatrixPetsc& SparseMatrixPetsc ) ;
97
98         friend SparseMatrixPetsc operator* (const SparseMatrixPetsc& SparseMatrixPetsc, double value ) ;
99
100         friend SparseMatrixPetsc operator/ (const SparseMatrixPetsc& SparseMatrixPetsc, double value) ;
101
102         friend SparseMatrixPetsc operator*(const SparseMatrixPetsc& M, const SparseMatrixPetsc& N) ;
103
104         void viewMatrix() const ;
105         //Save matrix coefficients into a file in ascii or binary mode
106         void saveMatrix(std::string filename, bool binaryMode=false) const ;
107         double getMatrixCoeff(int i, int j) const;    
108
109         bool containsPetscMatrix() const;
110         Mat getPetscMatrix() const;
111         //returns the array of matrix coefficients
112         std::vector< double > getArray();
113     
114     void diagonalShift(double lambda);
115     void zeroEntries();//sets the matrix coefficients to zero
116     
117     std::vector< double > getEigenvalues( int nev, EPSWhich which=EPS_SMALLEST_MAGNITUDE, double tol=1e-6) const;
118     std::vector< Vector > getEigenvectors(int nev, EPSWhich which=EPS_SMALLEST_MAGNITUDE, double tol=1e-6) const;
119     MEDCoupling::DataArrayDouble * getEigenvectorsDataArrayDouble(int nev, EPSWhich which=EPS_SMALLEST_MAGNITUDE, double tol=1e-6) const;
120     std::vector< double > getSingularValues( int nsv, SVDWhich which=SVD_SMALLEST, double tol=1e-6) const;
121     std::vector< Vector > getSingularVectors(int nsv, SVDWhich which=SVD_SMALLEST, double tol=1e-6) const;
122     double getConditionNumber(bool isSingular=false, double tol=1e-6) const;
123         
124     bool isSymmetric(double tol=1.e-6) const ;
125     
126     void  leftDiagonalScale(Vector v);
127     void rightDiagonalScale(Vector v);
128     
129 private:
130         Mat _mat;
131
132         int _numberOfNonZeros ;//The maximum number of nonzeros coefficients per line (or an upper bound)
133         
134         int computeSpectrum(int nev, double ** valP, double ***vecP, EPSWhich which=EPS_SMALLEST_MAGNITUDE, double tol=1e-6) const;
135         int computeSVD     (int nsv, double ** valS, double ***vecS, SVDWhich which=SVD_SMALLEST          , double tol=1e-6) const;
136
137         Vector vecToVector(const Vec& vec) const ;
138         Vec vectorToVec( const Vector& myVector ) const ;
139 };
140
141
142 #endif /* SOURCE_DIRECTORY__BASE_INC_SparseMatrixPetsc_HXX_ */