Actual source code: ex1.c


  2: static char help[] = "Tests solving linear system on 0 by 0 matrix, and KSPLSQR convergence test handling.\n\n";

  4: #include <petscksp.h>

  6: static PetscErrorCode GetConvergenceTestName(PetscErrorCode (*converged)(KSP, PetscInt, PetscReal, KSPConvergedReason *, void *), char name[], size_t n)
  7: {
  8:   if (converged == KSPConvergedDefault) {
  9:     PetscStrncpy(name, "default", n);
 10:   } else if (converged == KSPConvergedSkip) {
 11:     PetscStrncpy(name, "skip", n);
 12:   } else if (converged == KSPLSQRConvergedDefault) {
 13:     PetscStrncpy(name, "lsqr", n);
 14:   } else {
 15:     PetscStrncpy(name, "other", n);
 16:   }
 17:   return 0;
 18: }

 20: int main(int argc, char **args)
 21: {
 22:   Mat       C;
 23:   PetscInt  N = 0;
 24:   Vec       u, b, x;
 25:   KSP       ksp;
 26:   PetscReal norm;
 27:   PetscBool flg = PETSC_FALSE;

 30:   PetscInitialize(&argc, &args, (char *)0, help);

 32:   /* create stiffness matrix */
 33:   MatCreate(PETSC_COMM_WORLD, &C);
 34:   MatSetSizes(C, PETSC_DECIDE, PETSC_DECIDE, N, N);
 35:   MatSetFromOptions(C);
 36:   MatSetUp(C);
 37:   MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY);
 38:   MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY);

 40:   /* create right hand side and solution */
 41:   VecCreate(PETSC_COMM_WORLD, &u);
 42:   VecSetSizes(u, PETSC_DECIDE, N);
 43:   VecSetFromOptions(u);
 44:   VecDuplicate(u, &b);
 45:   VecDuplicate(u, &x);
 46:   VecSet(u, 0.0);
 47:   VecSet(b, 0.0);

 49:   VecAssemblyBegin(b);
 50:   VecAssemblyEnd(b);

 52:   /* solve linear system */
 53:   KSPCreate(PETSC_COMM_WORLD, &ksp);
 54:   KSPSetOperators(ksp, C, C);
 55:   KSPSetFromOptions(ksp);
 56:   KSPSolve(ksp, b, u);

 58:   /* test proper handling of convergence test by KSPLSQR */
 59:   PetscOptionsGetBool(NULL, NULL, "-test_lsqr", &flg, NULL);
 60:   if (flg) {
 61:     char     *type;
 62:     char      convtestname[16];
 63:     PetscBool islsqr;
 64:     PetscErrorCode (*converged)(KSP, PetscInt, PetscReal, KSPConvergedReason *, void *);
 65:     PetscErrorCode (*converged1)(KSP, PetscInt, PetscReal, KSPConvergedReason *, void *);
 66:     PetscErrorCode (*destroy)(void *), (*destroy1)(void *);
 67:     void *ctx, *ctx1;

 69:     {
 70:       const char *typeP;
 71:       KSPGetType(ksp, &typeP);
 72:       PetscStrallocpy(typeP, &type);
 73:     }
 74:     PetscStrcmp(type, KSPLSQR, &islsqr);
 75:     KSPGetConvergenceTest(ksp, &converged, &ctx, &destroy);
 76:     GetConvergenceTestName(converged, convtestname, 16);
 77:     PetscPrintf(PETSC_COMM_WORLD, "convergence test: %s\n", convtestname);
 78:     KSPSetType(ksp, KSPLSQR);
 79:     KSPGetConvergenceTest(ksp, &converged1, &ctx1, &destroy1);
 82:     if (islsqr) {
 86:     }
 87:     GetConvergenceTestName(converged1, convtestname, 16);
 88:     KSPViewFromOptions(ksp, NULL, "-ksp1_view");
 89:     PetscPrintf(PETSC_COMM_WORLD, "convergence test: %s\n", convtestname);
 90:     KSPSetType(ksp, type);
 91:     KSPGetConvergenceTest(ksp, &converged1, &ctx1, &destroy1);
 95:     GetConvergenceTestName(converged1, convtestname, 16);
 96:     KSPViewFromOptions(ksp, NULL, "-ksp2_view");
 97:     PetscPrintf(PETSC_COMM_WORLD, "convergence test: %s\n", convtestname);
 98:     PetscFree(type);
 99:   }

101:   MatMult(C, u, x);
102:   VecAXPY(x, -1.0, b);
103:   VecNorm(x, NORM_2, &norm);

105:   KSPDestroy(&ksp);
106:   VecDestroy(&u);
107:   VecDestroy(&x);
108:   VecDestroy(&b);
109:   MatDestroy(&C);
110:   PetscFinalize();
111:   return 0;
112: }

114: /*TEST

116:     test:
117:       args:  -pc_type jacobi -ksp_monitor_short -ksp_gmres_cgs_refinement_type refine_always

119:     test:
120:       suffix: 2
121:       nsize: 2
122:       args: -pc_type jacobi -ksp_monitor_short -ksp_gmres_cgs_refinement_type refine_always

124:     test:
125:       suffix: 3
126:       args: -pc_type sor -pc_sor_symmetric -ksp_monitor_short -ksp_gmres_cgs_refinement_type refine_always

128:     test:
129:       suffix: 5
130:       args: -pc_type eisenstat -ksp_monitor_short -ksp_gmres_cgs_refinement_type refine_always

132:     testset:
133:       args: -test_lsqr -ksp{,1,2}_view -pc_type jacobi
134:       filter: grep -E "(^  type:|preconditioning|norm type|convergence test:)"
135:       test:
136:         suffix: lsqr_0
137:         args: -ksp_convergence_test {{default skip}separate output}
138:       test:
139:         suffix: lsqr_1
140:         args: -ksp_type cg -ksp_convergence_test {{default skip}separate output}
141:       test:
142:         suffix: lsqr_2
143:         args: -ksp_type lsqr

145: TEST*/