Actual source code: ex1.c
1: /*$Id: ex1.c,v 1.18 2001/08/07 21:30:50 bsmith Exp $*/
3: static char help[] = "Tests solving linear system on 0 by 0 matrix.\n\n";
5: #include petscksp.h
9: int main(int argc,char **args)
10: {
11: Mat C;
12: int ierr,N = 0;
13: Vec u,b,x;
14: KSP ksp;
15: PetscScalar zero = 0.0,mone = -1.0;
16: PetscReal norm;
18: PetscInitialize(&argc,&args,(char *)0,help);
20: /* create stiffness matrix */
21: MatCreate(PETSC_COMM_SELF,PETSC_DECIDE,PETSC_DECIDE,N,N,&C);
22: MatSetFromOptions(C);
23: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
24: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
26: /* create right hand side and solution */
28: VecCreateSeq(PETSC_COMM_SELF,N,&u);
29: VecDuplicate(u,&b);
30: VecDuplicate(u,&x);
31: VecSet(&zero,u);
32: VecSet(&zero,b);
34: VecAssemblyBegin(b);
35: VecAssemblyEnd(b);
38: /* solve linear system */
39: KSPCreate(PETSC_COMM_WORLD,&ksp);
40: KSPSetOperators(ksp,C,C,DIFFERENT_NONZERO_PATTERN);
41: KSPSetFromOptions(ksp);
42: KSPSetRhs(ksp,b);
43: KSPSetSolution(ksp,u);
44: KSPSolve(ksp);
46: MatMult(C,u,x);
47: VecAXPY(&mone,b,x);
48: VecNorm(x,NORM_2,&norm);
49: printf("Norm of residual %g\n",norm);
51: KSPDestroy(ksp);
52: VecDestroy(u);
53: VecDestroy(x);
54: VecDestroy(b);
55: MatDestroy(C);
56: PetscFinalize();
57: return 0;
58: }