Actual source code: ex20.c

petsc-3.12.5 2020-03-29
Report Typos and Errors

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

  4:  #include <petscmat.h>

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

 15:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 16:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

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

 22:   /* Create the matrix for the five point stencil, YET AGAIN */
 23:   MatCreate(PETSC_COMM_WORLD,&C);
 24:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
 25:   MatSetFromOptions(C);
 26:   MatSetUp(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:   }

 38:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 39:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
 40:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
 41:   MatView(C,PETSC_VIEWER_STDOUT_WORLD);
 42:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
 43:   MatView(C,PETSC_VIEWER_STDOUT_WORLD);

 45:   PetscStrcpy(mtype,MATSAME);
 46:   PetscOptionsGetString(NULL,NULL,"-conv_mat_type",mtype,256,NULL);
 47:   MatConvert(C,mtype,MAT_INITIAL_MATRIX,&A);
 48:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
 49:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 50:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
 51:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_IMPL);
 52:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 53:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);

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

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


 64: /*TEST

 66:    test:
 67:       args: -conv_mat_type seqaij

 69: TEST*/