Actual source code: ex100.c

petsc-3.11.3 2019-06-26
Report Typos and Errors
  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;


 18:   PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL);
 19:   PetscOptionsGetBool(NULL,NULL,"-test",&test,NULL);
 20:   PetscOptionsGetBool(NULL,NULL,"-draw",&draw,NULL);

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

 28:   MatCreateVecs(A,&x,&b);
 29:   VecSet(b,1);

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

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

 39:   KSPSetOperators(ksp,A,A);
 40:   KSPSetFromOptions(ksp);
 41:   KSPSolve(ksp,b,x);

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

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

 60:   VecDestroy(&x);
 61:   VecDestroy(&b);
 62:   MatDestroy(&A);
 63:   KSPDestroy(&ksp);

 65:   return(0);
 66: }

 68: /* ------------------------------------------------------- */

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

 72: /*
 73: #define PYTHON_EXE "python2.5"
 74: #define PYTHON_LIB "/usr/lib/libpython2.5"
 75: */

 77: #if !defined(PYTHON_EXE)
 78: #define PYTHON_EXE 0
 79: #endif
 80: #if !defined(PYTHON_LIB)
 81: #define PYTHON_LIB 0
 82: #endif

 84: int main(int argc, char *argv[])
 85: {

 88:   PetscInitialize(&argc,&argv,0,help);if (ierr) return ierr;
 89:   PetscPythonInitialize(PYTHON_EXE,PYTHON_LIB);
 90:   RunTest();PetscPythonPrintError();
 91:   PetscFinalize();
 92:   return ierr;
 93: }

 95: /*TEST

 97:     test:
 98:       args: -ksp_monitor_short
 99:       requires: Section 1.1 Suggested Reading
100:       localrunfiles: example100.py

102: TEST*/