[1] | 1 | /*!\file: NewMat.cpp
|
---|
| 2 | * \brief create matrix using the Petsc library
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | #ifdef HAVE_CONFIG_H
|
---|
[9320] | 6 | #include <config.h>
|
---|
[1] | 7 | #else
|
---|
| 8 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
| 9 | #endif
|
---|
| 10 |
|
---|
| 11 | /*Petsc includes: */
|
---|
[13784] | 12 | #include <petscksp.h>
|
---|
[1] | 13 |
|
---|
| 14 | #include "./petscpatches.h"
|
---|
| 15 | #include "../../../shared/shared.h"
|
---|
[15838] | 16 | #include "../../mpi/issmmpi.h"
|
---|
[1] | 17 |
|
---|
[12365] | 18 | /*NewMat(int M,int N){{{*/
|
---|
[15839] | 19 | Mat NewMat(int M,int N,ISSM_MPI_Comm comm){
|
---|
[1] | 20 |
|
---|
| 21 | /*output:*/
|
---|
| 22 | Mat outmatrix=NULL;
|
---|
| 23 |
|
---|
[5890] | 24 | /*parameters: */
|
---|
[22560] | 25 | double sparsity=0.001; //default
|
---|
[1] | 26 | int m,n;
|
---|
[5890] | 27 | int d_nz,o_nz,nnz;
|
---|
[1] | 28 |
|
---|
| 29 | /*Determine local sizes: */
|
---|
[13602] | 30 | m=DetermineLocalSize(M,comm);
|
---|
| 31 | n=DetermineLocalSize(N,comm);
|
---|
[13622] | 32 |
|
---|
[5890] | 33 | nnz=(int)((double)M*(double)N*sparsity); //number of non zeros.
|
---|
| 34 | d_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
|
---|
| 35 | o_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
|
---|
[1] | 36 |
|
---|
[25710] | 37 | #if PETSC_VERSION_GT(3,2,0)
|
---|
[13595] | 38 | MatCreateAIJ(comm,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
|
---|
[11910] | 39 | #else
|
---|
[13595] | 40 | MatCreateMPIAIJ(comm,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
|
---|
[11910] | 41 | #endif
|
---|
[1] | 42 |
|
---|
[5890] | 43 | return outmatrix;
|
---|
| 44 | }
|
---|
| 45 | /*}}}*/
|
---|
[15839] | 46 | /*NewMat(int M,int N,double sparsity,ISSM_MPI_Comm comm){{{*/
|
---|
| 47 | Mat NewMat(int M,int N,double sparsity,ISSM_MPI_Comm comm){
|
---|
[1] | 48 |
|
---|
[5890] | 49 | /*output:*/
|
---|
| 50 | Mat outmatrix=NULL;
|
---|
[1] | 51 |
|
---|
[5890] | 52 | /*parameters: */
|
---|
| 53 | int m,n;
|
---|
| 54 | int d_nz,o_nz;
|
---|
| 55 | int nnz;
|
---|
[1] | 56 |
|
---|
[5890] | 57 | /*Determine local sizes: */
|
---|
[13602] | 58 | m=DetermineLocalSize(M,comm);
|
---|
| 59 | n=DetermineLocalSize(N,comm);
|
---|
[13622] | 60 |
|
---|
[5890] | 61 | nnz=(int)((double)M*(double)N*sparsity); //number of non zeros.
|
---|
| 62 | d_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
|
---|
| 63 | o_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
|
---|
[1] | 64 |
|
---|
[25710] | 65 | #if PETSC_VERSION_GT(3,2,0)
|
---|
[12106] | 66 | if(sparsity==1){
|
---|
[13595] | 67 | MatCreateDense(comm,m,n,M,N,NULL,&outmatrix);
|
---|
[12106] | 68 | }
|
---|
| 69 | else{
|
---|
[13595] | 70 | MatCreateAIJ(comm,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
|
---|
[12106] | 71 | }
|
---|
[11910] | 72 | #else
|
---|
[13595] | 73 | MatCreateMPIAIJ(comm,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
|
---|
[11910] | 74 | #endif
|
---|
[1] | 75 |
|
---|
[5890] | 76 | return outmatrix;
|
---|
| 77 | }
|
---|
| 78 | /*}}}*/
|
---|
[12365] | 79 | /*NewMat(int M,int N,int connectivity,int numberofdofspernode){{{*/
|
---|
[15839] | 80 | Mat NewMat(int M,int N,int connectivity,int numberofdofspernode,ISSM_MPI_Comm comm){
|
---|
[1] | 81 |
|
---|
[5890] | 82 | /*output:*/
|
---|
| 83 | Mat outmatrix=NULL;
|
---|
[1] | 84 |
|
---|
[5890] | 85 | /*parameters: */
|
---|
| 86 | int m,n;
|
---|
| 87 | int d_nz,o_nz;
|
---|
[6852] | 88 |
|
---|
[25710] | 89 | #if PETSC_VERSION_MAJOR >= 3
|
---|
| 90 | #if defined(_HAVE_PETSCDEV_) || PETSC_VERSION_MINOR >=4
|
---|
[13897] | 91 | MatType type;
|
---|
| 92 | #else
|
---|
[6852] | 93 | const MatType type;
|
---|
| 94 | #endif
|
---|
[13895] | 95 | #else
|
---|
| 96 | MatType type;
|
---|
| 97 | #endif
|
---|
[1] | 98 |
|
---|
[5890] | 99 | /*Determine local sizes: */
|
---|
[13602] | 100 | m=DetermineLocalSize(M,comm);
|
---|
| 101 | n=DetermineLocalSize(N,comm);
|
---|
[6852] | 102 |
|
---|
[5890] | 103 | /*Figure out number of non zeros per row: */
|
---|
| 104 | d_nz=(int)connectivity*numberofdofspernode/2;
|
---|
| 105 | o_nz=(int)connectivity*numberofdofspernode/2;
|
---|
[1] | 106 |
|
---|
[13595] | 107 | MatCreate(comm,&outmatrix);
|
---|
[5895] | 108 | MatSetSizes(outmatrix,m,n,M,N);
|
---|
| 109 | MatSetFromOptions(outmatrix);
|
---|
[26131] | 110 | MatSetOption(outmatrix,MAT_IGNORE_ZERO_ENTRIES,PETSC_TRUE);
|
---|
[1] | 111 |
|
---|
[5895] | 112 | /*preallocation according to type: */
|
---|
| 113 | MatGetType(outmatrix,&type);
|
---|
[13622] | 114 |
|
---|
[6852] | 115 | if((strcmp(type,"mpiaij")==0) || (strcmp(type,"mpidense")==0)){
|
---|
| 116 | MatMPIAIJSetPreallocation(outmatrix,d_nz,NULL,o_nz,NULL);
|
---|
| 117 | }
|
---|
[5895] | 118 |
|
---|
[1] | 119 | return outmatrix;
|
---|
| 120 | }
|
---|
[5890] | 121 | /*}}}*/
|
---|