source: issm/trunk-jpl/src/c/toolkits/petsc/patches/NewMat.cpp@ 15838

Last change on this file since 15838 was 15838, checked in by Eric.Larour, 12 years ago

CHG: initial conversion from mpi to issmmpi layer. Starting validation of the new code changes

File size: 2.9 KB
Line 
1/*!\file: NewMat.cpp
2 * \brief create matrix using the Petsc library
3 */
4
5#ifdef HAVE_CONFIG_H
6 #include <config.h>
7#else
8#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
9#endif
10
11/*Petsc includes: */
12#include <petscmat.h>
13#include <petscvec.h>
14#include <petscksp.h>
15
16#include "./petscpatches.h"
17#include "../../../shared/shared.h"
18#include "../../mpi/issmmpi.h"
19
20/*NewMat(int M,int N){{{*/
21Mat NewMat(int M,int N,COMM comm){
22
23 /*output:*/
24 Mat outmatrix=NULL;
25
26 /*parameters: */
27 double sparsity=.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}
47/*}}}*/
48/*NewMat(int M,int N,double sparsity,COMM comm){{{*/
49Mat NewMat(int M,int N,double sparsity,COMM comm){
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}
80/*}}}*/
81/*NewMat(int M,int N,int connectivity,int numberofdofspernode){{{*/
82Mat NewMat(int M,int N,int connectivity,int numberofdofspernode,COMM comm){
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 _PETSC_MAJOR_ == 2
117 if((strcmp(type,"mpiaij")==0) || (strcmp(type,"aijmumps")==0)){
118 MatMPIAIJSetPreallocation(outmatrix,d_nz,NULL,o_nz,NULL);
119 }
120 #else
121 if((strcmp(type,"mpiaij")==0) || (strcmp(type,"mpidense")==0)){
122 MatMPIAIJSetPreallocation(outmatrix,d_nz,NULL,o_nz,NULL);
123 }
124 #endif
125
126 return outmatrix;
127}
128/*}}}*/
Note: See TracBrowser for help on using the repository browser.