Actual source code: ex3.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  2: static char help[] = "Tests relaxation for dense matrices.\n\n";

  4: #include <petscmat.h>

  8: int main(int argc,char **args)
  9: {
 10:   Mat            C;
 11:   Vec            u,x,b,e;
 12:   PetscInt       i,n = 10,midx[3];
 14:   PetscScalar    v[3];
 15:   PetscReal      omega = 1.0,norm;

 17:   PetscInitialize(&argc,&args,(char*)0,help);
 18:   PetscOptionsGetReal(NULL,"-omega",&omega,NULL);
 19:   PetscOptionsGetInt(NULL,"-n",&n,NULL);

 21:   MatCreate(PETSC_COMM_SELF,&C);
 22:   MatSetSizes(C,n,n,n,n);
 23:   MatSetType(C,MATSEQDENSE);
 24:   MatSetUp(C);
 25:   VecCreateSeq(PETSC_COMM_SELF,n,&b);
 26:   VecCreateSeq(PETSC_COMM_SELF,n,&x);
 27:   VecCreateSeq(PETSC_COMM_SELF,n,&u);
 28:   VecCreateSeq(PETSC_COMM_SELF,n,&e);
 29:   VecSet(u,1.0);
 30:   VecSet(x,0.0);

 32:   v[0] = -1.; v[1] = 2.; v[2] = -1.;
 33:   for (i=1; i<n-1; i++) {
 34:     midx[0] = i-1; midx[1] = i; midx[2] = i+1;
 35:     MatSetValues(C,1,&i,3,midx,v,INSERT_VALUES);
 36:   }
 37:   i    = 0; midx[0] = 0; midx[1] = 1;
 38:   v[0] = 2.0; v[1] = -1.;
 39:   MatSetValues(C,1,&i,2,midx,v,INSERT_VALUES);
 40:   i    = n-1; midx[0] = n-2; midx[1] = n-1;
 41:   v[0] = -1.0; v[1] = 2.;
 42:   MatSetValues(C,1,&i,2,midx,v,INSERT_VALUES);

 44:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 45:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

 47:   MatMult(C,u,b);

 49:   for (i=0; i<n; i++) {
 50:     MatSOR(C,b,omega,SOR_FORWARD_SWEEP,0.0,1,1,x);
 51:     VecWAXPY(e,-1.0,x,u);
 52:     VecNorm(e,NORM_2,&norm);
 53:     PetscPrintf(PETSC_COMM_SELF,"2-norm of error %g\n",(double)norm);
 54:   }
 55:   MatDestroy(&C);
 56:   VecDestroy(&x);
 57:   VecDestroy(&b);
 58:   VecDestroy(&u);
 59:   VecDestroy(&e);
 60:   PetscFinalize();
 61:   return 0;
 62: }