Actual source code: ex4.c


  2: static char help[] = "Demonstrates the use of fast Richardson for SOR. And tests\n\
  3: the MatSOR() routines.\n\n";

  5: #include <petscpc.h>

  7: int main(int argc,char **args)
  8: {
  9:   Mat            mat;
 10:   Vec            b,u;
 11:   PC             pc;
 12:   PetscInt       n = 5,i,col[3];
 13:   PetscScalar    value[3];

 15:   PetscInitialize(&argc,&args,(char*)0,help);
 16:   /* Create vectors */
 17:   VecCreateSeq(PETSC_COMM_SELF,n,&b);
 18:   VecCreateSeq(PETSC_COMM_SELF,n,&u);

 20:   /* Create and assemble matrix */
 21:   MatCreateSeqDense(PETSC_COMM_SELF,n,n,NULL,&mat);
 22:   value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;
 23:   for (i=1; i<n-1; i++) {
 24:     col[0] = i-1; col[1] = i; col[2] = i+1;
 25:     MatSetValues(mat,1,&i,3,col,value,INSERT_VALUES);
 26:   }
 27:   i    = n - 1; col[0] = n - 2; col[1] = n - 1;
 28:   MatSetValues(mat,1,&i,2,col,value,INSERT_VALUES);
 29:   i    = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0;
 30:   MatSetValues(mat,1,&i,2,col,value,INSERT_VALUES);
 31:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
 32:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);

 34:   /* Create PC context and set up data structures */
 35:   PCCreate(PETSC_COMM_WORLD,&pc);
 36:   PCSetType(pc,PCSOR);
 37:   PCSetFromOptions(pc);
 38:   PCSetOperators(pc,mat,mat);
 39:   PCSetUp(pc);

 41:   value[0] = 1.0;
 42:   for (i=0; i<n; i++) {
 43:     VecSet(u,0.0);
 44:     VecSetValues(u,1,&i,value,INSERT_VALUES);
 45:     VecAssemblyBegin(u);
 46:     VecAssemblyEnd(u);
 47:     PCApply(pc,u,b);
 48:     VecView(b,PETSC_VIEWER_STDOUT_SELF);
 49:   }

 51:   /* Free data structures */
 52:   MatDestroy(&mat);
 53:   PCDestroy(&pc);
 54:   VecDestroy(&u);
 55:   VecDestroy(&b);
 56:   PetscFinalize();
 57:   return 0;
 58: }

 60: /*TEST

 62:    test:

 64: TEST*/