[petsc-users] create a matrix in C++ constructor

Umut Tabak u.tabak at tudelft.nl
Mon Dec 21 17:11:51 CST 2009


Matthew Knepley wrote:
> On Mon, Dec 21, 2009 at 5:01 PM, Umut Tabak <u.tabak at tudelft.nl 
> <mailto:u.tabak at tudelft.nl>> wrote:
>
>     Dear all,
>
>     I was trying to wrap around some Petsc code into a class, so that
>     there is a neat interface and implementation, I got into a problem
>     at the very beginning and did not understand the reason maybe some
>     of you could help me to understand this.
>
>     I have a Matrix A in the private section of my simple class.
>
>     And I would like to create this matrix inside the class ctor.
>     Whenever I call the MatCreate function I get a segmentation fault with
>
>
> Without seeing the code, we cannot say anything, but I bet you 
> misdeclared A.
>
>  
Dear Matthew,

Here is my simple starter design,
  
     8 class petscMatrix
     9 {
    10     public:
    11         petscMatrix();
    12         petscMatrix(PetscInt, PetscInt, char) 
throw(std::runtime_error);
    13         Mat* assignSubMatrix();
    14         int createMatrix();
    15     private:
    16         Mat A;        // Operation matrix
    17         PetscInt m,n; // the row and column count
    18                                // of the matrix
    19         char matType;
    20
    21};


And its implementation is:

    15 petscMatrix::petscMatrix(PetscInt rowSz, PetscInt colSz, char t) 
throw(runtime_error)
    16 {
    17   try{
    18     switch(t)
    19       {
    20         case('d'): // dense
    21             matType = t;
    22             m = rowSz;
    23             n = colSz;
    24         case('D'): // dense
    25             matType = t;
    26             m = rowSz;
    27             n = colSz;
    28         case('s'): //sparse
    29             matType = t;
    30             m = rowSz;
    31             n = colSz;
    32             MatCreate(MPI_COMM_SELF, &A);
    33             MatSetSizes(A, m, n, 0, 0);
    34             MatSetType(A,MATSEQAIJ);
    35         case('S'): //sparse
    36             matType = t;
    37             m = rowSz;
    38             n = colSz;
    39         default:
    40           throw runtime_error("Matrix type not recognized\nMatrix 
not initialized\nExiting...");
    41       }
    42   }
    43   catch(runtime_error &e)
    44   {
    45     cout << e.what() << endl;
    46     exit(1);
    47   }
    48 }

Maybe I am too tired, most probably, there is sth easy that I am missing...


More information about the petsc-users mailing list