Date: 2022-03-04T17:39:12+00:00

Actual source code: ex23.c

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

      4: /*T
      5:    Concepts: KSP^basic parallel example;
      6:    Processors: n
      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 uniprocessor example is ex1.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,tol=1000.*PETSC_MACHINE_EPSILON;  /* norm of solution error */
     29:   PetscInt       i,n = 10,col[3],its,rstart,rend,nlocal;
     30:   PetscScalar    one = 1.0,value[3];

     32:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
     33:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);

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

     40:   /*
     41:      Create vectors.  Note that we form 1 vector from scratch and
     42:      then duplicate as needed. For this simple case let PETSc decide how
     43:      many elements of the vector are stored on each processor. The second
     44:      argument to VecSetSizes() below causes PETSc to decide.
     45:   */
     46:   VecCreate(PETSC_COMM_WORLD,&x);
     47:   VecSetSizes(x,PETSC_DECIDE,n);
     48:   VecSetFromOptions(x);
     49:   VecDuplicate(x,&b);
     50:   VecDuplicate(x,&u);

     52:   /* Identify the starting and ending mesh points on each
     53:      processor for the interior part of the mesh. We let PETSc decide
     54:      above. */

     56:   VecGetOwnershipRange(x,&rstart,&rend);
     57:   VecGetLocalSize(x,&nlocal);

     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.

     67:      We pass in nlocal as the "local" size of the matrix to force it
     68:      to have the same parallel layout as the vector created above.
     69:   */
     70:   MatCreate(PETSC_COMM_WORLD,&A);
     71:   MatSetSizes(A,nlocal,nlocal,n,n);
     72:   MatSetFromOptions(A);
     73:   MatSetUp(A);

     75:   /*
     76:      Assemble matrix.

     78:      The linear system is distributed across the processors by
     79:      chunks of contiguous rows, which correspond to contiguous
     80:      sections of the mesh on which the problem is discretized.
     81:      For matrix assembly, each processor contributes entries for
     82:      the part that it owns locally.
     83:   */

     85:   if (!rstart) {
     86:     rstart = 1;
     87:     i      = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0;
     88:     MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
     89:   }
     90:   if (rend == n) {
     91:     rend = n-1;
     92:     i    = n-1; col[0] = n-2; col[1] = n-1; value[0] = -1.0; value[1] = 2.0;
     93:     MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
     94:   }

     96:   /* Set entries corresponding to the mesh interior */
     97:   value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;
     98:   for (i=rstart; i<rend; i++) {
     99:     col[0] = i-1; col[1] = i; col[2] = i+1;
    100:     MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);
    101:   }

    103:   /* Assemble the matrix */
    104:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
    105:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

    107:   /*
    108:      Set exact solution; then compute right-hand-side vector.
    109:   */
    110:   VecSet(u,one);
    111:   MatMult(A,u,b);

    113:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    114:                 Create the linear solver and set various options
    115:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    116:   /*
    117:      Create linear solver context
    118:   */
    119:   KSPCreate(PETSC_COMM_WORLD,&ksp);

    121:   /*
    122:      Set operators. Here the matrix that defines the linear system
    123:      also serves as the preconditioning matrix.
    124:   */
    125:   KSPSetOperators(ksp,A,A);

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

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

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

    157:   /*
    158:      View solver info; we could instead use the option -ksp_view to
    159:      print this info to the screen at the conclusion of KSPSolve().
    160:   */
    161:   KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);

    163:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    164:                       Check solution and clean up
    165:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    166:   /*
    167:      Check the error
    168:   */
    169:   VecAXPY(x,-1.0,u);
    170:   VecNorm(x,NORM_2,&norm);
    171:   KSPGetIterationNumber(ksp,&its);
    172:   if (norm > tol) {
    173:     PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g, Iterations %D\n",(double)norm,its);
    174:   }

    176:   /*
    177:      Free work space.  All PETSc objects should be destroyed when they
    178:      are no longer needed.
    179:   */
    180:   VecDestroy(&x); VecDestroy(&u);
    181:   VecDestroy(&b); MatDestroy(&A);
    182:   KSPDestroy(&ksp);

    184:   /*
    185:      Always call PetscFinalize() before exiting a program.  This routine
    186:        - finalizes the PETSc libraries as well as MPI
    187:        - provides summary and diagnostic information if certain runtime
    188:          options are chosen (e.g., -log_view).
    189:   */
    190:   PetscFinalize();
    191:   return ierr;
    192: }

    194: /*TEST

    196:    build:
    197:       requires: !complex !single

    199:    test:
    200:       args: -ksp_monitor_short -ksp_gmres_cgs_refinement_type refine_always

    202:    test:
    203:       suffix: 2
    204:       nsize: 3
    205:       args: -ksp_monitor_short -ksp_gmres_cgs_refinement_type refine_always

    207:    test:
    208:       suffix: 3
    209:       nsize: 2
    210:       args: -ksp_monitor_short -ksp_rtol 1e-6 -ksp_type pipefgmres

    212: TEST*/