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

      2: static char help[] = "KSP linear solver on an operator with a null space.\n\n";

      4: #include <petscksp.h>

      8: int main(int argc,char **args)
      9: {
     10:   Vec            x,b,u;      /* approx solution, RHS, exact solution */
     11:   Mat            A;            /* linear system matrix */
     12:   KSP            ksp;         /* KSP context */
     14:   PetscInt       i,n = 10,col[3],its,i1,i2;
     15:   PetscScalar    none = -1.0,value[3],avalue;
     16:   PetscReal      norm;
     17:   PC             pc;

     19:   PetscInitialize(&argc,&args,(char*)0,help);
     20:   PetscOptionsGetInt(NULL,"-n",&n,NULL);

     22:   /* Create vectors */
     23:   VecCreate(PETSC_COMM_WORLD,&x);
     24:   VecSetSizes(x,PETSC_DECIDE,n);
     25:   VecSetFromOptions(x);
     26:   VecDuplicate(x,&b);
     27:   VecDuplicate(x,&u);

     29:   /* create a solution that is orthogonal to the constants */
     30:   VecGetOwnershipRange(u,&i1,&i2);
     31:   for (i=i1; i<i2; i++) {
     32:     avalue = i;
     33:     VecSetValues(u,1,&i,&avalue,INSERT_VALUES);
     34:   }
     35:   VecAssemblyBegin(u);
     36:   VecAssemblyEnd(u);
     37:   VecSum(u,&avalue);
     38:   avalue = -avalue/(PetscReal)n;
     39:   VecShift(u,avalue);

     41:   /* Create and assemble matrix */
     42:   MatCreate(PETSC_COMM_WORLD,&A);
     43:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
     44:   MatSetFromOptions(A);
     45:   value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;
     46:   for (i=1; i<n-1; i++) {
     47:     col[0] = i-1; col[1] = i; col[2] = i+1;
     48:     MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);
     49:   }
     50:   i    = n - 1; col[0] = n - 2; col[1] = n - 1; value[1] = 1.0;
     51:   MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
     52:   i    = 0; col[0] = 0; col[1] = 1; value[0] = 1.0; value[1] = -1.0;
     53:   MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
     54:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
     55:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
     56:   MatMult(A,u,b);

     58:   /* Create KSP context; set operators and options; solve linear system */
     59:   KSPCreate(PETSC_COMM_WORLD,&ksp);
     60:   KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);

     62:   /* Insure that preconditioner has same null space as matrix */
     63:   /* currently does not do anything */
     64:   KSPGetPC(ksp,&pc);

     66:   KSPSetFromOptions(ksp);
     67:   KSPSolve(ksp,b,x);
     68:   /* KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD); */

     70:   /* Check error */
     71:   VecAXPY(x,none,u);
     72:   VecNorm(x,NORM_2,&norm);
     73:   KSPGetIterationNumber(ksp,&its);
     74:   PetscPrintf(PETSC_COMM_WORLD,"Norm of error %G, Iterations %D\n",norm,its);

     76:   /* Free work space */
     77:   VecDestroy(&x);VecDestroy(&u);
     78:   VecDestroy(&b);MatDestroy(&A);
     79:   KSPDestroy(&ksp);
     80:   PetscFinalize();
     81:   return 0;
     82: }