Actual source code: ex1.c

petsc-3.7.5 2017-01-01
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>

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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