Actual source code: ex37.c
petsc-3.4.2 2013-07-02

      2: static char help[] = "Tests MatCopy() and MatStore/RetrieveValues().\n\n";

      4: #include <petscmat.h>

      8: int main(int argc,char **args)
      9: {
     10:   Mat            C,A;
     11:   PetscInt       i, n = 10,midx[3],bs=1;
     13:   PetscScalar    v[3];
     14:   PetscBool      flg,isAIJ;
     15:   MatType        type;
     16:   PetscMPIInt    size;

     18:   PetscInitialize(&argc,&args,(char*)0,help);
     19:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
     20:   PetscOptionsGetInt(NULL,"-n",&n,NULL);
     21:   PetscOptionsGetInt(NULL,"-mat_block_size",&bs,NULL);

     23:   MatCreate(PETSC_COMM_WORLD,&C);
     24:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,n,n);
     25:   MatSetType(C,MATAIJ);
     26:   MatSetFromOptions(C);

     28:   MatGetType(C,&type);
     29:   if (size == 1) {
     30:     PetscObjectTypeCompare((PetscObject)C,MATSEQAIJ,&isAIJ);
     31:   } else {
     32:     PetscObjectTypeCompare((PetscObject)C,MATMPIAIJ,&isAIJ);
     33:   }
     34:   MatSeqAIJSetPreallocation(C,3,NULL);
     35:   MatMPIAIJSetPreallocation(C,3,NULL,3,NULL);
     36:   MatSeqBAIJSetPreallocation(C,bs,3,NULL);
     37:   MatMPIBAIJSetPreallocation(C,bs,3,NULL,3,NULL);

     39:   v[0] = -1.; v[1] = 2.; v[2] = -1.;
     40:   for (i=1; i<n-1; i++) {
     41:     midx[2] = i-1; midx[1] = i; midx[0] = i+1;
     42:     MatSetValues(C,1,&i,3,midx,v,INSERT_VALUES);
     43:   }
     44:   i    = 0; midx[0] = 0; midx[1] = 1;
     45:   v[0] = 2.0; v[1] = -1.;
     46:   MatSetValues(C,1,&i,2,midx,v,INSERT_VALUES);
     47:   i    = n-1; midx[0] = n-2; midx[1] = n-1;
     48:   v[0] = -1.0; v[1] = 2.;
     49:   MatSetValues(C,1,&i,2,midx,v,INSERT_VALUES);

     51:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
     52:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

     54:   MatCreate(PETSC_COMM_WORLD,&A);
     55:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
     56:   MatSetFromOptions(A);
     57:   MatSetUp(A);
     58:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
     59:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

     61:   /* test matrices with different nonzero patterns - Note: A is created with different nonzero pattern of C! */
     62:   MatCopy(C,A,DIFFERENT_NONZERO_PATTERN);
     63:   MatEqual(A,C,&flg);
     64:   if (!flg) SETERRQ(PETSC_COMM_SELF,1,"MatCopy(C,A,DIFFERENT_NONZERO_PATTERN): Matrices are NOT equal");

     66:   PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
     67:   PetscPrintf(PETSC_COMM_WORLD,"A is obtained with MatCopy(,,DIFFERENT_NONZERO_PATTERN):\n");
     68:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
     69:   MatDestroy(&A);

     71:   /* test matrices with same nonzero pattern */
     72:   MatDuplicate(C,MAT_DO_NOT_COPY_VALUES,&A);
     73:   MatCopy(C,A,SAME_NONZERO_PATTERN);
     74:   MatEqual(A,C,&flg);
     75:   if (!flg) SETERRQ(PETSC_COMM_SELF,1,"MatCopy(C,A,SAME_NONZERO_PATTERN): Matrices are NOT equal");

     77:   PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
     78:   PetscPrintf(PETSC_COMM_WORLD,"\nA is obtained with MatCopy(,,SAME_NONZERO_PATTERN):\n");
     79:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);

     81:   PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
     82:   PetscPrintf(PETSC_COMM_WORLD,"A:\n");
     83:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);

     85:   /* test MatStore/RetrieveValues() */
     86:   if (isAIJ) {
     87:     MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
     88:     MatStoreValues(A);
     89:     MatZeroEntries(A);
     90:     MatRetrieveValues(A);
     91:   }

     93:   MatDestroy(&C);
     94:   MatDestroy(&A);
     95:   PetscFinalize();
     96:   return 0;
     97: }