Actual source code: ex1.c

  1: /*$Id: ex1.c,v 1.18 2001/08/07 21:30:50 bsmith Exp $*/

  3: static char help[] = "Tests solving linear system on 0 by 0 matrix.nn";

 5:  #include petscsles.h

  7: int main(int argc,char **args)
  8: {
  9:   Mat         C;
 10:   int         ierr,N = 0,its;
 11:   Vec         u,b,x;
 12:   SLES        sles;
 13:   PetscScalar zero = 0.0,mone = -1.0;
 14:   PetscReal   norm;

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

 18:   /* create stiffness matrix */
 19:   MatCreate(PETSC_COMM_SELF,PETSC_DECIDE,PETSC_DECIDE,N,N,&C);
 20:   MatSetFromOptions(C);
 21:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 22:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

 24:   /* create right hand side and solution */

 26:   VecCreateSeq(PETSC_COMM_SELF,N,&u);
 27:   VecDuplicate(u,&b);
 28:   VecDuplicate(u,&x);
 29:   VecSet(&zero,u);
 30:   VecSet(&zero,b);

 32:   VecAssemblyBegin(b);
 33:   VecAssemblyEnd(b);


 36:   /* solve linear system */
 37:   SLESCreate(PETSC_COMM_WORLD,&sles);
 38:   SLESSetOperators(sles,C,C,DIFFERENT_NONZERO_PATTERN);
 39:   SLESSetFromOptions(sles);
 40:   SLESSolve(sles,b,u,&its);

 42:   MatMult(C,u,x);
 43:   VecAXPY(&mone,b,x);
 44:   VecNorm(x,NORM_2,&norm);
 45:   printf("Norm of residual %gn",norm);

 47:   SLESDestroy(sles);
 48:   VecDestroy(u);
 49:   VecDestroy(x);
 50:   VecDestroy(b);
 51:   MatDestroy(C);
 52:   PetscFinalize();
 53:   return 0;
 54: }