Actual source code: ex1.c

petsc-3.8.4 2018-03-24
Report Typos and Errors
  1:  #include <petscksp.h>

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

  5: PetscErrorCode RunTest(void)
  6: {
  7:   PetscInt       N    = 100;
  8:   PetscBool      draw = 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,"-draw",&draw,NULL);

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

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

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

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

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

 42:   VecDuplicate(b,&r);
 43:   MatMult(A,x,r);
 44:   VecAYPX(r,-1,b);
 45:   VecNorm(r,NORM_2,&rnorm);
 46:   PetscPrintf(PETSC_COMM_WORLD,"error norm = %g\n",rnorm);

 48:   if (draw) {
 49:     VecView(x,PETSC_VIEWER_DRAW_WORLD);
 50:     PetscSleep(2);
 51:   }

 53:   VecDestroy(&x);
 54:   VecDestroy(&b);
 55:   VecDestroy(&r);
 56:   MatDestroy(&A);
 57:   KSPDestroy(&ksp);

 59:   return(0);
 60: }

 62: /* ------------------------------------------------------- */

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

 66: /*
 67: #define PYTHON_EXE "python2.5"
 68: #define PYTHON_LIB "/usr/lib/libpython2.5"
 69: */

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

 78: int main(int argc, char *argv[])
 79: {

 82:   PetscInitialize(&argc,&argv,0,help);if (ierr) return ierr;
 83:   PetscPythonInitialize(PYTHON_EXE,PYTHON_LIB);
 84:   RunTest();PetscPythonPrintError();
 85:   PetscFinalize();
 86:   return ierr;
 87: }

 89: /* ------------------------------------------------------- */