Actual source code: ex1.c

petsc-3.8.4 2018-03-24
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,changepcside = 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:      Test if you can change the KSP side and type after they have been previously set
107:   */
108:   PetscOptionsGetBool(NULL,NULL,"-change_pc_side",&changepcside,NULL);
109:   if (changepcside) {
110:     KSPSetUp(ksp);
111:     PetscOptionsInsertString(NULL,"-ksp_norm_type unpreconditioned -ksp_pc_side right");
112:     KSPSetFromOptions(ksp);
113:     KSPSetUp(ksp);
114:   }

116:   /*
117:      Set linear solver defaults for this problem (optional).
118:      - By extracting the KSP and PC contexts from the KSP context,
119:        we can then directly call any KSP and PC routines to set
120:        various options.
121:      - The following four statements are optional; all of these
122:        parameters could alternatively be specified at runtime via
123:        KSPSetFromOptions();
124:   */
125:   KSPGetPC(ksp,&pc);
126:   PCSetType(pc,PCJACOBI);
127:   KSPSetTolerances(ksp,1.e-5,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);

129:   /*
130:     Set runtime options, e.g.,
131:         -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
132:     These options will override those specified above as long as
133:     KSPSetFromOptions() is called _after_ any other customization
134:     routines.
135:   */
136:   KSPSetFromOptions(ksp);

138:   if (nonzeroguess) {
139:     PetscScalar p = .5;
140:     VecSet(x,p);
141:     KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
142:   }

144:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
145:                       Solve the linear system
146:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
147:   /*
148:      Solve linear system
149:   */
150:   KSPSolve(ksp,b,x);

152:   /*
153:      View solver info; we could instead use the option -ksp_view to
154:      print this info to the screen at the conclusion of KSPSolve().
155:   */
156:   KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);

158:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
159:                       Check solution and clean up
160:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
161:   /*
162:      Check the error
163:   */
164:   VecAXPY(x,-1.0,u);
165:   VecNorm(x,NORM_2,&norm);
166:   KSPGetIterationNumber(ksp,&its);
167:   PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g, Iterations %D\n",(double)norm,its);

169:   /*
170:      Free work space.  All PETSc objects should be destroyed when they
171:      are no longer needed.
172:   */
173:   VecDestroy(&x); VecDestroy(&u);
174:   VecDestroy(&b); MatDestroy(&A);
175:   KSPDestroy(&ksp);

177:   /*
178:      Always call PetscFinalize() before exiting a program.  This routine
179:        - finalizes the PETSc libraries as well as MPI
180:        - provides summary and diagnostic information if certain runtime
181:          options are chosen (e.g., -log_view).
182:   */
183:   PetscFinalize();
184:   return ierr;
185: }