Actual source code: ex7.c

  1: /*$Id: ex7.c,v 1.20 2001/09/11 16:33:24 bsmith Exp $*/

  3: static char help[] = "Reads a PETSc matrix and vector from a file and solves a linear system.n
  4:  Tests inplace factorization for SeqBAIJ. Input parameters includen
  5:   -f0 <input_file> : first file to load (small system)nn";

  7: /*T
  8:    Concepts: SLES^solving a linear system
  9:    Concepts: PetscLog^profiling multiple stages of code;
 10:    Processors: n
 11: T*/

 13: /* 
 14:   Include "petscsles.h" so that we can use SLES solvers.  Note that this file
 15:   automatically includes:
 16:      petsc.h       - base PETSc routines   petscvec.h - vectors
 17:      petscsys.h    - system routines       petscmat.h - matrices
 18:      petscis.h     - index sets            petscksp.h - Krylov subspace methods
 19:      petscviewer.h - viewers               petscpc.h  - preconditioners
 20: */
 21:  #include petscsles.h

 23: int main(int argc,char **args)
 24: {
 25:   SLES        sles;             /* linear solver context */
 26:   Mat         A,B;                /* matrix */
 27:   Vec         x,b,u;          /* approx solution, RHS, exact solution */
 28:   PetscViewer fd;               /* viewer */
 29:   char        file[2][128];     /* input file name */
 30:   int         ierr,its;
 31:   PetscTruth  flg;
 32:   PetscReal   norm;
 33:   PetscScalar zero = 0.0,none = -1.0;

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

 37:   /* 
 38:      Determine files from which we read the two linear systems
 39:      (matrix and right-hand-side vector).
 40:   */
 41:   PetscOptionsGetString(PETSC_NULL,"-f0",file[0],127,&flg);
 42:   if (!flg) SETERRQ(1,"Must indicate binary file with the -f0 option");


 45:   /* 
 46:        Open binary file.  Note that we use PETSC_BINARY_RDONLY to indicate
 47:        reading from this file.
 48:   */
 49:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[0],PETSC_BINARY_RDONLY,&fd);

 51:   /*
 52:        Load the matrix and vector; then destroy the viewer.
 53:   */
 54:   MatLoad(fd,MATSEQBAIJ,&A);
 55:   MatConvert(A,MATSAME,&B);
 56:   VecLoad(fd,&b);
 57:   PetscViewerDestroy(fd);

 59:   /* 
 60:        If the loaded matrix is larger than the vector (due to being padded 
 61:        to match the block size of the system), then create a new padded vector.
 62:   */
 63:   {
 64:       int    m,n,j,mvec,start,end,index;
 65:       Vec    tmp;
 66:       PetscScalar *bold;

 68:       /* Create a new vector b by padding the old one */
 69:       MatGetLocalSize(A,&m,&n);
 70:       VecCreate(PETSC_COMM_WORLD,&tmp);
 71:       VecSetSizes(tmp,m,PETSC_DECIDE);
 72:       VecSetFromOptions(tmp);
 73:       VecGetOwnershipRange(b,&start,&end);
 74:       VecGetLocalSize(b,&mvec);
 75:       VecGetArray(b,&bold);
 76:       for (j=0; j<mvec; j++) {
 77:         index = start+j;
 78:         ierr  = VecSetValues(tmp,1,&index,bold+j,INSERT_VALUES);
 79:       }
 80:       VecRestoreArray(b,&bold);
 81:       VecDestroy(b);
 82:       VecAssemblyBegin(tmp);
 83:       VecAssemblyEnd(tmp);
 84:       b = tmp;
 85:     }
 86:   VecDuplicate(b,&x);
 87:   VecDuplicate(b,&u);
 88:   VecSet(&zero,x);

 90:   /*
 91:       Create linear solver; set operators; set runtime options.
 92:   */
 93:   SLESCreate(PETSC_COMM_WORLD,&sles);
 94:   SLESSetOperators(sles,A,B,DIFFERENT_NONZERO_PATTERN);
 95:   SLESSetFromOptions(sles);

 97:   /* 
 98:        Here we explicitly call SLESSetUp() and SLESSetUpOnBlocks() to
 99:        enable more precise profiling of setting up the preconditioner.
100:        These calls are optional, since both will be called within
101:        SLESSolve() if they haven't been called already.
102:   */
103:   SLESSetUp(sles,b,x);
104:   SLESSetUpOnBlocks(sles);

106:   SLESSolve(sles,b,x,&its);

108:   /*
109:             Check error, print output, free data structures.
110:             This stage is not profiled separately.
111:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

113:   /* 
114:      Check error
115:   */
116:   MatMult(A,x,u);
117:   VecAXPY(&none,b,u);
118:   VecNorm(u,NORM_2,&norm);

120:   PetscPrintf(PETSC_COMM_WORLD,"Number of iterations = %3dn",its);
121:   PetscPrintf(PETSC_COMM_WORLD,"Residual norm = %An",norm);

123:   /* 
124:        Free work space.  All PETSc objects should be destroyed when they
125:        are no longer needed.
126:   */
127:   MatDestroy(A);
128:   MatDestroy(B);
129:   VecDestroy(b);
130:   VecDestroy(u); VecDestroy(x);
131:   SLESDestroy(sles);


134:   PetscFinalize();
135:   return 0;
136: }