Actual source code: ex4.c

  1: /*$Id: ex4.c,v 1.45 2001/08/07 03:03:43 balay Exp $*/

  3: static char help[] = "Demonstrates the use of fast Richardson for SOR. And testsn
  4: the MatRelax() routines.nn";

 6:  #include petscpc.h

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

 16:   PetscInitialize(&argc,&args,(char *)0,help);

 18:   /* Create vectors */
 19:   VecCreateSeq(PETSC_COMM_SELF,n,&b);
 20:   VecCreateSeq(PETSC_COMM_SELF,n,&u);

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

 36:   /* Create PC context and set up data structures */
 37:   PCCreate(PETSC_COMM_WORLD,&pc);
 38:   PCSetType(pc,PCSOR);
 39:   PCSetFromOptions(pc);
 40:   PCSetOperators(pc,mat,mat,DIFFERENT_NONZERO_PATTERN);
 41:   PCSetVector(pc,u);
 42:   PCSetUp(pc);

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

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