Actual source code: ex55.c

  1: static const char help[]="Example demonstrating PCCOMPOSITE where one of the inner PCs uses a different operator\n\
  2: \n";

  4: #include <petscksp.h>

  6: int main(int argc, char **argv)
  7: {
  8:   PetscInt       n=10,i,col[3];
  9:   Vec            x,b;
 10:   Mat            A,B;
 11:   KSP            ksp;
 12:   PC             pc,subpc;
 13:   PetscScalar    value[3];

 15:   PetscInitialize(&argc,&argv,(char*)0,help);

 17:   /* Create a diagonal matrix with a given distribution of diagonal elements */
 18:   MatCreate(PETSC_COMM_WORLD,&A);
 19:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
 20:   MatSetFromOptions(A);
 21:   MatSetUp(A);
 22:    /*
 23:      Assemble matrix
 24:   */
 25:   value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;
 26:   for (i=1; i<n-1; i++) {
 27:     col[0] = i-1; col[1] = i; col[2] = i+1;
 28:     MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);
 29:   }
 30:   i    = n - 1; col[0] = n - 2; col[1] = n - 1;
 31:   MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 32:   i    = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0;
 33:   MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 34:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 35:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 37:   MatCreateVecs(A,&x,&b);

 39:   /* Create a KSP object */
 40:   KSPCreate(PETSC_COMM_WORLD,&ksp);
 41:   KSPSetOperators(ksp,A,A);

 43:   /* Set up a composite preconditioner */
 44:   KSPGetPC(ksp,&pc);
 45:   PCSetType(pc,PCCOMPOSITE); /* default composite with single Identity PC */
 46:   PCCompositeSetType(pc,PC_COMPOSITE_ADDITIVE);
 47:   PCCompositeAddPCType(pc,PCLU);
 48:   PCCompositeGetPC(pc,0,&subpc);
 49:   /*  B is set to the diagonal of A; this demonstrates that setting the operator for a subpc changes the preconditioning */
 50:   MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&B);
 51:   MatGetDiagonal(A,b);
 52:   MatDiagonalSet(B,b,ADD_VALUES);
 53:   PCSetOperators(subpc,B,B);
 54:   PCCompositeAddPCType(pc,PCNONE);

 56:   KSPSetFromOptions(ksp);
 57:   KSPSolve(ksp,b,x);

 59:   KSPDestroy(&ksp);
 60:   MatDestroy(&A);
 61:   MatDestroy(&B);
 62:   VecDestroy(&x);
 63:   VecDestroy(&b);
 64:   PetscFinalize();
 65:   return 0;
 66: }

 68: /*TEST

 70:    test:
 71:      args: -ksp_monitor -pc_composite_type multiplicative

 73: TEST*/