Date: 2013-07-02T14:49:02+00:00

Actual source code: ex58.c
petsc-3.4.2 2013-07-02

      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:   Modified from ex1.c for testing matrix operations when matrix structure is changed.
     11:   Contributed by Jose E. Roman, Feb. 2012.
     12: */
     13: #include <petscksp.h>

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

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

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

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

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

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

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

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

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

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

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

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

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

    142:   if (nonzeroguess) {
    143:     PetscScalar p = .5;
    144:     VecSet(x,p);
    145:     KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
    146:   }

    148:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    149:                       Solve the linear system
    150:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    151:   /*
    152:      Solve linear system
    153:   */
    154:   KSPSolve(ksp,b,x);

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

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

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