Actual source code: ex48.c


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

  4: /*
  5:     Test example that demonstrates how MINRES can produce a dp of zero
  6:     but still converge.

  8:     Provided by: Mark Filipiak <mjf@staffmail.ed.ac.uk>
  9: */
 10: #include <petscksp.h>

 12: int main(int argc,char **args)
 13: {
 14:   Vec            x, b, u;      /* approx solution, RHS, exact solution */
 15:   Mat            A;            /* linear system matrix */
 16:   KSP            ksp;         /* linear solver context */
 17:   PC             pc;           /* preconditioner context */
 18:   PetscReal      norm;
 20:   PetscInt       i,n = 2,col[3],its;
 21:   PetscMPIInt    size;
 22:   PetscScalar    one = 1.0,value[3];
 23:   PetscBool      nonzeroguess = PETSC_FALSE;

 25:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 26:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 27:   if (size != 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"This is a uniprocessor example only!");
 28:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 29:   PetscOptionsGetBool(NULL,NULL,"-nonzero_guess",&nonzeroguess,NULL);

 31:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 32:          Compute the matrix and right-hand-side vector that define
 33:          the linear system, Ax = b.
 34:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 36:   /*
 37:      Create vectors.  Note that we form 1 vector from scratch and
 38:      then duplicate as needed.
 39:   */
 40:   VecCreate(PETSC_COMM_WORLD,&x);
 41:   PetscObjectSetName((PetscObject) x, "Solution");
 42:   VecSetSizes(x,PETSC_DECIDE,n);
 43:   VecSetFromOptions(x);
 44:   VecDuplicate(x,&b);
 45:   VecDuplicate(x,&u);

 47:   /*
 48:      Create matrix.  When using MatCreate(), the matrix format can
 49:      be specified at runtime.

 51:      Performance tuning note:  For problems of substantial size,
 52:      preallocation of matrix memory is crucial for attaining good
 53:      performance. See the matrix chapter of the users manual for details.
 54:   */
 55:   MatCreate(PETSC_COMM_WORLD,&A);
 56:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
 57:   MatSetFromOptions(A);
 58:   MatSetUp(A);

 60:   /*
 61:      Assemble matrix
 62:   */
 63:   value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;
 64:   for (i=1; i<n-1; i++) {
 65:     col[0] = i-1; col[1] = i; col[2] = i+1;
 66:     MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);
 67:   }
 68:   i    = n - 1; col[0] = n - 2; col[1] = n - 1;
 69:   MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 70:   i    = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0;
 71:   MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 72:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 73:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 75:   /*
 76:      Set constant right-hand-side vector.
 77:   */
 78:   VecSet(b,one);
 79:   /*
 80:      Solution = RHS for the matrix [[2 -1] [-1 2]] and RHS [1 1]
 81:   */
 82:   VecSet(u,one);

 84:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 85:                 Create the linear solver and set various options
 86:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 87:   /*
 88:      Create linear solver context
 89:   */
 90:   KSPCreate(PETSC_COMM_WORLD,&ksp);

 92:   /*
 93:      Set operators. Here the matrix that defines the linear system
 94:      also serves as the preconditioning matrix.
 95:   */
 96:   KSPSetOperators(ksp,A,A);

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

111:   /*
112:     Set runtime options, e.g.,
113:         -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
114:     These options will override those specified above as long as
115:     KSPSetFromOptions() is called _after_ any other customization
116:     routines.
117:   */
118:   KSPSetFromOptions(ksp);

120:   if (nonzeroguess) {
121:     PetscScalar p = .5;
122:     VecSet(x,p);
123:     KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
124:   }

126:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
127:                       Solve the linear system
128:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
129:   /*
130:      Solve linear system
131:   */
132:   KSPSolve(ksp,b,x);

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

140:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
141:                       Check solution and clean up
142:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
143:   /*
144:      Check the error
145:   */
146:   VecAXPY(x,-1.0,u);
147:   VecNorm(x,NORM_2,&norm);
148:   KSPGetIterationNumber(ksp,&its);
149:   PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g, Iterations %D\n",(double)norm,its);

151:   /*
152:      Free work space.  All PETSc objects should be destroyed when they
153:      are no longer needed.
154:   */
155:   VecDestroy(&x); VecDestroy(&u);
156:   VecDestroy(&b); MatDestroy(&A);
157:   KSPDestroy(&ksp);

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

169: /*TEST

171:    test:
172:       args: -ksp_monitor_short -ksp_converged_reason -ksp_type minres -pc_type jacobi -ksp_error_if_not_converged

174: TEST*/