Actual source code: ex1.c
petsc-3.6.1 2015-08-06
1: #include <petscksp.h>
3: /* ------------------------------------------------------- */
7: PetscErrorCode RunTest(void)
8: {
9: PetscInt N = 100;
10: PetscBool draw = PETSC_FALSE;
11: PetscReal rnorm;
12: Mat A;
13: Vec b,x,r;
14: KSP ksp;
15: PC pc;
20: PetscOptionsGetInt(0,"-N",&N,NULL);
21: PetscOptionsGetBool(0,"-draw",&draw,NULL);
23: MatCreate(PETSC_COMM_WORLD,&A);
24: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
25: MatSetType(A,MATPYTHON);
26: MatPythonSetType(A,"example1.py:Laplace1D");
27: MatSetUp(A);
29: MatCreateVecs(A,&x,&b);
30: VecSet(b,1);
32: KSPCreate(PETSC_COMM_WORLD,&ksp);
33: KSPSetType(ksp,KSPPYTHON);
34: KSPPythonSetType(ksp,"example1.py:ConjGrad");
36: KSPGetPC(ksp,&pc);
37: PCSetType(pc,PCPYTHON);
38: PCPythonSetType(pc,"example1.py:Jacobi");
40: KSPSetOperators(ksp,A,A);
41: KSPSetFromOptions(ksp);
42: KSPSolve(ksp,b,x);
44: VecDuplicate(b,&r);
45: MatMult(A,x,r);
46: VecAYPX(r,-1,b);
47: VecNorm(r,NORM_2,&rnorm);
48: PetscPrintf(PETSC_COMM_WORLD,"error norm = %g\n",rnorm);
50: if (draw) {
51: VecView(x,PETSC_VIEWER_DRAW_WORLD);
52: PetscSleep(2);
53: }
55: VecDestroy(&x);
56: VecDestroy(&b);
57: VecDestroy(&r);
58: MatDestroy(&A);
59: KSPDestroy(&ksp);
61: return(0);
62: }
64: /* ------------------------------------------------------- */
66: static char help[] = "Python-implemented Mat/KSP/PC.\n\n";
68: /*
69: #define PYTHON_EXE "python2.5"
70: #define PYTHON_LIB "/usr/lib/libpython2.5"
71: */
73: #if !defined(PYTHON_EXE)
74: #define PYTHON_EXE 0
75: #endif
76: #if !defined(PYTHON_LIB)
77: #define PYTHON_LIB 0
78: #endif
82: int main(int argc, char *argv[])
83: {
86: PetscInitialize(&argc,&argv,0,help);
88: PetscPythonInitialize(PYTHON_EXE,PYTHON_LIB);
90: RunTest();PetscPythonPrintError();
92: PetscFinalize();
94: return 0;
95: }
97: /* ------------------------------------------------------- */