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

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

      2: static char help[] = "Solves a tridiagonal linear system.  Designed to compare SOR for different Mat impls.\n\n";

      4: #include <petscksp.h>

      8: int main(int argc,char **args)
      9: {
     10:   KSP            ksp;      /* linear solver context */
     11:   Mat            A;        /* linear system matrix */
     12:   Vec            x,b;      /* approx solution, RHS */
     13:   PetscInt       Ii,Istart,Iend;
     15:   PetscScalar    v[3] = {-1./2., 1., -1./2.};
     16:   PetscInt       j[3];
     17:   PetscInt       k=15;
     18:   PetscInt       M,m=420;

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

     22:   PetscOptionsGetInt(NULL,"-k",&k,NULL);
     23:   PetscOptionsGetInt(NULL,"-m",&m,NULL);

     25:   KSPCreate(PETSC_COMM_WORLD,&ksp);
     26:   KSPSetFromOptions(ksp);
     27:   KSPGetOperators(ksp,&A,NULL,NULL);

     29:   MatSetSizes(A,m,m,PETSC_DETERMINE,PETSC_DETERMINE);
     30:   MatSetFromOptions(A);
     31:   MatSetUp(A);
     32:   MatGetOwnershipRange(A,&Istart,&Iend);
     33:   MatGetSize(A,&M,NULL);
     34:   for (Ii=Istart; Ii<Iend; Ii++) {
     35:     j[0] = Ii - k;
     36:     j[1] = Ii;
     37:     j[2] = (Ii + k) < M ? (Ii + k) : -1;
     38:     MatSetValues(A,1,&Ii,3,j,v,INSERT_VALUES);
     39:   }
     40:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
     41:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
     42:   MatGetVecs(A,&x,&b);

     44:   VecSetFromOptions(b);
     45:   VecSet(b,1.0);
     46:   VecSetFromOptions(x);
     47:   VecSet(x,2.0);

     49:   KSPSolve(ksp,b,x);

     51:   VecDestroy(&b);
     52:   VecDestroy(&x);
     53:   KSPDestroy(&ksp);

     55:   PetscFinalize();
     56:   return 0;
     57: }