Actual source code: ex58.c

petsc-3.3-p7 2013-05-11
  2: /* Program usage:  mpiexec 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:   Modified from ex1.c for testing matrix operations when matrix structure is changed.
 13:   Contributed by Jose E. Roman, Feb. 2012.
 14: */
 15: #include <petscksp.h>

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

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


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

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

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

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

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

 83:   /* 
 84:      jroman: added matrices
 85:   */
 86:   MatCreate(PETSC_COMM_WORLD,&B);
 87:   MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n,n);
 88:   MatSetFromOptions(B);
 89:   MatSetUp(B);
 90:   for (i=0; i<n; i++) {
 91:     MatSetValue(B,i,i,value[1],INSERT_VALUES);
 92:     if (n-i+n/3<n) {
 93:       MatSetValue(B,n-i+n/3,i,value[0],INSERT_VALUES);
 94:       MatSetValue(B,i,n-i+n/3,value[0],INSERT_VALUES);
 95:     }
 96:   }
 97:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
 98:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
 99:   MatDuplicate(A,MAT_COPY_VALUES,&C);
100:   MatAXPY(C,2.0,B,DIFFERENT_NONZERO_PATTERN);

102:   /* 
103:      Set exact solution; then compute right-hand-side vector.
104:   */
105:   VecSet(u,one);
106:   MatMult(C,u,b);

108:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
109:                 Create the linear solver and set various options
110:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
111:   /* 
112:      Create linear solver context
113:   */
114:   KSPCreate(PETSC_COMM_WORLD,&ksp);

116:   /* 
117:      Set operators. Here the matrix that defines the linear system
118:      also serves as the preconditioning matrix.
119:   */
120:   KSPSetOperators(ksp,C,C,DIFFERENT_NONZERO_PATTERN);

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

135:   /* 
136:     Set runtime options, e.g.,
137:         -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
138:     These options will override those specified above as long as
139:     KSPSetFromOptions() is called _after_ any other customization
140:     routines.
141:   */
142:   KSPSetFromOptions(ksp);

144:   if (nonzeroguess) {
145:     PetscScalar p = .5;
146:     VecSet(x,p);
147:     KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
148:   }
149: 
150:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
151:                       Solve the linear system
152:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
153:   /* 
154:      Solve linear system
155:   */
156:   KSPSolve(ksp,b,x);

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

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

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