Actual source code: ex172.c

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

  2: static char help[] = "Test MatAXPY and SUBSET_NONZERO_PATTERN [-different] [-skip]\n by default subset pattern is used \n\n";

  4: /* A test contributed by Jose E. Roman, Oct. 2014 */

  6:  #include <petscmat.h>

  8: int main(int argc,char **args)
  9: {
 11:   Mat            A,B,C;
 12:   PetscBool      different=PETSC_FALSE,skip=PETSC_FALSE;
 13:   PetscInt       m0,m1,n=128,i;

 15:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 16:   PetscOptionsGetBool(NULL,NULL,"-different",&different,NULL);
 17:   PetscOptionsGetBool(NULL,NULL,"-skip",&skip,NULL);
 18:   /*
 19:      Create matrices
 20:      A = tridiag(1,-2,1) and B = diag(7);
 21:   */
 22:   MatCreate(PETSC_COMM_WORLD,&A);
 23:   MatCreate(PETSC_COMM_WORLD,&B);
 24:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
 25:   MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n,n);
 26:   MatSetFromOptions(A);
 27:   MatSetFromOptions(B);
 28:   MatSetUp(A);
 29:   MatSetUp(B);
 30:   MatGetOwnershipRange(A,&m0,&m1);
 31:   for (i=m0;i<m1;i++) {
 32:     if (i>0) { MatSetValue(A,i,i-1,-1.0,INSERT_VALUES); }
 33:     if (i<n-1) { MatSetValue(A,i,i+1,-1.0,INSERT_VALUES); }
 34:     MatSetValue(A,i,i,2.0,INSERT_VALUES);
 35:     MatSetValue(B,i,i,7.0,INSERT_VALUES);
 36:   }
 37:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 38:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
 39:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
 40:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

 42:   MatDuplicate(A,MAT_COPY_VALUES,&C);
 43:   /* Add B */
 44:   MatAXPY(C,1.0,B,(different)?DIFFERENT_NONZERO_PATTERN:SUBSET_NONZERO_PATTERN);
 45:   /* Add A */
 46:   if (!skip) { MatAXPY(C,1.0,A,SUBSET_NONZERO_PATTERN); }

 48:   /*
 49:      Free memory
 50:   */
 51:   MatDestroy(&A);
 52:   MatDestroy(&B);
 53:   MatDestroy(&C);
 54:   PetscFinalize();
 55:   return ierr;
 56: }