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