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 |
|
---|
18 | #include "../../../shared/shared.h"
|
---|
19 | #include "../../../include/include.h"
|
---|
20 |
|
---|
21 | /*NewMat(int M,int N){{{1*/
|
---|
22 | Mat NewMat(int M,int N){
|
---|
23 |
|
---|
24 | /*output:*/
|
---|
25 | Mat outmatrix=NULL;
|
---|
26 |
|
---|
27 | /*parameters: */
|
---|
28 | double sparsity=.001; //default
|
---|
29 | int m,n;
|
---|
30 | int d_nz,o_nz,nnz;
|
---|
31 |
|
---|
32 | /*Determine local sizes: */
|
---|
33 | m=PetscDetermineLocalSize(M);
|
---|
34 | n=PetscDetermineLocalSize(N);
|
---|
35 |
|
---|
36 | nnz=(int)((double)M*(double)N*sparsity); //number of non zeros.
|
---|
37 | d_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
|
---|
38 | o_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
|
---|
39 |
|
---|
40 | MatCreateMPIAIJ(MPI_COMM_WORLD,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
|
---|
41 |
|
---|
42 | return outmatrix;
|
---|
43 | }
|
---|
44 | /*}}}*/
|
---|
45 | /*NewMat(int M,int N,double sparsity){{{1*/
|
---|
46 | Mat NewMat(int M,int N,double sparsity){
|
---|
47 |
|
---|
48 | /*output:*/
|
---|
49 | Mat outmatrix=NULL;
|
---|
50 |
|
---|
51 | /*parameters: */
|
---|
52 | int m,n;
|
---|
53 | int d_nz,o_nz;
|
---|
54 | int nnz;
|
---|
55 |
|
---|
56 | /*Determine local sizes: */
|
---|
57 | m=PetscDetermineLocalSize(M);
|
---|
58 | n=PetscDetermineLocalSize(N);
|
---|
59 |
|
---|
60 | nnz=(int)((double)M*(double)N*sparsity); //number of non zeros.
|
---|
61 | d_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
|
---|
62 | o_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
|
---|
63 |
|
---|
64 | MatCreateMPIAIJ(MPI_COMM_WORLD,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
|
---|
65 |
|
---|
66 | return outmatrix;
|
---|
67 | }
|
---|
68 | /*}}}*/
|
---|
69 | /*NewMat(int M,int N,int connectivity,int numberofdofspernode){{{1*/
|
---|
70 | Mat NewMat(int M,int N,int connectivity,int numberofdofspernode){
|
---|
71 |
|
---|
72 | /*output:*/
|
---|
73 | Mat outmatrix=NULL;
|
---|
74 |
|
---|
75 | /*parameters: */
|
---|
76 | int m,n;
|
---|
77 | int d_nz,o_nz;
|
---|
78 | int nnz;
|
---|
79 |
|
---|
80 | #if _PETSC_MAJOR_ >= 3
|
---|
81 | const MatType type;
|
---|
82 | #else
|
---|
83 | MatType type;
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | /*Determine local sizes: */
|
---|
87 | m=PetscDetermineLocalSize(M);
|
---|
88 | n=PetscDetermineLocalSize(N);
|
---|
89 |
|
---|
90 | /*Figure out number of non zeros per row: */
|
---|
91 | d_nz=(int)connectivity*numberofdofspernode/2;
|
---|
92 | o_nz=(int)connectivity*numberofdofspernode/2;
|
---|
93 |
|
---|
94 | MatCreate(MPI_COMM_WORLD,&outmatrix);
|
---|
95 | MatSetSizes(outmatrix,m,n,M,N);
|
---|
96 | MatSetFromOptions(outmatrix);
|
---|
97 |
|
---|
98 | /*preallocation according to type: */
|
---|
99 | MatGetType(outmatrix,&type);
|
---|
100 |
|
---|
101 | #if _PETSC_MAJOR_ == 2
|
---|
102 | if((strcmp(type,"mpiaij")==0) || (strcmp(type,"aijmumps")==0)){
|
---|
103 | MatMPIAIJSetPreallocation(outmatrix,d_nz,NULL,o_nz,NULL);
|
---|
104 | }
|
---|
105 | #else
|
---|
106 | if((strcmp(type,"mpiaij")==0) || (strcmp(type,"mpidense")==0)){
|
---|
107 | MatMPIAIJSetPreallocation(outmatrix,d_nz,NULL,o_nz,NULL);
|
---|
108 | }
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | return outmatrix;
|
---|
112 | }
|
---|
113 | /*}}}*/
|
---|