Actual source code: ex4.c

petsc-3.13.6 2020-09-29
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>

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

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

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

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

 42:   value[0] = 1.0;
 43:   for (i=0; i<n; i++) {
 44:     VecSet(u,0.0);
 45:     VecSetValues(u,1,&i,value,INSERT_VALUES);
 46:     VecAssemblyBegin(u);
 47:     VecAssemblyEnd(u);
 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 ierr;
 59: }

 61: /*TEST

 63:    test:

 65: TEST*/