Actual source code: ex37.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

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

  4:  #include <petscmat.h>

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

 16:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 17:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 18:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 19:   PetscOptionsGetInt(NULL,NULL,"-mat_block_size",&bs,NULL);

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

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

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

 49:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 50:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

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

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

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

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

 76:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
 77:   PetscPrintf(PETSC_COMM_WORLD,"\nA is obtained with MatCopy(,,SAME_NONZERO_PATTERN):\n");
 78:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 79:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
 80: 
 81:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
 82:   PetscPrintf(PETSC_COMM_WORLD,"A:\n");
 83:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 84:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
 85: 
 86:   /* test MatStore/RetrieveValues() */
 87:   if (isAIJ) {
 88:     MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
 89:     MatStoreValues(A);
 90:     MatZeroEntries(A);
 91:     MatRetrieveValues(A);
 92:   }

 94:   MatDestroy(&C);
 95:   MatDestroy(&A);
 96:   PetscFinalize();
 97:   return ierr;
 98: }