Actual source code: ex100.c

  1: #include <petscksp.h>

  3: /* ------------------------------------------------------- */

  5: PetscErrorCode RunTest(void)
  6: {
  7:   PetscInt  N = 100, its = 0;
  8:   PetscBool draw = PETSC_FALSE, test = PETSC_FALSE;
  9:   PetscReal rnorm;
 10:   Mat       A;
 11:   Vec       b, x, r;
 12:   KSP       ksp;
 13:   PC        pc;


 16:   PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL);
 17:   PetscOptionsGetBool(NULL, NULL, "-test", &test, NULL);
 18:   PetscOptionsGetBool(NULL, NULL, "-draw", &draw, NULL);

 20:   MatCreate(PETSC_COMM_WORLD, &A);
 21:   MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, N, N);
 22:   MatSetType(A, MATPYTHON);
 23:   MatPythonSetType(A, "example100.py:Laplace1D");
 24:   MatSetUp(A);

 26:   MatCreateVecs(A, &x, &b);
 27:   VecSet(b, 1);

 29:   KSPCreate(PETSC_COMM_WORLD, &ksp);
 30:   KSPSetType(ksp, KSPPYTHON);
 31:   KSPPythonSetType(ksp, "example100.py:ConjGrad");

 33:   KSPGetPC(ksp, &pc);
 34:   PCSetType(pc, PCPYTHON);
 35:   PCPythonSetType(pc, "example100.py:Jacobi");

 37:   KSPSetOperators(ksp, A, A);
 38:   KSPSetFromOptions(ksp);
 39:   KSPSolve(ksp, b, x);

 41:   if (test) {
 42:     KSPGetTotalIterations(ksp, &its);
 43:     PetscPrintf(PETSC_COMM_WORLD, "Number of KSP iterations = %" PetscInt_FMT "\n", its);
 44:   } else {
 45:     VecDuplicate(b, &r);
 46:     MatMult(A, x, r);
 47:     VecAYPX(r, -1, b);
 48:     VecNorm(r, NORM_2, &rnorm);
 49:     PetscPrintf(PETSC_COMM_WORLD, "error norm = %g\n", (double)rnorm);
 50:     VecDestroy(&r);
 51:   }

 53:   if (draw) {
 54:     VecView(x, PETSC_VIEWER_DRAW_WORLD);
 55:     PetscSleep(2);
 56:   }

 58:   VecDestroy(&x);
 59:   VecDestroy(&b);
 60:   MatDestroy(&A);
 61:   KSPDestroy(&ksp);

 63:   return 0;
 64: }

 66: /* ------------------------------------------------------- */

 68: static char help[] = "Python-implemented Mat/KSP/PC.\n\n";

 70: #if !defined(PYTHON_EXE)
 71:   #define PYTHON_EXE 0
 72: #endif
 73: #if !defined(PYTHON_LIB)
 74:   #define PYTHON_LIB 0
 75: #endif

 77: int main(int argc, char *argv[])
 78: {
 80:   PetscInitialize(&argc, &argv, 0, help);
 81:   PetscPythonInitialize(PYTHON_EXE, PYTHON_LIB);
 82:   RunTest();
 83:   PetscPythonPrintError();
 84:   PetscFinalize();
 85:   return 0;
 86: }

 88: /*TEST

 90:     test:
 91:       args: -ksp_monitor_short
 92:       requires: petsc4py
 93:       localrunfiles: example100.py

 95: TEST*/