Date: 2013-07-02T14:48:26+00:00

Actual source code: ex1.c
petsc-3.4.2 2013-07-02

      2: static char help[] = "Tests solving linear system on 0 by 0 matrix.\n\n";

      4: #include <petscksp.h>

      8: int main(int argc,char **args)
      9: {
     10:   Mat            C;
     12:   PetscInt       N = 0;
     13:   Vec            u,b,x;
     14:   KSP            ksp;
     15:   PetscReal      norm;

     17:   PetscInitialize(&argc,&args,(char*)0,help);

     19:   /* create stiffness matrix */
     20:   MatCreate(PETSC_COMM_WORLD,&C);
     21:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,N,N);
     22:   MatSetFromOptions(C);
     23:   MatSetUp(C);
     24:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
     25:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

     27:   /* create right hand side and solution */
     28:   VecCreate(PETSC_COMM_WORLD,&u);
     29:   VecSetSizes(u,PETSC_DECIDE,N);
     30:   VecSetFromOptions(u);
     31:   VecDuplicate(u,&b);
     32:   VecDuplicate(u,&x);
     33:   VecSet(u,0.0);
     34:   VecSet(b,0.0);

     36:   VecAssemblyBegin(b);
     37:   VecAssemblyEnd(b);

     40:   /* solve linear system */
     41:   KSPCreate(PETSC_COMM_WORLD,&ksp);
     42:   KSPSetOperators(ksp,C,C,DIFFERENT_NONZERO_PATTERN);
     43:   KSPSetFromOptions(ksp);
     44:   KSPSolve(ksp,b,u);

     46:   MatMult(C,u,x);
     47:   VecAXPY(x,-1.0,b);
     48:   VecNorm(x,NORM_2,&norm);

     50:   KSPDestroy(&ksp);
     51:   VecDestroy(&u);
     52:   VecDestroy(&x);
     53:   VecDestroy(&b);
     54:   MatDestroy(&C);
     55:   PetscFinalize();
     56:   return 0;
     57: }