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 = %D\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",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: /*
71: #define PYTHON_EXE "python2.5"
72: #define PYTHON_LIB "/usr/lib/libpython2.5"
73: */
75: #if !defined(PYTHON_EXE)
76: #define PYTHON_EXE 0
77: #endif
78: #if !defined(PYTHON_LIB)
79: #define PYTHON_LIB 0
80: #endif
82: int main(int argc, char *argv[])
83: {
85: PetscInitialize(&argc,&argv,0,help);
86: PetscPythonInitialize(PYTHON_EXE,PYTHON_LIB);
87: RunTest();
88: PetscPythonPrintError();
89: PetscFinalize();
90: return 0;
91: }
93: /*TEST
95: test:
96: args: -ksp_monitor_short
97: requires: petsc4py
98: localrunfiles: example100.py
100: TEST*/