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.\n\n";

 5:  #include petscsles.h

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

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

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

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

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

 34:   VecAssemblyBegin(b);
 35:   VecAssemblyEnd(b);


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

 44:   MatMult(C,u,x);
 45:   VecAXPY(&mone,b,x);
 46:   VecNorm(x,NORM_2,&norm);
 47:   printf("Norm of residual %g\n",norm);

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