Actual source code: ex60.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  2: static char help[] = "Tests MatGetColumnVector().";

  4: #include <petscmat.h>

  8: int main(int argc,char **args)
  9: {
 10:   Mat            C;
 11:   PetscInt       i,j,m = 3,n = 2,Ii,J,col = 0;
 12:   PetscMPIInt    size,rank;
 14:   PetscScalar    v;
 15:   Vec            yy;

 17:   PetscInitialize(&argc,&args,(char*)0,help);
 18:   PetscOptionsGetInt(NULL,"-col",&col,NULL);

 20:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 21:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 22:   n    = 2*size;

 24:   /* create the matrix for the five point stencil, YET AGAIN*/
 25:   MatCreateAIJ(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,
 26:                       m*n,m*n,5,NULL,5,NULL,&C);
 27:   for (i=0; i<m; i++) {
 28:     for (j=2*rank; j<2*rank+2; j++) {
 29:       v = -1.0;  Ii = j + n*i;
 30:       if (i>0)   {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 31:       if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 32:       if (j>0)   {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 33:       if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 34:       v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
 35:     }
 36:   }
 37:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 38:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
 39:   MatView(C,PETSC_VIEWER_STDOUT_WORLD);

 41:   VecCreate(PETSC_COMM_WORLD,&yy);
 42:   VecSetSizes(yy,PETSC_DECIDE,m*n);
 43:   VecSetFromOptions(yy);

 45:   MatGetColumnVector(C,yy,col);

 47:   VecView(yy,PETSC_VIEWER_STDOUT_WORLD);

 49:   VecDestroy(&yy);
 50:   MatDestroy(&C);
 51:   PetscFinalize();
 52:   return 0;
 53: }