Actual source code: ex16.c

  1: /*$Id: ex16.c,v 1.25 2001/08/07 21:30:54 bsmith Exp $*/

  3: /* Usage:  mpirun ex16 [-help] [all PETSc options] */

  5: static char help[] = "Solves a sequence of linear systems with different right-hand-side vectors.n
  6: Input parameters include:n
  7:   -ntimes <ntimes>  : number of linear systems to solven
  8:   -view_exact_sol   : write exact solution vector to stdoutn
  9:   -m <mesh_x>       : number of mesh points in x-directionn
 10:   -n <mesh_n>       : number of mesh points in y-directionnn";

 12: /*T
 13:    Concepts: SLES^repeatedly solving linear systems;
 14:    Concepts: SLES^Laplacian, 2d
 15:    Concepts: Laplacian, 2d
 16:    Processors: n
 17: T*/

 19: /* 
 20:   Include "petscsles.h" so that we can use SLES solvers.  Note that this file
 21:   automatically includes:
 22:      petsc.h       - base PETSc routines   petscvec.h - vectors
 23:      petscsys.h    - system routines       petscmat.h - matrices
 24:      petscis.h     - index sets            petscksp.h - Krylov subspace methods
 25:      petscviewer.h - viewers               petscpc.h  - preconditioners
 26: */
 27:  #include petscsles.h

 29: #undef __FUNCT__
 31: int main(int argc,char **args)
 32: {
 33:   Vec         x,b,u;  /* approx solution, RHS, exact solution */
 34:   Mat         A;        /* linear system matrix */
 35:   SLES        sles;     /* linear solver context */
 36:   PetscReal   norm;     /* norm of solution error */
 37:   int         ntimes,i,j,k,I,J,Istart,Iend,ierr;
 38:   int         m = 8,n = 7,its;
 39:   PetscTruth  flg;
 40:   PetscScalar v,one = 1.0,neg_one = -1.0,rhs;

 42:   PetscInitialize(&argc,&args,(char *)0,help);
 43:   PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
 44:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);

 46:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 47:          Compute the matrix for use in solving a series of
 48:          linear systems of the form, A x_i = b_i, for i=1,2,...
 49:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 50:   /* 
 51:      Create parallel matrix, specifying only its global dimensions.
 52:      When using MatCreate(), the matrix format can be specified at
 53:      runtime. Also, the parallel partitioning of the matrix is
 54:      determined by PETSc at runtime.
 55:   */
 56:   MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n,&A);
 57:   MatSetFromOptions(A);

 59:   /* 
 60:      Currently, all PETSc parallel matrix formats are partitioned by
 61:      contiguous chunks of rows across the processors.  Determine which
 62:      rows of the matrix are locally owned. 
 63:   */
 64:   MatGetOwnershipRange(A,&Istart,&Iend);

 66:   /* 
 67:      Set matrix elements for the 2-D, five-point stencil in parallel.
 68:       - Each processor needs to insert only elements that it owns
 69:         locally (but any non-local elements will be sent to the
 70:         appropriate processor during matrix assembly). 
 71:       - Always specify global rows and columns of matrix entries.
 72:    */
 73:   for (I=Istart; I<Iend; I++) {
 74:     v = -1.0; i = I/n; j = I - i*n;
 75:     if (i>0)   {J = I - n; MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);}
 76:     if (i<m-1) {J = I + n; MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);}
 77:     if (j>0)   {J = I - 1; MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);}
 78:     if (j<n-1) {J = I + 1; MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);}
 79:     v = 4.0; MatSetValues(A,1,&I,1,&I,&v,INSERT_VALUES);
 80:   }

 82:   /* 
 83:      Assemble matrix, using the 2-step process:
 84:        MatAssemblyBegin(), MatAssemblyEnd()
 85:      Computations can be done while messages are in transition
 86:      by placing code between these two statements.
 87:   */
 88:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 89:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 91:   /* 
 92:      Create parallel vectors.
 93:       - When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
 94:         we specify only the vector's global
 95:         dimension; the parallel partitioning is determined at runtime. 
 96:       - When solving a linear system, the vectors and matrices MUST
 97:         be partitioned accordingly.  PETSc automatically generates
 98:         appropriately partitioned matrices and vectors when MatCreate()
 99:         and VecCreate() are used with the same communicator. 
100:       - Note: We form 1 vector from scratch and then duplicate as needed.
101:   */
102:   VecCreate(PETSC_COMM_WORLD,&u);
103:   VecSetSizes(u,PETSC_DECIDE,m*n);
104:   VecSetFromOptions(u);
105:   VecDuplicate(u,&b);
106:   VecDuplicate(b,&x);

108:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
109:                 Create the linear solver and set various options
110:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

112:   /* 
113:      Create linear solver context
114:   */
115:   SLESCreate(PETSC_COMM_WORLD,&sles);

117:   /* 
118:      Set operators. Here the matrix that defines the linear system
119:      also serves as the preconditioning matrix.
120:   */
121:   SLESSetOperators(sles,A,A,SAME_PRECONDITIONER);

123:   /* 
124:     Set runtime options, e.g.,
125:         -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
126:     These options will override those specified above as long as
127:     SLESSetFromOptions() is called _after_ any other customization
128:     routines.
129:   */
130:   SLESSetFromOptions(sles);

132:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
133:        Solve several linear systems of the form  A x_i = b_i
134:        I.e., we retain the same matrix (A) for all systems, but
135:        change the right-hand-side vector (b_i) at each step.

137:        In this case, we simply call SLESSolve() multiple times.  The
138:        preconditioner setup operations (e.g., factorization for ILU)
139:        be done during the first call to SLESSolve() only; such operations
140:        will NOT be repeated for successive solves.
141:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

143:   ntimes = 2;
144:   PetscOptionsGetInt(PETSC_NULL,"-ntimes",&ntimes,PETSC_NULL);
145:   for (k=1; k<ntimes+1; k++) {

147:     /* 
148:        Set exact solution; then compute right-hand-side vector.  We use
149:        an exact solution of a vector with all elements equal to 1.0*k.
150:     */
151:     rhs = one * (PetscReal)k;
152:     VecSet(&rhs,u);
153:     MatMult(A,u,b);

155:     /*
156:        View the exact solution vector if desired
157:     */
158:     PetscOptionsHasName(PETSC_NULL,"-view_exact_sol",&flg);
159:     if (flg) {VecView(u,PETSC_VIEWER_STDOUT_WORLD);}

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

163:     /* 
164:        Check the error
165:     */
166:     VecAXPY(&neg_one,u,x);
167:     VecNorm(x,NORM_2,&norm);

169:     /*
170:        Print convergence information.  PetscPrintf() produces a single 
171:        print statement from all processes that share a communicator.
172:     */
173:     PetscPrintf(PETSC_COMM_WORLD,"Norm of error %A System %d: iterations %dn",norm,k,its);
174:   }

176:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
177:                       Clean up
178:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
179:   /* 
180:      Free work space.  All PETSc objects should be destroyed when they
181:      are no longer needed.
182:   */
183:   SLESDestroy(sles);
184:   VecDestroy(u);  VecDestroy(x);
185:   VecDestroy(b);  MatDestroy(A);

187:   /*
188:      Always call PetscFinalize() before exiting a program.  This routine
189:        - finalizes the PETSc libraries as well as MPI
190:        - provides summary and diagnostic information if certain runtime
191:          options are chosen (e.g., -log_summary). 
192:   */
193:   PetscFinalize();
194:   return 0;
195: }