Actual source code: ex1.c

  2: /* Program usage:  mpirun ex1 [-help] [all PETSc options] */

  4: static char help[] = "Solves a tridiagonal linear system with KSP.\n\n";

  6: /*T
  7:    Concepts: KSP^solving a system of linear equations
  8:    Processors: 1
  9: T*/

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

 19:   Note:  The corresponding parallel example is ex23.c
 20: */
 21:  #include petscksp.h

 25: int main(int argc,char **args)
 26: {
 27:   Vec            x, b, u;      /* approx solution, RHS, exact solution */
 28:   Mat            A;            /* linear system matrix */
 29:   KSP            ksp;         /* linear solver context */
 30:   PC             pc;           /* preconditioner context */
 31:   PetscReal      norm;         /* norm of solution error */
 33:   PetscInt       i,n = 10,col[3],its;
 34:   PetscMPIInt    size;
 35:   PetscScalar    neg_one = -1.0,one = 1.0,value[3];

 37:   PetscInitialize(&argc,&args,(char *)0,help);
 38:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 39:   if (size != 1) SETERRQ(1,"This is a uniprocessor example only!");
 40:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);

 42:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 43:          Compute the matrix and right-hand-side vector that define
 44:          the linear system, Ax = b.
 45:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 47:   /* 
 48:      Create vectors.  Note that we form 1 vector from scratch and
 49:      then duplicate as needed.
 50:   */
 51:   VecCreate(PETSC_COMM_WORLD,&x);
 52:   PetscObjectSetName((PetscObject) x, "Solution");
 53:   VecSetSizes(x,PETSC_DECIDE,n);
 54:   VecSetFromOptions(x);
 55:   VecDuplicate(x,&b);
 56:   VecDuplicate(x,&u);

 58:   /* 
 59:      Create matrix.  When using MatCreate(), the matrix format can
 60:      be specified at runtime.

 62:      Performance tuning note:  For problems of substantial size,
 63:      preallocation of matrix memory is crucial for attaining good 
 64:      performance. See the matrix chapter of the users manual for details.
 65:   */
 66:   MatCreate(PETSC_COMM_WORLD,&A);
 67:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
 68:   MatSetFromOptions(A);

 70:   /* 
 71:      Assemble matrix
 72:   */
 73:   value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;
 74:   for (i=1; i<n-1; i++) {
 75:     col[0] = i-1; col[1] = i; col[2] = i+1;
 76:     MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);
 77:   }
 78:   i = n - 1; col[0] = n - 2; col[1] = n - 1;
 79:   MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 80:   i = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0;
 81:   MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 82:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 83:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 85:   /* 
 86:      Set exact solution; then compute right-hand-side vector.
 87:   */
 88:   VecSet(u,one);
 89:   MatMult(A,u,b);

 91:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 92:                 Create the linear solver and set various options
 93:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 94:   /* 
 95:      Create linear solver context
 96:   */
 97:   KSPCreate(PETSC_COMM_WORLD,&ksp);

 99:   /* 
100:      Set operators. Here the matrix that defines the linear system
101:      also serves as the preconditioning matrix.
102:   */
103:   KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);

105:   /* 
106:      Set linear solver defaults for this problem (optional).
107:      - By extracting the KSP and PC contexts from the KSP context,
108:        we can then directly call any KSP and PC routines to set
109:        various options.
110:      - The following four statements are optional; all of these
111:        parameters could alternatively be specified at runtime via
112:        KSPSetFromOptions();
113:   */
114:   KSPGetPC(ksp,&pc);
115:   PCSetType(pc,PCJACOBI);
116:   KSPSetTolerances(ksp,1.e-7,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);

118:   /* 
119:     Set runtime options, e.g.,
120:         -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
121:     These options will override those specified above as long as
122:     KSPSetFromOptions() is called _after_ any other customization
123:     routines.
124:   */
125:   KSPSetFromOptions(ksp);
126: 
127:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
128:                       Solve the linear system
129:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
130:   /* 
131:      Solve linear system
132:   */
133:   KSPSolve(ksp,b,x);

135:   /* 
136:      View solver info; we could instead use the option -ksp_view to
137:      print this info to the screen at the conclusion of KSPSolve().
138:   */
139:   KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);

141:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
142:                       Check solution and clean up
143:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
144:   /* 
145:      Check the error
146:   */
147:   VecAXPY(x,neg_one,u);
148:   VecNorm(x,NORM_2,&norm);
149:   KSPGetIterationNumber(ksp,&its);
150:   PetscPrintf(PETSC_COMM_WORLD,"Norm of error %A, Iterations %D\n",
151:                      norm,its);
152:   /* 
153:      Free work space.  All PETSc objects should be destroyed when they
154:      are no longer needed.
155:   */
156:   VecDestroy(x); VecDestroy(u);
157:   VecDestroy(b); MatDestroy(A);
158:   KSPDestroy(ksp);

160:   /*
161:      Always call PetscFinalize() before exiting a program.  This routine
162:        - finalizes the PETSc libraries as well as MPI
163:        - provides summary and diagnostic information if certain runtime
164:          options are chosen (e.g., -log_summary).
165:   */
166:   PetscFinalize();
167:   return 0;
168: }