Actual source code: ex3.c

  1: /*$Id: ex3.c,v 1.19 2001/08/07 21:30:08 bsmith Exp $*/

  3: static char help[] = "Tests relaxation for dense matrices.nn";

 5:  #include petscmat.h

  7: int main(int argc,char **args)
  8: {
  9:   Mat         C;
 10:   Vec         u,x,b,e;
 11:   int         i,n = 10,midx[3],ierr;
 12:   PetscScalar v[3],one = 1.0,zero = 0.0,mone = -1.0;
 13:   PetscReal   omega = 1.0,norm;

 15:   PetscInitialize(&argc,&args,(char *)0,help);
 16:   PetscOptionsGetReal(PETSC_NULL,"-omega",&omega,PETSC_NULL);
 17:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);

 19:   MatCreate(PETSC_COMM_SELF,n,n,n,n,&C);
 20:   MatSetType(C,MATSEQDENSE);
 21:   VecCreateSeq(PETSC_COMM_SELF,n,&b);
 22:   VecCreateSeq(PETSC_COMM_SELF,n,&x);
 23:   VecCreateSeq(PETSC_COMM_SELF,n,&u);
 24:   VecCreateSeq(PETSC_COMM_SELF,n,&e);
 25:   VecSet(&one,u);
 26:   VecSet(&zero,x);

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

 40:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 41:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

 43:   MatMult(C,u,b);

 45:   for (i=0; i<n; i++) {
 46:     MatRelax(C,b,omega,SOR_FORWARD_SWEEP,0.0,1,1,x);
 47:     VecWAXPY(&mone,x,u,e);
 48:     VecNorm(e,NORM_2,&norm);
 49:     PetscPrintf(PETSC_COMM_SELF,"2-norm of error %gn",norm);
 50:   }
 51:   MatDestroy(C);
 52:   VecDestroy(x);
 53:   VecDestroy(b);
 54:   VecDestroy(u);
 55:   VecDestroy(e);
 56:   PetscFinalize();
 57:   return 0;
 58: }

 60: