Actual source code: ex4.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  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>

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

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

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

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

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

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

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