Actual source code: ex20.c

  2: static char help[] = "Tests converting a matrix to another format with MatConvert().\n\n";

 4:  #include petscmat.h

  8: int main(int argc,char **args)
  9: {
 10:   Mat               C,A;
 11:   PetscInt          i,j,m = 5,n = 4,Ii,J;
 12:   PetscErrorCode    ierr;
 13:   PetscMPIInt       rank,size;
 14:   PetscScalar       v;
 15:   char              mtype[256];

 17:   PetscInitialize(&argc,&args,(char *)0,help);
 18:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 20:   /* This example does not work correctly for np > 2 */
 21:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 22:   if (size > 2) SETERRQ(1,"Use np <= 2");

 24:  /* Create the matrix for the five point stencil, YET AGAIN */
 25:   MatCreate(PETSC_COMM_WORLD,&C);
 26:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
 27:   MatSetFromOptions(C);
 28:   for (i=0; i<m; i++) {
 29:     for (j=2*rank; j<2*rank+2; j++) {
 30:       v = -1.0;  Ii = j + n*i;
 31:       if (i>0)   {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 32:       if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 33:       if (j>0)   {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 34:       if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 35:       v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
 36:     }
 37:   }

 39:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 40:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
 41:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
 42:   MatView(C,PETSC_VIEWER_STDOUT_WORLD);
 43:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
 44:   MatView(C,PETSC_VIEWER_STDOUT_WORLD);
 45: 
 46:   PetscStrcpy(mtype,MATSAME);
 47:   PetscOptionsGetString(PETSC_NULL,"-conv_mat_type",mtype,256,PETSC_NULL);
 48:   MatConvert(C,mtype,MAT_INITIAL_MATRIX,&A);
 49:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
 50:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 51:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
 52:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_IMPL);
 53:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);

 55:   /* Free data structures */
 56:   MatDestroy(A);
 57:   MatDestroy(C);

 59:   PetscFinalize();
 60:   return 0;
 61: }