xref: /petsc/src/ksp/ksp/tests/ex85.c (revision 226f8a8a5081bc6ad7227cd631662400f0d6e2a0)
11bec1829SPablo Brubeck static char help[] = "Estimate eigenvalues with KSP.\n\n";
21bec1829SPablo Brubeck 
31bec1829SPablo Brubeck /*
41bec1829SPablo Brubeck     Test example that demonstrates how KSP can estimate eigenvalues.
51bec1829SPablo Brubeck 
61bec1829SPablo Brubeck     Contributed by: Pablo Brubeck <brubeck@protonmail.com>
71bec1829SPablo Brubeck */
81bec1829SPablo Brubeck #include <petscksp.h>
91bec1829SPablo Brubeck 
101bec1829SPablo Brubeck int main(int argc, char **args)
111bec1829SPablo Brubeck {
121bec1829SPablo Brubeck   Vec         x, b; /* approx solution, RHS */
131bec1829SPablo Brubeck   Mat         A;    /* linear system matrix */
141bec1829SPablo Brubeck   KSP         ksp;  /* linear solver context */
151bec1829SPablo Brubeck   PC          pc;   /* preconditioner context */
161bec1829SPablo Brubeck   PetscInt    n = 6, i, j, col[2];
171bec1829SPablo Brubeck   PetscScalar value[4];
181bec1829SPablo Brubeck   PetscMPIInt size;
194d86920dSPierre Jolivet 
201bec1829SPablo Brubeck   PetscFunctionBeginUser;
21c8025a54SPierre Jolivet   PetscCall(PetscInitialize(&argc, &args, NULL, help));
221bec1829SPablo Brubeck   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
231bec1829SPablo Brubeck   PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This is a uniprocessor example only!");
241bec1829SPablo Brubeck 
251bec1829SPablo Brubeck   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
261bec1829SPablo Brubeck          Compute the matrix and right-hand-side vector that define
271bec1829SPablo Brubeck          the linear system, Ax = b.
281bec1829SPablo Brubeck      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
291bec1829SPablo Brubeck 
301bec1829SPablo Brubeck   /*
311bec1829SPablo Brubeck      Create vectors.  Note that we form 1 vector from scratch and
321bec1829SPablo Brubeck      then duplicate as needed.
331bec1829SPablo Brubeck   */
341bec1829SPablo Brubeck   PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
351bec1829SPablo Brubeck   PetscCall(PetscObjectSetName((PetscObject)x, "Solution"));
361bec1829SPablo Brubeck   PetscCall(VecSetSizes(x, PETSC_DECIDE, n));
371bec1829SPablo Brubeck   PetscCall(VecSetFromOptions(x));
381bec1829SPablo Brubeck   PetscCall(VecDuplicate(x, &b));
391bec1829SPablo Brubeck 
401bec1829SPablo Brubeck   /*
411bec1829SPablo Brubeck      Create matrix.  When using MatCreate(), the matrix format can
421bec1829SPablo Brubeck      be specified at runtime.
431bec1829SPablo Brubeck 
441bec1829SPablo Brubeck      Performance tuning note:  For problems of substantial size,
451bec1829SPablo Brubeck      preallocation of matrix memory is crucial for attaining good
461bec1829SPablo Brubeck      performance. See the matrix chapter of the users manual for details.
471bec1829SPablo Brubeck   */
481bec1829SPablo Brubeck   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
491bec1829SPablo Brubeck   PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, n, n));
501bec1829SPablo Brubeck   PetscCall(MatSetFromOptions(A));
511bec1829SPablo Brubeck   PetscCall(MatSetUp(A));
521bec1829SPablo Brubeck 
531bec1829SPablo Brubeck   /*
541bec1829SPablo Brubeck      Assemble matrix
551bec1829SPablo Brubeck   */
561bec1829SPablo Brubeck   value[0] = 2.0;
571bec1829SPablo Brubeck   value[1] = -1.0;
581bec1829SPablo Brubeck   value[2] = -1.0;
591bec1829SPablo Brubeck   value[3] = 2.0;
601bec1829SPablo Brubeck   for (i = 0; 2 * i < n; i++) {
611bec1829SPablo Brubeck     col[0] = 2 * i;
621bec1829SPablo Brubeck     col[1] = 2 * i + 1;
631bec1829SPablo Brubeck     PetscCall(MatSetValues(A, 2, col, 2, col, value, INSERT_VALUES));
641bec1829SPablo Brubeck     for (j = 0; j < 4; j++) value[j] *= 3.0;
651bec1829SPablo Brubeck   }
661bec1829SPablo Brubeck   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
671bec1829SPablo Brubeck   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
681bec1829SPablo Brubeck 
691bec1829SPablo Brubeck   /*
701bec1829SPablo Brubeck      Set random right-hand-side vector.
711bec1829SPablo Brubeck   */
721bec1829SPablo Brubeck   PetscCall(VecSetRandom(b, NULL));
731bec1829SPablo Brubeck 
741bec1829SPablo Brubeck   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
751bec1829SPablo Brubeck                 Create the linear solver and set various options
761bec1829SPablo Brubeck      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
771bec1829SPablo Brubeck   /*
781bec1829SPablo Brubeck      Create linear solver context
791bec1829SPablo Brubeck   */
801bec1829SPablo Brubeck   PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
811bec1829SPablo Brubeck 
821bec1829SPablo Brubeck   /*
831bec1829SPablo Brubeck      Set operators. Here the matrix that defines the linear system
84*7addb90fSBarry Smith      also serves as the matrix from which the preconditioner is constructed.
851bec1829SPablo Brubeck   */
861bec1829SPablo Brubeck   PetscCall(KSPSetOperators(ksp, A, A));
871bec1829SPablo Brubeck 
881bec1829SPablo Brubeck   /*
891bec1829SPablo Brubeck      Set linear solver defaults for this problem (optional).
901bec1829SPablo Brubeck      - By extracting the KSP and PC contexts from the KSP context,
911bec1829SPablo Brubeck        we can then directly call any KSP and PC routines to set
921bec1829SPablo Brubeck        various options.
931bec1829SPablo Brubeck      - The following four statements are optional; all of these
941bec1829SPablo Brubeck        parameters could alternatively be specified at runtime via
951bec1829SPablo Brubeck        KSPSetFromOptions();
961bec1829SPablo Brubeck   */
971bec1829SPablo Brubeck   PetscCall(KSPGetPC(ksp, &pc));
981bec1829SPablo Brubeck   PetscCall(PCSetType(pc, PCJACOBI));
99b3480c81SBarry Smith   PetscCall(KSPSetTolerances(ksp, 1.e-5, 1.e-5, PETSC_CURRENT, PETSC_CURRENT));
1001bec1829SPablo Brubeck 
1011bec1829SPablo Brubeck   /*
1021bec1829SPablo Brubeck     Set runtime options, e.g.,
1031bec1829SPablo Brubeck         -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
1041bec1829SPablo Brubeck     These options will override those specified above as long as
1051bec1829SPablo Brubeck     KSPSetFromOptions() is called _after_ any other customization
1061bec1829SPablo Brubeck     routines.
1071bec1829SPablo Brubeck   */
1081bec1829SPablo Brubeck   PetscCall(KSPSetFromOptions(ksp));
1091bec1829SPablo Brubeck 
1101bec1829SPablo Brubeck   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1111bec1829SPablo Brubeck                       Solve the linear system
1121bec1829SPablo Brubeck      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1131bec1829SPablo Brubeck   /*
1141bec1829SPablo Brubeck      Solve linear system
1151bec1829SPablo Brubeck   */
1161bec1829SPablo Brubeck   PetscCall(KSPSolve(ksp, b, x));
1171bec1829SPablo Brubeck 
1181bec1829SPablo Brubeck   /*
1191bec1829SPablo Brubeck      View solver info; we could instead use the option -ksp_view to
1201bec1829SPablo Brubeck      print this info to the screen at the conclusion of KSPSolve().
1211bec1829SPablo Brubeck   */
1221bec1829SPablo Brubeck   PetscCall(KSPView(ksp, PETSC_VIEWER_STDOUT_WORLD));
1231bec1829SPablo Brubeck 
1241bec1829SPablo Brubeck   /*
1251bec1829SPablo Brubeck      Free work space.  All PETSc objects should be destroyed when they
1261bec1829SPablo Brubeck      are no longer needed.
1271bec1829SPablo Brubeck   */
1281bec1829SPablo Brubeck   PetscCall(VecDestroy(&x));
1291bec1829SPablo Brubeck   PetscCall(VecDestroy(&b));
1301bec1829SPablo Brubeck   PetscCall(MatDestroy(&A));
1311bec1829SPablo Brubeck   PetscCall(KSPDestroy(&ksp));
1321bec1829SPablo Brubeck 
1331bec1829SPablo Brubeck   /*
1341bec1829SPablo Brubeck      Always call PetscFinalize() before exiting a program.  This routine
1351bec1829SPablo Brubeck        - finalizes the PETSc libraries as well as MPI
1361bec1829SPablo Brubeck        - provides summary and diagnostic information if certain runtime
1371bec1829SPablo Brubeck          options are chosen (e.g., -log_view).
1381bec1829SPablo Brubeck   */
1391bec1829SPablo Brubeck   PetscCall(PetscFinalize());
1401bec1829SPablo Brubeck   return 0;
1411bec1829SPablo Brubeck }
1421bec1829SPablo Brubeck 
1431bec1829SPablo Brubeck /*TEST
1441bec1829SPablo Brubeck 
1451bec1829SPablo Brubeck    test:
1461bec1829SPablo Brubeck       suffix: cg_none
1471bec1829SPablo Brubeck       filter: grep -v "variant HERMITIAN"
1481bec1829SPablo Brubeck       args: -ksp_type cg -pc_type none -ksp_view_eigenvalues -ksp_view_singularvalues -ksp_converged_reason
1491bec1829SPablo Brubeck 
1501bec1829SPablo Brubeck    test:
1511bec1829SPablo Brubeck       suffix: cg_jacobi
1521bec1829SPablo Brubeck       filter: grep -v "variant HERMITIAN"
1531bec1829SPablo Brubeck       args: -ksp_type cg -pc_type jacobi -ksp_view_eigenvalues -ksp_view_singularvalues -ksp_converged_reason
1541bec1829SPablo Brubeck 
1551bec1829SPablo Brubeck    test:
1561bec1829SPablo Brubeck       suffix: fcg_none
1571bec1829SPablo Brubeck       args: -ksp_type fcg -pc_type none -ksp_view_eigenvalues -ksp_view_singularvalues -ksp_converged_reason
1581bec1829SPablo Brubeck 
1591bec1829SPablo Brubeck    test:
1601bec1829SPablo Brubeck       suffix: fcg_jacobi
1611bec1829SPablo Brubeck       args: -ksp_type fcg -pc_type jacobi -ksp_view_eigenvalues -ksp_view_singularvalues -ksp_converged_reason
1621bec1829SPablo Brubeck 
1631bec1829SPablo Brubeck    test:
1641bec1829SPablo Brubeck       suffix: minres_none
1651bec1829SPablo Brubeck       args: -ksp_type minres -pc_type none -ksp_view_eigenvalues -ksp_view_singularvalues -ksp_converged_reason
1661bec1829SPablo Brubeck 
1671bec1829SPablo Brubeck    test:
1681bec1829SPablo Brubeck       suffix: minres_jacobi
1691bec1829SPablo Brubeck       args: -ksp_type minres -pc_type jacobi -ksp_view_eigenvalues -ksp_view_singularvalues -ksp_converged_reason
1701bec1829SPablo Brubeck 
1711bec1829SPablo Brubeck    test:
1721bec1829SPablo Brubeck       suffix: gmres_none
1731bec1829SPablo Brubeck       args: -ksp_type gmres -pc_type none -ksp_view_eigenvalues -ksp_view_singularvalues -ksp_converged_reason
1741bec1829SPablo Brubeck 
1751bec1829SPablo Brubeck    test:
1761bec1829SPablo Brubeck       suffix: gmres_jacobi_left
1771bec1829SPablo Brubeck       args: -ksp_type gmres -ksp_pc_side left -pc_type jacobi -ksp_view_eigenvalues -ksp_view_singularvalues -ksp_converged_reason
1781bec1829SPablo Brubeck 
1791bec1829SPablo Brubeck    test:
1801bec1829SPablo Brubeck       suffix: gmres_jacobi_right
1811bec1829SPablo Brubeck       args: -ksp_type gmres -ksp_pc_side right -pc_type jacobi -ksp_view_eigenvalues -ksp_view_singularvalues -ksp_converged_reason
1821bec1829SPablo Brubeck 
1831bec1829SPablo Brubeck    test:
1841bec1829SPablo Brubeck       suffix: fgmres_none
1851bec1829SPablo Brubeck       args: -ksp_type fgmres -pc_type none -ksp_view_eigenvalues -ksp_view_singularvalues -ksp_converged_reason
1861bec1829SPablo Brubeck 
1871bec1829SPablo Brubeck    test:
1881bec1829SPablo Brubeck       suffix: fgmres_jacobi
1891bec1829SPablo Brubeck       args: -ksp_type fgmres -pc_type jacobi -ksp_view_eigenvalues -ksp_view_singularvalues -ksp_converged_reason
1901bec1829SPablo Brubeck 
1911bec1829SPablo Brubeck TEST*/
192