Actual source code: ex1.c

petsc-master 2017-01-20
Report Typos and Errors

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

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

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

 17:   Note:  The corresponding parallel example is ex23.c
 18: */
 19:  #include <petscksp.h>

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

 34:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 35:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 36:   if (size != 1) SETERRQ(PETSC_COMM_WORLD,1,"This is a uniprocessor example only!");
 37:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 38:   PetscOptionsGetBool(NULL,NULL,"-nonzero_guess",&nonzeroguess,NULL);


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

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

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

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

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-5,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);

127:   if (nonzeroguess) {
128:     PetscScalar p = .5;
129:     VecSet(x,p);
130:     KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
131:   }

133:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
134:                       Solve the linear system
135:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
136:   /*
137:      Solve linear system
138:   */
139:   KSPSolve(ksp,b,x);

141:   /*
142:      View solver info; we could instead use the option -ksp_view to
143:      print this info to the screen at the conclusion of KSPSolve().
144:   */
145:   KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);

147:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
148:                       Check solution and clean up
149:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
150:   /*
151:      Check the error
152:   */
153:   VecAXPY(x,-1.0,u);
154:   VecNorm(x,NORM_2,&norm);
155:   KSPGetIterationNumber(ksp,&its);
156:   PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g, Iterations %D\n",(double)norm,its);

158:   /*
159:      Free work space.  All PETSc objects should be destroyed when they
160:      are no longer needed.
161:   */
162:   VecDestroy(&x); VecDestroy(&u);
163:   VecDestroy(&b); MatDestroy(&A);
164:   KSPDestroy(&ksp);

166:   /*
167:      Always call PetscFinalize() before exiting a program.  This routine
168:        - finalizes the PETSc libraries as well as MPI
169:        - provides summary and diagnostic information if certain runtime
170:          options are chosen (e.g., -log_view).
171:   */
172:   PetscFinalize();
173:   return ierr;
174: }