| 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 | #ifdef _HAVE_PETSCDEV_
|
|---|
| 42 | MatCreateAIJ(MPI_COMM_WORLD,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
|
|---|
| 43 | #else
|
|---|
| 44 | MatCreateMPIAIJ(MPI_COMM_WORLD,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
|
|---|
| 45 | #endif
|
|---|
| 46 |
|
|---|
| 47 | return outmatrix;
|
|---|
| 48 | }
|
|---|
| 49 | /*}}}*/
|
|---|
| 50 | /*NewMat(int M,int N,double sparsity){{{1*/
|
|---|
| 51 | Mat NewMat(int M,int N,double sparsity){
|
|---|
| 52 |
|
|---|
| 53 | /*output:*/
|
|---|
| 54 | Mat outmatrix=NULL;
|
|---|
| 55 |
|
|---|
| 56 | /*parameters: */
|
|---|
| 57 | int m,n;
|
|---|
| 58 | int d_nz,o_nz;
|
|---|
| 59 | int nnz;
|
|---|
| 60 |
|
|---|
| 61 | /*Determine local sizes: */
|
|---|
| 62 | m=DetermineLocalSize(M);
|
|---|
| 63 | n=DetermineLocalSize(N);
|
|---|
| 64 |
|
|---|
| 65 | nnz=(int)((double)M*(double)N*sparsity); //number of non zeros.
|
|---|
| 66 | d_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
|
|---|
| 67 | o_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
|
|---|
| 68 |
|
|---|
| 69 | #ifdef _HAVE_PETSCDEV_
|
|---|
| 70 | MatCreateAIJ(MPI_COMM_WORLD,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
|
|---|
| 71 | #else
|
|---|
| 72 | MatCreateMPIAIJ(MPI_COMM_WORLD,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
|
|---|
| 73 | #endif
|
|---|
| 74 |
|
|---|
| 75 | return outmatrix;
|
|---|
| 76 | }
|
|---|
| 77 | /*}}}*/
|
|---|
| 78 | /*NewMat(int M,int N,int connectivity,int numberofdofspernode){{{1*/
|
|---|
| 79 | Mat NewMat(int M,int N,int connectivity,int numberofdofspernode){
|
|---|
| 80 |
|
|---|
| 81 | /*output:*/
|
|---|
| 82 | Mat outmatrix=NULL;
|
|---|
| 83 |
|
|---|
| 84 | /*parameters: */
|
|---|
| 85 | int m,n;
|
|---|
| 86 | int d_nz,o_nz;
|
|---|
| 87 | int nnz;
|
|---|
| 88 |
|
|---|
| 89 | #if _PETSC_MAJOR_ >= 3
|
|---|
| 90 | const MatType type;
|
|---|
| 91 | #else
|
|---|
| 92 | MatType type;
|
|---|
| 93 | #endif
|
|---|
| 94 |
|
|---|
| 95 | /*Determine local sizes: */
|
|---|
| 96 | m=DetermineLocalSize(M);
|
|---|
| 97 | n=DetermineLocalSize(N);
|
|---|
| 98 |
|
|---|
| 99 | /*Figure out number of non zeros per row: */
|
|---|
| 100 | d_nz=(int)connectivity*numberofdofspernode/2;
|
|---|
| 101 | o_nz=(int)connectivity*numberofdofspernode/2;
|
|---|
| 102 |
|
|---|
| 103 | MatCreate(MPI_COMM_WORLD,&outmatrix);
|
|---|
| 104 | MatSetSizes(outmatrix,m,n,M,N);
|
|---|
| 105 | MatSetFromOptions(outmatrix);
|
|---|
| 106 |
|
|---|
| 107 | /*preallocation according to type: */
|
|---|
| 108 | MatGetType(outmatrix,&type);
|
|---|
| 109 |
|
|---|
| 110 | #if _PETSC_MAJOR_ == 2
|
|---|
| 111 | if((strcmp(type,"mpiaij")==0) || (strcmp(type,"aijmumps")==0)){
|
|---|
| 112 | MatMPIAIJSetPreallocation(outmatrix,d_nz,NULL,o_nz,NULL);
|
|---|
| 113 | }
|
|---|
| 114 | #else
|
|---|
| 115 | if((strcmp(type,"mpiaij")==0) || (strcmp(type,"mpidense")==0)){
|
|---|
| 116 | MatMPIAIJSetPreallocation(outmatrix,d_nz,NULL,o_nz,NULL);
|
|---|
| 117 | }
|
|---|
| 118 | #endif
|
|---|
| 119 |
|
|---|
| 120 | return outmatrix;
|
|---|
| 121 | }
|
|---|
| 122 | /*}}}*/
|
|---|