Ice Sheet System Model  4.18
Code documentation
Functions
NewMat.cpp File Reference

create matrix using the Petsc library More...

#include <petscmat.h>
#include <petscvec.h>
#include <petscksp.h>
#include "./petscpatches.h"
#include "../../../shared/shared.h"
#include "../../mpi/issmmpi.h"

Go to the source code of this file.

Functions

Mat NewMat (int M, int N, ISSM_MPI_Comm comm)
 
Mat NewMat (int M, int N, double sparsity, ISSM_MPI_Comm comm)
 
Mat NewMat (int M, int N, int connectivity, int numberofdofspernode, ISSM_MPI_Comm comm)
 

Detailed Description

create matrix using the Petsc library

Definition in file NewMat.cpp.

Function Documentation

◆ NewMat() [1/3]

Mat NewMat ( int  M,
int  N,
ISSM_MPI_Comm  comm 
)

Definition at line 21 of file NewMat.cpp.

21  {
22 
23  /*output:*/
24  Mat outmatrix=NULL;
25 
26  /*parameters: */
27  double sparsity=0.001; //default
28  int m,n;
29  int d_nz,o_nz,nnz;
30 
31  /*Determine local sizes: */
32  m=DetermineLocalSize(M,comm);
33  n=DetermineLocalSize(N,comm);
34 
35  nnz=(int)((double)M*(double)N*sparsity); //number of non zeros.
36  d_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
37  o_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
38 
39  #if _PETSC_MAJOR_ == 3 && _PETSC_MINOR_ > 2
40  MatCreateAIJ(comm,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
41  #else
42  MatCreateMPIAIJ(comm,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
43  #endif
44 
45  return outmatrix;
46 }

◆ NewMat() [2/3]

Mat NewMat ( int  M,
int  N,
double  sparsity,
ISSM_MPI_Comm  comm 
)

Definition at line 49 of file NewMat.cpp.

49  {
50 
51  /*output:*/
52  Mat outmatrix=NULL;
53 
54  /*parameters: */
55  int m,n;
56  int d_nz,o_nz;
57  int nnz;
58 
59  /*Determine local sizes: */
60  m=DetermineLocalSize(M,comm);
61  n=DetermineLocalSize(N,comm);
62 
63  nnz=(int)((double)M*(double)N*sparsity); //number of non zeros.
64  d_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
65  o_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
66 
67  #if _PETSC_MAJOR_ == 3 && _PETSC_MINOR_ > 2
68  if(sparsity==1){
69  MatCreateDense(comm,m,n,M,N,NULL,&outmatrix);
70  }
71  else{
72  MatCreateAIJ(comm,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
73  }
74  #else
75  MatCreateMPIAIJ(comm,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
76  #endif
77 
78  return outmatrix;
79 }

◆ NewMat() [3/3]

Mat NewMat ( int  M,
int  N,
int  connectivity,
int  numberofdofspernode,
ISSM_MPI_Comm  comm 
)

Definition at line 82 of file NewMat.cpp.

82  {
83 
84  /*output:*/
85  Mat outmatrix=NULL;
86 
87  /*parameters: */
88  int m,n;
89  int d_nz,o_nz;
90 
91  #if _PETSC_MAJOR_ >= 3
92  #if defined(_HAVE_PETSCDEV_) || _PETSC_MINOR_ >=4
93  MatType type;
94  #else
95  const MatType type;
96  #endif
97  #else
98  MatType type;
99  #endif
100 
101  /*Determine local sizes: */
102  m=DetermineLocalSize(M,comm);
103  n=DetermineLocalSize(N,comm);
104 
105  /*Figure out number of non zeros per row: */
106  d_nz=(int)connectivity*numberofdofspernode/2;
107  o_nz=(int)connectivity*numberofdofspernode/2;
108 
109  MatCreate(comm,&outmatrix);
110  MatSetSizes(outmatrix,m,n,M,N);
111  MatSetFromOptions(outmatrix);
112 
113  /*preallocation according to type: */
114  MatGetType(outmatrix,&type);
115 
116  if((strcmp(type,"mpiaij")==0) || (strcmp(type,"mpidense")==0)){
117  MatMPIAIJSetPreallocation(outmatrix,d_nz,NULL,o_nz,NULL);
118  }
119 
120  return outmatrix;
121 }
DetermineLocalSize
int DetermineLocalSize(int global_size, ISSM_MPI_Comm comm)
Definition: DetermineLocalSize.cpp:9