From 2f83d3a7a2b4a8cc874336468a35cc457a9b489c Mon Sep 17 00:00:00 2001 From: michael Date: Mon, 13 Sep 2021 13:38:52 +0200 Subject: [PATCH] Added a function getArray to retrieve the list of matrix coeffcients --- CDMATH/base/inc/GenericMatrix.hxx | 3 ++ CDMATH/base/src/Matrix.cxx | 6 +-- CDMATH/linearsolver/inc/SparseMatrixPetsc.hxx | 5 ++ CDMATH/linearsolver/src/SparseMatrixPetsc.cxx | 51 ++++++++++++++++++- 4 files changed, 59 insertions(+), 6 deletions(-) diff --git a/CDMATH/base/inc/GenericMatrix.hxx b/CDMATH/base/inc/GenericMatrix.hxx index 52f3db9..36cf745 100755 --- a/CDMATH/base/inc/GenericMatrix.hxx +++ b/CDMATH/base/inc/GenericMatrix.hxx @@ -47,6 +47,9 @@ class GenericMatrix virtual double operator ()( int i, int j ) const = 0; + //returns the array of matrix coefficients + virtual std::vector< double > getArray() = 0; + virtual bool isSymmetric(double tol=1e-6) const ; bool isSquare() const ; diff --git a/CDMATH/base/src/Matrix.cxx b/CDMATH/base/src/Matrix.cxx index 20dfed6..dd37b6d 100755 --- a/CDMATH/base/src/Matrix.cxx +++ b/CDMATH/base/src/Matrix.cxx @@ -80,11 +80,9 @@ Matrix::min() const } std::vector< double > -Matrix::getArray() +Matrix::getArray() { - int numberOfRows =getNumberOfRows(); - int numberOfColums=getNumberOfColumns(); - int size=_numberOfRows*numberOfColums; + int size=_numberOfRows*_numberOfColumns; vector< double > result(size); double* values = result.data(); diff --git a/CDMATH/linearsolver/inc/SparseMatrixPetsc.hxx b/CDMATH/linearsolver/inc/SparseMatrixPetsc.hxx index 37449c4..9bb8049 100755 --- a/CDMATH/linearsolver/inc/SparseMatrixPetsc.hxx +++ b/CDMATH/linearsolver/inc/SparseMatrixPetsc.hxx @@ -10,6 +10,7 @@ #include #include +#include #include "MEDCouplingUMesh.hxx" #include "Vector.hxx" @@ -101,10 +102,14 @@ public: friend SparseMatrixPetsc operator*(const SparseMatrixPetsc& M, const SparseMatrixPetsc& N) ; void viewMatrix() const ; + //Save matrix coefficients into a file in ascii or binary mode + void saveMatrix(std::string filename, bool binaryMode=false) const ; double getMatrixCoeff(int i, int j) const; bool containsPetscMatrix() const; Mat getPetscMatrix() const; + //returns the array of matrix coefficients + std::vector< double > getArray(); void diagonalShift(double lambda); void zeroEntries();//sets the matrix coefficients to zero diff --git a/CDMATH/linearsolver/src/SparseMatrixPetsc.cxx b/CDMATH/linearsolver/src/SparseMatrixPetsc.cxx index 58ab952..d9cd0f9 100755 --- a/CDMATH/linearsolver/src/SparseMatrixPetsc.cxx +++ b/CDMATH/linearsolver/src/SparseMatrixPetsc.cxx @@ -8,8 +8,6 @@ #include "SparseMatrixPetsc.hxx" #include "CdmathException.hxx" -#include - using namespace std; //---------------------------------------------------------------------- @@ -415,6 +413,32 @@ SparseMatrixPetsc::viewMatrix() const MatView(_mat,PETSC_VIEWER_STDOUT_SELF); } + +void +SparseMatrixPetsc::saveMatrix(string filename, bool binaryMode) const +{ + MatAssemblyBegin(_mat, MAT_FINAL_ASSEMBLY); + MatAssemblyEnd(_mat, MAT_FINAL_ASSEMBLY); + + PetscViewer fileViewer; + PetscViewerCreate(PETSC_COMM_WORLD,&fileViewer); + PetscViewerFileSetMode(fileViewer,FILE_MODE_WRITE); + PetscViewerFileSetName(fileViewer,filename.c_str()); + + if( binaryMode) + { + PetscViewerSetType(fileViewer, PETSCVIEWERBINARY); + PetscViewerASCIIOpen(PETSC_COMM_WORLD, filename.c_str(), &fileViewer); + } + else + { + PetscViewerSetType(fileViewer, PETSCVIEWERASCII); + PetscViewerBinaryOpen(PETSC_COMM_WORLD, filename.c_str(), FILE_MODE_WRITE, &fileViewer); + } + + MatView(_mat,fileViewer); +} + double SparseMatrixPetsc::getMatrixCoeff(int i, int j) const { @@ -427,6 +451,29 @@ SparseMatrixPetsc::getMatrixCoeff(int i, int j) const return res; } +std::vector< double > +SparseMatrixPetsc::getArray() +{ + int size=_numberOfRows*_numberOfColumns; + + vector< double > result(size); + double* values = result.data(); + + int * idxm = new int[_numberOfRows]; + int * idxn = new int[_numberOfColumns]; + for (int i=0;i<_numberOfRows;i++) + idxm[i]=i; + for (int i=0;i<_numberOfColumns;i++) + idxn[i]=i; + + MatAssemblyBegin(_mat, MAT_FINAL_ASSEMBLY); + MatAssemblyEnd(_mat, MAT_FINAL_ASSEMBLY); + + MatGetValues(_mat,_numberOfRows, idxm,_numberOfColumns, idxn,values); + + return result; +} + void SparseMatrixPetsc::diagonalShift(double lambda) { -- 2.39.2